|
| 1 | +""" |
| 2 | +Start a 20 second hand washing timer via proximity sensor. |
| 3 | +Countdown the seconds with text and beeps. |
| 4 | +Display a bitmaps for waiting and washing modes. |
| 5 | +""" |
| 6 | + |
| 7 | +import time |
| 8 | +import board |
| 9 | +from adafruit_clue import clue |
| 10 | +from adafruit_display_text import label |
| 11 | +from adafruit_bitmap_font import bitmap_font |
| 12 | +import displayio |
| 13 | +import pulseio |
| 14 | + |
| 15 | +clue.display.brightness = 0.8 |
| 16 | +clue_display = displayio.Group(max_size=4) |
| 17 | + |
| 18 | +# draw the background image |
| 19 | +wash_on_file = open("wash_on.bmp", "rb") |
| 20 | +wash_on_bmp = displayio.OnDiskBitmap(wash_on_file) |
| 21 | +wash_on_sprite = displayio.TileGrid(wash_on_bmp, pixel_shader=displayio.ColorConverter()) |
| 22 | +clue_display.append(wash_on_sprite) |
| 23 | + |
| 24 | +# draw the foreground image |
| 25 | +wash_off_file = open("wash_off.bmp", "rb") |
| 26 | +wash_off_bmp = displayio.OnDiskBitmap(wash_off_file) |
| 27 | +wash_off_sprite = displayio.TileGrid(wash_off_bmp, pixel_shader=displayio.ColorConverter()) |
| 28 | +clue_display.append(wash_off_sprite) |
| 29 | + |
| 30 | + |
| 31 | +# Create text |
| 32 | +# first create the group |
| 33 | +text_group = displayio.Group(max_size=5, scale=1) |
| 34 | +# Make a label |
| 35 | +title_font = bitmap_font.load_font("/font/RacingSansOne-Regular-38.bdf") |
| 36 | +title_font.load_glyphs("HandWashTimer".encode('utf-8')) |
| 37 | +title_label = label.Label(title_font, text="Hand Wash", color=0x001122) |
| 38 | +# Position the label |
| 39 | +title_label.x = 10 |
| 40 | +title_label.y = 16 |
| 41 | +# Append label to group |
| 42 | +text_group.append(title_label) |
| 43 | + |
| 44 | +title2_label = label.Label(title_font, text="Timer", color=0x001122) |
| 45 | +# Position the label |
| 46 | +title2_label.x = 6 |
| 47 | +title2_label.y = 52 |
| 48 | +# Append label to group |
| 49 | +text_group.append(title2_label) |
| 50 | + |
| 51 | +timer_font = bitmap_font.load_font("/font/RacingSansOne-Regular-29.bdf") |
| 52 | +timer_font.load_glyphs("0123456789ADSWabcdefghijklmnopqrstuvwxyz:!".encode('utf-8')) |
| 53 | +timer_label = label.Label(timer_font, text="Wave to start", color=0x4f3ab1, max_glyphs=15) |
| 54 | +timer_label.x = 24 |
| 55 | +timer_label.y = 100 |
| 56 | +text_group.append(timer_label) |
| 57 | + |
| 58 | +clue_display.append(text_group) |
| 59 | +clue.display.show(clue_display) |
| 60 | + |
| 61 | +def countdown(seconds): |
| 62 | + for i in range(seconds): |
| 63 | + buzzer.duty_cycle = 2**15 |
| 64 | + timer_label.text = ("Scrub time: {}".format(seconds-i)) |
| 65 | + buzzer.duty_cycle = 0 |
| 66 | + time.sleep(1) |
| 67 | + timer_label.text = ("Done!") |
| 68 | + wash_off_sprite.x = 0 |
| 69 | + buzzer.duty_cycle = 2**15 |
| 70 | + time.sleep(0.3) |
| 71 | + buzzer.duty_cycle = 0 |
| 72 | + timer_label.x = 24 |
| 73 | + timer_label.y = 100 |
| 74 | + timer_label.text = ("Wave to start") |
| 75 | + |
| 76 | +# setup buzzer |
| 77 | +buzzer = pulseio.PWMOut(board.SPEAKER, variable_frequency=True) |
| 78 | +buzzer.frequency = 1000 |
| 79 | + |
| 80 | +while True: |
| 81 | + # print("Distance: {}".format(clue.proximity)) # use to test the sensor |
| 82 | + if clue.proximity > 1: |
| 83 | + timer_label.x = 12 |
| 84 | + timer_label.y = 226 |
| 85 | + timer_label.text = "Scrub Away!" |
| 86 | + wash_off_sprite.x = 300 |
| 87 | + time.sleep(2) |
| 88 | + countdown(20) |
0 commit comments