| 1 |
# DotStar strip driver for MicroPython |
| 2 |
# |
| 3 |
# The MIT License (MIT) |
| 4 |
# |
| 5 |
# Copyright (c) 2016 Damien P. George (original Neopixel object) |
| 6 |
# Copyright (c) 2017 Ladyada |
| 7 |
# Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 8 |
# Copyright (c) 2019 Matt Trentini (porting back to MicroPython) |
| 9 |
# |
| 10 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 |
# of this software and associated documentation files (the "Software"), to deal |
| 12 |
# in the Software without restriction, including without limitation the rights |
| 13 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 |
# copies of the Software, and to permit persons to whom the Software is |
| 15 |
# furnished to do so, subject to the following conditions: |
| 16 |
# |
| 17 |
# The above copyright notice and this permission notice shall be included in |
| 18 |
# all copies or substantial portions of the Software. |
| 19 |
# |
| 20 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 |
# THE SOFTWARE. |
| 27 |
|
| 28 |
START_HEADER_SIZE = 4 |
| 29 |
LED_START = 0b11100000 # Three "1" bits, followed by 5 brightness bits |
| 30 |
|
| 31 |
# Pixel color order constants |
| 32 |
RGB = (0, 1, 2) |
| 33 |
RBG = (0, 2, 1) |
| 34 |
GRB = (1, 0, 2) |
| 35 |
GBR = (1, 2, 0) |
| 36 |
BRG = (2, 0, 1) |
| 37 |
BGR = (2, 1, 0) |
| 38 |
|
| 39 |
|
| 40 |
class DotStar: |
| 41 |
""" |
| 42 |
A sequence of dotstars. |
| 43 |
|
| 44 |
:param SPI spi: The SPI object to write output to. |
| 45 |
:param int n: The number of dotstars in the chain |
| 46 |
:param float brightness: Brightness of the pixels between 0.0 and 1.0 |
| 47 |
:param bool auto_write: True if the dotstars should immediately change when |
| 48 |
set. If False, `show` must be called explicitly. |
| 49 |
:param tuple pixel_order: Set the pixel order on the strip - different |
| 50 |
strips implement this differently. If you send red, and it looks blue |
| 51 |
or green on the strip, modify this! It should be one of the values above |
| 52 |
|
| 53 |
|
| 54 |
Example for TinyPICO: |
| 55 |
|
| 56 |
.. code-block:: python |
| 57 |
|
| 58 |
from dotstar import DotStar |
| 59 |
from machine import Pin, SPI |
| 60 |
|
| 61 |
spi = SPI(sck=Pin(12), mosi=Pin(13), miso=Pin(18)) # Configure SPI - note: miso is unused |
| 62 |
dotstar = DotStar(spi, 1) |
| 63 |
dotstar[0] = (128, 0, 0) # Red |
| 64 |
""" |
| 65 |
|
| 66 |
def __init__(self, spi, n, *, brightness=1.0, auto_write=True, pixel_order=BGR): |
| 67 |
self._spi = spi |
| 68 |
self._n = n |
| 69 |
# Supply one extra clock cycle for each two pixels in the strip. |
| 70 |
self.end_header_size = n // 16 |
| 71 |
if n % 16 != 0: |
| 72 |
self.end_header_size += 1 |
| 73 |
self._buf = bytearray(n * 4 + START_HEADER_SIZE + self.end_header_size) |
| 74 |
self.end_header_index = len(self._buf) - self.end_header_size |
| 75 |
self.pixel_order = pixel_order |
| 76 |
# Four empty bytes to start. |
| 77 |
for i in range(START_HEADER_SIZE): |
| 78 |
self._buf[i] = 0x00 |
| 79 |
# Mark the beginnings of each pixel. |
| 80 |
for i in range(START_HEADER_SIZE, self.end_header_index, 4): |
| 81 |
self._buf[i] = 0xFF |
| 82 |
# 0xff bytes at the end. |
| 83 |
for i in range(self.end_header_index, len(self._buf)): |
| 84 |
self._buf[i] = 0xFF |
| 85 |
self._brightness = 1.0 |
| 86 |
# Set auto_write to False temporarily so brightness setter does _not_ |
| 87 |
# call show() while in __init__. |
| 88 |
self.auto_write = False |
| 89 |
self.brightness = brightness |
| 90 |
self.auto_write = auto_write |
| 91 |
|
| 92 |
def deinit(self): |
| 93 |
"""Blank out the DotStars and release the resources.""" |
| 94 |
self.auto_write = False |
| 95 |
for i in range(START_HEADER_SIZE, self.end_header_index): |
| 96 |
if i % 4 != 0: |
| 97 |
self._buf[i] = 0 |
| 98 |
self.show() |
| 99 |
if self._spi: |
| 100 |
self._spi.deinit() |
| 101 |
|
| 102 |
def __enter__(self): |
| 103 |
return self |
| 104 |
|
| 105 |
def __exit__(self, exception_type, exception_value, traceback): |
| 106 |
self.deinit() |
| 107 |
|
| 108 |
def __repr__(self): |
| 109 |
return "[" + ", ".join([str(x) for x in self]) + "]" |
| 110 |
|
| 111 |
def _set_item(self, index, value): |
| 112 |
""" |
| 113 |
value can be one of three things: |
| 114 |
a (r,g,b) list/tuple |
| 115 |
a (r,g,b, brightness) list/tuple |
| 116 |
a single, longer int that contains RGB values, like 0xFFFFFF |
| 117 |
brightness, if specified should be a float 0-1 |
| 118 |
|
| 119 |
Set a pixel value. You can set per-pixel brightness here, if it's not passed it |
| 120 |
will use the max value for pixel brightness value, which is a good default. |
| 121 |
|
| 122 |
Important notes about the per-pixel brightness - it's accomplished by |
| 123 |
PWMing the entire output of the LED, and that PWM is at a much |
| 124 |
slower clock than the rest of the LEDs. This can cause problems in |
| 125 |
Persistence of Vision Applications |
| 126 |
""" |
| 127 |
|
| 128 |
offset = index * 4 + START_HEADER_SIZE |
| 129 |
rgb = value |
| 130 |
if isinstance(value, int): |
| 131 |
rgb = (value >> 16, (value >> 8) & 0xFF, value & 0xFF) |
| 132 |
|
| 133 |
if len(rgb) == 4: |
| 134 |
brightness = value[3] |
| 135 |
# Ignore value[3] below. |
| 136 |
else: |
| 137 |
brightness = 1 |
| 138 |
|
| 139 |
# LED startframe is three "1" bits, followed by 5 brightness bits |
| 140 |
# then 8 bits for each of R, G, and B. The order of those 3 are configurable and |
| 141 |
# vary based on hardware |
| 142 |
# same as math.ceil(brightness * 31) & 0b00011111 |
| 143 |
# Idea from https://www.codeproject.com/Tips/700780/Fast-floor-ceiling-functions |
| 144 |
brightness_byte = 32 - int(32 - brightness * 31) & 0b00011111 |
| 145 |
self._buf[offset] = brightness_byte | LED_START |
| 146 |
self._buf[offset + 1] = rgb[self.pixel_order[0]] |
| 147 |
self._buf[offset + 2] = rgb[self.pixel_order[1]] |
| 148 |
self._buf[offset + 3] = rgb[self.pixel_order[2]] |
| 149 |
|
| 150 |
def __setitem__(self, index, val): |
| 151 |
if isinstance(index, slice): |
| 152 |
start, stop, step = index.indices(self._n) |
| 153 |
length = stop - start |
| 154 |
if step != 0: |
| 155 |
# same as math.ceil(length / step) |
| 156 |
# Idea from https://fizzbuzzer.com/implement-a-ceil-function/ |
| 157 |
length = (length + step - 1) // step |
| 158 |
if len(val) != length: |
| 159 |
raise ValueError("Slice and input sequence size do not match.") |
| 160 |
for val_i, in_i in enumerate(range(start, stop, step)): |
| 161 |
self._set_item(in_i, val[val_i]) |
| 162 |
else: |
| 163 |
self._set_item(index, val) |
| 164 |
|
| 165 |
if self.auto_write: |
| 166 |
self.show() |
| 167 |
|
| 168 |
def __getitem__(self, index): |
| 169 |
if isinstance(index, slice): |
| 170 |
out = [] |
| 171 |
for in_i in range(*index.indices(self._n)): |
| 172 |
out.append( |
| 173 |
tuple(self._buf[in_i * 4 + (3 - i) + START_HEADER_SIZE] for i in range(3)) |
| 174 |
) |
| 175 |
return out |
| 176 |
if index < 0: |
| 177 |
index += len(self) |
| 178 |
if index >= self._n or index < 0: |
| 179 |
raise IndexError |
| 180 |
offset = index * 4 |
| 181 |
return tuple(self._buf[offset + (3 - i) + START_HEADER_SIZE] for i in range(3)) |
| 182 |
|
| 183 |
def __len__(self): |
| 184 |
return self._n |
| 185 |
|
| 186 |
@property |
| 187 |
def brightness(self): |
| 188 |
"""Overall brightness of the pixel""" |
| 189 |
return self._brightness |
| 190 |
|
| 191 |
@brightness.setter |
| 192 |
def brightness(self, brightness): |
| 193 |
self._brightness = min(max(brightness, 0.0), 1.0) |
| 194 |
if self.auto_write: |
| 195 |
self.show() |
| 196 |
|
| 197 |
def fill(self, color): |
| 198 |
"""Colors all pixels the given ***color***.""" |
| 199 |
auto_write = self.auto_write |
| 200 |
self.auto_write = False |
| 201 |
for i in range(self._n): |
| 202 |
self[i] = color |
| 203 |
if auto_write: |
| 204 |
self.show() |
| 205 |
self.auto_write = auto_write |
| 206 |
|
| 207 |
def show(self): |
| 208 |
"""Shows the new colors on the pixels themselves if they haven't already |
| 209 |
been autowritten. |
| 210 |
|
| 211 |
The colors may or may not be showing after this function returns because |
| 212 |
it may be done asynchronously.""" |
| 213 |
# Create a second output buffer if we need to compute brightness |
| 214 |
buf = self._buf |
| 215 |
if self.brightness < 1.0: |
| 216 |
buf = bytearray(self._buf) |
| 217 |
# Four empty bytes to start. |
| 218 |
for i in range(START_HEADER_SIZE): |
| 219 |
buf[i] = 0x00 |
| 220 |
for i in range(START_HEADER_SIZE, self.end_header_index): |
| 221 |
buf[i] = self._buf[i] if i % 4 == 0 else int(self._buf[i] * self._brightness) |
| 222 |
# Four 0xff bytes at the end. |
| 223 |
for i in range(self.end_header_index, len(buf)): |
| 224 |
buf[i] = 0xFF |
| 225 |
|
| 226 |
if self._spi: |
| 227 |
self._spi.write(buf) |