Subversion Repositories Code-Repo

Compare Revisions

Ignore whitespace Rev 290 → Rev 342

/PIC Projects/Cerebot_32MX7_LED_Cube/Ethernet API/cube.py
0,0 → 1,152
import socket, struct, time
 
# Set the address of the Cerebot board
dst_addr = '00183E00D7EB'.decode('hex')
# Open a socket on the eth0
sock = socket.socket(socket.AF_PACKET, socket.SOCK_RAW)
sock.bind(("eth0", 0x1))
# Acquire MAC address of local machine
if_name, if_proto, pkt_type, hw_type, hw_addr = sock.getsockname()
 
# Create and initialize the frame buffer
frame_buffer_size = 1536
frame_buffer = [0] * frame_buffer_size
 
def cube_init():
'''Sets the cube into ethernet mode.'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
txOpcode = "01".decode('hex')
sock.send(txFrame + txOpcode)
# Wait a few seconds for the cube to reset itself
time.sleep(6)
 
def cube_reset():
'''Resets the cube into idle mode.'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
txOpcode = "02".decode('hex')
sock.send(txFrame + txOpcode)
 
def cube_clear():
'''Clear the cube's internal buffer.'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 1)
txOpcode = "0A".decode('hex')
sock.send(txFrame + txOpcode)
 
def cube_brightness(value):
'''Sets the global brightness value from 0 to 255.'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 2)
txOpcode = "0B".decode('hex')
txData = format(value, '02x').decode('hex')
sock.send(txFrame + txOpcode + txData)
 
def cube_rotate(direction):
'''Rotate the entire cube (0 = clockwise, 1 = counterclockwise).'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 2)
txOpcode = "0C".decode('hex')
txData = format(direction, '02x').decode('hex')
sock.send(txFrame + txOpcode + txData)
 
def cube_rotate_shell(direction, shell):
'''Rotate a specific layer (0 = clockwise, 0 = outermost layer).'''
# Generate and send the frame
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 3)
txOpcode = "0D".decode('hex')
txData = ''.join("%02x%02x" % (direction, shell))
sock.send(txFrame + txOpcode + txData.decode('hex'))
 
# def cube_update():
# '''Update the cube with the current frame buffer.
# Note: this requires jumbo frames (1536 bytes).'''
# # Generate the header, opcode, and format the frame buffer
# txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 0x0601)
# txOpcode = "10".decode('hex')
# txData = ''.join("%02x" % (x) for x in frame_buffer)
# sock.send(txFrame + txOpcode + txData.decode('hex'))
 
def cube_update_pixel(x, y, z, r, g, b):
'''Set a specific pixel on the cube.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 7)
txOpcode = "11".decode('hex')
frame = [x, y, z, r, g, b]
txData = ''.join("%02x" % (x) for x in frame)
sock.send(txFrame + txOpcode + txData.decode('hex'))
 
def cube_update():
'''Updates the cube with the current frame buffer.
The buffer is sent in three frames, one for each color channel.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 514)
txOpcode = "12".decode('hex')
for c in range(3):
txColorCh = "%02x" % c
txData = ''.join( ["%02x" % (x) for x in frame_buffer[c::3]])
payload = txFrame + txOpcode + txColorCh.decode('hex') + txData.decode('hex')
sock.send(payload)
time.sleep(0.01) # This value can be smaller (and should be) to reduce flickering
 
def cube_update_text_scrolling(string, r, g, b, update_rate):
'''Sets the scrolling text on the cube.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, len(string) + 6)
txOpcode = "20".decode('hex')
txData = ''.join("%02x%02x%02x%02x%02x" % (len(string), r, g, b, update_rate))
txString = string.encode('hex')
sock.send(txFrame + txOpcode + txData.decode('hex') + txString.decode('hex'))
 
def cube_update_text_static(string, r, g, b):
'''Sets the static text on the cube.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, len(string) + 5)
txOpcode = "21".decode('hex')
txData = ''.join("%02x%02x%02x%02x" % (len(string), r, g, b))
txString = string.encode('hex')
sock.send(txFrame + txOpcode + txData.decode('hex') + txString.decode('hex'))
 
def cube_update_text_insert(character, r, g, b, delay):
'''Appends a character to the beginning of the text buffer.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 6)
txOpcode = "22".decode('hex')
txData = ''.join("%02x%02x%02x%02x%02x" % (r, g, b, delay, character))
sock.send(txFrame + txOpcode + txData.decode('hex'))
 
def cube_update_waterfall(c0, c1, c2, c3, c4, c5, c6, c7):
'''Fills in one row and shifts rows by one.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 9)
txOpcode = "30".decode('hex')
txData = ''.join("%02x%02x%02x%02x%02x%02x%02x%02x" % (c0, c1, c2, c3, c4, c5, c6, c7))
sock.send(txFrame + txOpcode + txData.decode('hex'))
 
