diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-03-27 20:07:47 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-04-06 21:04:20 (GMT) |
| commit | 0ac2321e8287be908bc6684696568993591914a7 (patch) | |
| tree | 554a6c05ecfa7f15316cde02326e056b2abeb367 | |
| parent | 4a7d9246d9a665a5e50d6f9f6e2933a804bed640 (diff) | |
wasp: Factor out the sleep/wake code
Pulling this out into a method allows it to be called by an application.
| -rw-r--r-- | wasp/wasp.py | 42 |
1 files changed, 26 insertions, 16 deletions
diff --git a/wasp/wasp.py b/wasp/wasp.py index e9647a9..dd72692 100644 --- a/wasp/wasp.py +++ b/wasp/wasp.py @@ -133,10 +133,33 @@ class Manager(): """Reset the keep awake timer.""" self.sleep_at = watch.rtc.uptime + 15 + def sleep(self): + """Enter the deepest sleep state possible. + """ + watch.backlight.set(0) + if not self.app.sleep(): + self.switch(self.applications[0]) + self.app.sleep() + watch.display.poweroff() + self.charging = watch.battery.charging() + self.sleep_at = None + + def wake(self): + """Return to a running state. + """ + watch.display.poweron() + self.app.wake() + watch.backlight.set(self._brightness) + + # Discard any pending touch events + _ = watch.touch.get_event() + + self.keep_awake() + def _handle_event(self, event): """Process an event. """ - self.sleep_at = watch.rtc.uptime + 15 + self.keep_awake() event_mask = self.event_mask if event[0] < 5: @@ -178,13 +201,7 @@ class Manager(): self._handle_event(event) if watch.rtc.uptime > self.sleep_at: - watch.backlight.set(0) - if not self.app.sleep(): - self.switch(self.applications[0]) - self.app.sleep() - watch.display.poweroff() - self.charging = watch.battery.charging() - self.sleep_at = None + self.sleep() gc.collect() else: @@ -192,14 +209,7 @@ class Manager(): charging = watch.battery.charging() if watch.button.value() or self.charging != charging: - watch.display.poweron() - self.app.wake() - watch.backlight.set(self._brightness) - - # Discard any pending touch events - _ = watch.touch.get_event() - - self.sleep_at = watch.rtc.uptime + 15 + self.wake() def run(self): """Run the system manager synchronously. |
