Skip to content

Commit 43f44e4

Browse files
authored
emulation on host: millis()/micros() now start at 0 (#7810)
emulation on host: millis()/micros() now start at 0
1 parent 100a8df commit 43f44e4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: tests/host/common/Arduino.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,24 @@
1818

1919
#include <unistd.h>
2020

21+
static struct timeval gtod0 = { 0, 0 };
22+
2123
extern "C" unsigned long millis()
2224
{
2325
timeval time;
2426
gettimeofday(&time, NULL);
25-
return (time.tv_sec * 1000) + (time.tv_usec / 1000);
27+
if (gtod0.tv_sec == 0)
28+
memcpy(&gtod0, &time, sizeof gtod0);
29+
return ((time.tv_sec - gtod0.tv_sec) * 1000) + ((time.tv_usec - gtod0.tv_usec) / 1000);
2630
}
2731

2832
extern "C" unsigned long micros()
2933
{
3034
timeval time;
3135
gettimeofday(&time, NULL);
32-
return (time.tv_sec * 1000000) + time.tv_usec;
36+
if (gtod0.tv_sec == 0)
37+
memcpy(&gtod0, &time, sizeof gtod0);
38+
return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec;
3339
}
3440

3541

Diff for: tests/host/common/ArduinoMain.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ int main (int argc, char* const argv [])
295295
// install exit handler in case Esp.restart() is called
296296
atexit(cleanup);
297297

298+
// first call to millis(): now is millis() and micros() beginning
299+
millis();
300+
298301
setup();
299302
while (!user_exit)
300303
{

0 commit comments

Comments
 (0)