From 43f44e4b1339142aa75516db07f4bbda4678564d Mon Sep 17 00:00:00 2001 From: david gauchard Date: Tue, 5 Jan 2021 12:21:16 +0100 Subject: [PATCH] emulation on host: millis()/micros() now start at 0 (#7810) emulation on host: millis()/micros() now start at 0 --- tests/host/common/Arduino.cpp | 10 ++++++++-- tests/host/common/ArduinoMain.cpp | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 8a0162b47..c02457f1a 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -18,18 +18,24 @@ #include +static struct timeval gtod0 = { 0, 0 }; + extern "C" unsigned long millis() { timeval time; gettimeofday(&time, NULL); - return (time.tv_sec * 1000) + (time.tv_usec / 1000); + if (gtod0.tv_sec == 0) + memcpy(>od0, &time, sizeof gtod0); + return ((time.tv_sec - gtod0.tv_sec) * 1000) + ((time.tv_usec - gtod0.tv_usec) / 1000); } extern "C" unsigned long micros() { timeval time; gettimeofday(&time, NULL); - return (time.tv_sec * 1000000) + time.tv_usec; + if (gtod0.tv_sec == 0) + memcpy(>od0, &time, sizeof gtod0); + return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec; } diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index a9d8d696f..051ca0dbc 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -295,6 +295,9 @@ int main (int argc, char* const argv []) // install exit handler in case Esp.restart() is called atexit(cleanup); + // first call to millis(): now is millis() and micros() beginning + millis(); + setup(); while (!user_exit) {