widgets: Refactor the stopwatch as a widget

This is purely a refactoring for the purposes of code reuse. No change
of behaviour is expected.

Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
This commit is contained in:
Daniel Thompson 2021-06-20 17:03:05 +01:00
parent 29d619aebc
commit 9099c2398e
3 changed files with 69 additions and 50 deletions

View file

@ -21,8 +21,8 @@ class StopwatchApp():
ICON = icons.app
def __init__(self):
self._timer = wasp.widgets.Stopwatch(120-36)
self._reset()
self._count = 0
def foreground(self):
"""Activate the application."""
@ -53,34 +53,27 @@ class StopwatchApp():
if not state:
return
if self._started_at:
self._update()
self._started_at = 0
if self._timer.started:
self._timer.stop()
else:
uptime = wasp.watch.rtc.get_uptime_ms()
uptime //= 10
self._started_at = uptime - self._count
self._update()
self._timer.start()
def touch(self, event):
if self._started_at:
self._update()
self._splits.insert(0, self._count)
if self._timer.started:
self._splits.insert(0, self._timer.count)
del self._splits[4:]
self._nsplits += 1
else:
self._reset()
self._update()
self._update()
self._draw_splits()
def tick(self, ticks):
self._update()
def _reset(self):
self._started_at = 0
self._count = 0
self._last_count = -1
self._timer.reset()
self._splits = []
self._nsplits = 0
@ -114,41 +107,10 @@ class StopwatchApp():
draw = wasp.watch.drawable
draw.fill()
self._last_count = -1
self._update()
wasp.system.bar.draw()
self._timer.draw()
self._draw_splits()
def _update(self):
# Before we do anything else let's make sure _count is
# up to date
if self._started_at:
uptime = wasp.watch.rtc.get_uptime_ms()
uptime //= 10
self._count = uptime - self._started_at
if self._count > 999*60*100:
self._reset()
# Update the statusbar
wasp.system.bar.update()
if self._last_count != self._count:
centisecs = self._count
secs = centisecs // 100
centisecs %= 100
minutes = secs // 60
secs %= 60
t1 = '{}:{:02}'.format(minutes, secs)
t2 = '{:02}'.format(centisecs)
draw = wasp.watch.drawable
draw.set_font(fonts.sans36)
draw.set_color(draw.lighten(wasp.system.theme('ui'), wasp.system.theme('contrast')))
w = fonts.width(fonts.sans36, t1)
draw.string(t1, 180-w, 120-36)
draw.fill(0, 0, 120-36, 180-w, 36)
draw.set_font(fonts.sans24)
draw.string(t2, 180, 120-36+18, width=46)
self._last_count = self._count
self._timer.update()

View file

@ -80,7 +80,7 @@ def test_stopwatch(system):
wasp.watch.button.value(0)
system.step()
assert(system.app._started_at > 0)
assert(system.app._timer._started_at > 0)
wasp.watch.button.value(1)
system.step()
@ -89,7 +89,7 @@ def test_stopwatch(system):
wasp.watch.button.value(0)
system.step()
assert(system.app._started_at == 0)
assert(system.app._timer._started_at == 0)
wasp.watch.button.value(1)
system.step()

View file

@ -452,6 +452,63 @@ class Spinner():
return False
class Stopwatch:
"""A stopwatch widget"""
def __init__(self, y):
self._y = y
self.reset()
def start(self):
uptime = wasp.watch.rtc.get_uptime_ms() // 10
self._started_at = uptime - self.count
def stop(self):
self._started_at = 0
@property
def started(self):
return bool(self._started_at)
def reset(self):
self.count = 0
self._started_at = 0
self._last_count = -1
def draw(self):
self._last_count = -1
self.update()
def update(self):
# Before we do anything else let's make sure count is
# up to date
if self._started_at:
uptime = wasp.watch.rtc.get_uptime_ms() // 10
self.count = uptime - self._started_at
if self.count > 999*60*100:
self.reset()
if self._last_count != self.count:
centisecs = self.count
secs = centisecs // 100
centisecs %= 100
minutes = secs // 60
secs %= 60
t1 = '{}:{:02}'.format(minutes, secs)
t2 = '{:02}'.format(centisecs)
y = self._y
draw = wasp.watch.drawable
draw.set_font(fonts.sans36)
draw.set_color(draw.lighten(wasp.system.theme('ui'), wasp.system.theme('contrast')))
w = fonts.width(fonts.sans36, t1)
draw.string(t1, 180-w, y)
draw.fill(0, 0, y, 180-w, 36)
draw.set_font(fonts.sans24)
draw.string(t2, 180, y+18, width=46)
self._last_count = self.count
class ConfirmationView:
"""Confirmation widget allowing user confirmation of a setting."""