2016-04-28 08:37:17 -04:00
|
|
|
# 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-10-19 16:09:51 -04:00
|
|
|
|
2016-02-11 08:21:07 -05:00
|
|
|
class NeoPixel:
|
2016-10-19 16:09:51 -04:00
|
|
|
ORDER = (1, 0, 2, 3)
|
|
|
|
|
2021-01-26 04:28:20 -05:00
|
|
|
def __init__(self, pin, n, bpp=3, timing=1):
|
2016-02-11 08:21:07 -05:00
|
|
|
self.pin = pin
|
|
|
|
self.n = n
|
2016-10-19 16:09:51 -04:00
|
|
|
self.bpp = bpp
|
|
|
|
self.buf = bytearray(n * bpp)
|
2016-05-05 16:47:37 -04:00
|
|
|
self.pin.init(pin.OUT)
|
2021-01-26 04:28:20 -05:00
|
|
|
self.timing = timing
|
2016-02-11 08:21:07 -05:00
|
|
|
|
2021-03-26 03:12:13 -04:00
|
|
|
def __len__(self):
|
|
|
|
return self.n
|
|
|
|
|
2016-02-11 08:21:07 -05:00
|
|
|
def __setitem__(self, index, val):
|
2016-10-19 16:09:51 -04:00
|
|
|
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):
|
2016-10-19 16:09:51 -04:00
|
|
|
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
|
|
|
|
2016-05-06 03:28:14 -04:00
|
|
|
def fill(self, color):
|
2016-10-19 16:09:51 -04:00
|
|
|
for i in range(self.n):
|
|
|
|
self[i] = color
|
2016-05-06 03:28:14 -04:00
|
|
|
|
2016-02-11 08:21:07 -05:00
|
|
|
def write(self):
|
2021-01-26 04:28:20 -05:00
|
|
|
neopixel_write(self.pin, self.buf, self.timing)
|