Make the datastream generation way faster.

Add requirements file
master
Raphaël Vinot 2015-03-30 01:00:46 +02:00
parent 3731e08bb5
commit 28deb6bbf1
4 changed files with 58 additions and 30 deletions

View File

@ -0,0 +1,2 @@
redis
pyserial

View File

@ -8,11 +8,11 @@ from data_generator import prepare_data
# Config, will be checked upstream
height = 5
width = 8
framerate = 30
framerate = 40
#####################################
gamma = 1.7
brightness = 4
brightness = 0.2
dimension = 0
# TODO: test with real serial
@ -53,12 +53,12 @@ def setup():
size(width, height)
dimension = width * height
frameRate(framerate)
data, gammatable = prepare_data(dimension, gamma, brightness)
data = prepare_data(dimension, gamma, brightness)
loadPixels()
for i in range(dimension):
pixels[i] = color(0, 0, 0)
updatePixels()
send_TCP(ledTCP, data, long_line, gammatable)
send_TCP(ledTCP, data, long_line)
def draw():
global current_px
@ -72,4 +72,4 @@ def draw():
current_px = 0
else:
current_px += 1
send_TCP(ledTCP, data, long_line, gammatable)
send_TCP(ledTCP, data, long_line)

View File

@ -1,12 +1,53 @@
import jarray
import math
import itertools
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)]
_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, gamma, 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, "b")
return data, gammatable
return data
def image2data(data, long_line, gammatable):
def image2data(data, long_line):
offset = 0
pixel_nb = 0
for x in range(0, height):
@ -14,30 +55,14 @@ def image2data(data, long_line, gammatable):
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)
py_bytes = _encode_pixel(px)
for b in py_bytes:
if b > 127:
# Convert to signed bytes (expected by jarray)
b -= 2**8
b -= 256
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

@ -25,6 +25,7 @@ def send_config(socket, height, width, framerate):
socket.write(struct.pack('<i', width))
socket.write(struct.pack('<i', framerate))
def send_TCP(socket, data, long_line, gammatable):
image2data(data, long_line, gammatable)
def send_TCP(socket, data, long_line):
image2data(data, long_line)
# print data
socket.write(data)