1
0
Fork 0
wasp-os/wasp/apps/clock.py

99 lines
2.5 KiB
Python
Raw Normal View History

# 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.
"""
import wasp
import icons
import fonts.clock as digits
DIGITS = (
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-19 20:54:27 +01:00
MONTH = 'JanFebMarAprMayJunJulAugSepOctNovDec'
class ClockApp():
2020-02-23 21:51:58 +01:00
"""Simple digital clock application.
.. figure:: res/ClockApp.png
:width: 179
Screenshot of the clock application
2020-02-23 21:51:58 +01:00
"""
NAME = 'Clock'
ICON = icons.clock
def __init__(self):
self.meter = wasp.widgets.BatteryMeter()
self.notifier = wasp.widgets.StatusBar()
def foreground(self):
"""Activate the application."""
self.on_screen = ( -1, -1, -1, -1, -1, -1 )
self.draw()
wasp.system.request_tick(1000)
def sleep(self):
return True
def wake(self):
self.update()
def tick(self, ticks):
self.update()
def draw(self):
2020-02-23 21:51:58 +01:00
"""Redraw the display from scratch."""
draw = wasp.watch.drawable
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 )
self.update()
self.meter.draw()
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.
"""
now = wasp.watch.rtc.get_localtime()
2020-02-19 20:54:27 +01:00
if now[3] == self.on_screen[3] and now[4] == self.on_screen[4]:
if now[5] != self.on_screen[5]:
self.meter.update()
self.notifier.update()
self.on_screen = now
return False
draw = wasp.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)
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)
self.meter.update()
self.notifier.update()
return True