1
0
Fork 0

wasp: manager: Start refining the application interface

This is the first step in starting to formalize the seperation of
applications from the system manager.
This commit is contained in:
Daniel Thompson 2020-03-07 11:52:42 +00:00
parent 1ebafc083b
commit 138425f4d4
2 changed files with 60 additions and 8 deletions

View file

@ -1,5 +1,7 @@
import fonts.clock as digits import fonts.clock as digits
import watch
import widgets import widgets
import manager
from draw565 import Draw565 from draw565 import Draw565
@ -25,20 +27,40 @@ class ClockApp(object):
""" """
def __init__(self): def __init__(self):
self.on_screen = ( -1, -1, -1, -1, -1, -1 )
self.meter = widgets.BatteryMeter() self.meter = widgets.BatteryMeter()
def draw(self, watch): def handle_event(self, event_view):
"""Process events that the app is subscribed to."""
if event_view[0] == manager.EVENT_TICK:
self.update()
else:
# TODO: Raise an unexpected event exception
pass
def foreground(self, manager, effect=None):
"""Activate the application."""
self.on_screen = ( -1, -1, -1, -1, -1, -1 )
self.draw(effect)
manager.request_tick(1000)
def tick(self, ticks):
self.update()
def background(self):
"""De-activate the application (without losing state)."""
pass
def draw(self, effect=None):
"""Redraw the display from scratch.""" """Redraw the display from scratch."""
display = watch.display display = watch.display
display.fill(0) display.fill(0)
display.rleblit(digits.clock_colon, pos=(2*48, 80), fg=0xb5b6) display.rleblit(digits.clock_colon, pos=(2*48, 80), fg=0xb5b6)
self.on_screen = ( -1, -1, -1, -1, -1, -1 ) self.on_screen = ( -1, -1, -1, -1, -1, -1 )
self.update(watch) self.update()
self.meter.draw() self.meter.draw()
def update(self, watch): def update(self):
"""Update the display (if needed). """Update the display (if needed).
The updates are a lazy as possible and rely on an prior call to The updates are a lazy as possible and rely on an prior call to

View file

@ -2,21 +2,51 @@ import clock
import gc import gc
import machine import machine
EVENT_TICK = 0x100
EVENT_KEYMASK = 0xff
class Manager(object): class Manager(object):
def __init__(self, watch): def __init__(self, watch):
self.watch = watch self.watch = watch
self.app = None
self.switch(clock.ClockApp()) self.switch(clock.ClockApp())
self.sleep_at = watch.rtc.uptime + 90 self.sleep_at = watch.rtc.uptime + 90
self.charging = True self.charging = True
def switch(self, app): def switch(self, app):
if self.app:
self.app.background(self)
# Clear out any configuration from the old application
self.tick_period_ms = 0
self.tick_expiry = None
self.app = app self.app = app
app.draw(self.watch) app.foreground(self)
def request_tick(self, period_ms=None):
"""Request (and subscribe to) a periodic tick event.
Note: With the current simplistic timer implementation sub-second
tick intervals are not possible.
"""
self.tick_period_ms = period_ms
self.tick_expiry = self.watch.rtc.get_uptime_ms() + period_ms
def tick(self): def tick(self):
rtc = self.watch.rtc
if self.sleep_at: if self.sleep_at:
if self.watch.rtc.update(): if rtc.update() and self.tick_expiry:
self.app.update(self.watch) now = rtc.get_uptime_ms()
if self.tick_expiry <= now:
ticks = 0
while self.tick_expiry <= now:
self.tick_expiry += self.tick_period_ms
ticks += 1
self.app.tick(ticks)
if self.watch.button.value(): if self.watch.button.value():
self.sleep_at = self.watch.rtc.uptime + 15 self.sleep_at = self.watch.rtc.uptime + 15
@ -34,7 +64,7 @@ class Manager(object):
charging = self.watch.battery.charging() charging = self.watch.battery.charging()
if self.watch.button.value() or self.charging != charging: if self.watch.button.value() or self.charging != charging:
self.watch.display.poweron() self.watch.display.poweron()
self.app.update(self.watch) self.app.tick(None)
self.watch.backlight.set(2) self.watch.backlight.set(2)
self.sleep_at = self.watch.rtc.uptime + 15 self.sleep_at = self.watch.rtc.uptime + 15