Added UDP; Pretty Printed

This commit is contained in:
starcalc 2019-08-02 23:18:01 +02:00
parent 964262b797
commit 91a8cc9190
1 changed files with 69 additions and 18 deletions

View File

@ -4,8 +4,11 @@
#include <Adafruit_GFX.h>
#include <Adafruit_NeoPixel.h>
#include <ArduinoOTA.h>
#include "NeoPatterns.h"
#include <WiFiUdp.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
@ -20,6 +23,10 @@ Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
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[] = {
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) {
if (next) {
effect++;
if (effect>10) { effect=0; }
if (effect > 10) {
effect = 0;
}
} else {
effect--;
if (effect<0) { effect=10; }
if (effect < 0) {
effect = 10;
}
}
switch (effect)
{
@ -352,14 +363,22 @@ bool onSetCommand(const HomieRange& range, const String& value) {
NextPrev(false);
} else if (value == "darker") {
curBrightness -= 40;
if (curBrightness < 0) { curBrightness = 0; }
if (curBrightness < 0) {
curBrightness = 0;
}
setBrightness(curBrightness);
} else if (value == "brighter") {
curBrightness += 40;
if (curBrightness > 255) { curBrightness = 255; }
if (curBrightness > 255) {
curBrightness = 255;
}
setBrightness(curBrightness);
} else if (value == "toggle") {
if (curBrightness>0) { curBrightness = 0; } else { curBrightness = 255; }
if (curBrightness > 0) {
curBrightness = 0;
} else {
curBrightness = 255;
}
setBrightness(curBrightness);
}
homieNode.setProperty("command").send(value);
@ -413,10 +432,42 @@ void setup() {
matrix.setBrightness(255);
strip.Plasma(); // Default effect
Udp.begin(localUdpPort);
}
void loop() {
Homie.loop();
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));
}
}