/ MagTag_Kitchen_Timer / code.py
code.py
1 # SPDX-FileCopyrightText: 2020 Eva Herrada for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import terminalio 7 from adafruit_magtag.magtag import MagTag 8 9 magtag = MagTag() 10 magtag.peripherals.neopixel_disable = False 11 12 magtag.add_text( 13 text_font=terminalio.FONT, 14 text_position=(140, 55), 15 text_scale=7, 16 text_anchor_point=(0.5, 0.5), 17 ) 18 19 magtag.set_text("00:00") 20 21 # Function that makes the neopixels display the seconds left 22 def update_neopixels(seconds): 23 n = seconds // 15 24 for j in range(n): 25 magtag.peripherals.neopixels[3 - j] = (128, 0, 0) 26 magtag.peripherals.neopixels[3 - n] = (int(((seconds / 15) % 1) * 128), 0, 0) 27 28 29 alarm_set = False 30 while True: 31 if not alarm_set: 32 # Set the timer to 1 minute 33 if magtag.peripherals.button_a_pressed: 34 alarm_time = 60 35 alarm_set = True 36 start = time.time() 37 magtag.set_text("01:00") 38 last_set = 60 39 magtag.peripherals.neopixels.fill((128, 0, 0)) 40 41 # Set the timer to 5 minutes 42 elif magtag.peripherals.button_b_pressed: 43 alarm_time = 300 44 alarm_set = True 45 start = time.time() 46 magtag.set_text("05:00") 47 last_set = 300 48 magtag.peripherals.neopixels.fill((128, 0, 0)) 49 50 # Set the timer to 20 minutes 51 elif magtag.peripherals.button_c_pressed: 52 alarm_time = 1200 53 alarm_set = True 54 start = time.time() 55 magtag.set_text("20:00") 56 last_set = 1200 57 magtag.peripherals.neopixels.fill((128, 0, 0)) 58 59 else: 60 time.sleep(1) 61 remaining = alarm_time - (time.time() - start) 62 if (remaining < 0): 63 remaining = 0 64 print(remaining) 65 if remaining == 0: 66 magtag.peripherals.neopixels.fill((255, 0, 0)) 67 # Play alarm and flash neopixels to indicate the timer is done 68 for i in range(2): 69 magtag.peripherals.neopixels.fill((255, 0, 0)) 70 magtag.peripherals.play_tone(3000, 0.5) 71 magtag.peripherals.neopixels.fill((0, 0, 0)) 72 time.sleep(0.1) 73 magtag.peripherals.neopixels.fill((255, 0, 0)) 74 magtag.peripherals.play_tone(3000, 0.5) 75 magtag.peripherals.neopixels.fill((0, 0, 0)) 76 time.sleep(0.5) 77 alarm_set = False 78 magtag.set_text("00:00") 79 last_set = 0 80 continue 81 82 update_neopixels(remaining % 60) 83 if remaining % 60 == 0 and remaining != last_set: 84 magtag.set_text("{:02d}:00".format(remaining // 60)) 85 last_set = remaining 86 87 # Reset the timer 88 if magtag.peripherals.button_d_pressed: 89 time.sleep(0.1) 90 magtag.peripherals.neopixels.fill((0, 0, 0)) 91 time.sleep(0.1) 92 magtag.peripherals.neopixels.fill((255, 0, 0)) 93 time.sleep(0.1) 94 magtag.peripherals.neopixels.fill((0, 0, 0)) 95 time.sleep(0.1) 96 97 alarm_set = False 98 magtag.set_text("00:00")