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>
This commit is contained in:
Daniel Thompson 2020-10-21 21:08:12 +01:00
parent 2b244ec2a3
commit 6cd1e86295

View file

@ -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):