2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Wasp-os system manager
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
2020-03-26 22:46:10 +01:00
|
|
|
|
2020-05-14 23:29:35 +02:00
|
|
|
.. data:: wasp.system
|
2020-03-26 22:46:10 +01:00
|
|
|
|
2020-05-14 23:29:35 +02:00
|
|
|
wasp.system is the system-wide singleton instance of :py:class:`.Manager`.
|
|
|
|
Application must use this instance to access the system services provided
|
|
|
|
by the manager.
|
|
|
|
|
|
|
|
.. data:: wasp.watch
|
|
|
|
|
|
|
|
wasp.watch is an import of :py:mod:`watch` and is simply provided as a
|
|
|
|
shortcut (and to reduce memory by keeping it out of other namespaces).
|
2020-03-26 22:46:10 +01:00
|
|
|
"""
|
2020-02-03 20:24:09 +01:00
|
|
|
import gc
|
|
|
|
import machine
|
2020-07-01 00:04:01 +02:00
|
|
|
import micropython
|
2020-03-22 13:37:19 +01:00
|
|
|
import watch
|
|
|
|
import widgets
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-05-10 10:33:26 +02:00
|
|
|
from apps.clock import ClockApp
|
2020-06-22 23:51:06 +02:00
|
|
|
from apps.heart import HeartApp
|
2020-05-10 10:33:26 +02:00
|
|
|
from apps.launcher import LauncherApp
|
2020-07-19 21:50:33 +02:00
|
|
|
from apps.pager import PagerApp, CrashApp, NotificationApp
|
2020-05-10 10:33:26 +02:00
|
|
|
from apps.settings import SettingsApp
|
2020-06-09 22:29:00 +02:00
|
|
|
from apps.steps import StepCounterApp
|
2021-01-03 15:59:14 +01:00
|
|
|
from apps.software import SoftwareApp
|
2020-05-10 10:33:26 +02:00
|
|
|
from apps.stopwatch import StopwatchApp
|
2020-03-08 18:37:43 +01:00
|
|
|
|
2020-03-27 21:09:28 +01:00
|
|
|
class EventType():
|
|
|
|
"""Enumerated interface actions.
|
2020-03-26 22:46:10 +01:00
|
|
|
|
2020-03-27 21:09:28 +01:00
|
|
|
MicroPython does not implement the enum module so EventType
|
2020-03-26 22:46:10 +01:00
|
|
|
is simply a regular object which acts as a namespace.
|
|
|
|
"""
|
2020-03-26 21:42:03 +01:00
|
|
|
DOWN = 1
|
|
|
|
UP = 2
|
|
|
|
LEFT = 3
|
|
|
|
RIGHT = 4
|
2020-03-27 21:09:28 +01:00
|
|
|
TOUCH = 5
|
2020-03-26 21:42:03 +01:00
|
|
|
|
2020-08-15 18:00:49 +02:00
|
|
|
HOME = 255
|
|
|
|
BACK = 254
|
|
|
|
NEXT = 253
|
2020-03-27 21:09:28 +01:00
|
|
|
|
|
|
|
class EventMask():
|
|
|
|
"""Enumerated event masks.
|
2020-03-26 22:46:10 +01:00
|
|
|
"""
|
2020-03-26 21:42:03 +01:00
|
|
|
TOUCH = 0x0001
|
|
|
|
SWIPE_LEFTRIGHT = 0x0002
|
|
|
|
SWIPE_UPDOWN = 0x0004
|
|
|
|
BUTTON = 0x0008
|
2020-08-15 18:00:49 +02:00
|
|
|
NEXT = 0x0010
|
2020-03-26 21:42:03 +01:00
|
|
|
|
2020-04-05 10:32:35 +02:00
|
|
|
class PinHandler():
|
|
|
|
"""Pin (and Signal) event generator.
|
2020-03-28 18:27:09 +01:00
|
|
|
|
|
|
|
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
|
2020-04-11 21:26:12 +02:00
|
|
|
otherwise.
|
2020-03-28 18:27:09 +01:00
|
|
|
"""
|
|
|
|
new_value = self._pin.value()
|
|
|
|
if self._value == new_value:
|
|
|
|
return None
|
|
|
|
|
|
|
|
self._value = new_value
|
|
|
|
return new_value
|
|
|
|
|
2020-11-14 13:24:28 +01:00
|
|
|
def _key_app(d):
|
|
|
|
"""Get a sort key for apps."""
|
|
|
|
return d.NAME
|
|
|
|
|
|
|
|
def _key_alarm(d):
|
|
|
|
"""Get a sort key for alarms."""
|
|
|
|
return d[0]
|
|
|
|
|
2020-03-26 21:42:03 +01:00
|
|
|
class Manager():
|
2020-05-14 23:41:05 +02:00
|
|
|
"""Wasp-os system manager
|
2020-03-26 22:46:10 +01:00
|
|
|
|
|
|
|
The manager is responsible for handling top-level UI events and
|
|
|
|
dispatching them to the foreground application. It also provides
|
|
|
|
services to the application.
|
|
|
|
|
|
|
|
The manager is expected to have a single system-wide instance
|
|
|
|
which can be accessed via :py:data:`wasp.system` .
|
|
|
|
"""
|
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
def __init__(self):
|
2020-03-07 12:52:42 +01:00
|
|
|
self.app = None
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-11-04 20:00:37 +01:00
|
|
|
self.bar = widgets.StatusBar()
|
|
|
|
|
2020-04-12 09:59:37 +02:00
|
|
|
self.quick_ring = []
|
2020-04-06 23:03:05 +02:00
|
|
|
self.launcher = LauncherApp()
|
2020-04-12 09:59:37 +02:00
|
|
|
self.launcher_ring = []
|
2020-07-19 21:50:33 +02:00
|
|
|
self.notifier = NotificationApp()
|
|
|
|
self.notifications = {}
|
2020-10-22 17:59:17 +02:00
|
|
|
self.musicstate = {}
|
|
|
|
self.musicinfo = {}
|
2020-04-12 09:59:37 +02:00
|
|
|
|
2020-12-31 11:09:38 +01:00
|
|
|
self._theme = (
|
|
|
|
b'\x7b\xef' # ble
|
|
|
|
b'\x7b\xef' # scroll-indicator
|
|
|
|
b'\x7b\xef' # battery
|
|
|
|
b'\xe7\x3c' # status-clock
|
|
|
|
b'\x7b\xef' # notify-icon
|
2020-12-31 20:12:38 +01:00
|
|
|
b'\xff\xff' # bright
|
|
|
|
b'\xbd\xb6' # mid
|
|
|
|
b'\x39\xff' # ui
|
|
|
|
b'\xff\x00' # spot1
|
|
|
|
b'\xdd\xd0' # spot2
|
|
|
|
b'\x00\x0f' # contrast
|
2020-12-31 11:09:38 +01:00
|
|
|
)
|
2020-12-05 19:27:55 +01:00
|
|
|
|
2020-04-12 09:59:37 +02:00
|
|
|
self.blank_after = 15
|
2020-04-06 23:03:05 +02:00
|
|
|
|
2020-11-14 13:24:28 +01:00
|
|
|
self._alarms = []
|
2020-03-26 23:12:05 +01:00
|
|
|
self._brightness = 2
|
2020-10-27 18:10:56 +01:00
|
|
|
self._notifylevel = 2
|
|
|
|
if 'P8' in watch.os.uname().machine:
|
|
|
|
self._nfylevels = [0, 225, 450]
|
|
|
|
else:
|
|
|
|
self._nfylevels = [0, 40, 80]
|
|
|
|
self._nfylev_ms = self._nfylevels[self._notifylevel - 1]
|
2020-04-05 10:32:35 +02:00
|
|
|
self._button = PinHandler(watch.button)
|
2020-04-12 09:59:37 +02:00
|
|
|
self._charging = True
|
2020-07-01 00:04:01 +02:00
|
|
|
self._scheduled = False
|
|
|
|
self._scheduling = False
|
2020-03-28 18:27:09 +01:00
|
|
|
|
|
|
|
# TODO: Eventually these should move to main.py
|
2020-09-16 22:47:22 +02:00
|
|
|
for app, qr in ( (ClockApp, True),
|
2020-09-22 22:14:45 +02:00
|
|
|
(StepCounterApp, True),
|
|
|
|
(StopwatchApp, True),
|
|
|
|
(HeartApp, True),
|
2021-01-03 15:59:14 +01:00
|
|
|
(SoftwareApp, False),
|
|
|
|
(SettingsApp, False) ):
|
2020-09-22 22:14:45 +02:00
|
|
|
try:
|
|
|
|
self.register(app(), qr)
|
|
|
|
except:
|
|
|
|
# Let's not bring the whole device down just because there's
|
|
|
|
# an exception starting one of the apps...
|
|
|
|
pass
|
2020-03-28 18:27:09 +01:00
|
|
|
|
2020-04-14 21:05:57 +02:00
|
|
|
def register(self, app, quick_ring=False):
|
2020-03-28 18:27:09 +01:00
|
|
|
"""Register an application with the system.
|
|
|
|
|
|
|
|
:param object app: The application to regsister
|
|
|
|
"""
|
2021-01-10 11:37:25 +01:00
|
|
|
if isinstance(app, str):
|
|
|
|
exec('import ' + app[:app.rindex('.')])
|
|
|
|
app = eval(app + '()')
|
|
|
|
|
2020-04-14 21:05:57 +02:00
|
|
|
if quick_ring == True:
|
|
|
|
self.quick_ring.append(app)
|
|
|
|
else:
|
|
|
|
self.launcher_ring.append(app)
|
2020-11-14 13:24:28 +01:00
|
|
|
self.launcher_ring.sort(key = _key_app)
|
2020-03-26 23:12:05 +01:00
|
|
|
|
2021-01-03 15:59:14 +01:00
|
|
|
def unregister(self, cls):
|
|
|
|
for app in self.launcher_ring:
|
2021-01-07 10:46:35 +01:00
|
|
|
if isinstance(app, cls):
|
2021-01-03 15:59:14 +01:00
|
|
|
self.launcher_ring.remove(app)
|
|
|
|
break
|
|
|
|
|
2020-03-26 23:12:05 +01:00
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Cached copy of the brightness current written to the hardware."""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@brightness.setter
|
|
|
|
def brightness(self, value):
|
|
|
|
self._brightness = value
|
|
|
|
watch.backlight.set(self._brightness)
|
|
|
|
|
2020-10-27 18:10:56 +01:00
|
|
|
@property
|
|
|
|
def notify_level(self):
|
|
|
|
"""Cached copy of the current notify level"""
|
|
|
|
return self._notifylevel
|
|
|
|
|
|
|
|
@notify_level.setter
|
|
|
|
def notify_level(self, value):
|
|
|
|
self._notifylevel = value
|
|
|
|
self._nfylev_ms = self._nfylevels[self._notifylevel - 1]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def notify_duration(self):
|
|
|
|
"""Cached copy of the current vibrator pulse duration in milliseconds"""
|
|
|
|
return self._nfylev_ms
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
def switch(self, app):
|
2020-03-26 22:46:10 +01:00
|
|
|
"""Switch to the requested application.
|
|
|
|
"""
|
2020-03-07 12:52:42 +01:00
|
|
|
if self.app:
|
2020-04-05 10:43:26 +02:00
|
|
|
if 'background' in dir(self.app):
|
2020-11-14 13:24:28 +01:00
|
|
|
try:
|
|
|
|
self.app.background()
|
|
|
|
except:
|
|
|
|
# Clear out the old app to ensure we don't recurse when
|
|
|
|
# we switch to to the CrashApp. It's a bit freaky but
|
|
|
|
# True has an empty directory this is better than
|
|
|
|
# None because it won't re-runt he system start up
|
|
|
|
# code.
|
|
|
|
self.app = True
|
|
|
|
raise
|
2020-03-22 13:37:19 +01:00
|
|
|
else:
|
|
|
|
# System start up...
|
|
|
|
watch.display.poweron()
|
|
|
|
watch.display.mute(True)
|
2020-03-26 23:12:05 +01:00
|
|
|
watch.backlight.set(self._brightness)
|
2020-03-22 13:37:19 +01:00
|
|
|
self.sleep_at = watch.rtc.uptime + 90
|
2020-03-07 12:52:42 +01:00
|
|
|
|
|
|
|
# Clear out any configuration from the old application
|
2020-03-08 11:18:08 +01:00
|
|
|
self.event_mask = 0
|
2020-03-07 12:52:42 +01:00
|
|
|
self.tick_period_ms = 0
|
|
|
|
self.tick_expiry = None
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
self.app = app
|
2020-03-22 13:37:19 +01:00
|
|
|
watch.display.mute(True)
|
2020-04-05 10:29:06 +02:00
|
|
|
watch.drawable.reset()
|
2020-03-22 13:37:19 +01:00
|
|
|
app.foreground()
|
|
|
|
watch.display.mute(False)
|
2020-03-07 12:52:42 +01:00
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
def navigate(self, direction=None):
|
2020-03-26 22:46:10 +01:00
|
|
|
"""Navigate to a new application.
|
|
|
|
|
|
|
|
Left/right navigation is used to switch between applications in the
|
|
|
|
quick application ring. Applications on the ring are not permitted
|
2020-03-27 21:09:28 +01:00
|
|
|
to subscribe to :py:data`EventMask.SWIPE_LEFTRIGHT` events.
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-04-06 23:03:05 +02:00
|
|
|
Swipe up is used to bring up the launcher. Clock applications are not
|
|
|
|
permitted to subscribe to :py:data`EventMask.SWIPE_UPDOWN` events since
|
|
|
|
they should expect to be the default application (and is important that
|
|
|
|
we can trigger the launcher from the default application).
|
|
|
|
|
2020-03-26 22:46:10 +01:00
|
|
|
:param int direction: The direction of the navigation
|
2020-03-08 11:18:08 +01:00
|
|
|
"""
|
2020-04-12 09:59:37 +02:00
|
|
|
app_list = self.quick_ring
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-03-27 21:09:28 +01:00
|
|
|
if direction == EventType.LEFT:
|
2020-04-06 23:03:05 +02:00
|
|
|
if self.app in app_list:
|
|
|
|
i = app_list.index(self.app) + 1
|
|
|
|
if i >= len(app_list):
|
|
|
|
i = 0
|
|
|
|
else:
|
2020-03-08 11:18:08 +01:00
|
|
|
i = 0
|
|
|
|
self.switch(app_list[i])
|
2020-03-27 21:09:28 +01:00
|
|
|
elif direction == EventType.RIGHT:
|
2020-04-06 23:03:05 +02:00
|
|
|
if self.app in app_list:
|
|
|
|
i = app_list.index(self.app) - 1
|
|
|
|
if i < 0:
|
|
|
|
i = len(app_list)-1
|
|
|
|
else:
|
|
|
|
i = 0
|
2020-03-08 11:18:08 +01:00
|
|
|
self.switch(app_list[i])
|
2020-04-06 23:03:05 +02:00
|
|
|
elif direction == EventType.UP:
|
|
|
|
self.switch(self.launcher)
|
|
|
|
elif direction == EventType.DOWN:
|
|
|
|
if self.app != app_list[0]:
|
|
|
|
self.switch(app_list[0])
|
|
|
|
else:
|
2020-07-19 21:50:33 +02:00
|
|
|
if len(self.notifications):
|
|
|
|
self.switch(self.notifier)
|
|
|
|
else:
|
|
|
|
# Nothing to notify... we must handle that here
|
|
|
|
# otherwise the display will flicker.
|
|
|
|
watch.vibrator.pulse()
|
|
|
|
|
2020-04-11 21:14:30 +02:00
|
|
|
elif direction == EventType.HOME or direction == EventType.BACK:
|
2020-04-06 23:03:05 +02:00
|
|
|
if self.app != app_list[0]:
|
2020-03-28 18:27:09 +01:00
|
|
|
self.switch(app_list[0])
|
|
|
|
else:
|
|
|
|
self.sleep()
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-07-19 21:50:33 +02:00
|
|
|
def notify(self, id, msg):
|
|
|
|
self.notifications[id] = msg
|
|
|
|
|
|
|
|
def unnotify(self, id):
|
|
|
|
if id in self.notifications:
|
|
|
|
del self.notifications[id]
|
|
|
|
|
2020-10-22 17:59:17 +02:00
|
|
|
def toggle_music(self, state):
|
|
|
|
self.musicstate = state
|
|
|
|
|
|
|
|
def set_music_info(self, info):
|
|
|
|
self.musicinfo = info
|
|
|
|
|
2020-11-14 13:24:28 +01:00
|
|
|
def set_alarm(self, time, action):
|
|
|
|
"""Queue an alarm.
|
|
|
|
|
|
|
|
:param int time: Time to trigger the alarm (use time.mktime)
|
|
|
|
:param function action: Action to perform when the alarm expires.
|
|
|
|
"""
|
|
|
|
self._alarms.append((time, action))
|
|
|
|
self._alarms.sort(key = _key_alarm)
|
|
|
|
|
|
|
|
def cancel_alarm(self, time, action):
|
|
|
|
"""Unqueue an alarm."""
|
|
|
|
try:
|
|
|
|
self._alarms.remove((time, action))
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
def request_event(self, event_mask):
|
2020-03-26 22:46:10 +01:00
|
|
|
"""Subscribe to events.
|
|
|
|
|
|
|
|
:param int event_mask: The set of events to subscribe to.
|
|
|
|
"""
|
2020-03-08 11:18:08 +01:00
|
|
|
self.event_mask |= event_mask
|
|
|
|
|
2020-03-07 12:52:42 +01:00
|
|
|
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
|
2020-03-22 13:37:19 +01:00
|
|
|
self.tick_expiry = watch.rtc.get_uptime_ms() + period_ms
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-26 23:12:05 +01:00
|
|
|
def keep_awake(self):
|
|
|
|
"""Reset the keep awake timer."""
|
2020-04-05 10:35:32 +02:00
|
|
|
self.sleep_at = watch.rtc.uptime + self.blank_after
|
2020-03-26 23:12:05 +01:00
|
|
|
|
2020-03-27 21:07:47 +01:00
|
|
|
def sleep(self):
|
|
|
|
"""Enter the deepest sleep state possible.
|
|
|
|
"""
|
|
|
|
watch.backlight.set(0)
|
2020-04-05 10:43:26 +02:00
|
|
|
if 'sleep' not in dir(self.app) or not self.app.sleep():
|
2020-04-12 09:59:37 +02:00
|
|
|
self.switch(self.quick_ring[0])
|
2020-03-27 21:07:47 +01:00
|
|
|
self.app.sleep()
|
|
|
|
watch.display.poweroff()
|
2020-06-09 12:18:27 +02:00
|
|
|
watch.touch.sleep()
|
2020-04-12 09:59:37 +02:00
|
|
|
self._charging = watch.battery.charging()
|
2020-03-27 21:07:47 +01:00
|
|
|
self.sleep_at = None
|
|
|
|
|
|
|
|
def wake(self):
|
|
|
|
"""Return to a running state.
|
|
|
|
"""
|
2020-11-14 21:36:41 +01:00
|
|
|
if not self.sleep_at:
|
|
|
|
watch.display.poweron()
|
|
|
|
if 'wake' in dir(self.app):
|
|
|
|
self.app.wake()
|
|
|
|
watch.backlight.set(self._brightness)
|
|
|
|
watch.touch.wake()
|
2020-03-27 21:07:47 +01:00
|
|
|
|
|
|
|
self.keep_awake()
|
|
|
|
|
2020-03-28 18:27:09 +01:00
|
|
|
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)
|
|
|
|
|
2020-03-27 21:09:28 +01:00
|
|
|
def _handle_touch(self, event):
|
|
|
|
"""Process a touch event.
|
2020-03-26 22:46:10 +01:00
|
|
|
"""
|
2020-03-27 21:07:47 +01:00
|
|
|
self.keep_awake()
|
2020-03-08 21:48:48 +01:00
|
|
|
event_mask = self.event_mask
|
2020-08-15 18:00:49 +02:00
|
|
|
|
|
|
|
# Handle context sensitive events such as NEXT
|
|
|
|
if event[0] == EventType.NEXT:
|
|
|
|
if bool(event_mask & EventMask.NEXT) and not self.app.swipe(event):
|
|
|
|
# The app has already handled this one (mark as no event)
|
|
|
|
event[0] = 0
|
|
|
|
elif self.app == self.quick_ring[0] and len(self.notifications):
|
|
|
|
event[0] = EventType.DOWN
|
|
|
|
elif self.app == self.notifier:
|
|
|
|
event[0] = EventType.UP
|
|
|
|
else:
|
|
|
|
event[0] = EventType.RIGHT
|
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
if event[0] < 5:
|
2020-03-08 21:48:48 +01:00
|
|
|
updown = event[0] == 1 or event[0] == 2
|
2020-03-27 21:09:28 +01:00
|
|
|
if (bool(event_mask & EventMask.SWIPE_UPDOWN) and updown) or \
|
|
|
|
(bool(event_mask & EventMask.SWIPE_LEFTRIGHT) and not updown):
|
|
|
|
if self.app.swipe(event):
|
2020-03-08 21:48:48 +01:00
|
|
|
self.navigate(event[0])
|
|
|
|
else:
|
|
|
|
self.navigate(event[0])
|
2020-03-27 21:09:28 +01:00
|
|
|
elif event[0] == 5 and self.event_mask & EventMask.TOUCH:
|
2020-03-08 11:18:08 +01:00
|
|
|
self.app.touch(event)
|
2020-08-15 18:00:49 +02:00
|
|
|
|
2020-06-09 12:18:27 +02:00
|
|
|
watch.touch.reset_touch_data()
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-03-26 22:46:10 +01:00
|
|
|
def _tick(self):
|
|
|
|
"""Handle the system tick.
|
|
|
|
|
|
|
|
This function may be called frequently and includes short
|
|
|
|
circuit logic to quickly exit if we haven't reached a tick
|
|
|
|
expiry point.
|
|
|
|
"""
|
2020-03-22 13:37:19 +01:00
|
|
|
rtc = watch.rtc
|
2020-11-14 13:24:28 +01:00
|
|
|
update = rtc.update()
|
|
|
|
|
|
|
|
alarms = self._alarms
|
|
|
|
if update and alarms:
|
|
|
|
now = rtc.time()
|
|
|
|
head = alarms[0]
|
|
|
|
|
|
|
|
if head[0] <= now:
|
|
|
|
alarms.remove(head)
|
|
|
|
head[1]()
|
2020-03-07 12:52:42 +01:00
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
if self.sleep_at:
|
2020-11-14 13:24:28 +01:00
|
|
|
if update and self.tick_expiry:
|
2020-03-07 12:52:42 +01:00
|
|
|
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)
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-28 18:27:09 +01:00
|
|
|
state = self._button.get_event()
|
|
|
|
if None != state:
|
|
|
|
self._handle_button(state)
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
event = watch.touch.get_event()
|
2020-03-08 11:18:08 +01:00
|
|
|
if event:
|
2020-03-27 21:09:28 +01:00
|
|
|
self._handle_touch(event)
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-03-28 18:27:09 +01:00
|
|
|
if self.sleep_at and watch.rtc.uptime > self.sleep_at:
|
2020-03-27 21:07:47 +01:00
|
|
|
self.sleep()
|
2020-02-03 23:45:12 +01:00
|
|
|
|
|
|
|
gc.collect()
|
2020-02-03 20:24:09 +01:00
|
|
|
else:
|
2020-04-12 09:59:37 +02:00
|
|
|
if 1 == self._button.get_event() or \
|
2020-04-14 21:05:57 +02:00
|
|
|
self._charging != watch.battery.charging():
|
2020-03-27 21:07:47 +01:00
|
|
|
self.wake()
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-04-11 21:14:30 +02:00
|
|
|
def run(self, no_except=True):
|
2020-03-08 11:18:08 +01:00
|
|
|
"""Run the system manager synchronously.
|
|
|
|
|
|
|
|
This allows all watch management activities to handle in the
|
|
|
|
normal execution context meaning any exceptions and other problems
|
|
|
|
can be observed interactively via the console.
|
|
|
|
"""
|
2020-12-12 22:35:45 +01:00
|
|
|
global free
|
|
|
|
|
2020-07-01 00:04:01 +02:00
|
|
|
if self._scheduling:
|
|
|
|
print('Watch already running in the background')
|
|
|
|
return
|
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
if not self.app:
|
2020-04-12 09:59:37 +02:00
|
|
|
self.switch(self.quick_ring[0])
|
2020-12-12 22:35:45 +01:00
|
|
|
if watch.free:
|
|
|
|
gc.collect()
|
|
|
|
free = gc.mem_free()
|
2020-03-22 13:37:19 +01:00
|
|
|
|
2020-03-26 22:46:10 +01:00
|
|
|
# Reminder: wasptool uses this string to confirm the device has
|
|
|
|
# been set running again.
|
2020-03-22 13:37:19 +01:00
|
|
|
print('Watch is running, use Ctrl-C to stop')
|
|
|
|
|
2020-04-11 21:14:30 +02:00
|
|
|
if not no_except:
|
|
|
|
# This is a simplified (uncommented) version of the loop
|
|
|
|
# below
|
|
|
|
while True:
|
|
|
|
self._tick()
|
|
|
|
machine.deepsleep()
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
while True:
|
2020-04-11 21:14:30 +02:00
|
|
|
try:
|
|
|
|
self._tick()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise
|
|
|
|
except Exception as e:
|
2020-04-18 16:32:46 +02:00
|
|
|
# Only print the exception if the watch provides a way to do so!
|
|
|
|
if 'print_exception' in dir(watch):
|
|
|
|
watch.print_exception(e)
|
2020-04-11 21:14:30 +02:00
|
|
|
self.switch(CrashApp(e))
|
|
|
|
|
2020-03-26 22:46:10 +01:00
|
|
|
# Currently there is no code to control how fast the system
|
|
|
|
# ticks. In other words this code will break if we improve the
|
2020-07-01 00:04:01 +02:00
|
|
|
# power management... we are currently relying on not being able
|
2020-03-26 22:46:10 +01:00
|
|
|
# to stay in the low-power state for very long.
|
2020-02-03 20:24:09 +01:00
|
|
|
machine.deepsleep()
|
2020-03-22 13:37:19 +01:00
|
|
|
|
2020-07-01 00:04:01 +02:00
|
|
|
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."""
|
2020-12-12 22:35:45 +01:00
|
|
|
global free
|
|
|
|
|
2020-07-01 00:04:01 +02:00
|
|
|
if not self.app:
|
|
|
|
self.switch(self.quick_ring[0])
|
2020-12-12 22:35:45 +01:00
|
|
|
if watch.free:
|
|
|
|
gc.collect()
|
|
|
|
free = gc.mem_free()
|
2020-07-01 00:04:01 +02:00
|
|
|
|
|
|
|
if enable:
|
|
|
|
watch.schedule = self._schedule
|
|
|
|
else:
|
|
|
|
watch.schedule = watch.nop
|
|
|
|
|
|
|
|
self._scheduling = enable
|
|
|
|
|
2020-12-05 19:27:55 +01:00
|
|
|
def set_theme(self, new_theme) -> bool:
|
|
|
|
"""Sets the system theme.
|
|
|
|
|
|
|
|
Accepts anything that supports indexing,
|
|
|
|
and has a len() equivalent to the default theme."""
|
|
|
|
if len(self._theme) != len(new_theme):
|
|
|
|
return False
|
|
|
|
self._theme = new_theme
|
|
|
|
return True
|
|
|
|
|
|
|
|
def theme(self, theme_part: str) -> int:
|
|
|
|
"""Returns the relevant part of theme. For more see ../tools/themer.py"""
|
|
|
|
theme_parts = ("ble",
|
|
|
|
"scroll-indicator",
|
2020-12-13 17:46:19 +01:00
|
|
|
"battery",
|
2020-12-05 19:27:55 +01:00
|
|
|
"status-clock",
|
|
|
|
"notify-icon",
|
2020-12-31 20:12:38 +01:00
|
|
|
"bright",
|
|
|
|
"mid",
|
|
|
|
"ui",
|
|
|
|
"spot1",
|
|
|
|
"spot2",
|
|
|
|
"contrast")
|
2020-12-05 19:27:55 +01:00
|
|
|
if theme_part not in theme_parts:
|
|
|
|
raise IndexError('Theme part {} does not exist'.format(theme_part))
|
|
|
|
idx = theme_parts.index(theme_part) * 2
|
2020-12-31 11:09:38 +01:00
|
|
|
return (self._theme[idx] << 8) | self._theme[idx+1]
|
2020-12-05 19:27:55 +01:00
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
system = Manager()
|