Libraries
Once you've gotten CircuitPython onto your Circuit Playground Bluefruit boards, it's time to add some libraries. You can follow this guide page for the basics of downloading and transferring libraries to the board.
From the library bundle you downloaded in that guide page, transfer the following libraries onto the CPB boards' /lib directories:
- neopixel.mpy
- adafruit_ble
- adafruit_bluefruit_connect
Text Editor
Adafruit recommends using the Mu editor for using your CircuitPython code with the Circuit Playground Bluefruit boards. You can get more info in this guide.
Alternatively, you can use any text editor that saves files.
Central
Next we'll code the CPB that will act as the Central/client device. This is the one that will have the slide pots on it and will send out RGB color data packets to the peripheral device.
Copy the code below and paste it into Mu. Then, save it to your CPB as code.py.
# SPDX-FileCopyrightText: 2019 John Edgar Park for Adafruit Industries # # SPDX-License-Identifier: MIT """ Demonstration of a Bluefruit BLE Central/client. Connects to the first BLE UART peripheral it finds. Sends Bluefruit ColorPackets, read from three potentiometers, to the peripheral. """ import time import board from analogio import AnalogIn #from adafruit_bluefruit_connect.packet import Packet # Only the packet classes that are imported will be known to Packet. from adafruit_bluefruit_connect.color_packet import ColorPacket from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService def scale(value): """Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)""" return int(value / 65535 * 255) ble = BLERadio() a4 = AnalogIn(board.A4) a5 = AnalogIn(board.A5) a6 = AnalogIn(board.A6) uart_connection = None # See if any existing connections are providing UARTService. if ble.connected: for connection in ble.connections: if UARTService in connection: uart_connection = connection break while True: if not uart_connection: for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): if UARTService in adv.services: uart_connection = ble.connect(adv) break # Stop scanning whether or not we are connected. ble.stop_scan() while uart_connection and uart_connection.connected: r = scale(a4.value) g = scale(a5.value) b = scale(a6.value) color = (r, g, b) print(color) color_packet = ColorPacket(color) try: uart_connection[UARTService].write(color_packet.to_bytes()) except OSError: pass time.sleep(0.3)
Peripheral
The second CPB will act as a peripheral device. It will receive the RGB color data packets from the client and update the NeoPixel color to match the data.
Copy the code below and paste it into a new document in Mu, then save it to your second CPB as code.py.
# SPDX-FileCopyrightText: 2019 John Edgar Park for Adafruit Industries # # SPDX-License-Identifier: MIT """ Used along with cpb_remote_color_client.py. Receives Bluefruit LE ColorPackets from a central, and updates NeoPixels to show the history of the received packets. """ import board import neopixel from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService from adafruit_bluefruit_connect.packet import Packet # Only the packet classes that are imported will be known to Packet. from adafruit_bluefruit_connect.color_packet import ColorPacket ble = BLERadio() uart = UARTService() advertisement = ProvideServicesAdvertisement(uart) NUM_PIXELS = 10 np = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1) next_pixel = 0 def mod(i): """Wrap i to modulus NUM_PIXELS.""" return i % NUM_PIXELS while True: # Advertise when not connected. ble.start_advertising(advertisement) while not ble.connected: pass while ble.connected: packet = Packet.from_stream(uart) if isinstance(packet, ColorPacket): print(packet.color) np[next_pixel] = packet.color np[mod(next_pixel + 1)] = (0, 0, 0) next_pixel = (next_pixel + 1) % NUM_PIXELS
Next we'll assemble the parts to use our color mixer!
Page last edited January 22, 2025
Text editor powered by tinymce.