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