summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Thompson <daniel@redfelineninja.org.uk>2020-11-14 12:25:32 (GMT)
committerDaniel Thompson <daniel@redfelineninja.org.uk>2020-11-14 12:25:32 (GMT)
commitdd5a1e75af9b1d9a3ce3b302743af2d0e5f290f8 (patch)
treef3e75f8b18bf08e78c330305b0a52ff0d9eaa69c
parentd0a99d5636e9e597a44d872b5350c3588b032439 (diff)
apps: steps: Use the alarm system to reset the step counter at midnight
This is way better than zeroing it the first time in the day we use the step counter! Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
-rw-r--r--wasp/apps/steps.py37
1 files changed, 29 insertions, 8 deletions
diff --git a/wasp/apps/steps.py b/wasp/apps/steps.py
index cd888bd..ed83b43 100644
--- a/wasp/apps/steps.py
+++ b/wasp/apps/steps.py
@@ -43,14 +43,41 @@ class StepCounterApp():
def __init__(self):
watch.accel.reset()
self._count = 0
- self._prev_day = -1
+ self._wake = 0
def foreground(self):
- """Activate the application."""
+ """Cancel the alarm and draw the application.
+
+ Cancelling the alarm has two effects. Firstly it ensures the
+ step count won't change whilst we are watching it and, secondly
+ it ensures that if the time of day has been set to a value in
+ the past the we reconfigure the alarm.
+
+ This does in the side effect that if the application of open at
+ midnight then the reset doesn't happen for that day.
+ """
+ wasp.system.cancel_alarm(self._wake, self._reset)
wasp.system.bar.clock = True
self._draw()
wasp.system.request_tick(1000)
+ def background(self):
+ """Set an alarm to trigger at midnight and reset the counter."""
+ now = watch.rtc.get_localtime()
+ yyyy = now[0]
+ mm = now[1]
+ dd = now[2]
+ then = (yyyy, mm, dd+1, 0, 0, 0, 0, 0, 0)
+
+ self._wake = time.mktime(then)
+ wasp.system.set_alarm(self._wake, self._reset)
+
+ def _reset(self):
+ """"Reset the step counter and re-arm the alarm."""
+ watch.accel.steps = 0
+ self._wake += 24 * 60 * 60
+ wasp.system.set_alarm(self._wake, self._reset)
+
def tick(self, ticks):
self._count += 686;
self._update()
@@ -70,12 +97,6 @@ class StepCounterApp():
# Update the status bar
now = wasp.system.bar.update()
- # Reset the step counter if we have move onto the next day
- if now and now[2] != self._prev_day:
- watch.accel.steps = 0
- draw.fill(0, 60, 132-18, 180, 36)
- self._prev_day = now[2]
-
# Update the step count
count = watch.accel.steps
t = str(count)