-
Notifications
You must be signed in to change notification settings - Fork 1
/
fakepixel.py
77 lines (59 loc) · 2.27 KB
/
fakepixel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys
def Color(red, green, blue, white=0):
return (white << 24) | (red << 16) | (green << 8) | blue
class ws:
WS2811_STRIP_RGB = 1
WS2811_STRIP_RBG = 2
WS2811_STRIP_GRB = 3
WS2811_STRIP_GBR = 4
WS2811_STRIP_BRG = 5
WS2811_STRIP_BGR = 6
SK6812_STRIP_RGBW = 10
class Fake_NeoPixel:
def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False,
brightness=255, channel=None, strip_type=None, debug=False):
self.dest = sys.stdout
self._ledstr = '●' # small
# self._ledstr = '⬤ ' # big
self._led_data = [0]*num
self._brightness = brightness
self.debug = debug
def __del__(self):
self._cleanup()
def _cleanup(self):
# Ensure default colour, and show cursor again
print('\x1b[39;49m\x1b[?25h', file=self.dest, flush=True)
def begin(self):
pass
def show(self):
b = self._brightness / 255.0
def col(bits, shift):
w = ((bits >> 24) & 0xff)
c = ((bits >> shift) & 0xff)
return min(255, round((c + w) * b))
print('\x1b[?25l', # Hide cursor
'' if self.debug else '\x0d', # Go back to start of line
'\x1b[48;2;64;64;64m', # Set grey background
' ',
''.join('\x1b[38;2;{r};{g};{b}m{s}'.format(r=col(c, 16),
g=col(c, 8),
b=col(c, 0),
s=self._ledstr)
for c in self._led_data), # Coloured pixels
' ',
'\x1b[39;49m', # Reset colour to default
' ', # Overwrite any following chars if present
'\n' if self.debug else '',
sep='', end='', file=self.dest, flush=True)
def setPixelColor(self, i, c):
self._led_data[i] = c
def setPixelColorRGB(self, i, r, g, b, w=0):
self._led_data[i] = Color(r, g, b, w)
def setBrightness(self, b):
self._brightness = b
def getPixels(self):
return self._led_data
def numPixels(self):
return len(self._led_data)
def getPixelColor(self, n):
return self._led_data[n]