1
0
Fork 0

wasp: nrf_rtc: Add a tiny bit of extra resolution

We now have a couple of applications (stopwatch, Game of Life) that benefit
from sub-second precision. The micropython RTC/utime code for nrf still
needs a major overhaul but this allows us to paper over the cracks for
just a little longer.
pull/24/head
Daniel Thompson 2020-04-18 15:34:49 +01:00
parent 5b277e94f1
commit 9348e758b2
2 changed files with 15 additions and 10 deletions

View File

@ -163,7 +163,7 @@ class GameOfLifeApp():
"""Activate the application."""
self._draw()
wasp.system.request_event(wasp.EventMask.TOUCH)
wasp.system.request_tick(1000)
wasp.system.request_tick(625)
def tick(self, ticks):
"""Notify the application that its periodic tick is due."""

View File

@ -20,8 +20,8 @@ class RTC(object):
def __init__(self, counter):
self.counter = counter
self.uptime = 0
self.set_localtime((2020, 2, 18, 12, 0, 0, 0, 0))
self._uptime = 0
self.set_localtime((2020, 3, 1, 3, 0, 0, 0, 0))
def update(self):
newcount = self.counter.counter()
@ -30,11 +30,11 @@ class RTC(object):
return False
if split < 0:
split += (1 << 24)
elapsed = split // 8
self.lastcount += elapsed * 8
self.lastcount &= (1 << 24) - 1
self.uptime += elapsed
self.lastcount += split
self.lastcount &= (1 << 24) - 1
self._uptime += split
return True
def set_localtime(self, t):
@ -51,16 +51,21 @@ class RTC(object):
t = (yyyy, mm, dd, HH, MM, SS, 0, 0)
lt = time.mktime(t)
self.offset = lt - self.uptime
self.offset = lt - self._uptime
def get_localtime(self):
self.update()
return time.localtime(self.offset + self.uptime)
return time.localtime(self.offset + (self._uptime >> 3))
def get_time(self):
localtime = self.get_localtime()
return localtime[3:6]
@property
def uptime(self):
"""Provide the current uptime in seconds."""
return self._uptime // 8
def get_uptime_ms(self):
"""Return the current uptime in milliseconds."""
return self.uptime * 1000
return self._uptime * 125