1
0
Fork 0

apps: fibonacci clock: Updates to structure like clock app

Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
Daniel Thompson 2020-10-25 08:55:00 +00:00
parent 1b99764e8d
commit f95f45cda3

View file

@ -1,6 +1,8 @@
# SPDX-License-Identifier: LGPL-3.0-or-later # SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2020 Johannes Wache # Copyright (C) 2020 Johannes Wache
"""Fibonacci clock """Fibonacci clock
~~~~~~~~~~~~~~~~~~
The Fibonacci sequence is a sequence of numbers created by the Italian The Fibonacci sequence is a sequence of numbers created by the Italian
mathematician Fibonacci in the 13th century. This is a sequence starting with mathematician Fibonacci in the 13th century. This is a sequence starting with
@ -22,6 +24,8 @@ result by 5 to get the actual number.
import wasp import wasp
import icons import icons
COLORS = [0xffff,0xf800,0x07e0,0x001f] # White, red, green and blue
FIELDS = b'\x05\x03\x02\x01\x01'
MONTH = 'JanFebMarAprMayJunJulAugSepOctNovDec' MONTH = 'JanFebMarAprMayJunJulAugSepOctNovDec'
# 2-bit RLE, generated from res/fibo_icon.png, 246 bytes # 2-bit RLE, generated from res/fibo_icon.png, 246 bytes
@ -48,54 +52,45 @@ icon = (
class FibonacciClockApp(): class FibonacciClockApp():
"""Displays the time as a Fibonacci Clock """Displays the time as a Fibonacci Clock
.. figure:: res/FiboApp.png
:width: 179
Screenshot of the fibonacci clock application
""" """
NAME = 'Fibo' NAME = 'Fibo'
ICON = icon ICON = icon
def __init__(self): def __init__(self):
self.meter = wasp.widgets.BatteryMeter() self._bar = wasp.widgets.StatusBar()
self.notifier = wasp.widgets.NotificationBar()
self.fields = b'\x05\x03\x02\x01\x01'
self.color_codes = [0xffff,0xf800,0x07e0,0x001f] # White, red, green and blue
def foreground(self): def foreground(self):
"""Activate the application.""" """Activate the application."""
self.on_screen = ( -1, -1, -1, -1, -1, -1 ) self._bar.clock = False
self.draw() self._draw(True)
wasp.system.request_tick(1000) wasp.system.request_tick(1000)
def sleep(self): def sleep(self):
return True return True
def wake(self): def wake(self):
self.update() self._draw()
def tick(self, ticks): def tick(self, ticks):
self.update() self._draw()
def draw(self): def _draw(self, redraw=False):
"""Redraw the display from scratch.""" """Draw or lazily update the display."""
draw = wasp.watch.drawable draw = wasp.watch.drawable
draw.fill() if redraw:
self.on_screen = ( -1, -1, -1, -1, -1, -1 ) now = wasp.watch.rtc.get_localtime()
self.update() draw.fill()
self.meter.draw() self._bar.draw()
else:
def update(self): now = self._bar.update()
"""Update the display (if needed). if not now or self._min == now[4]:
return
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()
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
#calculate colors of fields: #calculate colors of fields:
field_colors = bytearray(5) field_colors = bytearray(5)
@ -104,27 +99,24 @@ class FibonacciClockApp():
if (hr >= 12): if (hr >= 12):
hr -= 12 hr -= 12
for i in range(5): for i in range(5):
if ((hr - self.fields[i]) >= 0): if ((hr - FIELDS[i]) >= 0):
hr -= self.fields[i] hr -= FIELDS[i]
field_colors[i] += 1 field_colors[i] += 1
if ((mn - self.fields[i]) >= 0): if ((mn - FIELDS[i]) >= 0):
mn -= self.fields[i] mn -= FIELDS[i]
field_colors[i] += 2 field_colors[i] += 2
draw.fill(x=71,y=60,w=23,h=23,bg=self.color_codes[field_colors[4]]) # 1 field draw.fill(x=71,y=60,w=23,h=23,bg=COLORS[field_colors[4]]) # 1 field
draw.fill(x=71,y=85,w=23,h=23,bg=self.color_codes[field_colors[3]]) # 1 field draw.fill(x=71,y=85,w=23,h=23,bg=COLORS[field_colors[3]]) # 1 field
draw.fill(x=21,y=60,w=48,h=48,bg=self.color_codes[field_colors[2]]) # 2 field draw.fill(x=21,y=60,w=48,h=48,bg=COLORS[field_colors[2]]) # 2 field
draw.fill(x=21,y=110,w=73,h=73,bg=self.color_codes[field_colors[1]]) # 3 field draw.fill(x=21,y=110,w=73,h=73,bg=COLORS[field_colors[1]]) # 3 field
draw.fill(x=96,y=60,w=123,h=123,bg=self.color_codes[field_colors[0]]) # 5 field draw.fill(x=96,y=60,w=123,h=123,bg=COLORS[field_colors[0]]) # 5 field
self.on_screen = now
month = now[1] - 1 month = now[1] - 1
month = MONTH[month*3:(month+1)*3] month = MONTH[month*3:(month+1)*3]
draw.string('{} {} {}'.format(now[2], month, now[0]), draw.string('{} {} {}'.format(now[2], month, now[0]),
0, 202, width=240) 0, 202, width=240)
self.meter.update() # Record the minute that is currently being displayed
self.notifier.update() self._min = now[4]
return True