Handle offset change in PhasedLoop and PhasedLoopHandler

When the PhasedLoop offset_ is changed, the last_time_ must be adjusted
to have an even interval. Additionally, in the PhaseLoopHandler Call
method, schedule has to be called after fn_ because if fn_ changes
the offset then it will not be updated on the next callback since
schedule has already scheduled the next time.

Change-Id: I12110134f00ee948f986e0df9f8304fbc2b08c31
Signed-off-by: milind <milind.upadhyay@gmail.com>
diff --git a/aos/util/phased_loop.cc b/aos/util/phased_loop.cc
index 1e520b1..53f6f4d 100644
--- a/aos/util/phased_loop.cc
+++ b/aos/util/phased_loop.cc
@@ -18,6 +18,9 @@
 void PhasedLoop::set_interval_and_offset(
     const monotonic_clock::duration interval,
     const monotonic_clock::duration offset) {
+  // Update last_time_ to the new offset so that we have an even interval
+  last_time_ += offset - offset_;
+
   interval_ = interval;
   offset_ = offset;
   CHECK(offset_ >= monotonic_clock::duration(0));
@@ -55,6 +58,7 @@
   next_time += offset_;
 
   const monotonic_clock::duration difference = next_time - last_time_;
+
   const int result = difference / interval_;
   CHECK_EQ(
       0, (next_time - offset_).time_since_epoch().count() % interval_.count());
@@ -64,5 +68,5 @@
   return result;
 }
 
-}  // namespace timing
+}  // namespace time
 }  // namespace aos