Skip to content

Instantly share code, notes, and snippets.

@ansemjo
Last active January 27, 2020 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansemjo/01af24c9c0f745685f36bc895470ab3d to your computer and use it in GitHub Desktop.
Save ansemjo/01af24c9c0f745685f36bc895470ab3d to your computer and use it in GitHub Desktop.
Stupid simple library to use APA102 DotStar LEDs with an Adafruit FT232H breakout

Adafruit DotStar + FT232H library

Simple library to drive APA102 LED strips with an Adafruit FT232H breakout board over its SPI pins.

The packed format for the pixels is known from an exploration of the APA102 LEDs on Tim's Blog.

Example Usage

  • Connect the LED strip to the first four pins: 5V, GND, D0 (CLK --> CI), D1 (MOSI --> DI)
  • Follow the Adafruit guide to install adafruit-blinka and set BLINKA_FT232H=1 in your environment
  • Import the library in Python: from ft232h_dotstar import *
  • Instantiate as strip = Dotstar(SPI(), 10)

For example to create a simple rainbow fade on a strip with 10 dots:

VID_20200127_161324

$ python
Python 3.8.1 (default, Jan  8 2020, 23:09:20) 
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ft232h_dotstar import *
>>> from time import sleep
>>> spi = SPI()
>>> spi.try_lock()
True
>>> spi.configure(30000000)
>>> strip = Dotstar(spi, 10)
>>> while True:
...     for i in range(360):
...             strip.rainbow(i/360, 120/360, 1, 1)
...             sleep(0.01)
... 
import board
import colorsys as __colorsys
import math as __math
# shortcut to boards spi interface
SPI = board.SPI
# pack rgb into a dotstar 32 bit value
# b'111' + brightness[5] + {blue, green, red}[8]
def rgb(red, green, blue, v = 31):
return bytes([224 | v, blue, green, red])
# convert hue, saturation and value to packed dotstar vrgb
# inputs are floats in range [0, 1]
def hsv(hue, sat, val):
v5 = val / (1/31)
gv = __math.ceil(v5)
r, g, b = __colorsys.hsv_to_rgb(hue, sat, v5/gv)
return rgb(int(r*255), int(g*255), int(b*255), gv)
# shorthands for some useful values
START = b'\x00\x00\x00\x00'
FULL = END = b'\xff\xff\xff\xff'
OFF = b'\xe0\x00\x00\x00'
RED = lambda v=1: hsv( 0, 1, v)
YELLOW = lambda v=1: hsv( 60/360, 1, v)
GREEN = lambda v=1: hsv(120/360, 1, v)
CYAN = lambda v=1: hsv(180/360, 1, v)
BLUE = lambda v=1: hsv(240/360, 1, v)
PINK = lambda v=1: hsv(300/360, 1, v)
# repeat a pattern in a list
def repeat(b, n):
return [b for i in range(n)]
# simple NeoPixel-like class to drive Dotstar LEDs
class Dotstar:
# e.g.: strip = Dotstar(SPI(), 10)
def __init__(self, spi, num):
self.__spi = spi
self.dots = [OFF for i in range(num)]
# indexing for individual pixels
def __getitem__(self, key):
return self.dots.__getitem__(key)
def __setitem__(self, key, value):
return self.dots.__setitem__(key, value)
# update strip
def show(self, dots = None):
if dots is not None:
for i in range(len(self.dots)):
self.dots[i] = dots[i]
return self.__spi.write(START + b''.join(self.dots) + END)
# fill entire strip with one color
def fill(self, color):
return self.show(repeat(color, len(self.dots)))
# fill strip with a rainbow section given by starthue and angle
def rainbow(self, starthue, angle, sat, val):
incr = angle / len(self.dots)
for d in range(len(self.dots)):
self.dots[d] = hsv(starthue + d*incr, sat, val)
self.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment