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_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)
}; };
@ -82,24 +89,24 @@ bool onSetVU(const HomieRange& range, const String& value) {
{ {
if (remaining.length() == 1) if (remaining.length() == 1)
{ {
current = remaining.substring(0,1); current = remaining.substring(0, 1);
} else { } else {
current = remaining.substring(0,2); current = remaining.substring(0, 2);
} }
int currentvalue = (int) strtol(&current[0], NULL, 10); int currentvalue = (int) strtol(&current[0], NULL, 10);
// White peak // White peak
strip.setPixelColor(strip.numToPos(56+i-currentvalue*8), 16777215); // White dot strip.setPixelColor(strip.numToPos(56 + i - currentvalue * 8), 16777215); // White dot
// Shaded bar "below" the peak // Shaded bar "below" the peak
if (currentvalue>0) if (currentvalue > 0)
{ {
for (int j=currentvalue-1;j>-1;j--) for (int j = currentvalue - 1; j > -1; j--)
{ {
strip.setPixelColor(strip.numToPos(56+i-j*8), 4276545); // Greyscale strip.setPixelColor(strip.numToPos(56 + i - j * 8), 4276545); // Greyscale
} }
} }
i++; i++;
remaining = remaining.substring(2); remaining = remaining.substring(2);
} while (remaining.length()>0); // TODO: Not bigger than strip } while (remaining.length() > 0); // TODO: Not bigger than strip
strip.show(); strip.show();
homieNode.setProperty("VU_" + String(range.index)).send(value); homieNode.setProperty("VU_" + String(range.index)).send(value);
} }
@ -156,7 +163,7 @@ bool onSetEffect(const HomieRange& range, const String& value) {
String firstfourbytes; String firstfourbytes;
if (value.length() > 3) if (value.length() > 3)
{ {
firstfourbytes = value.substring(0,4); firstfourbytes = value.substring(0, 4);
} else { } else {
firstfourbytes = value; firstfourbytes = value;
} }
@ -169,7 +176,7 @@ bool onSetEffect(const HomieRange& range, const String& value) {
// Deactivate NeoPattern // Deactivate NeoPattern
strip.None(); strip.None();
String command = value.substring(0, sep); String command = value.substring(0, sep);
String parameters = value.substring(sep+1); String parameters = value.substring(sep + 1);
int sep2 = parameters.indexOf("|"); int sep2 = parameters.indexOf("|");
if (sep2 > 0) { if (sep2 > 0) {
@ -286,13 +293,17 @@ 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)
{ {
@ -340,7 +351,7 @@ bool NextPrev(bool next=false) {
strip.None(); strip.None();
matrix.ScrollText("CTDO"); matrix.ScrollText("CTDO");
break; break;
} }
} }
@ -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);
@ -382,7 +401,7 @@ void setup() {
homieNode.advertise("icon").settable(onSetIcon); homieNode.advertise("icon").settable(onSetIcon);
homieNode.advertise("command").settable(onSetCommand); homieNode.advertise("command").settable(onSetCommand);
homieNode.advertiseRange("pixels", 0, (NUMPIXELS - 1) * 7).settable(onSetPixels); homieNode.advertiseRange("pixels", 0, (NUMPIXELS - 1) * 7).settable(onSetPixels);
homieNode.advertiseRange("vu", 0, sqrt(NUMPIXELS)-1).settable(onSetVU); homieNode.advertiseRange("vu", 0, sqrt(NUMPIXELS) - 1).settable(onSetVU);
strip.begin(); strip.begin();
strip.clear(); strip.clear();
@ -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));
}
} }