2020-04-12 09:59:37 +02:00
|
|
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
# Copyright (C) 2020 Daniel Thompson
|
|
|
|
|
2021-01-13 22:51:17 +01:00
|
|
|
"""Stopwatch
|
|
|
|
~~~~~~~~~~~~
|
|
|
|
|
|
|
|
Simple stop/start watch with support for split times.
|
|
|
|
|
2023-03-07 02:02:07 +01:00
|
|
|
.. figure:: res/screenshots/StopwatchApp.png
|
2021-01-13 22:51:17 +01:00
|
|
|
:width: 179
|
|
|
|
"""
|
2020-04-12 09:59:37 +02:00
|
|
|
import wasp
|
|
|
|
import icons
|
2020-04-14 21:03:04 +02:00
|
|
|
import fonts
|
2020-04-12 09:59:37 +02:00
|
|
|
|
|
|
|
class StopwatchApp():
|
2021-01-13 22:51:17 +01:00
|
|
|
"""Stopwatch application."""
|
2020-12-31 17:18:13 +01:00
|
|
|
# Stopwatch requires too many pixels to fit into the launcher
|
2021-01-13 22:51:17 +01:00
|
|
|
|
2020-12-31 17:18:13 +01:00
|
|
|
NAME = 'Stopclock'
|
2020-04-12 09:59:37 +02:00
|
|
|
ICON = icons.app
|
|
|
|
|
2020-04-14 21:03:04 +02:00
|
|
|
def __init__(self):
|
2021-06-20 18:03:05 +02:00
|
|
|
self._timer = wasp.widgets.Stopwatch(120-36)
|
2020-04-14 21:03:04 +02:00
|
|
|
self._reset()
|
|
|
|
|
2020-04-12 09:59:37 +02:00
|
|
|
def foreground(self):
|
|
|
|
"""Activate the application."""
|
2020-11-04 20:00:37 +01:00
|
|
|
wasp.system.bar.clock = True
|
2020-04-12 09:59:37 +02:00
|
|
|
self._draw()
|
2020-04-14 21:03:04 +02:00
|
|
|
wasp.system.request_tick(97)
|
|
|
|
wasp.system.request_event(wasp.EventMask.TOUCH |
|
2020-08-15 21:43:22 +02:00
|
|
|
wasp.EventMask.BUTTON |
|
|
|
|
wasp.EventMask.NEXT)
|
2020-04-14 21:03:04 +02:00
|
|
|
|
|
|
|
def sleep(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def wake(self):
|
|
|
|
self._update()
|
|
|
|
|
2020-08-15 21:43:22 +02:00
|
|
|
def swipe(self, event):
|
|
|
|
"""Handle NEXT events by augmenting the default processing by resetting
|
|
|
|
the count if we are not currently timing something.
|
|
|
|
|
|
|
|
No other swipe event is possible for this application.
|
|
|
|
"""
|
2021-10-15 23:02:33 +02:00
|
|
|
if not self._timer._started_at:
|
2020-08-15 21:43:22 +02:00
|
|
|
self._reset()
|
|
|
|
return True # Request system default handling
|
|
|
|
|
2020-04-14 21:03:04 +02:00
|
|
|
def press(self, button, state):
|
|
|
|
if not state:
|
|
|
|
return
|
|
|
|
|
2021-06-20 18:03:05 +02:00
|
|
|
if self._timer.started:
|
|
|
|
self._timer.stop()
|
2020-04-14 21:03:04 +02:00
|
|
|
else:
|
2021-06-20 18:03:05 +02:00
|
|
|
self._timer.start()
|
2020-04-14 21:03:04 +02:00
|
|
|
|
|
|
|
def touch(self, event):
|
2021-06-20 18:03:05 +02:00
|
|
|
if self._timer.started:
|
|
|
|
self._splits.insert(0, self._timer.count)
|
2020-04-14 21:03:04 +02:00
|
|
|
del self._splits[4:]
|
2020-06-27 15:34:36 +02:00
|
|
|
self._nsplits += 1
|
2020-04-14 21:03:04 +02:00
|
|
|
else:
|
|
|
|
self._reset()
|
|
|
|
|
2021-06-20 18:03:05 +02:00
|
|
|
self._update()
|
2020-04-14 21:03:04 +02:00
|
|
|
self._draw_splits()
|
|
|
|
|
|
|
|
def tick(self, ticks):
|
|
|
|
self._update()
|
|
|
|
|
|
|
|
def _reset(self):
|
2021-06-20 18:03:05 +02:00
|
|
|
self._timer.reset()
|
2020-04-14 21:03:04 +02:00
|
|
|
self._splits = []
|
2020-06-27 15:34:36 +02:00
|
|
|
self._nsplits = 0
|
2020-04-14 21:03:04 +02:00
|
|
|
|
|
|
|
def _draw_splits(self):
|
|
|
|
draw = wasp.watch.drawable
|
|
|
|
splits = self._splits
|
|
|
|
if 0 == len(splits):
|
|
|
|
draw.fill(0, 0, 120, 240, 120)
|
|
|
|
return
|
2020-06-27 15:34:36 +02:00
|
|
|
y = 240 - 6 - (len(splits) * 24)
|
2021-10-15 23:02:33 +02:00
|
|
|
|
2020-12-31 20:12:38 +01:00
|
|
|
draw.set_font(fonts.sans24)
|
|
|
|
draw.set_color(wasp.system.theme('mid'))
|
|
|
|
|
2020-06-27 15:34:36 +02:00
|
|
|
n = self._nsplits
|
2020-04-14 21:03:04 +02:00
|
|
|
for i, s in enumerate(splits):
|
2020-06-27 15:34:36 +02:00
|
|
|
centisecs = s
|
|
|
|
secs = centisecs // 100
|
|
|
|
centisecs %= 100
|
|
|
|
minutes = secs // 60
|
|
|
|
secs %= 60
|
2020-04-14 21:03:04 +02:00
|
|
|
|
2020-06-27 15:34:36 +02:00
|
|
|
t = '# {} {:02}:{:02}.{:02}'.format(n, minutes, secs, centisecs)
|
|
|
|
n -= 1
|
2020-04-14 21:03:04 +02:00
|
|
|
|
|
|
|
w = fonts.width(fonts.sans24, t)
|
|
|
|
draw.string(t, 0, y + (i*24), 240)
|
2020-04-12 09:59:37 +02:00
|
|
|
|
|
|
|
def _draw(self):
|
|
|
|
"""Draw the display from scratch."""
|
|
|
|
draw = wasp.watch.drawable
|
|
|
|
draw.fill()
|
2020-04-14 21:03:04 +02:00
|
|
|
|
2020-11-04 20:00:37 +01:00
|
|
|
wasp.system.bar.draw()
|
2021-06-20 18:03:05 +02:00
|
|
|
self._timer.draw()
|
2020-04-14 21:03:04 +02:00
|
|
|
self._draw_splits()
|
|
|
|
|
|
|
|
def _update(self):
|
2020-11-04 20:00:37 +01:00
|
|
|
wasp.system.bar.update()
|
2021-06-20 18:03:05 +02:00
|
|
|
self._timer.update()
|