Added UDP; Pretty Printed
This commit is contained in:
parent
964262b797
commit
91a8cc9190
|
@ -4,8 +4,11 @@
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_NeoPixel.h>
|
#include <Adafruit_NeoPixel.h>
|
||||||
|
|
||||||
|
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include "NeoPatterns.h"
|
#include "NeoPatterns.h"
|
||||||
|
|
||||||
|
#include <WiFiUdp.h>
|
||||||
#ifdef __AVR__
|
#ifdef __AVR__
|
||||||
#include <avr/power.h>
|
#include <avr/power.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,6 +23,10 @@ Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
|
||||||
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
|
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
|
||||||
NEO_GRB + NEO_KHZ800);
|
NEO_GRB + NEO_KHZ800);
|
||||||
|
|
||||||
|
WiFiUDP Udp;
|
||||||
|
unsigned int localUdpPort = 4210; // local port to listen on
|
||||||
|
char incomingPacket[255]; // buffer for incoming packets
|
||||||
|
|
||||||
const uint16_t colors[] = {
|
const uint16_t colors[] = {
|
||||||
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255)
|
matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255)
|
||||||
};
|
};
|
||||||
|
@ -289,10 +296,14 @@ void loopHandler() {
|
||||||
bool NextPrev(bool next = false) {
|
bool NextPrev(bool next = false) {
|
||||||
if (next) {
|
if (next) {
|
||||||
effect++;
|
effect++;
|
||||||
if (effect>10) { effect=0; }
|
if (effect > 10) {
|
||||||
|
effect = 0;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
effect--;
|
effect--;
|
||||||
if (effect<0) { effect=10; }
|
if (effect < 0) {
|
||||||
|
effect = 10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
switch (effect)
|
switch (effect)
|
||||||
{
|
{
|
||||||
|
@ -352,14 +363,22 @@ bool onSetCommand(const HomieRange& range, const String& value) {
|
||||||
NextPrev(false);
|
NextPrev(false);
|
||||||
} else if (value == "darker") {
|
} else if (value == "darker") {
|
||||||
curBrightness -= 40;
|
curBrightness -= 40;
|
||||||
if (curBrightness < 0) { curBrightness = 0; }
|
if (curBrightness < 0) {
|
||||||
|
curBrightness = 0;
|
||||||
|
}
|
||||||
setBrightness(curBrightness);
|
setBrightness(curBrightness);
|
||||||
} else if (value == "brighter") {
|
} else if (value == "brighter") {
|
||||||
curBrightness += 40;
|
curBrightness += 40;
|
||||||
if (curBrightness > 255) { curBrightness = 255; }
|
if (curBrightness > 255) {
|
||||||
|
curBrightness = 255;
|
||||||
|
}
|
||||||
setBrightness(curBrightness);
|
setBrightness(curBrightness);
|
||||||
} else if (value == "toggle") {
|
} else if (value == "toggle") {
|
||||||
if (curBrightness>0) { curBrightness = 0; } else { curBrightness = 255; }
|
if (curBrightness > 0) {
|
||||||
|
curBrightness = 0;
|
||||||
|
} else {
|
||||||
|
curBrightness = 255;
|
||||||
|
}
|
||||||
setBrightness(curBrightness);
|
setBrightness(curBrightness);
|
||||||
}
|
}
|
||||||
homieNode.setProperty("command").send(value);
|
homieNode.setProperty("command").send(value);
|
||||||
|
@ -413,10 +432,42 @@ void setup() {
|
||||||
matrix.setBrightness(255);
|
matrix.setBrightness(255);
|
||||||
|
|
||||||
strip.Plasma(); // Default effect
|
strip.Plasma(); // Default effect
|
||||||
|
Udp.begin(localUdpPort);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
Homie.loop();
|
Homie.loop();
|
||||||
ArduinoOTA.handle();
|
ArduinoOTA.handle();
|
||||||
|
//UDP
|
||||||
|
int packetSize = Udp.parsePacket();
|
||||||
|
if (packetSize)
|
||||||
|
{
|
||||||
|
// receive incoming UDP packets
|
||||||
|
// Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
|
||||||
|
int len = Udp.read(incomingPacket, 255);
|
||||||
|
if (len > 0)
|
||||||
|
{
|
||||||
|
// Serial.printf("Size: %d", len);
|
||||||
|
if (len == 192)
|
||||||
|
{
|
||||||
|
// Serial.printf("UDP packet contents: %s\n", incomingPacket);
|
||||||
|
int i = 0;
|
||||||
|
// Kein Effekt
|
||||||
|
strip.Stop();
|
||||||
|
for (int i=0; i<193; i=i+3)
|
||||||
|
{
|
||||||
|
// Serial.printf("Pixel %d to R %d - G %d - B %d\n", i/3, incomingPacket[i], incomingPacket[i+1], incomingPacket[i+2]);
|
||||||
|
strip.setPixelColor(strip.numToPos(i/3), incomingPacket[i], incomingPacket[i+1], incomingPacket[i+2]);
|
||||||
|
}
|
||||||
|
strip.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//uint16_t data=incomingPacket[0]<<8 | incomingPacket[1];
|
||||||
|
|
||||||
|
//printBinary(mapData(data));
|
||||||
|
//shiftRelais(mapData(data));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue