blob: bea262d9ef98a6b7d7770a6c97c89ee8d09f09e6 [file] [log] [blame]
Brian3afd6fc2014-04-02 20:41:49 -07001#include "aos/common/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_;
Brian Silvermandcaa3f72015-11-29 05:32:08 +000018 CHECK_EQ(difference, interval_ * result);
Austin Schuh8aec1ed2016-05-01 13:29:20 -070019 CHECK_EQ(
20 0, (next_time - offset_).time_since_epoch().count() % interval_.count());
Brian Silvermandcaa3f72015-11-29 05:32:08 +000021 CHECK_GE(next_time, now);
22 CHECK_LE(next_time - now, interval_);
23 last_time_ = next_time;
24 return result;
25}
26
brians343bc112013-02-10 01:53:46 +000027} // namespace timing
28} // namespace aos