2016-04-28 13:37:17 +01:00
|
|
|
# NeoPixel driver for MicroPython on ESP8266
|
|
|
|
# MIT license; Copyright (c) 2016 Damien P. George
|
|
|
|
|
2016-02-11 13:21:07 +00:00
|
|
|
from esp import neopixel_write
|
|
|
|
|
2016-10-19 21:09:51 +01:00
|
|
|
|
2016-02-11 13:21:07 +00:00
|
|
|
class NeoPixel:
|
2016-10-19 21:09:51 +01:00
|
|
|
ORDER = (1, 0, 2, 3)
|
|
|
|
|
|
|
|
def __init__(self, pin, n, bpp=3):
|
2016-02-11 13:21:07 +00:00
|
|
|
self.pin = pin
|
|
|
|
self.n = n
|
2016-10-19 21:09:51 +01:00
|
|
|
self.bpp = bpp
|
|
|
|
self.buf = bytearray(n * bpp)
|
2016-05-05 21:47:37 +01:00
|
|
|
self.pin.init(pin.OUT)
|
2016-02-11 13:21:07 +00:00
|
|
|
|
|
|
|
def __setitem__(self, index, val):
|
2016-10-19 21:09:51 +01:00
|
|
|
offset = index * self.bpp
|
|
|
|
for i in range(self.bpp):
|
|
|
|
self.buf[offset + self.ORDER[i]] = val[i]
|
2016-02-11 13:21:07 +00:00
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2016-10-19 21:09:51 +01:00
|
|
|
offset = index * self.bpp
|
|
|
|
return tuple(self.buf[offset + self.ORDER[i]]
|
|
|
|
for i in range(self.bpp))
|
2016-02-11 13:21:07 +00:00
|
|
|
|
2016-05-06 08:28:14 +01:00
|
|
|
def fill(self, color):
|
2016-10-19 21:09:51 +01:00
|
|
|
for i in range(self.n):
|
|
|
|
self[i] = color
|
2016-05-06 08:28:14 +01:00
|
|
|
|
2016-02-11 13:21:07 +00:00
|
|
|
def write(self):
|
|
|
|
neopixel_write(self.pin, self.buf, True)
|