/ CLUE_BBQ / code.py
code.py
  1  # SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
  2  #
  3  # SPDX-License-Identifier: MIT
  4  
  5  # Adafruit BBQ display works with ibbq protocol-based BLE temperature probes
  6  
  7  import time
  8  
  9  import displayio
 10  import _bleio
 11  import adafruit_ble
 12  from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
 13  from adafruit_ble_ibbq import IBBQService
 14  from adafruit_clue import clue
 15  from adafruit_display_shapes.circle import Circle
 16  from adafruit_display_text import label
 17  from adafruit_bitmap_font import bitmap_font
 18  
 19  clue.display.brightness = 1.0
 20  homescreen_screen = displayio.Group()
 21  temperatures_screen = displayio.Group()
 22  
 23  # define custom colors
 24  GREEN = 0x00D929
 25  BLUE = 0x0000FF
 26  RED = 0xFF0000
 27  ORANGE = 0xFF6A00
 28  YELLOW = 0xFFFF00
 29  PURPLE = 0xE400FF
 30  BLACK = 0x000000
 31  WHITE = 0xFFFFFF
 32  BURNT = 0xBB4E00
 33  
 34  unit_mode = False  # set the temperature unit_mode. True = centigrade, False = farenheit
 35  
 36  # Setup homescreen
 37  color_bitmap = displayio.Bitmap(120, 120, 1)
 38  color_palette = displayio.Palette(1)
 39  color_palette[0] = BURNT
 40  bg_sprite = displayio.TileGrid(color_bitmap, x=120, y=0, pixel_shader=color_palette)
 41  homescreen_screen.append(bg_sprite)
 42  
 43  clue_color = [GREEN, BLUE, RED, ORANGE, YELLOW, PURPLE]
 44  
 45  outer_circle = Circle(120, 120, 119, fill=BLACK, outline=BURNT)
 46  homescreen_screen.append(outer_circle)
 47  
 48  
 49  title_font = bitmap_font.load_font("/font/GothamBlack-50.bdf")
 50  title_font.load_glyphs("BQLUE".encode("utf-8"))
 51  title_label = label.Label(title_font, text="BBQLUE", color=clue.ORANGE)
 52  title_label.x = 12
 53  title_label.y = 120
 54  homescreen_screen.append(title_label)
 55  
 56  clue.display.show(homescreen_screen)
 57  
 58  # Setup temperatures screen
 59  temp_font = bitmap_font.load_font("/font/GothamBlack-25.bdf")
 60  temp_font.load_glyphs("0123456789FC.-<".encode("utf-8"))
 61  
 62  my_labels_config = [
 63      (0, "", GREEN, 2, 100),
 64      (1, "", BLUE, 2, 150),
 65      (2, "", RED, 2, 200),
 66      (3, "", ORANGE, 135, 100),
 67      (4, "", YELLOW, 135, 150),
 68      (5, "", PURPLE, 135, 200),
 69  ]
 70  
 71  my_labels = {}  # dictionary of configured my_labels
 72  
 73  text_group = displayio.Group()
 74  
 75  for label_config in my_labels_config:
 76      (name, text, color, x, y) = label_config  # unpack a tuple into five var names
 77      templabel = label.Label(temp_font, text=text, color=color)
 78      templabel.x = x
 79      templabel.y = y
 80      my_labels[name] = templabel
 81      text_group.append(templabel)
 82  
 83  temperatures_screen.append(text_group)
 84  
 85  temp_title_label = label.Label(title_font, text="BBQLUE", color=clue.ORANGE)
 86  temp_title_label.x = 12
 87  temp_title_label.y = 30
 88  temperatures_screen.append(temp_title_label)
 89  
 90  # PyLint can't find BLERadio for some reason so special case it here.
 91  ble = adafruit_ble.BLERadio()  # pylint: disable=no-member
 92  
 93  ibbq_connection = None
 94  
 95  while True:
 96      # re-display homescreen here
 97      clue.display.show(homescreen_screen)
 98  
 99      print("Scanning...")
100      for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
101          clue.pixel.fill((50, 50, 0))
102          if IBBQService in adv.services:
103              print("found an IBBq advertisement")
104              ibbq_connection = ble.connect(adv)
105              print("Connected")
106              break
107  
108      # Stop scanning whether or not we are connected.
109      ble.stop_scan()
110  
111      try:
112          if ibbq_connection and ibbq_connection.connected:
113              clue.pixel.fill((0, 0, 50))
114              ibbq_service = ibbq_connection[IBBQService]
115              ibbq_service.init()
116              while ibbq_connection.connected:
117  
118                  if clue.button_a:  # hold a to swap between C and F
119                      print("unit_mode swapped")
120                      unit_mode = not unit_mode
121                      clue.red_led = True
122                      clue.play_tone(1200, 0.1)
123                      clue.red_led = False
124                      time.sleep(0.1)  # debounce
125  
126                  temps = ibbq_service.temperatures
127                  batt = ibbq_service.battery_level
128                  if temps is not None:
129                      probe_count = len(temps)  # check how many probes there are
130                      for i in range(probe_count):
131                          if temps[i] != 0 and temps[i] < 1000:  # unplugged probes
132                              if unit_mode:
133                                  clue.pixel.fill((50, 0, 0))
134                                  temp = temps[i]
135                                  my_labels[i].text = "{} C".format(temp)
136                                  clue.pixel.fill((0, 0, 0))
137                                  print("Probe", i + 1, "Temperature:", temp, "C")
138                              else:  # F
139                                  clue.pixel.fill((50, 0, 0))
140                                  temp = temps[i] * 9 / 5 + 32
141                                  my_labels[i].text = "{} F".format(temp)
142                                  clue.pixel.fill((0, 0, 0))
143                                  print("Probe", i + 1, "Temperature:", temp, "F")
144                          else:
145                              print(
146                                  "Probe", i + 1, "is unplugged",
147                              )
148                              my_labels[i].text = "  ---"
149                      clue.display.show(temperatures_screen)
150  
151      except _bleio.ConnectionError:
152          continue