Implemented streaming of images over UDP with Ethernet Shield
This commit is contained in:
parent
0845f3705a
commit
db3ffa126e
1 changed files with 50 additions and 84 deletions
|
@ -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);
|
||||||
|
|
||||||
void setup() {
|
// Network configuration
|
||||||
strip.begin();
|
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
|
||||||
strip.show(); // Initialize all pixels to 'off'
|
IPAddress ip(172, 22, 239, 182);
|
||||||
Serial.begin(115200);
|
unsigned int localPort = 8888;
|
||||||
}
|
|
||||||
|
|
||||||
int j = 0;
|
// buffers for receiving and sending data
|
||||||
int counter = 0;
|
char packetBuffer[UDP_PACKET_MAX_SIZE]; // buffer to hold incoming packet
|
||||||
char buf[3*WIDTH*HEIGHT];
|
|
||||||
|
// Create EthernetUdp instance to send and receive packets over UDP
|
||||||
|
EthernetUDP Udp;
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// 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.");
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
Reference in a new issue