From dd39dcb295c8168f24f0b5c37e059b76b6bafde6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 12 Apr 2015 19:37:43 +0200 Subject: [PATCH] Cleanup and hide the complexity. --- .../PixelControl_TCP/PixelControl_TCP.pyde | 57 +++++-------------- .../PixelControl_TCP/data_generator.py | 1 + .../processing/PixelControl_TCP/network.py | 4 +- .../processing/PixelControl_TCP/prepare.py | 25 ++++++++ 4 files changed, 43 insertions(+), 44 deletions(-) create mode 100644 v2/backend/processing/PixelControl_TCP/prepare.py diff --git a/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde index 8407398..8f6198c 100644 --- a/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde +++ b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde @@ -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 diff --git a/v2/backend/processing/PixelControl_TCP/data_generator.py b/v2/backend/processing/PixelControl_TCP/data_generator.py index 7bc3019..f14fbfb 100644 --- a/v2/backend/processing/PixelControl_TCP/data_generator.py +++ b/v2/backend/processing/PixelControl_TCP/data_generator.py @@ -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: diff --git a/v2/backend/processing/PixelControl_TCP/network.py b/v2/backend/processing/PixelControl_TCP/network.py index f4b2ae0..0199949 100644 --- a/v2/backend/processing/PixelControl_TCP/network.py +++ b/v2/backend/processing/PixelControl_TCP/network.py @@ -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) diff --git a/v2/backend/processing/PixelControl_TCP/prepare.py b/v2/backend/processing/PixelControl_TCP/prepare.py new file mode 100644 index 0000000..9f7b79c --- /dev/null +++ b/v2/backend/processing/PixelControl_TCP/prepare.py @@ -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