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() {
// initialize ethernet device
Ethernet.begin(mac, ip);
// Start UDP
Udp.begin(localPort);
strip.begin(); strip.begin();
strip.show(); // Initialize all pixels to 'off' // Initialize all pixels to 'off'
Serial.begin(115200); for (int i=0; i<256; i++) {
} strip.setPixelColor(i, strip.Color(0, 0, 0));
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]));
}
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(); strip.show();
// delay(50); Serial.begin(115200);
Serial.println("Ready.");
} }
void rainbow(uint8_t wait) { void loop() {
uint16_t i, j; // if there is data available, read a packet
int packetSize = Udp.parsePacket();
for(j=0; j<256; j++) { if (packetSize) {
for(i=0; i<strip.numPixels(); i++) { // read the packet into packet buffer
strip.setPixelColor(i, Wheel((i+j) & 255)); 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(); 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);
} }
} }