2020-03-22 16:40:18 +01:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2020-05-14 23:29:35 +02:00
|
|
|
"""Digital clock
|
|
|
|
~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Shows a time (as HH:MM) together with a battery meter and the date.
|
|
|
|
"""
|
|
|
|
|
2020-03-22 13:37:19 +01:00
|
|
|
import wasp
|
|
|
|
|
2020-04-06 23:03:05 +02:00
|
|
|
import icons
|
2020-02-19 20:37:31 +01:00
|
|
|
import fonts.clock as digits
|
2020-02-03 20:24:09 +01:00
|
|
|
|
|
|
|
DIGITS = (
|
2020-10-25 09:49:06 +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-03-22 13:37:19 +01:00
|
|
|
class ClockApp():
|
2020-02-23 21:51:58 +01:00
|
|
|
"""Simple digital clock application.
|
2020-08-16 19:48:15 +02:00
|
|
|
|
|
|
|
.. figure:: res/ClockApp.png
|
|
|
|
:width: 179
|
|
|
|
|
|
|
|
Screenshot of the clock application
|
2020-02-23 21:51:58 +01:00
|
|
|
"""
|
2020-04-06 23:03:05 +02:00
|
|
|
NAME = 'Clock'
|
|
|
|
ICON = icons.clock
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-03-28 18:19:34 +01:00
|
|
|
def foreground(self):
|
2020-10-25 09:49:06 +01:00
|
|
|
"""Activate the application.
|
|
|
|
|
|
|
|
Configure the status bar, redraw the display and request a periodic
|
|
|
|
tick callback every second.
|
|
|
|
"""
|
2020-11-04 20:00:37 +01:00
|
|
|
wasp.system.bar.clock = False
|
2020-10-25 09:49:06 +01:00
|
|
|
self._draw(True)
|
2020-03-22 13:37:19 +01:00
|
|
|
wasp.system.request_tick(1000)
|
2020-03-07 12:52:42 +01:00
|
|
|
|
2020-03-08 11:18:08 +01:00
|
|
|
def sleep(self):
|
2020-10-25 09:49:06 +01:00
|
|
|
"""Prepare to enter the low power mode.
|
|
|
|
|
|
|
|
:returns: True, which tells the system manager not to automatically
|
|
|
|
switch to the default application before sleeping.
|
|
|
|
"""
|
2020-03-08 11:18:08 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
def wake(self):
|
2020-10-25 09:49:06 +01:00
|
|
|
"""Return from low power mode.
|
2020-03-08 11:18:08 +01:00
|
|
|
|
2020-10-25 09:49:06 +01:00
|
|
|
Time will have changes whilst we have been asleep so we must
|
|
|
|
udpate the display (but there is no need for a full redraw because
|
|
|
|
the display RAM is preserved during a sleep.
|
|
|
|
"""
|
|
|
|
self._draw()
|
2020-02-03 20:24:09 +01:00
|
|
|
|
2020-10-25 09:49:06 +01:00
|
|
|
def tick(self, ticks):
|
|
|
|
"""Periodic callback to update the display."""
|
|
|
|
self._draw()
|
2020-02-03 23:35:16 +01:00
|
|
|
|
2020-10-25 09:49:06 +01:00
|
|
|
def _draw(self, redraw=False):
|
|
|
|
"""Draw or lazily update the display.
|
2020-02-23 21:51:58 +01:00
|
|
|
|
2020-10-25 09:49:06 +01:00
|
|
|
The updates are as lazy by default and avoid spending time redrawing
|
|
|
|
if the time on display has not changed. However if redraw is set to
|
|
|
|
True then a full redraw is be performed.
|
2020-02-23 21:51:58 +01:00
|
|
|
"""
|
2020-03-22 13:37:19 +01:00
|
|
|
draw = wasp.watch.drawable
|
2020-12-13 17:39:29 +01:00
|
|
|
hi = wasp.system.theme('accent-hi')
|
|
|
|
mid = wasp.system.theme('accent-mid')
|
|
|
|
lo = wasp.system.theme('accent-lo')
|
2020-10-25 09:49:06 +01:00
|
|
|
|
|
|
|
if redraw:
|
|
|
|
now = wasp.watch.rtc.get_localtime()
|
|
|
|
|
|
|
|
# Clear the display and draw that static parts of the watch face
|
|
|
|
draw.fill()
|
2020-12-13 17:39:29 +01:00
|
|
|
draw.rleblit(digits.clock_colon, pos=(2*48, 80), fg=mid)
|
2020-10-25 09:49:06 +01:00
|
|
|
|
|
|
|
# Redraw the status bar
|
2020-11-04 20:00:37 +01:00
|
|
|
wasp.system.bar.draw()
|
2020-10-25 09:49:06 +01:00
|
|
|
else:
|
|
|
|
# The update is doubly lazy... we update the status bar and if
|
|
|
|
# the status bus update reports a change in the time of day
|
|
|
|
# then we compare the minute on display to make sure we
|
|
|
|
# only update the main clock once per minute.
|
2020-11-04 20:00:37 +01:00
|
|
|
now = wasp.system.bar.update()
|
2020-10-25 09:49:06 +01:00
|
|
|
if not now or self._min == now[4]:
|
|
|
|
# Skip the update
|
|
|
|
return
|
|
|
|
|
|
|
|
# Format the month as text
|
|
|
|
month = now[1] - 1
|
|
|
|
month = MONTH[month*3:(month+1)*3]
|
|
|
|
|
|
|
|
# Draw the changeable parts of the watch face
|
2020-12-13 17:39:29 +01:00
|
|
|
draw.rleblit(DIGITS[now[4] % 10], pos=(4*48, 80), fg=hi)
|
|
|
|
draw.rleblit(DIGITS[now[4] // 10], pos=(3*48, 80), fg=lo)
|
|
|
|
draw.rleblit(DIGITS[now[3] % 10], pos=(1*48, 80), fg=hi)
|
|
|
|
draw.rleblit(DIGITS[now[3] // 10], pos=(0*48, 80), fg=lo)
|
|
|
|
draw.set_color(hi)
|
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-10-25 09:49:06 +01:00
|
|
|
# Record the minute that is currently being displayed
|
|
|
|
self._min = now[4]
|