blob: a349718c401bc838315380c8cb2e111d2b2e49c7 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/util/phased_loop.h"
brians343bc112013-02-10 01:53:46 +00002
brians343bc112013-02-10 01:53:46 +00003namespace aos {
4namespace time {
5
Austin Schuh8aec1ed2016-05-01 13:29:20 -07006int PhasedLoop::Iterate(const monotonic_clock::time_point now) {
7 const monotonic_clock::time_point next_time =
8 monotonic_clock::time_point(
9 (((now - offset_).time_since_epoch() + monotonic_clock::duration(1)) /
10 interval_) *
11 interval_) +
12 ((now.time_since_epoch() < offset_) ? monotonic_clock::zero()
13 : interval_) +
14 offset_;
Brian Silvermandcaa3f72015-11-29 05:32:08 +000015
Austin Schuh8aec1ed2016-05-01 13:29:20 -070016 const monotonic_clock::duration difference = next_time - last_time_;
17 const int result = difference / interval_;
Austin Schuh8aec1ed2016-05-01 13:29:20 -070018 CHECK_EQ(
19 0, (next_time - offset_).time_since_epoch().count() % interval_.count());
Brian Silvermandcaa3f72015-11-29 05:32:08 +000020 CHECK_GE(next_time, now);
21 CHECK_LE(next_time - now, interval_);
22 last_time_ = next_time;
23 return result;
24}
25
brians343bc112013-02-10 01:53:46 +000026} // namespace timing
27} // namespace aos