| 265 |
Kevin |
1 |
import socket, struct, time
|
|
|
2 |
|
|
|
3 |
# Set the address of the Cerebot board
|
|
|
4 |
dst_addr = '00183E00D7EB'.decode('hex')
|
|
|
5 |
# Open a socket on the eth0
|
|
|
6 |
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
|
|
|
7 |
sock.bind(("eth0", 0x1))
|
|
|
8 |
# Acquire MAC address of local machine
|
|
|
9 |
if_name, if_proto, pkt_type, hw_type, hw_addr = sock.getsockname()
|
|
|
10 |
|
|
|
11 |
# Create and initialize the frame buffer
|
|
|
12 |
frame_buffer_size = 1536
|
|
|
13 |
frame_buffer = [0] * frame_buffer_size
|
|
|
14 |
|
|
|
15 |
def cube_init():
|
|
|
16 |
'''Sets the cube into ethernet mode.'''
|
|
|
17 |
# Generate and send the frame
|
|
|
18 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
|
|
|
19 |
txOpcode = "01".decode('hex')
|
|
|
20 |
sock.send(txFrame + txOpcode)
|
|
|
21 |
# Wait a few seconds for the cube to reset itself
|
|
|
22 |
time.sleep(6)
|
|
|
23 |
|
|
|
24 |
def cube_reset():
|
|
|
25 |
'''Resets the cube into idle mode.'''
|
|
|
26 |
# Generate and send the frame
|
|
|
27 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
|
|
|
28 |
txOpcode = "02".decode('hex')
|
|
|
29 |
sock.send(txFrame + txOpcode)
|
|
|
30 |
|
|
|
31 |
def cube_clear():
|
|
|
32 |
'''Clear the cube's internal buffer.'''
|
|
|
33 |
# Generate and send the frame
|
|
|
34 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
|
|
|
35 |
txOpcode = "0A".decode('hex')
|
|
|
36 |
sock.send(txFrame + txOpcode)
|
|
|
37 |
|
|
|
38 |
def cube_brightness(value):
|
|
|
39 |
'''Sets the global brightness value from 0 to 255.'''
|
|
|
40 |
# Generate and send the frame
|
|
|
41 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 2)
|
|
|
42 |
txOpcode = "0B".decode('hex')
|
|
|
43 |
txData = format(value, '02x').decode('hex')
|
|
|
44 |
sock.send(txFrame + txOpcode + txData)
|
|
|
45 |
|
|
|
46 |
def cube_rotate(direction):
|
|
|
47 |
'''Rotate the entire cube (0 = clockwise, 1 = counterclockwise).'''
|
|
|
48 |
# Generate and send the frame
|
|
|
49 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 2)
|
|
|
50 |
txOpcode = "0C".decode('hex')
|
|
|
51 |
txData = format(direction, '02x').decode('hex')
|
|
|
52 |
sock.send(txFrame + txOpcode + txData)
|
|
|
53 |
|
|
|
54 |
def cube_rotate_shell(direction, shell):
|
|
|
55 |
'''Rotate a specific layer (0 = clockwise, 0 = outermost layer).'''
|
|
|
56 |
# Generate and send the frame
|
|
|
57 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 3)
|
|
|
58 |
txOpcode = "0D".decode('hex')
|
|
|
59 |
txData = ''.join("%02x%02x" % (direction, shell))
|
|
|
60 |
sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
61 |
|
|
|
62 |
# def cube_update():
|
|
|
63 |
# '''Update the cube with the current frame buffer.
|
|
|
64 |
# Note: this requires jumbo frames (1536 bytes).'''
|
|
|
65 |
# # Generate the header, opcode, and format the frame buffer
|
|
|
66 |
# txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 0x0601)
|
|
|
67 |
# txOpcode = "10".decode('hex')
|
|
|
68 |
# txData = ''.join("%02x" % (x) for x in frame_buffer)
|
|
|
69 |
# sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
70 |
|
|
|
71 |
def cube_update_pixel(x, y, z, r, g, b):
|
|
|
72 |
'''Set a specific pixel on the cube.'''
|
|
|
73 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 7)
|
|
|
74 |
txOpcode = "11".decode('hex')
|
|
|
75 |
frame = [x, y, z, r, g, b]
|
|
|
76 |
txData = ''.join("%02x" % (x) for x in frame)
|
|
|
77 |
sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
78 |
|
|
|
79 |
def cube_update():
|
|
|
80 |
'''Updates the cube with the current frame buffer.
|
|
|
81 |
The buffer is sent in three frames, one for each color channel.'''
|
|
|
82 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 514)
|
|
|
83 |
txOpcode = "12".decode('hex')
|
|
|
84 |
for c in range(3):
|
|
|
85 |
txColorCh = "%02x" % c
|
|
|
86 |
txData = ''.join( ["%02x" % (x) for x in frame_buffer[c::3]])
|
|
|
87 |
payload = txFrame + txOpcode + txColorCh.decode('hex') + txData.decode('hex')
|
|
|
88 |
sock.send(payload)
|
| 290 |
Kevin |
89 |
time.sleep(0.01) # This value can be smaller (and should be) to reduce flickering
|
| 265 |
Kevin |
90 |
|
| 266 |
Kevin |
91 |
def cube_update_text_scrolling(string, r, g, b, update_rate):
|
| 265 |
Kevin |
92 |
'''Sets the scrolling text on the cube.'''
|
|
|
93 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, len(string) + 6)
|
|
|
94 |
txOpcode = "20".decode('hex')
|
|
|
95 |
txData = ''.join("%02x%02x%02x%02x%02x" % (len(string), r, g, b, update_rate))
|
|
|
96 |
txString = string.encode('hex')
|
|
|
97 |
sock.send(txFrame + txOpcode + txData.decode('hex') + txString.decode('hex'))
|
|
|
98 |
|
| 266 |
Kevin |
99 |
def cube_update_text_static(string, r, g, b):
|
|
|
100 |
'''Sets the static text on the cube.'''
|
|
|
101 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, len(string) + 5)
|
|
|
102 |
txOpcode = "21".decode('hex')
|
|
|
103 |
txData = ''.join("%02x%02x%02x%02x" % (len(string), r, g, b))
|
|
|
104 |
txString = string.encode('hex')
|
|
|
105 |
sock.send(txFrame + txOpcode + txData.decode('hex') + txString.decode('hex'))
|
|
|
106 |
|
| 268 |
Kevin |
107 |
def cube_update_text_insert(character, r, g, b, delay):
|
| 266 |
Kevin |
108 |
'''Appends a character to the beginning of the text buffer.'''
|
| 268 |
Kevin |
109 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 6)
|
| 266 |
Kevin |
110 |
txOpcode = "22".decode('hex')
|
| 268 |
Kevin |
111 |
txData = ''.join("%02x%02x%02x%02x%02x" % (r, g, b, delay, character))
|
| 266 |
Kevin |
112 |
sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
113 |
|
| 265 |
Kevin |
114 |
def cube_update_waterfall(c0, c1, c2, c3, c4, c5, c6, c7):
|
|
|
115 |
'''Fills in one row and shifts rows by one.'''
|
|
|
116 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 9)
|
|
|
117 |
txOpcode = "30".decode('hex')
|
|
|
118 |
txData = ''.join("%02x%02x%02x%02x%02x%02x%02x%02x" % (c0, c1, c2, c3, c4, c5, c6, c7))
|
|
|
119 |
sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
120 |
|
|
|
121 |
def cube_update_sphere(layer, r, g, b):
|
|
|
122 |
'''Sets the sphere layer to the specified color.'''
|
|
|
123 |
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 5)
|
|
|
124 |
txOpcode = "31".decode('hex')
|
|
|
125 |
txData = ''.join("%02x%02x%02x%02x" % (layer, r, g, b))
|
|
|
126 |
sock.send(txFrame + txOpcode + txData.decode('hex'))
|
|
|
127 |
|
|
|
128 |
def fb_clear():
|
|
|
129 |
'''Clears the frame buffer/'''
|
|
|
130 |
for row in range(8):
|
|
|
131 |
for column in range(8):
|
|
|
132 |
for layer in range(8):
|
|
|
133 |
fb_set_pixel(row, column, layer, 0x00, 0x00, 0x00)
|
|
|
134 |
|
|
|
135 |
def fb_set_pixel(row, column, layer, R, G, B):
|
|
|
136 |
'''Sets a pixel to the given color in the frame buffer/'''
|
|
|
137 |
frame_buffer[layer * 192 + column * 24 + row * 3 + 0] = R
|
|
|
138 |
frame_buffer[layer * 192 + column * 24 + row * 3 + 1] = G
|
|
|
139 |
frame_buffer[layer * 192 + column * 24 + row * 3 + 2] = B
|
|
|
140 |
|
|
|
141 |
def fb_set_layer(layer, R, G, B):
|
|
|
142 |
'''Sets an entire layer to the given color in the frame buffer'''
|
|
|
143 |
for row in range(8):
|
|
|
144 |
for column in range(8):
|
|
|
145 |
fb_set_pixel(row, column, layer, R, G, B)
|
|
|
146 |
|
|
|
147 |
def fb_set_all(R, G, B):
|
|
|
148 |
'''Sets all the pixels in the frame buffer to the given color'''
|
|
|
149 |
for row in range(8):
|
|
|
150 |
for column in range(8):
|
|
|
151 |
for layer in range(8):
|
|
|
152 |
fb_set_pixel(row, column, layer, R, G, B)
|