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
|
|
|
|
|
|
|
|
class NeoPixel:
|
|
|
|
def __init__(self, pin, n):
|
|
|
|
self.pin = pin
|
|
|
|
self.n = n
|
|
|
|
self.buf = bytearray(n * 3)
|
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):
|
|
|
|
r, g, b = val
|
|
|
|
self.buf[index * 3] = g
|
|
|
|
self.buf[index * 3 + 1] = r
|
|
|
|
self.buf[index * 3 + 2] = b
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
i = index * 3
|
2016-05-03 06:17:37 -04:00
|
|
|
return self.buf[i + 1], self.buf[i], self.buf[i + 2]
|
2016-02-11 08:21:07 -05:00
|
|
|
|
|
|
|
def write(self):
|
|
|
|
neopixel_write(self.pin, self.buf, True)
|