diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-06-30 22:04:01 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2020-06-30 22:04:01 (GMT) |
| commit | ae5743529f82d398876456b7e175defb19a6ccfc (patch) | |
| tree | abaebb87754a952a15d7b062d36bfd9b668794e3 /wasp/wasp.py | |
| parent | 11be7ca3280134cf4e565198f87c316179cf2dc9 (diff) | |
wasp: Switch to scheduling from interrupt
This has two useful properties. Firstly it means the watch will be
maintained in the background, allowing the REPL to be used for
notifications and other updates. Secondly it will save a little bit
of power by reducing the work needed to handle spurious wake ups.
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/wasp.py')
| -rw-r--r-- | wasp/wasp.py | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/wasp/wasp.py b/wasp/wasp.py index 092ad29..a9f1a30 100644 --- a/wasp/wasp.py +++ b/wasp/wasp.py @@ -17,6 +17,7 @@ import gc import machine +import micropython import watch import widgets @@ -106,6 +107,8 @@ class Manager(): self._brightness = 2 self._button = PinHandler(watch.button) self._charging = True + self._scheduled = False + self._scheduling = False # TODO: Eventually these should move to main.py self.register(ClockApp(), True) @@ -242,7 +245,8 @@ class Manager(): """Return to a running state. """ watch.display.poweron() - self.app.wake() + if 'wake' in dir(self.app): + self.app.wake() watch.backlight.set(self._brightness) watch.touch.wake() @@ -326,6 +330,10 @@ class Manager(): normal execution context meaning any exceptions and other problems can be observed interactively via the console. """ + if self._scheduling: + print('Watch already running in the background') + return + if not self.app: self.switch(self.quick_ring[0]) @@ -353,8 +361,36 @@ class Manager(): # Currently there is no code to control how fast the system # ticks. In other words this code will break if we improve the - # power management... we are currently relying on no being able + # power management... we are currently relying on not being able # to stay in the low-power state for very long. machine.deepsleep() + def _work(self): + self._scheduled = False + try: + self._tick() + except Exception as e: + # Only print the exception if the watch provides a way to do so! + if 'print_exception' in dir(watch): + watch.print_exception(e) + self.switch(CrashApp(e)) + + def _schedule(self): + """Asynchronously schedule a system management cycle.""" + if not self._scheduled: + self._scheduled = True + micropython.schedule(Manager._work, self) + + def schedule(self, enable=True): + """Run the system manager synchronously.""" + if not self.app: + self.switch(self.quick_ring[0]) + + if enable: + watch.schedule = self._schedule + else: + watch.schedule = watch.nop + + self._scheduling = enable + system = Manager() |
