2020-02-19 20:37:31 +01:00
|
|
|
import fonts.clock as digits
|
2020-03-07 12:52:42 +01:00
|
|
|
import watch
|
2020-02-03 23:35:16 +01:00
|
|
|
import widgets
|
2020-03-07 12:52:42 +01:00
|
|
|
import manager
|
2020-02-03 20:24:09 +01:00
|
|
|
|
|
|
|
DIGITS = (
|
2020-02-19 20:37:31 +01:00
|
|
|
digits.clock_0,
|
|
|
|
digits.clock_1,
|
|
|
|
digits.clock_2,
|
|
|
|
digits.clock_3,
|
|
|
|
digits.clock_4,
|
|
|
|
digits.clock_5,
|
|
|
|
digits.clock_6,
|
|
|
|
digits.clock_7,
|
|
|
|
digits.clock_8,
|
|
|
|
digits.clock_9
|
2020-02-03 20:24:09 +01:00
|
|
|
)
|
|
|
|
|
2020-02-19 20:54:27 +01:00
|
|
|
MONTH = 'JanFebMarAprMayJunJulAugSepOctNovDec'
|
|
|
|
|
2020-02-03 20:24:09 +01:00
|
|
|
class ClockApp(object):
|
2020-02-23 21:51:58 +01:00
|
|
|
"""Simple digital clock application.
|
|
|
|
|
|
|
|
Shows a time (as HH:MM) together with a battery meter and the date.
|
|
|
|
"""
|
2020-02-03 20:24:09 +01:00
|
|
|
|
|
|
|
def __init__(self):
|
2020-02-03 23:35:16 +01:00
|
|
|
self.meter = widgets.BatteryMeter()
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-07 12:52:42 +01:00
|
|
|
def handle_event(self, event_view):
|
|
|
|
"""Process events that the app is subscribed to."""
|
|
|
|
if event_view[0] == manager.EVENT_TICK:
|
|
|
|
self.update()
|
|
|
|
else:
|
|
|
|
# TODO: Raise an unexpected event exception
|
|
|
|
pass
|
|
|
|
|
|
|
|
def foreground(self, manager, effect=None):
|
|
|
|
"""Activate the application."""
|
|
|
|
self.on_screen = ( -1, -1, -1, -1, -1, -1 )
|
|
|
|
self.draw(effect)
|
|
|
|
manager.request_tick(1000)
|
|
|
|
|
|
|
|
def tick(self, ticks):
|
|
|
|
self.update()
|
|
|
|
|
|
|
|
def background(self):
|
|
|
|
"""De-activate the application (without losing state)."""
|
|
|
|
pass
|
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
def sleep(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def wake(self):
|
|
|
|
self.update()
|
|
|
|
|
2020-03-07 12:52:42 +01:00
|
|
|
def draw(self, effect=None):
|
2020-02-23 21:51:58 +01:00
|
|
|
"""Redraw the display from scratch."""
|
2020-03-09 01:00:13 +01:00
|
|
|
draw = watch.drawable
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
draw.fill()
|
|
|
|
draw.rleblit(digits.clock_colon, pos=(2*48, 80), fg=0xb5b6)
|
2020-02-19 20:54:27 +01:00
|
|
|
self.on_screen = ( -1, -1, -1, -1, -1, -1 )
|
2020-03-07 12:52:42 +01:00
|
|
|
self.update()
|
2020-02-03 23:35:16 +01:00
|
|
|
self.meter.draw()
|
|
|
|
|
2020-03-07 12:52:42 +01:00
|
|
|
def update(self):
|
2020-02-23 21:51:58 +01:00
|
|
|
"""Update the display (if needed).
|
|
|
|
|
|
|
|
The updates are a lazy as possible and rely on an prior call to
|
|
|
|
draw() to ensure the screen is suitably prepared.
|
|
|
|
"""
|
2020-02-19 20:54:27 +01:00
|
|
|
now = watch.rtc.get_localtime()
|
|
|
|
if now[3] == self.on_screen[3] and now[4] == self.on_screen[4]:
|
2020-02-23 21:19:37 +01:00
|
|
|
if now[5] != self.on_screen[5]:
|
2020-02-04 09:47:14 +01:00
|
|
|
self.meter.update()
|
2020-02-23 21:19:37 +01:00
|
|
|
self.on_screen = now
|
2020-02-04 09:47:14 +01:00
|
|
|
return False
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-09 01:00:13 +01:00
|
|
|
draw = watch.drawable
|
|
|
|
draw.rleblit(DIGITS[now[4] % 10], pos=(4*48, 80))
|
|
|
|
draw.rleblit(DIGITS[now[4] // 10], pos=(3*48, 80), fg=0xbdb6)
|
|
|
|
draw.rleblit(DIGITS[now[3] % 10], pos=(1*48, 80))
|
|
|
|
draw.rleblit(DIGITS[now[3] // 10], pos=(0*48, 80), fg=0xbdb6)
|
2020-02-03 20:24:09 +01:00
|
|
|
self.on_screen = now
|
|
|
|
|
2020-02-19 20:54:27 +01:00
|
|
|
month = now[1] - 1
|
|
|
|
month = MONTH[month*3:(month+1)*3]
|
2020-02-23 21:51:58 +01:00
|
|
|
draw.string('{} {} {}'.format(now[2], month, now[0]),
|
2020-02-19 20:54:27 +01:00
|
|
|
0, 180, width=240)
|
|
|
|
|
2020-02-03 23:35:16 +01:00
|
|
|
self.meter.update()
|
2020-02-04 09:47:14 +01:00
|
|
|
return True
|