From 9099c2398e5a169db95ef6b36cda4c44e47a32d1 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Sun, 20 Jun 2021 17:03:05 +0100 Subject: [PATCH] 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 --- wasp/apps/stopwatch.py | 58 +++++------------------------ wasp/boards/simulator/test_smoke.py | 4 +- wasp/widgets.py | 57 ++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 50 deletions(-) diff --git a/wasp/apps/stopwatch.py b/wasp/apps/stopwatch.py index c46226e..3aa24d3 100644 --- a/wasp/apps/stopwatch.py +++ b/wasp/apps/stopwatch.py @@ -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() diff --git a/wasp/boards/simulator/test_smoke.py b/wasp/boards/simulator/test_smoke.py index bc33eb7..e0afa30 100644 --- a/wasp/boards/simulator/test_smoke.py +++ b/wasp/boards/simulator/test_smoke.py @@ -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() diff --git a/wasp/widgets.py b/wasp/widgets.py index 970ab74..496b229 100644 --- a/wasp/widgets.py +++ b/wasp/widgets.py @@ -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."""