diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-02-03 19:12:04 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-02-03 19:12:04 (GMT) |
| commit | 127df66335657cbd77539a509d507a4fcfb403b2 (patch) | |
| tree | fc8f2a6d27f3ef4d626cdd0b0e6bf1ba8cc03712 | |
| parent | 8168dd5939fcad61890d4ab988fcb0cbfbe4dbae (diff) | |
wasp: pinetime: Basic RTC support
Currently this supports time only (no date) and it based on the
RTCounter class which is customized for nRF ports. At present
the nRF port doesn't have proper machine.rtc support so we have
implemented within wasp instead.
| -rw-r--r-- | wasp/boards/pinetime/manifest.py | 1 | ||||
| -rw-r--r-- | wasp/boards/pinetime/watch.py | 8 | ||||
| -rw-r--r-- | wasp/drivers/nrf_rtc.py | 58 |
3 files changed, 64 insertions, 3 deletions
diff --git a/wasp/boards/pinetime/manifest.py b/wasp/boards/pinetime/manifest.py index 5e525e3..ea772ac 100644 --- a/wasp/boards/pinetime/manifest.py +++ b/wasp/boards/pinetime/manifest.py @@ -3,6 +3,7 @@ freeze('../..', 'boot.py', 'demo.py', 'drivers/battery.py', + 'drivers/nrf_rtc.py', 'drivers/signal.py', 'drivers/st7789.py', 'drivers/vibrator.py', diff --git a/wasp/boards/pinetime/watch.py b/wasp/boards/pinetime/watch.py index d6a8210..a8708c7 100644 --- a/wasp/boards/pinetime/watch.py +++ b/wasp/boards/pinetime/watch.py @@ -1,9 +1,10 @@ from machine import Pin -from machine import RTCounter as RTC +from machine import RTCounter #from machine import Signal from machine import SPI from drivers.battery import Battery +from drivers.nrf_rtc import RTC from drivers.signal import Signal from drivers.st7789 import ST7789_SPI from drivers.vibrator import Vibrator @@ -50,11 +51,12 @@ display = Display() backlight.set(1) # Start measuring time (and feeding the watchdog) -rtc = RTC(1, mode=RTC.PERIODIC) -rtc.start() +rtc = RTC(RTCounter(1, mode=RTCounter.PERIODIC)) +rtc.counter.start() battery = Battery( Pin('BATTERY', Pin.IN), Signal(Pin('CHARGING', Pin.IN), invert=True), Signal(Pin('USB_PWR', Pin.IN), invert=True)) vibrator = Vibrator(Pin('MOTOR', Pin.OUT, value=0), active_low=True) +button = Pin('BUTTON', Pin.IN) diff --git a/wasp/drivers/nrf_rtc.py b/wasp/drivers/nrf_rtc.py new file mode 100644 index 0000000..c130a76 --- /dev/null +++ b/wasp/drivers/nrf_rtc.py @@ -0,0 +1,58 @@ +""" Real Time Clock based on the nRF-family low power counter """ + +#class Stim(object): +# def __init__(self): +# self(0) +# +# def __call__(self, v): +# self.c = v +# +# def counter(self): +# return self.c + +class RTC(object): + """Real Time Clock based on the nRF-family low power counter. + + TODO: Maintain hh:mm:ss as an array so we can report time + without memory allocation. + """ + + def __init__(self, counter): + self.counter = counter + self.uptime = 0 + self.set_time((12, 0, 0)) + + def update(self): + newcount = self.counter.counter() + split = newcount - self.lastcount + if split == 0: + return False + if split < 0: + split += (1 << 24) + elapsed = split // 8 + self.lastcount += elapsed * 8 + self.lastcount &= (1 << 24) - 1 + + self.uptime += elapsed + + self.ss += elapsed + if self.ss >= 60: + self.mm += self.ss // 60 + self.ss %= 60 + + if self.mm >= 60: + self.hh += self.mm // 60 + self.mm %= 60 + self.hh %= 24 + + return True + + def set_time(self, t): + self.lastcount = self.counter.counter() + self.hh = t[0] + self.mm = t[1] + self.ss = t[2] + + def get_time(self): + self.update() + return (self.hh, self.mm, self.ss) |
