Accurate Periodic Timer

Hi Hugo,

We don’t strictly need a perfectly accurate timer (though this is something we miss - it would be interesting to see support for syncing an external sub-second RTC), however we do need one that doesn’t drift AKA a periodic timer.

Ignoring the individual jitter of each imp.wakeup(), the issue we suffer from is a delay in scheduling the next imp.wakeup(), a primitive periodic timer could efficiently meet this requirement. For example, we ask to wakeup after 1s but wakeup after 1.1s due to blocking or busy code, 10 seconds later and we’ve drifted a whole second. So instead we keep having to realign to the millis() count and reduce our wakeup time each time to compensate - which works, but could be performed much better and efficiently by a primitive periodic timer.

We do have some blocking code and we have some genuinely busy loops that add jitter, in both cases we can’t avoid this, the blocking code is due to I2C accesses which I don’t believe have a non-blocking option unless we really slow down the transfer rates by inserting imp.wakeup()s, and our other code is necessary as part of the functionality of the device and it’s not that we’re hitting 100% utilisation, it’s just that it runs long enough to add some jitter.

Many thanks