John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame^] | 1 | #include "aos/util/phased_loop.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | namespace aos { |
| 4 | namespace time { |
| 5 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 6 | int 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 Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 15 | |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 16 | const monotonic_clock::duration difference = next_time - last_time_; |
| 17 | const int result = difference / interval_; |
Austin Schuh | 8aec1ed | 2016-05-01 13:29:20 -0700 | [diff] [blame] | 18 | CHECK_EQ( |
| 19 | 0, (next_time - offset_).time_since_epoch().count() % interval_.count()); |
Brian Silverman | dcaa3f7 | 2015-11-29 05:32:08 +0000 | [diff] [blame] | 20 | CHECK_GE(next_time, now); |
| 21 | CHECK_LE(next_time - now, interval_); |
| 22 | last_time_ = next_time; |
| 23 | return result; |
| 24 | } |
| 25 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | } // namespace timing |
| 27 | } // namespace aos |