fix tick overflow and implement periodic timer class.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from micropython import const
|
||||
from supervisor import ticks_ms
|
||||
|
||||
_TICKS_PERIOD = const(1 << 29)
|
||||
_TICKS_MAX = const(_TICKS_PERIOD - 1)
|
||||
@@ -13,3 +14,17 @@ def ticks_diff(new, start):
|
||||
|
||||
def check_deadline(new, start, ms):
|
||||
return ticks_diff(new, start) < ms
|
||||
|
||||
|
||||
class PeriodicTimer:
|
||||
def __init__(self, period):
|
||||
self.period = period
|
||||
self.last_tick = ticks_ms()
|
||||
|
||||
def tick(self):
|
||||
now = ticks_ms()
|
||||
if ticks_diff(now, self.last_tick) >= self.period:
|
||||
self.last_tick = now
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
Reference in New Issue
Block a user