circuitpython/ports/esp8266/modules/neopixel.py

36 lines
907 B
Python
Raw Normal View History

# NeoPixel driver for MicroPython on ESP8266
# MIT license; Copyright (c) 2016 Damien P. George
2016-02-11 08:21:07 -05:00
from esp import neopixel_write
2016-02-11 08:21:07 -05:00
class NeoPixel:
ORDER = (1, 0, 2, 3)
def __init__(self, pin, n, bpp=3, timing=1):
2016-02-11 08:21:07 -05:00
self.pin = pin
self.n = n
self.bpp = bpp
self.buf = bytearray(n * bpp)
self.pin.init(pin.OUT)
self.timing = timing
2016-02-11 08:21:07 -05:00
def __len__(self):
return self.n
2016-02-11 08:21:07 -05:00
def __setitem__(self, index, val):
offset = index * self.bpp
for i in range(self.bpp):
self.buf[offset + self.ORDER[i]] = val[i]
2016-02-11 08:21:07 -05:00
def __getitem__(self, index):
offset = index * self.bpp
return tuple(self.buf[offset + self.ORDER[i]] for i in range(self.bpp))
2016-02-11 08:21:07 -05:00
def fill(self, color):
for i in range(self.n):
self[i] = color
2016-02-11 08:21:07 -05:00
def write(self):
neopixel_write(self.pin, self.buf, self.timing)