Cleanup and hide the complexity.

master
Raphaël Vinot 2015-04-12 19:37:43 +02:00
parent bd9268860b
commit dd39dcb295
4 changed files with 43 additions and 44 deletions

View File

@ -1,69 +1,40 @@
add_library('net')
import math
import time
import struct
from network import receive_config, send_config, send_TCP
from data_generator import prepare_data
from network import send_TCP
from prepare import prepare
# Config, will be checked upstream
height = 5
width = 8
height = 25
width = 20
framerate = 40
brightness = 0.1
#####################################
receiver_IP = "127.0.0.1"
receiver_port = 9999
brightness = 0.2
dimension = 0
# Do we have one single long line?
long_line = True
# TODO: test with real serial
# https://www.pjrc.com/teensy/td_uart.html
long_line = True
ledTCP = None
data = None
current_px = 0
def TCPConfigure(server, port):
return Client(this, server, port)
def check_config(max_height, max_width, max_framerate):
if height <= 0 or height > max_height:
return False, "height cannot be higher than {}. Current: {}.".format(max_height, height)
if width <= 0 or width > max_width:
return False, "width cannot be higher than {}. Current: {}.".format(max_width, width)
if framerate <= 0 or framerate > max_framerate:
return False, "framerate cannot be higher than {}. Current: {}.".format(max_framerate, framerate)
return True, None
def setup():
global dimension
global data
global ledTCP
ledTCP = TCPConfigure("127.0.0.1", 9999)
max_height, max_width, max_framerate = receive_config(ledTCP)
good, reason = check_config(max_height, max_width, max_framerate)
if not good:
raise Exception(reason)
send_config(ledTCP, height, width, framerate)
size(width, height)
dimension = width * height
frameRate(framerate)
data = prepare_data(dimension, brightness)
loadPixels()
for i in range(dimension):
pixels[i] = color(0, 0, 0)
updatePixels()
global data
ledTCP, data = prepare(Client, receiver_IP, receiver_port, height, width, framerate, brightness)
background(0)
send_TCP(ledTCP, data, long_line)
def draw():
global current_px
pixels[current_px] = color(0, 255, 0)
if current_px == 0:
pixels[len(pixels) - 1] = color(0, 0, 0)
pixels[len(pixels) - 1] = color(255, 0, 0)
else:
pixels[current_px - 1] = color(0, 0, 0)
pixels[current_px - 1] = color(255, 0, 0)
updatePixels()
if current_px == len(pixels) - 1:
current_px = 0

View File

@ -50,6 +50,7 @@ def prepare_data(dimension, b):
def image2data(data, long_line):
offset = 0
pixel_nb = 0
loadPixels()
for x in range(0, height):
pixel_line = pixels[pixel_nb:pixel_nb+width]
if long_line and pixel_nb/width%2 == 1:

View File

@ -3,6 +3,9 @@ import time
import struct
from data_generator import image2data
def TCPConfigure(cl_class, server, port):
return cl_class(this, server, port)
def receive_config(socket):
max_height = jarray.zeros(4, "b")
max_width = jarray.zeros(4, "b")
@ -27,5 +30,4 @@ def send_config(socket, height, width, framerate):
def send_TCP(socket, data, long_line):
image2data(data, long_line)
# print data
socket.write(data)

View File

@ -0,0 +1,25 @@
from network import receive_config, send_config, TCPConfigure
from data_generator import prepare_data
def check_config(max_height, max_width, max_framerate, height, width, framerate):
if height <= 0 or height > max_height:
return False, "height cannot be higher than {}. Current: {}.".format(max_height, height)
if width <= 0 or width > max_width:
return False, "width cannot be higher than {}. Current: {}.".format(max_width, width)
if framerate <= 0 or framerate > max_framerate:
return False, "framerate cannot be higher than {}. Current: {}.".format(max_framerate, framerate)
return True, None
def prepare(cl_class, server_ip, server_port, height, width, framerate, brightness):
# Just to make sure pixels[] is initialized.
loadPixels()
ledTCP = TCPConfigure(cl_class, server_ip, server_port)
max_height, max_width, max_framerate = receive_config(ledTCP)
good, reason = check_config(max_height, max_width, max_framerate, height, width, framerate)
if not good:
raise Exception(reason)
send_config(ledTCP, height, width, framerate)
size(width, height)
dimension = width * height
data = prepare_data(dimension, brightness)
return ledTCP, data