Skip to content

Commit 93c2a89

Browse files
committed
initial neopixel
1 parent bcb6917 commit 93c2a89

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

neopixel.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# NeoPixel driver for MicroPython on ESP8266
2+
# MIT license; Copyright (c) 2016 Damien P. George
3+
4+
import nativeio
5+
from neopixel_write import neopixel_write
6+
7+
class NeoPixel:
8+
def __init__(self, pin, n):
9+
self.pin = nativeio.DigitalInOut(pin)
10+
self.n = n
11+
self.buf = bytearray(n * 3)
12+
self.pin.switch_to_output()
13+
14+
def __enter__(self):
15+
return self
16+
17+
def __exit__(self, exception_type, exception_value, traceback):
18+
self.pin.deinit()
19+
20+
def __setitem__(self, index, val):
21+
r, g, b = val
22+
self.buf[index * 3] = g
23+
self.buf[index * 3 + 1] = r
24+
self.buf[index * 3 + 2] = b
25+
26+
def __getitem__(self, index):
27+
i = index * 3
28+
return self.buf[i + 1], self.buf[i], self.buf[i + 2]
29+
30+
def fill(self, color):
31+
r, g, b = color
32+
for i in range(len(self.buf) / 3):
33+
self.buf[i * 3] = g
34+
self.buf[i * 3 + 1] = r
35+
self.buf[i * 3 + 2] = b
36+
37+
def write(self):
38+
neopixel_write(self.pin, self.buf, True)

0 commit comments

Comments
 (0)