def cube_update_sphere(layer, r, g, b):
'''Sets the sphere layer to the specified color.'''
txFrame = struct.pack("!6s6sH", dst_addr, hw_addr, 5)
txOpcode = "31".decode('hex')
txData = ''.join("%02x%02x%02x%02x" % (layer, r, g, b))
sock.send(txFrame + txOpcode + txData.decode('hex'))
 
def fb_clear():
'''Clears the frame buffer/'''
for row in range(8):
for column in range(8):
for layer in range(8):
fb_set_pixel(row, column, layer, 0x00, 0x00, 0x00)
 
def fb_set_pixel(row, column, layer, R, G, B):
'''Sets a pixel to the given color in the frame buffer/'''
frame_buffer[layer * 192 + column * 24 + row * 3 + 0] = R
frame_buffer[layer * 192 + column * 24 + row * 3 + 1] = G
frame_buffer[layer * 192 + column * 24 + row * 3 + 2] = B
 
def fb_set_layer(layer, R, G, B):
'''Sets an entire layer to the given color in the frame buffer'''
for row in range(8):
for column in range(8):
fb_set_pixel(row, column, layer, R, G, B)
def fb_set_all(R, G, B):
'''Sets all the pixels in the frame buffer to the given color'''
for row in range(8):
for column in range(8):
for layer in range(8):
fb_set_pixel(row, column, layer, R, G, B)
/PIC Projects/Cerebot_32MX7_LED_Cube/Ethernet API/animations.py
0,0 → 1,59
from cube import *
from time import sleep
 
def solid_colors(delay):
fb_set_all(0xFF, 0x00, 0x00)
cube_update()
sleep(delay)
fb_set_all(0x00, 0xFF, 0x00)
cube_update()
sleep(delay)
fb_set_all(0x00, 0x00, 0xFF)
cube_update()
sleep(delay)
 
def row_column_sweep(delay):
# Sweep across three colors (R,G,B)
for color in range(3):
# Sweep across each row
for row in range(8):
fb_set_clear()
for column in range(8):
for layer in range(8):
if color % 3 == 0:
fb_set_pixel(row, column, layer, 0xFF, 0x00, 0x00)
elif color % 3 == 1:
fb_set_pixel(row, column, layer, 0x00, 0xFF, 0x00)
else:
fb_set_pixel(row, column, layer, 0x00, 0x00, 0xFF)
cube_update()
sleep(delay)
# Sweep across each column
for column in range(8):
fb_set_clear()
for row in range(8):
for layer in range(8):
if color % 3 == 0:
fb_set_pixel(row, column, layer, 0xFF, 0x00, 0x00)
elif color % 3 == 1:
fb_set_pixel(row, column, layer, 0x00, 0xFF, 0x00)
else:
fb_set_pixel(row, column, layer, 0x00, 0x00, 0xFF)
cube_update()
sleep(delay)
# Sweep across each layer
for layer in range(7, -1, -1):
fb_set_clear()
for layer_2 in range(8):
if color % 3 == 0:
if layer_2 == layer:
set_layer(layer_2, 0xFF, 0x00, 0x00)
elif color % 3 == 1:
if layer_2 == layer:
set_layer(layer_2, 0x00, 0xFF, 0x00)
else:
if layer_2 == layer:
set_layer(layer_2, 0x00, 0x00, 0xFF)
cube_update()
sleep(delay)
/PIC Projects/Cerebot_32MX7_LED_Cube/Ethernet API/main.py
0,0 → 1,44
from cube import *
import animations
from time import sleep
 
if __name__ == '__main__':
# Put the cube into ethernet mode
print "Initializing cube. Wait 6 seconds...\n"
# cube_init()
# ----- Begin animations -----
 
cube_clear()
cube_update_text("CCM LAB ", 0xFF, 0xFF, 0xFF, 100)
print "Looping animations...\n"
while(1):
 
# for i in range(9):
# cube_clear()
# cube_update_sphere(i, 0xFF, 0x00, 0x00)
# sleep(0.1)
 
cube_update_waterfall(1, 2, 3, 4, 5, 6, 7, 8)
sleep(0.1)
 
# #cube_update_pixel(x, y, z, r, g, b)
# for x in range(8):
# for y in range(8):
# for z in range(8):
# cube_update_pixel(x, y, z, 255, 255, 255)
# time.sleep(0.11)
# cube_update_pixel(x, y, z, 0, 0, 0)
# time.sleep(0.1)
# #time.sleep(0.1)
# ----- End Animations -----
 
# Reset the cube into idle mode (optional)
print "Animations done. Returning board to idle mode..."
cube_reset()