Skip to content

Commit

Permalink
🐎 Improve uptime accuracy (#315)
Browse files Browse the repository at this point in the history
improve uptime accuracy by storing milliseconds and only rounding when
publishing value
drawback: less time before rollover, but still long enough
  • Loading branch information
bertmelis authored and marvinroger committed Mar 10, 2017
1 parent 13e1cfd commit 2564160
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Homie/Uptime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
using namespace HomieInternals;

Uptime::Uptime()
: _seconds(0)
: _milliseconds(0)
, _lastTick(0) {
}

void Uptime::update() {
uint32_t now = millis();
_seconds += (now - _lastTick) / 1000UL;
_milliseconds += (now - _lastTick);
_lastTick = now;
}

uint64_t Uptime::getSeconds() const {
return _seconds;
return (_milliseconds / 1000ULL);
}
2 changes: 1 addition & 1 deletion src/Homie/Uptime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Uptime {
uint64_t getSeconds() const;

private:
uint64_t _seconds;
uint64_t _milliseconds;
uint32_t _lastTick;
};
} // namespace HomieInternals

0 comments on commit 2564160

Please sign in to comment.