-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.py
62 lines (53 loc) · 1.89 KB
/
remote.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
#!/usr/bin/env python
from serial import Serial
from itertools import product
from collections import defaultdict
import struct
COLS = 20
ROWS = 16
PIXELS = set(product(xrange(COLS), xrange(ROWS)))
class Remote(object):
BLACK = True
WHITE = False
COLORS = (BLACK, WHITE)
def __init__(self, serial_class=Serial, port='/dev/ttyUSB0'):
self.port = serial_class(port, 9600, timeout=0.1)
self.framebuf = [None] * ROWS * COLS
self.clip = {color: defaultdict(int) for color in self.COLORS}
for pixel in PIXELS:
self.set_pixel(pixel, self.BLACK)
self.flush_pixels()
def get_pixel(self, pixel):
return self.framebuf[pixel2fbindex(pixel)]
def set_pixel(self, pixel, value):
fbi = pixel2fbindex(pixel)
if self.framebuf[fbi] == value:
return
self.framebuf[fbi] = value
self.clip[value][pixel[int(not value)]] |= 1 << pixel[int(value)]
def flush_pixels(self):
for color, lines in self.clip.items():
if not lines:
continue
for num, line in lines.items():
if color == self.BLACK:
self.port.write(chr(0x20 | (COLS - num - 1)) + struct.pack('>H', line))
elif color == self.WHITE:
self.port.write(chr(0x10 | (ROWS - num - 1)) + struct.pack('>I', line ^ 0xFFFFFF)[1:])
else:
raise ValueError('Invalid color {0}'.format(color))
while not self.port.read():
pass
lines.clear()
def pixel2fbindex(pixel):
col, row = pixel
return row * COLS + col
if __name__ == '__main__':
from time import sleep
r = Remote()
for y in xrange(ROWS):
for x in xrange(COLS):
r.set_pixel((x, y), False)
r.flush_pixels()
sleep(0.05)
r.set_pixel((x, y), True)