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)
|
|
|
|
|
|
|
|
def __init__(self, pin, n, bpp=3):
|
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)
|
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
|
2020-02-26 23:36:53 -05:00
|
|
|
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):
|
|
|
|
neopixel_write(self.pin, self.buf, True)
|