diff --git a/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.cpp b/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.cpp new file mode 100644 index 0000000..8249ae3 --- /dev/null +++ b/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.cpp @@ -0,0 +1,297 @@ +/* OctoWS2811 - High Performance WS2811 LED Display Library + http://www.pjrc.com/teensy/td_libs_OctoWS2811.html + Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#include +#include "OctoWS2811.h" + + +uint16_t OctoWS2811::stripLen; +void * OctoWS2811::frameBuffer; +void * OctoWS2811::drawBuffer; +uint8_t OctoWS2811::params; +DMAChannel OctoWS2811::dma1; +DMAChannel OctoWS2811::dma2; +DMAChannel OctoWS2811::dma3; + +static const uint8_t ones = 0xFF; +static volatile uint8_t update_in_progress = 0; +static uint32_t update_completed_at = 0; + +OctoWS2811::OctoWS2811() +{ + // Unusable +} + +void OctoWS2811::attach(uint32_t numPerStrip, void *frameBuf, void *drawBuf, uint8_t config) +{ + stripLen = numPerStrip; + frameBuffer = frameBuf; + drawBuffer = drawBuf; + params = config; +} + +// Waveform timing: these set the high time for a 0 and 1 bit, as a fraction of +// the total 800 kHz or 400 kHz clock cycle. The scale is 0 to 255. The Worldsemi +// datasheet seems T1H should be 600 ns of a 1250 ns cycle, or 48%. That may +// erroneous information? Other sources reason the chip actually samples the +// line close to the center of each bit time, so T1H should be 80% if TOH is 20%. +// The chips appear to work based on a simple one-shot delay triggered by the +// rising edge. At least 1 chip tested retransmits 0 as a 330 ns pulse (26%) and +// a 1 as a 660 ns pulse (53%). Perhaps it's actually sampling near 500 ns? +// There doesn't seem to be any advantage to making T1H less, as long as there +// is sufficient low time before the end of the cycle, so the next rising edge +// can be detected. T0H has been lengthened slightly, because the pulse can +// narrow if the DMA controller has extra latency during bus arbitration. If you +// have an insight about tuning these parameters AND you have actually tested on +// real LED strips, please contact paul@pjrc.com. Please do not email based only +// on reading the datasheets and purely theoretical analysis. +#define WS2811_TIMING_T0H 60 +#define WS2811_TIMING_T1H 176 + +// Discussion about timing and flicker & color shift issues: +// http://forum.pjrc.com/threads/23877-WS2812B-compatible-with-OctoWS2811-library?p=38190&viewfull=1#post38190 + + +void OctoWS2811::begin(void) +{ + uint32_t bufsize, frequency; + + bufsize = stripLen*24; + + // set up the buffers + memset(frameBuffer, 0, bufsize); + if (drawBuffer) { + memset(drawBuffer, 0, bufsize); + } else { + drawBuffer = frameBuffer; + } + + // configure the 8 output pins + GPIOD_PCOR = 0xFF; + pinMode(2, OUTPUT); // strip #1 + pinMode(14, OUTPUT); // strip #2 + pinMode(7, OUTPUT); // strip #3 + pinMode(8, OUTPUT); // strip #4 + pinMode(6, OUTPUT); // strip #5 + pinMode(20, OUTPUT); // strip #6 + pinMode(21, OUTPUT); // strip #7 + pinMode(5, OUTPUT); // strip #8 + + // create the two waveforms for WS2811 low and high bits + frequency = (params & WS2811_400kHz) ? 400000 : 800000; + analogWriteResolution(8); + analogWriteFrequency(3, frequency); + analogWriteFrequency(4, frequency); + analogWrite(3, WS2811_TIMING_T0H); + analogWrite(4, WS2811_TIMING_T1H); + + // pin 16 triggers DMA(port B) on rising edge (configure for pin 3's waveform) + CORE_PIN16_CONFIG = PORT_PCR_IRQC(1)|PORT_PCR_MUX(3); + pinMode(3, INPUT_PULLUP); // pin 3 no longer needed + + // pin 15 triggers DMA(port C) on falling edge of low duty waveform + // pin 15 and 16 must be connected by the user: 16 is output, 15 is input + pinMode(15, INPUT); + CORE_PIN15_CONFIG = PORT_PCR_IRQC(2)|PORT_PCR_MUX(1); + + // pin 4 triggers DMA(port A) on falling edge of high duty waveform + CORE_PIN4_CONFIG = PORT_PCR_IRQC(2)|PORT_PCR_MUX(3); + + // DMA channel #1 sets WS2811 high at the beginning of each cycle + dma1.TCD->SADDR = &ones; + dma1.TCD->SOFF = 0; + dma1.TCD->ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0); + dma1.TCD->NBYTES_MLNO = 1; + dma1.TCD->SLAST = 0; + dma1.TCD->DADDR = &GPIOD_PSOR; + dma1.TCD->DOFF = 0; + dma1.TCD->CITER_ELINKNO = bufsize; + dma1.TCD->DLASTSGA = 0; + dma1.TCD->CSR = DMA_TCD_CSR_DREQ; + dma1.TCD->BITER_ELINKNO = bufsize; + + // DMA channel #2 writes the pixel data at 20% of the cycle + dma2.TCD->SADDR = frameBuffer; + dma2.TCD->SOFF = 1; + dma2.TCD->ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0); + dma2.TCD->NBYTES_MLNO = 1; + dma2.TCD->SLAST = -bufsize; + dma2.TCD->DADDR = &GPIOD_PDOR; + dma2.TCD->DOFF = 0; + dma2.TCD->CITER_ELINKNO = bufsize; + dma2.TCD->DLASTSGA = 0; + dma2.TCD->CSR = DMA_TCD_CSR_DREQ; + dma2.TCD->BITER_ELINKNO = bufsize; + + // DMA channel #3 clear all the pins low at 48% of the cycle + dma3.TCD->SADDR = &ones; + dma3.TCD->SOFF = 0; + dma3.TCD->ATTR = DMA_TCD_ATTR_SSIZE(0) | DMA_TCD_ATTR_DSIZE(0); + dma3.TCD->NBYTES_MLNO = 1; + dma3.TCD->SLAST = 0; + dma3.TCD->DADDR = &GPIOD_PCOR; + dma3.TCD->DOFF = 0; + dma3.TCD->CITER_ELINKNO = bufsize; + dma3.TCD->DLASTSGA = 0; + dma3.TCD->CSR = DMA_TCD_CSR_DREQ | DMA_TCD_CSR_INTMAJOR; + dma3.TCD->BITER_ELINKNO = bufsize; + +#ifdef __MK20DX256__ + MCM_CR = MCM_CR_SRAMLAP(1) | MCM_CR_SRAMUAP(0); + AXBS_PRS0 = 0x1032; +#endif + + // route the edge detect interrupts to trigger the 3 channels + dma1.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTB); + dma2.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTC); + dma3.triggerAtHardwareEvent(DMAMUX_SOURCE_PORTA); + + // enable a done interrupts when channel #3 completes + dma3.attachInterrupt(isr); + //pinMode(1, OUTPUT); // testing: oscilloscope trigger +} + +void OctoWS2811::isr(void) +{ + dma3.clearInterrupt(); + update_completed_at = micros(); + update_in_progress = 0; +} + +int OctoWS2811::busy(void) +{ + //if (DMA_ERQ & 0xE) return 1; + if (update_in_progress) return 1; + // busy for 50 us after the done interrupt, for WS2811 reset + if (micros() - update_completed_at < 50) return 1; + return 0; +} + +void OctoWS2811::show(void) +{ + uint32_t cv, sc; + + // wait for any prior DMA operation + while (update_in_progress) ; + // it's ok to copy the drawing buffer to the frame buffer + // during the 50us WS2811 reset time + if (drawBuffer != frameBuffer) { + // TODO: this could be faster with DMA, especially if the + // buffers are 32 bit aligned... but does it matter? + memcpy(frameBuffer, drawBuffer, stripLen * 24); + } + // wait for WS2811 reset + while (micros() - update_completed_at < 50) ; + + // ok to start, but we must be very careful to begin + // without any prior 3 x 800kHz DMA requests pending + sc = FTM1_SC; + cv = FTM1_C1V; + noInterrupts(); + // CAUTION: this code is timing critical. Any editing should be + // tested by verifying the oscilloscope trigger pulse at the end + // always occurs while both waveforms are still low. Simply + // counting CPU cycles does not take into account other complex + // factors, like flash cache misses and bus arbitration from USB + // or other DMA. Testing should be done with the oscilloscope + // display set at infinite persistence and a variety of other I/O + // performed to create realistic bus usage. Even then, you really + // should not mess with this timing critical code! + update_in_progress = 1; + while (FTM1_CNT <= cv) ; + while (FTM1_CNT > cv) ; // wait for beginning of an 800 kHz cycle + while (FTM1_CNT < cv) ; + FTM1_SC = sc & 0xE7; // stop FTM1 timer (hopefully before it rolls over) + //digitalWriteFast(1, HIGH); // oscilloscope trigger + PORTB_ISFR = (1<<0); // clear any prior rising edge + PORTC_ISFR = (1<<0); // clear any prior low duty falling edge + PORTA_ISFR = (1<<13); // clear any prior high duty falling edge + dma1.enable(); + dma2.enable(); // enable all 3 DMA channels + dma3.enable(); + FTM1_SC = sc; // restart FTM1 timer + //digitalWriteFast(1, LOW); + interrupts(); +} + +void OctoWS2811::setPixel(uint32_t num, int color) +{ + uint32_t strip, offset, mask; + uint8_t bit, *p; + + switch (params & 7) { + case WS2811_RBG: + color = (color&0xFF0000) | ((color<<8)&0x00FF00) | ((color>>8)&0x0000FF); + break; + case WS2811_GRB: + color = ((color<<8)&0xFF0000) | ((color>>8)&0x00FF00) | (color&0x0000FF); + break; + case WS2811_GBR: + color = ((color<<8)&0xFFFF00) | ((color>>16)&0x0000FF); + break; + default: + break; + } + strip = num / stripLen; // Cortex-M4 has 2 cycle unsigned divide :-) + offset = num % stripLen; + bit = (1<>= 1) { + if (color & mask) { + *p++ |= bit; + } else { + *p++ &= ~bit; + } + } +} + +int OctoWS2811::getPixel(uint32_t num) +{ + uint32_t strip, offset, mask; + uint8_t bit, *p; + int color=0; + + strip = num / stripLen; + offset = num % stripLen; + bit = (1<>= 1) { + if (*p++ & bit) color |= mask; + } + switch (params & 7) { + case WS2811_RBG: + color = (color&0xFF0000) | ((color<<8)&0x00FF00) | ((color>>8)&0x0000FF); + break; + case WS2811_GRB: + color = ((color<<8)&0xFF0000) | ((color>>8)&0x00FF00) | (color&0x0000FF); + break; + case WS2811_GBR: + color = ((color<<8)&0xFFFF00) | ((color>>16)&0x0000FF); + break; + default: + break; + } + return color; +} + + diff --git a/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.h b/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.h new file mode 100644 index 0000000..f1c9acc --- /dev/null +++ b/v2/backend/arduino/VideoDisplayTeensy31/OctoWS2811.h @@ -0,0 +1,76 @@ +/* OctoWS2811 - High Performance WS2811 LED Display Library + http://www.pjrc.com/teensy/td_libs_OctoWS2811.html + Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + + +#include +#include "DMAChannel.h" + +#if TEENSYDUINO < 120 +#error "Teensyduino version 1.20 or later is required to compile this library." +#endif +#ifdef __AVR__ +#error "The Audio Library only works with Teensy 3.X. Teensy 2.0 is unsupported." +#endif + +#define WS2811_RGB 0 // The WS2811 datasheet documents this way +#define WS2811_RBG 1 +#define WS2811_GRB 2 // Most LED strips are wired this way +#define WS2811_GBR 3 + +#define WS2811_800kHz 0x00 // Nearly all WS2811 are 800 kHz +#define WS2811_400kHz 0x10 // Adafruit's Flora Pixels + + +class OctoWS2811 { +public: + OctoWS2811(); + void attach(uint32_t numPerStrip, void *frameBuf, void *drawBuf, uint8_t config = WS2811_GRB); + + void begin(void); + + void setPixel(uint32_t num, int color); + void setPixel(uint32_t num, uint8_t red, uint8_t green, uint8_t blue) { + setPixel(num, color(red, green, blue)); + } + int getPixel(uint32_t num); + + void show(void); + int busy(void); + + int numPixels(void) { + return stripLen * 8; + } + int color(uint8_t red, uint8_t green, uint8_t blue) { + return (red << 16) | (green << 8) | blue; + } + + +private: + static uint16_t stripLen; + static void *frameBuffer; + static void *drawBuffer; + static uint8_t params; + static DMAChannel dma1, dma2, dma3; + static void isr(void); +}; + diff --git a/v2/backend/arduino/VideoDisplayTeensy31/VideoDisplayTeensy31.ino b/v2/backend/arduino/VideoDisplayTeensy31/VideoDisplayTeensy31.ino new file mode 100644 index 0000000..27bd681 --- /dev/null +++ b/v2/backend/arduino/VideoDisplayTeensy31/VideoDisplayTeensy31.ino @@ -0,0 +1,126 @@ +/* OctoWS2811 VideoDisplay.ino - Video on LEDs, from a PC, Mac, Raspberry Pi + http://www.pjrc.com/teensy/td_libs_OctoWS2811.html + Copyright (c) 2013 Paul Stoffregen, PJRC.COM, LLC + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + + + Required Connections + -------------------- + pin 2: LED Strip #1 OctoWS2811 drives 8 LED Strips. + pin 14: LED strip #2 All 8 are the same length. + pin 7: LED strip #3 + pin 8: LED strip #4 A 100 to 220 ohm resistor should used + pin 6: LED strip #5 between each Teensy pin and the + pin 20: LED strip #6 wire to the LED strip, to minimize + pin 21: LED strip #7 high frequency ringining & noise. + pin 5: LED strip #8 + pin 15 & 16 - Connect together, but do not use + pin 4: Do not use + pin 3: Do not use as PWM. Normal use is ok. + pin 12: Frame Sync + + When using more than 1 Teensy to display a video image, connect + the Frame Sync signal between every board. All boards will + synchronize their WS2811 update using this signal. + + Beware of image distortion from long LED strip lengths. During + the WS2811 update, the LEDs update in sequence, not all at the + same instant! The first pixel updates after 30 microseconds, + the second pixel after 60 us, and so on. A strip of 120 LEDs + updates in 3.6 ms, which is 10.8% of a 30 Hz video frame time. + Doubling the strip length to 240 LEDs increases the lag to 21.6% + of a video frame. For best results, use shorter length strips. + Multiple boards linked by the frame sync signal provides superior + video timing accuracy. + + A Multi-TT USB hub should be used if 2 or more Teensy boards + are connected. The Multi-TT feature allows proper USB bandwidth + allocation. Single-TT hubs, or direct connection to multiple + ports on the same motherboard, may give poor performance. +*/ + +// https://stackoverflow.com/questions/18806141/move-object-creation-to-setup-function-of-arduino + +#include "OctoWS2811.h" + +int height; +int width; +int ledsPerStrip; + +int count = 0; +DMAMEM int* displayMemory = 0; +int* drawingMemory = 0; +elapsedMicros elapsedUsecSinceLastFrameSync = 0; + + +const int config = WS2811_800kHz; // color config is on the PC side + +OctoWS2811 leds; + +void setup() { + pinMode(13, OUTPUT); + digitalWrite(13, HIGH); + Serial.setTimeout(50000); + // delay(1000); + Serial.readBytes((char *)&height, 4); + Serial.write(height); + Serial.readBytes((char *)&width, 4); + Serial.write(width); + digitalWrite(13, LOW); + pinMode(12, INPUT_PULLUP); // Frame Sync + Serial.setTimeout(50); + ledsPerStrip = width * height / 8; + displayMemory = new int[ledsPerStrip*6]; + drawingMemory = new int[ledsPerStrip*6]; + leds.attach(ledsPerStrip, displayMemory, drawingMemory, config); + leds.begin(); + leds.show(); +} + +void loop() { + int startChar = Serial.read(); + + if (startChar == 42) { + unsigned int startAt = micros(); + unsigned int usecUntilFrameSync = 0; + count = Serial.readBytes((char *)drawingMemory, sizeof(drawingMemory)); + if (count >= sizeof(drawingMemory)) { + unsigned int endAt = micros(); + unsigned int usToWaitBeforeSyncOutput = 100; + if (endAt - startAt < usecUntilFrameSync) { + usToWaitBeforeSyncOutput = usecUntilFrameSync - (endAt - startAt); + } + digitalWrite(12, HIGH); + pinMode(12, OUTPUT); + delayMicroseconds(usToWaitBeforeSyncOutput); + digitalWrite(12, LOW); + // WS2811 update begins immediately after falling edge of frame sync + digitalWrite(13, HIGH); + leds.show(); + digitalWrite(13, LOW); + } + } else if (startChar >= 0) { + //digitalWrite(13, HIGH); + //delay(100); + //digitalWrite(13, LOW); + // discard unknown characters + } +} + diff --git a/v2/backend/forwarding/forward.py b/v2/backend/forwarding/forward.py new file mode 100644 index 0000000..06c1294 --- /dev/null +++ b/v2/backend/forwarding/forward.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python + +import redis +import time +from serial import Serial, SerialException +import sys + +height = 5 +width = 8 + + +def send(r, s): + if not r.exists('new'): + return None + + data = r.rpop('new') + if data is not None and len(data) > 0: + s.write(data.encode()) + # size = s.write(data) + # print('Data sent ({} bytes)'.format(size)) + + +def serialConfigure(port_name, baudrate=9600): + ''' + We use a very low baudrate by default because the USB port on the teensy + enforce this value: http://www.pjrc.com/teensy/td_serial.html + ''' + ser = Serial() + ser.port = port_name + ser.baudrate = baudrate + ser.timeout = 5 + try: + ser.open() + except SerialException as e: + sys.stderr.write("Could not open serial port %s: %s\n" % (ser.portstr, e)) + return + + ser.write(height.to_bytes(4, byteorder='little')) + print(int.from_bytes(ser.read(4), byteorder='little')) + + ser.write(width.to_bytes(4, byteorder='little')) + print(int.from_bytes(ser.read(4), byteorder='little')) + + ser.timeout = 1 + return ser + +if __name__ == "__main__": + r = redis.Redis() + r.hset('config', 'imgsize', height * width * 24 + 1) + s = serialConfigure('/dev/ttyACM0') + while True: + while r.llen('new') > 0: + send(r, s) + time.sleep(10) diff --git a/v2/receiver.py b/v2/backend/forwarding/receiver.py similarity index 59% rename from v2/receiver.py rename to v2/backend/forwarding/receiver.py index 1fc3e9e..6b1dda8 100644 --- a/v2/receiver.py +++ b/v2/backend/forwarding/receiver.py @@ -1,4 +1,4 @@ -#!/usr/bon/env python +#!/usr/bin/env python import socketserver import redis @@ -13,19 +13,20 @@ class MyTCPHandler(socketserver.BaseRequestHandler): client. """ + def get_config(self): + self.imgsize = int(self.r.hget('config', 'imgsize')) + def handle(self): - r = redis.Redis() - data = None + print('Start receiving from {}...'.format(self.client_address[0])) + self.r = redis.Redis() + self.get_config() while True: - temp = self.request.recv(1024).strip() - if data is None: - data = temp - else: - data += temp - if len(temp) == 0: + data = self.request.recv(self.imgsize) + print(len(data)) + self.r.lpush('new', data) + if len(data) == 0: break - print("{} sent a packet".format(self.client_address[0])) - r.sadd('new', data) + print('... Done with {}.'.format(self.client_address[0])) if __name__ == "__main__": diff --git a/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde new file mode 100644 index 0000000..a75ef04 --- /dev/null +++ b/v2/backend/processing/PixelControl_TCP/PixelControl_TCP.pyde @@ -0,0 +1,98 @@ +add_library('net') +import math +import jarray + +gamma = 1.7 +brightness = 4 +errorCount = 0 +framerate = 30 +dimension = 0 + +# TODO: test with real serial +# https://www.pjrc.com/teensy/td_uart.html + +long_line = False + +ledTCP = None +data = None + +current_px = 0 + +def TCPConfigure(server, port): + global ledTCP + ledTCP = Client(this, server, port) + +def image2data(data): + offset = 1 + 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) + println(data) + ledTCP.write(data) + +def prepare_data(): + global data + data = jarray.zeros(dimension * 24 + 1 , "b") + data[0] = ord('*') + +def setup(): + global gammatable + global dimension + size(5, 8) + dimension = width * height + frameRate(framerate) + TCPConfigure("127.0.0.1", 9999) + if (errorCount > 0): + exit() + gammatable = [int((math.pow(i / 255.0, gamma) * 255.0 + 0.5) * brightness) for i in range(0, 256)] + prepare_data() + loadPixels() + for i in range(dimension): + pixels[i] = color(0, 0, 0) + updatePixels() + send_TCP() + +def draw(): + global current_px + pixels[current_px] = color(0, 255, 0) + if current_px == 0: + pixels[len(pixels) - 1] = color(0, 0, 0) + else: + pixels[current_px - 1] = color(0, 0, 0) + updatePixels() + if current_px == len(pixels) - 1: + current_px = 0 + else: + current_px += 1 + send_TCP()