2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
import gc
|
|
|
|
import machine
|
2020-03-22 13:37:19 +01:00
|
|
|
import watch
|
|
|
|
import widgets
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-08 18:37:43 +01:00
|
|
|
from apps.clock import ClockApp
|
|
|
|
from apps.flashlight import FlashlightApp
|
2020-03-08 21:48:48 +01:00
|
|
|
from apps.testapp import TestApp
|
2020-03-08 18:37:43 +01:00
|
|
|
|
2020-03-26 21:42:03 +01:00
|
|
|
class Direction():
|
|
|
|
DOWN = 1
|
|
|
|
UP = 2
|
|
|
|
LEFT = 3
|
|
|
|
RIGHT = 4
|
|
|
|
|
|
|
|
class Event():
|
|
|
|
TOUCH = 0x0001
|
|
|
|
SWIPE_LEFTRIGHT = 0x0002
|
|
|
|
SWIPE_UPDOWN = 0x0004
|
|
|
|
BUTTON = 0x0008
|
|
|
|
|
|
|
|
class Manager():
|
2020-03-22 13:37:19 +01:00
|
|
|
def __init__(self):
|
2020-03-26 21:42:03 +01:00
|
|
|
"""System management
|
|
|
|
"""
|
2020-03-07 12:52:42 +01:00
|
|
|
self.app = None
|
2020-03-08 11:18:08 +01:00
|
|
|
|
|
|
|
self.applications = [
|
2020-03-08 18:37:43 +01:00
|
|
|
ClockApp(),
|
|
|
|
FlashlightApp(),
|
2020-03-08 21:48:48 +01:00
|
|
|
TestApp()
|
2020-03-08 11:18:08 +01:00
|
|
|
]
|
2020-02-04 09:49:10 +01:00
|
|
|
self.charging = True
|
2020-02-03 20:24:09 +01:00
|
|
|
|
|
|
|
def switch(self, app):
|
2020-03-07 12:52:42 +01:00
|
|
|
if self.app:
|
2020-03-08 11:18:08 +01:00
|
|
|
self.app.background()
|
2020-03-22 13:37:19 +01:00
|
|
|
else:
|
|
|
|
# System start up...
|
|
|
|
watch.display.poweron()
|
|
|
|
watch.display.mute(True)
|
|
|
|
watch.backlight.set(2)
|
|
|
|
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)
|
|
|
|
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):
|
|
|
|
"""Navigate between different applications.
|
|
|
|
|
|
|
|
Currently the direction is ignored.
|
|
|
|
"""
|
|
|
|
app_list = self.applications
|
|
|
|
|
2020-03-26 21:42:03 +01:00
|
|
|
if direction == Direction.LEFT:
|
2020-03-08 11:18:08 +01:00
|
|
|
i = app_list.index(self.app) + 1
|
|
|
|
if i >= len(app_list):
|
|
|
|
i = 0
|
|
|
|
self.switch(app_list[i])
|
2020-03-26 21:42:03 +01:00
|
|
|
elif direction == Direction.RIGHT:
|
2020-03-08 11:18:08 +01:00
|
|
|
i = app_list.index(self.app) - 1
|
|
|
|
if i < 0:
|
|
|
|
i = len(app_list)-1
|
|
|
|
self.switch(app_list[i])
|
|
|
|
|
|
|
|
def request_event(self, event_mask):
|
|
|
|
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-08 11:18:08 +01:00
|
|
|
def handle_event(self, event):
|
2020-03-22 13:37:19 +01:00
|
|
|
self.sleep_at = watch.rtc.uptime + 15
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-03-08 21:48:48 +01:00
|
|
|
event_mask = self.event_mask
|
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-26 21:42:03 +01:00
|
|
|
if (bool(event_mask & Event.SWIPE_UPDOWN) and updown) or \
|
|
|
|
(bool(event_mask & Event.SWIPE_LEFTRIGHT) and not updown):
|
2020-03-08 21:48:48 +01:00
|
|
|
if not self.app.swipe(event):
|
|
|
|
self.navigate(event[0])
|
|
|
|
else:
|
|
|
|
self.navigate(event[0])
|
2020-03-26 21:42:03 +01:00
|
|
|
elif event[0] == 5 and self.event_mask & Event.TOUCH:
|
2020-03-08 11:18:08 +01:00
|
|
|
self.app.touch(event)
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
def tick(self):
|
2020-03-22 13:37:19 +01:00
|
|
|
rtc = watch.rtc
|
2020-03-07 12:52:42 +01:00
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
if self.sleep_at:
|
2020-03-07 12:52:42 +01:00
|
|
|
if rtc.update() and self.tick_expiry:
|
|
|
|
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-22 13:37:19 +01:00
|
|
|
if watch.button.value():
|
|
|
|
self.sleep_at = watch.rtc.uptime + 15
|
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:
|
|
|
|
self.handle_event(event)
|
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
if watch.rtc.uptime > self.sleep_at:
|
|
|
|
watch.backlight.set(0)
|
2020-03-08 11:18:08 +01:00
|
|
|
if not self.app.sleep():
|
|
|
|
self.switch(self.applications[0])
|
|
|
|
self.app.sleep()
|
2020-03-22 13:37:19 +01:00
|
|
|
watch.display.poweroff()
|
|
|
|
self.charging = watch.battery.charging()
|
2020-02-03 20:24:09 +01:00
|
|
|
self.sleep_at = None
|
2020-02-03 23:45:12 +01:00
|
|
|
|
|
|
|
gc.collect()
|
2020-02-03 20:24:09 +01:00
|
|
|
else:
|
2020-03-22 13:37:19 +01:00
|
|
|
watch.rtc.update()
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
charging = watch.battery.charging()
|
|
|
|
if watch.button.value() or self.charging != charging:
|
|
|
|
watch.display.poweron()
|
2020-03-08 11:18:08 +01:00
|
|
|
self.app.wake()
|
2020-03-22 13:37:19 +01:00
|
|
|
watch.backlight.set(2)
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
# Discard any pending touch events
|
2020-03-22 13:37:19 +01:00
|
|
|
_ = watch.touch.get_event()
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
self.sleep_at = watch.rtc.uptime + 15
|
2020-02-03 20:24:09 +01:00
|
|
|
|
|
|
|
def run(self):
|
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-03-22 13:37:19 +01:00
|
|
|
if not self.app:
|
|
|
|
self.switch(self.applications[0])
|
|
|
|
|
|
|
|
print('Watch is running, use Ctrl-C to stop')
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
while True:
|
|
|
|
self.tick()
|
|
|
|
machine.deepsleep()
|
2020-03-22 13:37:19 +01:00
|
|
|
|
|
|
|
system = Manager()
|