diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-03-28 17:27:09 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-04-06 21:04:20 (GMT) |
| commit | b2622b579dfc265850401b44a8b825533ea2ec17 (patch) | |
| tree | 7152b6d100ef339e86306c2ec7ccb7d4d7b28fd3 /wasp/wasp.py | |
| parent | 0cc79876890713b49ff6ed155828707f494e37c2 (diff) | |
wasp: Add button presses to the event system.
Here the biggest changes are in the test application because we
refactor a number of the tests to make better use of the button.
Although applications may consume button events it does have a
default behavior which is to switch to the default application
(usually the clock).
Diffstat (limited to 'wasp/wasp.py')
| -rw-r--r-- | wasp/wasp.py | 80 |
1 files changed, 70 insertions, 10 deletions
diff --git a/wasp/wasp.py b/wasp/wasp.py index 6d3f3a5..f624a8f 100644 --- a/wasp/wasp.py +++ b/wasp/wasp.py @@ -39,6 +39,36 @@ class EventMask(): SWIPE_UPDOWN = 0x0004 BUTTON = 0x0008 +class Debounce(object): + """Pin (and Signal) debounce wrapper. + + TODO: Currently this driver doesn't actually implement any + debounce but it will! + """ + + def __init__(self, pin): + """ + :param Pin pin: The pin to generate events from + """ + self._pin = pin + self._value = pin.value() + + def get_event(self): + """Receive a pin change event. + + Check for a pending pin change event and, if an event is pending, + return it. + + :return: boolean of the pin state if an event is received, None + otherwise. + """ + new_value = self._pin.value() + if self._value == new_value: + return None + + self._value = new_value + return new_value + class Manager(): """WASP system manager @@ -53,14 +83,23 @@ class Manager(): def __init__(self): self.app = None - self.applications = [ - ClockApp(), - FlashlightApp(), - TestApp() - ] - self.charging = True + self.applications = [] + self.charging = True self._brightness = 2 + self._button = Debounce(watch.button) + + # TODO: Eventually these should move to main.py + self.register(ClockApp(), True) + self.register(FlashlightApp(), True) + self.register(TestApp(), True) + + def register(self, app, quick_ring=True): + """Register an application with the system. + + :param object app: The application to regsister + """ + self.applications.append(app) @property def brightness(self): @@ -115,6 +154,12 @@ class Manager(): if i < 0: i = len(app_list)-1 self.switch(app_list[i]) + elif direction == EventType.HOME: + i = app_list.index(self.app) + if i != 0: + self.switch(app_list[0]) + else: + self.sleep() def request_event(self, event_mask): """Subscribe to events. @@ -159,6 +204,20 @@ class Manager(): self.keep_awake() + def _handle_button(self, state): + """Process a button-press (or unpress) event. + """ + self.keep_awake() + + if bool(self.event_mask & EventMask.BUTTON): + # Currently we only support one button + if not self.app.press(EventType.HOME, state): + # If app reported None or False then we are done + return + + if state: + self.navigate(EventType.HOME) + def _handle_touch(self, event): """Process a touch event. """ @@ -196,14 +255,15 @@ class Manager(): ticks += 1 self.app.tick(ticks) - if watch.button.value(): - self.keep_awake() + state = self._button.get_event() + if None != state: + self._handle_button(state) event = watch.touch.get_event() if event: self._handle_touch(event) - if watch.rtc.uptime > self.sleep_at: + if self.sleep_at and watch.rtc.uptime > self.sleep_at: self.sleep() gc.collect() @@ -211,7 +271,7 @@ class Manager(): watch.rtc.update() charging = watch.battery.charging() - if watch.button.value() or self.charging != charging: + if 1 == self._button.get_event() or self.charging != charging: self.wake() def run(self): |
