Once you've finished setting up your Adafruit nRF52840 Feather with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click the Download Project Bundle button in the window below. It will download all needed files as a zipped folder.
# SPDX-FileCopyrightText: 2023 Trevor Beaton for Adafruit Industries # SPDX-License-Identifier: MIT import time import board import displayio import framebufferio import rgbmatrix import terminalio from adafruit_display_text import label from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService SEND_RATE = 10 # how often in seconds to send text count = 0 SCROLL_DELAY = 0.05 # delay between scrolls, in seconds ble = BLERadio() uart_server = UARTService() advertisement = ProvideServicesAdvertisement(uart_server) # Release any previously allocated displays displayio.release_displays() matrix = rgbmatrix.RGBMatrix( width=64, height=32, bit_depth=1, rgb_pins=[board.D6, board.A5, board.A1, board.A0, board.A4, board.D11], addr_pins=[board.D10, board.D5, board.D13, board.D9], clock_pin=board.D12, latch_pin=board.RX, output_enable_pin=board.TX, ) display = framebufferio.FramebufferDisplay(matrix, auto_refresh=True) main_group = displayio.Group() def scroll(line): line.x -= 1 line_width = line.bounding_box[2] if line.x < -line_width: line.x = display.width def update_display(text, color=0xFFFFFF): """Update the display with the provided text and color.""" if len(main_group) > 0: main_group.pop() text_area = label.Label(terminalio.FONT, text=text, color=color) text_area.x = display.width text_area.y = 13 main_group.append(text_area) display.root_group = main_group while True: print("WAITING...") update_display("WAITING...") ble.start_advertising(advertisement) while not ble.connected: scroll(main_group[0]) display.refresh(minimum_frames_per_second=0) time.sleep(SCROLL_DELAY) # Connected ble.stop_advertising() print("CONNECTED") update_display("CONNECTED") # Loop and read packets last_send = time.monotonic() while ble.connected: if uart_server.in_waiting: raw_bytes = uart_server.read(uart_server.in_waiting) received_text = raw_bytes.decode().strip() print("RX:", received_text) update_display(received_text, color=0x26B7FF) if time.monotonic() - last_send > SEND_RATE: transmit_text = "COUNT = {}".format(count) print("TX:", transmit_text) uart_server.write((transmit_text + "\r\n").encode()) count += 1 last_send = time.monotonic() scroll(main_group[0]) display.refresh(minimum_frames_per_second=0) time.sleep(SCROLL_DELAY) print("DISCONNECTED")
Text Editor
Adafruit recommends using the Mu editor for editing your CircuitPython code. You can get more info in this guide.
Alternatively, you can use any text editor that saves simple text files.
After downloading the Project Bundle, plug your Adafruit nRF52840 Express into the computer's USB port. You should see a new flash drive in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the Adafruit nRF52840 Express's CIRCUITPY drive.
- lib folder
- code.py
Your Adafruit nRF52840 Express CIRCUITPY drive should look like this after copying the lib folders and the code.py file.
SEND_RATE = 10 count = 0 SCROLL_DELAY = 0.05 ble = BLERadio() uart_server = UARTService() advertisement = ProvideServicesAdvertisement(uart_server)
- END_RATE: Sets how frequently the program sends text over Bluetooth (every 10 seconds).
- count: A variable initialized to 0, likely used for counting something (such as messages sent).
- SCROLL_DELAY: Time delay between each scroll movement on the display, set to 0.05 seconds.
- Bluetooth Setup: Initializes a BLE (Bluetooth Low Energy) radio, a UART service for Bluetooth communication, and an advertisement for that service.
Display Setup
displayio.release_displays() matrix = rgbmatrix.RGBMatrix( width=64, height=32, bit_depth=1, rgb_pins=[board.D6, board.A5, board.A1, board.A0, board.A4, board.D11], addr_pins=[board.D10, board.D5, board.D13, board.D9], clock_pin=board.D12, latch_pin=board.RX, output_enable_pin=board.TX, ) display = framebufferio.FramebufferDisplay(matrix, auto_refresh=True) main_group = displayio.Group()
- Display Release: Releases any previously allocated displays to avoid conflicts.
- Matrix Initialization: Sets up the RGB matrix display with specific configurations (size, pins, etc.).
- Display Initialization: Creates a framebuffer display from the RGB matrix.
-
Display Group: Initializes a
Group
object, a container for display elements.
Function Definitions
def scroll(line): line.x -= 1 line_width = line.bounding_box[2] if line.x < -line_width: line.x = display.width def update_display(text, color=0xFFFFFF): if len(main_group) > 0: main_group.pop() text_area = label.Label(terminalio.FONT, text=text, color=color) text_area.x = display.width text_area.y = 13 main_group.append(text_area) display.root_group = main_group
-
scroll()
Function: Defines how to scroll text across the display. It moves the text to the left and wraps around it when it reaches the edge. -
update_display()
Function: Updates the display with new text. It removes the old text (if any) and adds new text to the display group.
The Main Loop
while True: print("WAITING...") update_display("WAITING...") ble.start_advertising(advertisement) while not ble.connected: scroll(main_group[0]) display.refresh(minimum_frames_per_second=0) time.sleep(SCROLL_DELAY) # Connected ble.stop_advertising() print("CONNECTED") update_display("CONNECTED") # Loop and read packets last_send = time.monotonic() while ble.connected: if uart_server.in_waiting: raw_bytes = uart_server.read(uart_server.in_waiting) received_text = raw_bytes.decode().strip() print("RX:", received_text) update_display(received_text, color=0x26B7FF) if time.monotonic() - last_send > SEND_RATE: transmit_text = "COUNT = {}".format(count) print("TX:", transmit_text) uart_server.write((transmit_text + "\r\n").encode()) count += 1 last_send = time.monotonic() scroll(main_group[0]) display.refresh(minimum_frames_per_second=0) time.sleep(SCROLL_DELAY) print("DISCONNECTED")
- Outer While Loop: The main loop that runs forever.
- Waiting for Connection: The display shows "WAITING..." and starts advertising for a Bluetooth connection.
- Scrolling Text: Continuously scrolls text on the display while not connected.
- BLE Connection Established: Stops advertising once connected and updates the display.
-
Inner While Loop: While connected, it reads data from the UART service, displays it, and sends back a count message at regular intervals defined by
SEND_RATE
. - Scrolling and Refreshing Display: Continuously updates the display while connected.
- Disconnect Handling: Prints "DISCONNECTED" to the console when the connection is lost.
Text editor powered by tinymce.