From c718700bfcabfa58b41cd8d3b4ca863aac2054c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Wed, 29 Apr 2015 20:20:44 +0200 Subject: [PATCH] add very simple code randomly lighting up LEDs --- .gitignore | 3 + v2/backend/processing/Random/Random.pyde | 36 ++++++++++ .../processing/Random/data_generator.py | 71 +++++++++++++++++++ v2/backend/processing/Random/network.py | 33 +++++++++ v2/backend/processing/Random/prepare.py | 25 +++++++ 5 files changed, 168 insertions(+) create mode 100644 v2/backend/processing/Random/Random.pyde create mode 100644 v2/backend/processing/Random/data_generator.py create mode 100644 v2/backend/processing/Random/network.py create mode 100644 v2/backend/processing/Random/prepare.py diff --git a/.gitignore b/.gitignore index f7aebf8..1c7a66e 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,6 @@ application.windows32 application.windows64 application.macosx +# PyProcessing +*.class + diff --git a/v2/backend/processing/Random/Random.pyde b/v2/backend/processing/Random/Random.pyde new file mode 100644 index 0000000..bcc51c3 --- /dev/null +++ b/v2/backend/processing/Random/Random.pyde @@ -0,0 +1,36 @@ +add_library('net') +from network import send_TCP +from prepare import prepare +import random + +# Config, will be checked upstream +height = 25 +width = 20 +framerate = 30 +brightness = 0.1 +##################################### +receiver_IP = "10.2.113.151" +#receiver_IP = "127.0.0.1" +receiver_port = 9999 + +# Do we have one single long line? +long_line = False + +# TODO: test with real serial +# https://www.pjrc.com/teensy/td_uart.html + +ledTCP = None +data = None + +def setup(): + global ledTCP + global data + ledTCP, data = prepare(Client, receiver_IP, receiver_port, height, width, framerate, brightness) + background(0) + send_TCP(ledTCP, data, long_line) + +def draw(): + pixels[random.randint(0, len(pixels) - 1)] = color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) + pixels[random.randint(0, len(pixels) - 1)] = color(0, 0, 0) + updatePixels() + send_TCP(ledTCP, data, long_line) diff --git a/v2/backend/processing/Random/data_generator.py b/v2/backend/processing/Random/data_generator.py new file mode 100644 index 0000000..91b82fe --- /dev/null +++ b/v2/backend/processing/Random/data_generator.py @@ -0,0 +1,71 @@ +import jarray +import math +import itertools + +_byte_lookup = {} +brightness_max = 0.5 + +# Bit values which represent the zero and one bit pulses. +_ZERO = bytearray([0b00000000]) +_ONE = bytearray([0b11111111]) + +def _build_byte_lookup(): + """Return lookup table to map from every byte value 0-255 and the associated raw SPI data.""" + lookup = {} + for i in range(256): + value = bytearray() + for j in range(7, -1, -1): + if ((i >> j) & 1) == 0: + value += _ZERO + else: + value += _ONE + lookup[i] = value + return lookup + +def color_to_rgb(c): + """Convert a 24 bit color to RGB triplets.""" + return ((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF) + +def _encode_color_grb(c): + """Encode an RGB tuple into NeoPixel GRB 24 byte SPI bit stream.""" + return _byte_lookup[int(c[1] * brightness)] + _byte_lookup[int(c[0] * brightness)] + _byte_lookup[int(c[2] * brightness)] + +def _encode_pixel(c): + """Encode an RGB tuple into NeoPixel GRB 24 byte SPI bit stream.""" + rgb = color_to_rgb(c) + encoded = _encode_color_grb(rgb) + return encoded + +def prepare_data(dimension, b): + global brightness + global _byte_lookup + if b > brightness_max: + brightness = brightness_max + else: + brightness = b + _byte_lookup = _build_byte_lookup() + data = jarray.zeros(dimension * 24 + 1, "b") + return data + +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: + pixel_line = reversed(pixel_line) + for px in pixel_line: + py_bytes = _encode_pixel(px) + for b in py_bytes: + if b > 127: + # Convert to signed bytes (expected by jarray) + b -= 256 + data[offset] = b + else: + data[offset] = b + offset += 1 + pixel_nb +=1 + # New line + data[-1] = 10 + return data diff --git a/v2/backend/processing/Random/network.py b/v2/backend/processing/Random/network.py new file mode 100644 index 0000000..0199949 --- /dev/null +++ b/v2/backend/processing/Random/network.py @@ -0,0 +1,33 @@ +import jarray +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") + max_framerate = jarray.zeros(4, "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) + max_height = struct.unpack(" 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