Skip to content

Commit

Permalink
emulation on host: millis()/micros() now start at 0 (#7810)
Browse files Browse the repository at this point in the history
emulation on host: millis()/micros() now start at 0
  • Loading branch information
d-a-v authored Jan 5, 2021
1 parent 100a8df commit 43f44e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 8 additions & 2 deletions tests/host/common/Arduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,24 @@

#include <unistd.h>

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(&gtod0, &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(&gtod0, &time, sizeof gtod0);
return ((time.tv_sec - gtod0.tv_sec) * 1000000) + time.tv_usec - gtod0.tv_usec;
}


Expand Down
3 changes: 3 additions & 0 deletions tests/host/common/ArduinoMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 43f44e4

Please sign in to comment.