# SPDX-License-Identifier: LGPL-3.0-or-later # Copyright (C) 2020 Wolfgang Ginolas """Timer Application ~~~~~~~~~~~~~~~~~~~~ An application to set a vibration in a specified amount of time. Like a kitchen timer. .. figure:: res/screenshots/TimerApp.png :width: 179 Screenshot of the Timer Application """ import wasp import fonts import time import widgets import math from micropython import const # 2-bit RLE, generated from res/timer_icon.png, 345 bytes icon = ( b'\x02' b'`@' b'?\xff\r@\xb4I?\x14Q?\rW?\x08[?' b'\x04_?\x00c= _BUTTON_Y: self._start() def _start(self): self.state = _RUNNING now = wasp.watch.rtc.time() self.current_alarm = now + self.minutes.value * 60 + self.seconds.value wasp.system.set_alarm(self.current_alarm, self._alert) self._draw() def _stop(self): self.state = _STOPPED wasp.system.cancel_alarm(self.current_alarm, self._alert) self._draw() def _draw(self): """Draw the display from scratch.""" draw = wasp.watch.drawable draw.fill() sbar = wasp.system.bar sbar.clock = True sbar.draw() if self.state == _RINGING: draw.set_font(fonts.sans24) draw.string(self.NAME, 0, 150, width=240) draw.blit(icon, 73, 50) elif self.state == _RUNNING: self._draw_stop(104, _BUTTON_Y) draw.string(':', 110, 120-14, width=20) self._update() else: # _STOPPED draw.set_font(fonts.sans28) draw.string(':', 110, 120-14, width=20) self.minutes.draw() self.seconds.draw() self._draw_play(114, _BUTTON_Y) def _update(self): wasp.system.bar.update() draw = wasp.watch.drawable if self.state == _RUNNING: now = wasp.watch.rtc.time() s = self.current_alarm - now if s<0: s = 0 m = str(math.floor(s // 60)) s = str(math.floor(s) % 60) if len(m) < 2: m = '0' + m if len(s) < 2: s = '0' + s draw.set_font(fonts.sans28) draw.string(m, 50, 120-14, width=60) draw.string(s, 130, 120-14, width=60) def _draw_play(self, x, y): draw = wasp.watch.drawable for i in range(0,20): draw.fill(0xffff, x+i, y+i, 1, 40 - 2*i) def _draw_stop(self, x, y): wasp.watch.drawable.fill(0xffff, x, y, 40, 40) def _alert(self): self.state = _RINGING wasp.system.wake() wasp.system.switch(self)