Code the Color Remote with CircuitPython

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_bluefruit_connect

We also need to get a specific version of the adafruit_ble library that is not yet in the bundle as of this writing. You can get this by going here and downloading the adafruit-circuitpython-ble-5.x-mpy-2.0.1-alpha.2.zip file.

Unzip that file and copy the adafruit_ble folder to your CPB boards' /lib directories.

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.

"""
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.scanner import Scanner
from adafruit_ble.uart_client import UARTClient

def scale(value):
    """Scale an value from 0-65535 (AnalogIn range) to 0-255 (RGB range)"""
    return int(value / 65535 * 255)

scanner = Scanner()
uart_client = UARTClient()

a3 = AnalogIn(board.A4)
a4 = AnalogIn(board.A5)
a5 = AnalogIn(board.A6)

while True:
    uart_addresses = []
    # Keep trying to find a UART peripheral
    while not uart_addresses:
        uart_addresses = uart_client.scan(scanner)
    uart_client.connect(uart_addresses[0], 5)

    while uart_client.connected:
        r = scale(a3.value)
        g = scale(a4.value)
        b = scale(a5.value)

        color = (r, g, b)
        print(color)
        color_packet = ColorPacket(color)
        try:
            uart_client.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.

"""
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.uart_server import UARTServer
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

uart_server = UARTServer()

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.
    uart_server.start_advertising()
    while not uart_server.connected:
        pass

    while uart_server.connected:
        packet = Packet.from_stream(uart_server)
        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!

This guide was first published on Oct 22, 2019. It was last updated on Oct 22, 2019. This page (Code the Color Remote with CircuitPython) was last updated on Nov 13, 2019.