From db3ffa126e5462ca63644cf919b6332151d62140 Mon Sep 17 00:00:00 2001 From: Uwe Lippmann Date: Thu, 22 Jan 2015 22:04:40 +0100 Subject: [PATCH] Implemented streaming of images over UDP with Ethernet Shield --- arduino/rgb_led_matrix.ino | 134 ++++++++++++++----------------------- 1 file changed, 50 insertions(+), 84 deletions(-) diff --git a/arduino/rgb_led_matrix.ino b/arduino/rgb_led_matrix.ino index ea2808a..47de476 100644 --- a/arduino/rgb_led_matrix.ino +++ b/arduino/rgb_led_matrix.ino @@ -1,11 +1,15 @@ +#include +#include +#include #include "Adafruit_NeoPixel.h" #define PIN 6 -enum { - WIDTH=16, - HEIGHT=16, - N=WIDTH*HEIGHT*3 -}; +#define WIDTH 16 +#define HEIGHT 16 + +#define UDP_PACKET_MAX_SIZE 768 +#define PIXEL_OFFSET 0x80 + // Parameter 1 = number of pixels in strip // Parameter 2 = pin number (most are valid) // Parameter 3 = pixel type flags, add together as needed: @@ -15,92 +19,54 @@ enum { // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip) Adafruit_NeoPixel strip = Adafruit_NeoPixel(WIDTH * HEIGHT, PIN, NEO_GRB + NEO_KHZ800); +// Network configuration +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; +IPAddress ip(172, 22, 239, 182); +unsigned int localPort = 8888; + +// buffers for receiving and sending data +char packetBuffer[UDP_PACKET_MAX_SIZE]; // buffer to hold incoming packet + +// Create EthernetUdp instance to send and receive packets over UDP +EthernetUDP Udp; + void setup() { - strip.begin(); - strip.show(); // Initialize all pixels to 'off' + // initialize ethernet device + Ethernet.begin(mac, ip); + // Start UDP + Udp.begin(localPort); + strip.begin(); + // Initialize all pixels to 'off' + for (int i=0; i<256; i++) { + strip.setPixelColor(i, strip.Color(0, 0, 0)); + } + strip.show(); Serial.begin(115200); + Serial.println("Ready."); } -int j = 0; -int counter = 0; -char buf[3*WIDTH*HEIGHT]; - void loop() { - int incomingByte = 0; - while((Serial.available() > 0) && (incomingByte != -1)) { - incomingByte = Serial.read(); - if (incomingByte != -1) { - if(incomingByte == 254) - counter = 0; - else{ - buf[counter] = incomingByte; - counter++; - if (counter >= N) - counter = 0; - } - } - } - if (counter >= N-1) { - for (int i = 0; i < WIDTH*HEIGHT; i++) { - strip.setPixelColor(i, strip.Color(buf[3*i], buf[3*i+1], buf[3*i+2])); + // if there is data available, read a packet + int packetSize = Udp.parsePacket(); + + if (packetSize) { + // read the packet into packet buffer + Udp.read(packetBuffer, UDP_PACKET_MAX_SIZE); + Serial.println("Received packet:"); + Serial.print("Length: "); + Serial.println(packetSize); + + // assume an incoming uncompressed BMP image + // assume we know the pixels start at offset 54 + for (int i = 0; i < packetSize; i = i + 3) { + Serial.print(packetBuffer[i], HEX); + Serial.print(packetBuffer[i+1], HEX); + Serial.print(packetBuffer[i+2], HEX); + Serial.print(" "); + strip.setPixelColor(i / 3, packetBuffer[i], packetBuffer[i+1], packetBuffer[i+2]); } + Serial.print("\n"); strip.show(); } } -// Fill the dots one after the other with a color -void colorWipe(uint32_t c, uint8_t wait) { - for(uint16_t i=0; i