summaryrefslogtreecommitdiff
path: root/wasp/widgets.py
diff options
context:
space:
mode:
authorDaniel Thompson <daniel@redfelineninja.org.uk>2020-10-21 20:08:12 (GMT)
committerDaniel Thompson <daniel@redfelineninja.org.uk>2020-10-21 20:10:16 (GMT)
commit6cd1e86295359f2c80b479375c6a728adffaea80 (patch)
tree090252b4162b56b91919ff1eaa4662a819c302e1 /wasp/widgets.py
parent2b244ec2a3ed612ec6f784ed092549a7174d15c2 (diff)
widgets: clock: Add a simple clock widget
This is intended to be reused by any app that shows the clock as part of the status bar at the top of the display. Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'wasp/widgets.py')
-rw-r--r--wasp/widgets.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/wasp/widgets.py b/wasp/widgets.py
index 7b6fc04..2e56d4d 100644
--- a/wasp/widgets.py
+++ b/wasp/widgets.py
@@ -8,9 +8,11 @@ The widget library allows common fragments of logic and drawing code to be
shared between applications.
"""
+import fonts
import icons
import wasp
import watch
+
from micropython import const
class BatteryMeter:
@@ -70,6 +72,47 @@ class BatteryMeter:
self.level = level
+class Clock:
+ """Small clock widget."""
+ def __init__(self, enabled=True):
+ self.on_screen = None
+ self.enabled = enabled
+
+ def draw(self):
+ """Redraw the clock from scratch.
+
+ The container is required to clear the canvas prior to the redraw
+ and the clock is only drawn if it is enabled.
+ """
+ self.on_screen = None
+ self.update()
+
+ def update(self):
+ """Update the clock widget if needed.
+
+ This is a lazy update that only redraws if the time has changes
+ since the last call *and* the clock is enabled.
+
+ :returns: An time tuple if the time has changed since the last call,
+ None otherwise.
+ """
+ now = wasp.watch.rtc.get_localtime()
+ on_screen = self.on_screen
+
+ if on_screen and on_screen == now:
+ return None
+
+ if self.enabled and (not on_screen or now[4] != on_screen[4]):
+ t1 = '{:02}:{:02}'.format(now[3], now[4])
+
+ draw = wasp.watch.drawable
+ draw.set_font(fonts.sans28)
+ draw.set_color(0xe73c)
+ draw.string(t1, 52, 12, 138)
+
+ self.on_screen = now
+ return now
+
class NotificationBar:
"""Show BT status and if there are pending notifications."""
def __init__(self, x=8, y=8):