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