micropython/ports/esp8266/modules/neopixel.py

33 lines
836 B
Python
Raw Normal View History

# 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-02-11 13:21:07 +00:00
class NeoPixel:
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
self.bpp = bpp
self.buf = bytearray(n * bpp)
self.pin.init(pin.OUT)
2016-02-11 13:21:07 +00: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 13:21:07 +00: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 13:21:07 +00:00
def fill(self, color):
for i in range(self.n):
self[i] = color
2016-02-11 13:21:07 +00:00
def write(self):
neopixel_write(self.pin, self.buf, True)