From 6cd1e86295359f2c80b479375c6a728adffaea80 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Wed, 21 Oct 2020 21:08:12 +0100 Subject: [PATCH] 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 --- wasp/widgets.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) 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):