Implemented streaming of images over UDP with Ethernet Shield

This commit is contained in:
Uwe Lippmann 2015-01-22 22:04:40 +01:00
parent 0845f3705a
commit db3ffa126e

View file

@ -1,11 +1,15 @@
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "Adafruit_NeoPixel.h" #include "Adafruit_NeoPixel.h"
#define PIN 6 #define PIN 6
enum { #define WIDTH 16
WIDTH=16, #define HEIGHT 16
HEIGHT=16,
N=WIDTH*HEIGHT*3 #define UDP_PACKET_MAX_SIZE 768
}; #define PIXEL_OFFSET 0x80
// Parameter 1 = number of pixels in strip // Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid) // Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed: // 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) // NEO_KHZ800 800 KHz bitstream (e.g. High Density LED strip)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(WIDTH * HEIGHT, PIN, NEO_GRB + NEO_KHZ800); 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() { void setup() {
strip.begin(); // initialize ethernet device
strip.show(); // Initialize all pixels to 'off' 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.begin(115200);
Serial.println("Ready.");
} }
int j = 0;
int counter = 0;
char buf[3*WIDTH*HEIGHT];
void loop() { void loop() {
int incomingByte = 0; // if there is data available, read a packet
while((Serial.available() > 0) && (incomingByte != -1)) { int packetSize = Udp.parsePacket();
incomingByte = Serial.read();
if (incomingByte != -1) { if (packetSize) {
if(incomingByte == 254) // read the packet into packet buffer
counter = 0; Udp.read(packetBuffer, UDP_PACKET_MAX_SIZE);
else{ Serial.println("Received packet:");
buf[counter] = incomingByte; Serial.print("Length: ");
counter++; Serial.println(packetSize);
if (counter >= N)
counter = 0; // 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);
if (counter >= N-1) { Serial.print(packetBuffer[i+1], HEX);
for (int i = 0; i < WIDTH*HEIGHT; i++) { Serial.print(packetBuffer[i+2], HEX);
strip.setPixelColor(i, strip.Color(buf[3*i], buf[3*i+1], buf[3*i+2])); Serial.print(" ");
strip.setPixelColor(i / 3, packetBuffer[i], packetBuffer[i+1], packetBuffer[i+2]);
} }
Serial.print("\n");
strip.show(); 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<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
void colorFill(uint32_t c) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
}
strip.show();
// delay(50);
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
strip.show();
delay(wait);
}
}
// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
if(WheelPos < 85) {
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}