messing around

master
Raphaël Vinot 2015-03-21 20:36:53 +01:00
parent 3ee6e5332b
commit d163dfe7bd
3 changed files with 85 additions and 74 deletions

View File

@ -1,8 +1,9 @@
add_library('net')
import math
import jarray
import time
import struct
from network import receive_config, send_config, send_TCP
from data_generator import prepare_data
# Config, will be checked upstream
height = 5
@ -23,101 +24,42 @@ ledTCP = None
data = None
current_px = 0
gammatable = None
def TCPConfigure(server, port):
global ledTCP
ledTCP = Client(this, server, port)
def image2data(data):
offset = 0
pixel_nb = 0
for x in range(0, height):
pixel_line = pixels[pixel_nb:pixel_nb+width]
if long_line and pixel_nb/width%2 == 1:
pixel_line = reversed(pixel_line)
for px in pixel_line:
pixel = [colorWiring(px) for i in range(0, 8)]
imgmask = 0x800000
while imgmask != 0:
b = 0
for i in range(0, 8):
if ((pixel[i] & imgmask) != 0):
b |= (1 << i)
if b > 127:
# Convert to signed bytes (expected by jarray)
b -= 2**8
data[offset] = b
else:
data[offset] = b
offset += 1
imgmask >>= 1
pixel_nb +=1
def colorWiring(c):
red = (c & 0xFF0000) >> 16
green = (c & 0x00FF00) >> 8
blue = (c & 0x0000FF)
red = gammatable[red] >> 8
green = gammatable[green] >> 8
blue = gammatable[blue] >> 8
return (green << 16) | (red << 8) | (blue)
def send_TCP():
image2data(data)
ledTCP.write(data)
def prepare_data():
global data
data = jarray.zeros(dimension * 24, "b")
def receive_config():
max_height = jarray.zeros(1, "b")
max_width = jarray.zeros(1, "b")
max_framerate = jarray.zeros(1, "b")
while True:
available_bytes = ledTCP.available()
if available_bytes > 0:
break
time.sleep(1)
ledTCP.readBytes(max_height)
ledTCP.readBytes(max_width)
ledTCP.readBytes(max_framerate)
return max_height[0], max_width[0], max_framerate[0]
return Client(this, server, port)
def check_config(max_height, max_width, max_framerate):
if height > max_height:
return False, "height cannot be higher than {}".format(max_height)
return False, "height cannot be higher than {}. Current: {}.".format(max_height, height)
if width > max_width:
return False, "width cannot be higher than {}".format(max_width)
return False, "width cannot be higher than {}. Current: {}.".format(max_width, width)
if framerate > max_framerate:
return False, "framerate cannot be higher than {}".format(max_framerate)
return False, "framerate cannot be higher than {}. Current: {}.".format(max_framerate, framerate)
return True, None
def send_config():
ledTCP.write(height)
ledTCP.write(width)
ledTCP.write(framerate)
def setup():
global gammatable
global dimension
TCPConfigure("127.0.0.1", 9999)
max_height, max_width, max_framerate = receive_config()
global data
global ledTCP
ledTCP = TCPConfigure("127.0.0.1", 9999)
max_height, max_width, max_framerate = receive_config(ledTCP)
print(max_height, max_width, max_framerate)
good, reason = check_config(max_height, max_width, max_framerate)
if not good:
raise Exception(reason)
send_config()
send_config(ledTCP)
size(width, height)
dimension = width * height
frameRate(framerate)
gammatable = [int((math.pow(i / 255.0, gamma) * 255.0 + 0.5) * brightness) for i in range(0, 256)]
prepare_data()
data, gammatable = prepare_data(dimension, gamma, brightness)
loadPixels()
for i in range(dimension):
pixels[i] = color(0, 0, 0)
updatePixels()
send_TCP()
send_TCP(ledTCP, data, long_line, gammatable)
def draw():
global current_px
@ -131,4 +73,4 @@ def draw():
current_px = 0
else:
current_px += 1
send_TCP()
send_TCP(ledTCP, data, long_line, gammatable)

View File

@ -0,0 +1,43 @@
import jarray
import math
def prepare_data(dimension, gamma, brightness):
gammatable = [int((math.pow(i / 255.0, gamma) * 255.0 + 0.5) * brightness) for i in range(0, 256)]
data = jarray.zeros(dimension * 24, "b")
return data, gammatable
def image2data(data, long_line, gammatable):
offset = 0
pixel_nb = 0
for x in range(0, height):
pixel_line = pixels[pixel_nb:pixel_nb+width]
if long_line and pixel_nb/width%2 == 1:
pixel_line = reversed(pixel_line)
for px in pixel_line:
pixel = [colorWiring(px, gammatable) for i in range(0, 8)]
imgmask = 0x800000
while imgmask != 0:
b = 0
for i in range(0, 8):
if ((pixel[i] & imgmask) != 0):
b |= (1 << i)
if b > 127:
# Convert to signed bytes (expected by jarray)
b -= 2**8
data[offset] = b
else:
data[offset] = b
offset += 1
imgmask >>= 1
pixel_nb +=1
return data
def colorWiring(c, gammatable):
red = (c & 0xFF0000) >> 16
green = (c & 0x00FF00) >> 8
blue = (c & 0x0000FF)
red = gammatable[red] >> 8
green = gammatable[green] >> 8
blue = gammatable[blue] >> 8
return (green << 16) | (red << 8) | (blue)

View File

@ -0,0 +1,26 @@
import jarray
import time
from data_generator import image2data
def receive_config(socket):
max_height = jarray.zeros(1, "b")
max_width = jarray.zeros(1, "b")
max_framerate = jarray.zeros(1, "b")
while True:
available_bytes = socket.available()
if available_bytes > 0:
break
time.sleep(1)
socket.readBytes(max_height)
socket.readBytes(max_width)
socket.readBytes(max_framerate)
return max_height[0], max_width[0], max_framerate[0]
def send_config(socket, height, width, framerate):
socket.write(height)
socket.write(width)
socket.write(framerate)
def send_TCP(socket, data, long_line, gammatable):
image2data(data, long_line, gammatable)
socket.write(data)