forked from pbrook/pycubedemo
-
Notifications
You must be signed in to change notification settings - Fork 6
/
serialcube.py
177 lines (156 loc) · 4.8 KB
/
serialcube.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# LED cube serial driver
# Copyright (C) Paul Brook <[email protected]>
# Released under the terms of the GNU General Public License version 3
import numpy
import cubehelper
import socket
BUFFER_SIZE = 128
class TCPWriter(object):
def __init__(self, addr):
(host, port) = addr.split(':')
port = int(port)
if host == "":
host = "localhost"
self.sock = socket.create_connection((host, int(port)))
#self.sock.recv(4)
def write(self, b):
self.sock.sendall(b)
def FileWriter(name):
return open(name, "wb")
def SerialWriter(port):
import serial
ser = serial.Serial(port, 115200, timeout=2)
ser.read(4)
return ser
class SPIWriter(object):
def __init__(self, port):
import spidev
if port[:11] == '/dev/spidev':
port = port[11:]
bus = None
for sep in '.,:-':
if sep in port:
p = port.split(sep)
bus = int(p[0])
dev = int(p[1])
break
if bus is None:
if port == '':
bus = 1
else:
bus = int(port)
dev = 0
spi = spidev.SpiDev(bus, dev)
spi.max_speed_hz = 2000000
spi.mode = 3
spi.lsbfirst = False
spi.cshigh = False
spi.bits_per_word = 8
self.spi = spi
def write(self, b):
self.spi.writebytes(b)
def minicube_map(xyz):
return (0, xyz[0] + xyz[1] * 4 + xyz[2] * 16)
def maxicube_map(xyz):
(x, y, z) = xyz
pos = 0
board = y//2
pos = x + ((y ^ 1) % 2) * 8 + (z ^ 1) * 16
return (board, pos)
class Cube(object):
def __init__(self, args):
writers = {'tcp':TCPWriter, 'file':FileWriter, 'serial':SerialWriter, 'spi':SPIWriter}
if ':' in args.port:
(proto, port) = args.port.split(':', 1)
else:
proto = ''
if proto not in writers:
port = args.port
if ':' in port:
proto = 'tcp'
elif port[:8] == '/dev/tty':
proto = 'serial'
elif port[:8] == '/dev/spi':
proto = 'spi'
elif port[0] == '@':
proto = 'file'
port = port[1:]
else:
proto = 'file'
self.ser = writers[proto](port)
self.current_board = None
self.size = args.size
self.write_page = 0
self.display_page = 0
if self.size == 4:
self.mapfn = minicube_map
self.color = False
elif self.size == 8:
self.mapfn = maxicube_map
self.color = True
else:
raise Exception("Bad cube size: %d" % args.size)
self.buffer_len = 0
if BUFFER_SIZE > 0:
self.cmd_buffer = numpy.zeros(BUFFER_SIZE, numpy.uint8)
def _flush_data(self):
n = self.buffer_len
self.buffer_len = 0
if n == 0 or BUFFER_SIZE == 0:
return
if n == BUFFER_SIZE:
self.ser.write(self.cmd_buffer)
else:
self.ser.write(self.cmd_buffer[:n])
def do_cmd(self, cmd, d0, d1, d2):
if BUFFER_SIZE > 0:
pos = self.buffer_len
buf = self.cmd_buffer
buf[pos] = cmd
buf[pos + 1] = d0
buf[pos + 2] = d1
buf[pos + 3] = d2
try:
self.buffer_len += 4
if self.buffer_len == BUFFER_SIZE:
self._flush_data()
except:
self.buffer_len = 0
raise
else:
self.ser.write(bytearray((cmd, d0, d1, d2)))
def bus_reset(self):
self.do_cmd(0xff, 0xff, 0xff, 0xff)
self.do_cmd(0xe0, 0xf0, 0xf1, 0xf2)
self.current_board = None
def select_board(self, board = 0xff):
self.bus_reset()
self.do_cmd(0xe1, board, 0, 0)
self.current_board = board
def set_brightness(self, rgb):
self.select_board()
self.do_cmd(0xc0, rgb[0], rgb[1], rgb[2])
def clear(self):
self.select_board()
for i in range(0, 128):
self.do_cmd(i, 0, 0, 0)
def _flip(self):
self.select_board()
self.do_cmd(0x80, 0, self.display_page, self.write_page)
self._flush_data()
def single_buffer(self):
self.write_page = self.display_page
self._flip()
def swap(self):
self.display_page = self.write_page
self.write_page = 1 - self.write_page
self._flip()
def set_pixel(self, xyz, rgb):
(r, g, b) = cubehelper.color_to_int(rgb)
(board, offset) = self.mapfn(xyz)
if board != self.current_board:
self.select_board(board)
self.do_cmd(offset, r, g, b)
def render(self):
self.bus_reset()
self._flush_data()