code.py
1 # SPDX-FileCopyrightText: 2020 Carter Nelson for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 # Display stuff 6 import board 7 import displayio 8 import adafruit_imageload 9 from adafruit_bitmap_font import bitmap_font 10 from adafruit_display_text import label 11 # BLE stuff 12 from adafruit_ble import BLERadio 13 from adafruit_ble.advertising.standard import ProvideServicesAdvertisement 14 from adafruit_ble.services.nordic import UARTService 15 from adafruit_bluefruit_connect.packet import Packet 16 from adafruit_bluefruit_connect.color_packet import ColorPacket 17 18 #---| User Config |-------------------------------------------------- 19 BLE_NAME = "Candy Heart" 20 MESSAGE_DELIMITER = "," 21 MESSAGE_COLOR = 0xFF0000 22 #---| User Config |-------------------------------------------------- 23 24 # Setup BLE radio and service 25 ble = BLERadio() 26 uart = UARTService() 27 advertisement = ProvideServicesAdvertisement(uart) 28 ble._adapter.name = BLE_NAME #pylint: disable=protected-access 29 30 # Create the display 31 display = board.DISPLAY 32 33 # Load the candy heart BMP 34 bitmap, palette = adafruit_imageload.load("/images/heart_bw.bmp", 35 bitmap=displayio.Bitmap, 36 palette=displayio.Palette) 37 38 heart = displayio.TileGrid(bitmap, pixel_shader=palette) 39 40 # Set up message text 41 LINE1_MAX = 9 42 LINE2_MAX = 5 43 font = bitmap_font.load_font("/fonts/Multicolore_36.bdf") 44 line1 = label.Label(font, text="?"*LINE1_MAX) 45 line2 = label.Label(font, text="?"*LINE2_MAX) 46 line1.anchor_point = (0.5, 0) # middle top 47 line2.anchor_point = (0.5, 1.0) # middle bottom 48 line1.anchored_position = (120, 85) 49 line2.anchored_position = (120, 175) 50 line1.color = line2.color = MESSAGE_COLOR 51 52 # Set up group and add to display 53 group = displayio.Group() 54 group.append(heart) 55 group.append(line1) 56 group.append(line2) 57 display.show(group) 58 59 def update_heart(message, heart_color): 60 # turn off auto refresh while we change some things 61 display.auto_refresh = False 62 # set message text 63 text1, _, text2 = message.partition(MESSAGE_DELIMITER) 64 line1.text = text1[:LINE1_MAX] if len(text1) > LINE1_MAX else text1 65 line2.text = text1[:LINE2_MAX] if len(text2) > LINE2_MAX else text2 66 # update location for new text bounds 67 line1.anchored_position = (120, 85) 68 line2.anchored_position = (120, 175) 69 # set heart color 70 palette[1] = heart_color 71 # OK, now turn auto refresh back on to display 72 display.auto_refresh = True 73 74 # Initial update 75 text = "TEXT,ME" 76 color = 0x00FFFF 77 update_heart(text, color) 78 79 # Loop forever 80 while True: 81 # advertise and wait for connection 82 print("WAITING...") 83 ble.start_advertising(advertisement) 84 while not ble.connected: 85 pass 86 87 # connected 88 print("CONNECTED") 89 ble.stop_advertising() 90 91 # receive and handle BLE traffic 92 while ble.connected: 93 if uart.in_waiting: 94 raw_bytes = uart.read(uart.in_waiting) 95 if raw_bytes[0] == ord('!'): 96 # BLE Connect Control Packet 97 packet = Packet.from_bytes(raw_bytes) 98 if isinstance(packet, ColorPacket): 99 print("color = ", color) 100 color = packet.color 101 else: 102 # Just plain text 103 text = raw_bytes.decode("utf-8").strip() 104 print("text = ", text) 105 update_heart(text, color) 106 107 # disconnected 108 print("DISCONNECTED")