|
| 1 | +# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | + |
| 5 | +import time |
| 6 | +import board |
| 7 | +import displayio |
| 8 | +import framebufferio |
| 9 | +import rgbmatrix |
| 10 | +import terminalio |
| 11 | +from adafruit_display_text import label |
| 12 | +from adafruit_ble import BLERadio |
| 13 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 14 | +from adafruit_ble.services.nordic import UARTService |
| 15 | + |
| 16 | +SEND_RATE = 10 # how often in seconds to send text |
| 17 | + |
| 18 | +count = 0 |
| 19 | + |
| 20 | +SCROLL_DELAY = 0.05 # delay between scrolls, in seconds |
| 21 | + |
| 22 | +ble = BLERadio() |
| 23 | +uart_server = UARTService() |
| 24 | +advertisement = ProvideServicesAdvertisement(uart_server) |
| 25 | + |
| 26 | +# Release any previously allocated displays |
| 27 | +displayio.release_displays() |
| 28 | + |
| 29 | +matrix = rgbmatrix.RGBMatrix( |
| 30 | + width=64, height=32, bit_depth=1, |
| 31 | + rgb_pins=[board.D6, board.A5, board.A1, board.A0, board.A4, board.D11], |
| 32 | + addr_pins=[board.D10, board.D5, board.D13, board.D9], |
| 33 | + clock_pin=board.D12, latch_pin=board.RX, output_enable_pin=board.TX, |
| 34 | +) |
| 35 | +display = framebufferio.FramebufferDisplay(matrix, auto_refresh=True) |
| 36 | + |
| 37 | +main_group = displayio.Group() |
| 38 | + |
| 39 | +def scroll(line): |
| 40 | + line.x -= 1 |
| 41 | + line_width = line.bounding_box[2] |
| 42 | + if line.x < -line_width: |
| 43 | + line.x = display.width |
| 44 | + |
| 45 | +def update_display(text, color=0xFFFFFF): |
| 46 | + """Update the display with the provided text and color.""" |
| 47 | + if len(main_group) > 0: |
| 48 | + main_group.pop() |
| 49 | + text_area = label.Label(terminalio.FONT, text=text, color=color) |
| 50 | + text_area.x = display.width |
| 51 | + text_area.y = 13 |
| 52 | + main_group.append(text_area) |
| 53 | + display.show(main_group) |
| 54 | + |
| 55 | +while True: |
| 56 | + print("WAITING...") |
| 57 | + update_display("WAITING...") |
| 58 | + ble.start_advertising(advertisement) |
| 59 | + |
| 60 | + while not ble.connected: |
| 61 | + scroll(main_group[0]) |
| 62 | + display.refresh(minimum_frames_per_second=0) |
| 63 | + time.sleep(SCROLL_DELAY) |
| 64 | + |
| 65 | + # Connected |
| 66 | + ble.stop_advertising() |
| 67 | + print("CONNECTED") |
| 68 | + update_display("CONNECTED") |
| 69 | + |
| 70 | + # Loop and read packets |
| 71 | + last_send = time.monotonic() |
| 72 | + while ble.connected: |
| 73 | + if uart_server.in_waiting: |
| 74 | + raw_bytes = uart_server.read(uart_server.in_waiting) |
| 75 | + received_text = raw_bytes.decode().strip() |
| 76 | + print("RX:", received_text) |
| 77 | + update_display(received_text, color=0x26B7FF) |
| 78 | + |
| 79 | + if time.monotonic() - last_send > SEND_RATE: |
| 80 | + transmit_text = "COUNT = {}".format(count) |
| 81 | + print("TX:", transmit_text) |
| 82 | + uart_server.write((transmit_text + "\r\n").encode()) |
| 83 | + count += 1 |
| 84 | + last_send = time.monotonic() |
| 85 | + |
| 86 | + scroll(main_group[0]) |
| 87 | + display.refresh(minimum_frames_per_second=0) |
| 88 | + time.sleep(SCROLL_DELAY) |
| 89 | + |
| 90 | + print("DISCONNECTED") |
0 commit comments