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