From 91a8cc9190288eb171df344b20dee2518a2d83f8 Mon Sep 17 00:00:00 2001 From: starcalc Date: Fri, 2 Aug 2019 23:18:01 +0200 Subject: [PATCH] Added UDP; Pretty Printed --- esp-pixelbox.ino | 87 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/esp-pixelbox.ino b/esp-pixelbox.ino index b219993..c05e019 100644 --- a/esp-pixelbox.ino +++ b/esp-pixelbox.ino @@ -4,8 +4,11 @@ #include #include + #include #include "NeoPatterns.h" + +#include #ifdef __AVR__ #include #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) }; @@ -82,24 +89,24 @@ bool onSetVU(const HomieRange& range, const String& value) { { if (remaining.length() == 1) { - current = remaining.substring(0,1); + current = remaining.substring(0, 1); } else { - current = remaining.substring(0,2); + current = remaining.substring(0, 2); } int currentvalue = (int) strtol(¤t[0], NULL, 10); // 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 - 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++; remaining = remaining.substring(2); - } while (remaining.length()>0); // TODO: Not bigger than strip + } while (remaining.length() > 0); // TODO: Not bigger than strip strip.show(); homieNode.setProperty("VU_" + String(range.index)).send(value); } @@ -156,7 +163,7 @@ bool onSetEffect(const HomieRange& range, const String& value) { String firstfourbytes; if (value.length() > 3) { - firstfourbytes = value.substring(0,4); + firstfourbytes = value.substring(0, 4); } else { firstfourbytes = value; } @@ -169,7 +176,7 @@ bool onSetEffect(const HomieRange& range, const String& value) { // Deactivate NeoPattern strip.None(); String command = value.substring(0, sep); - String parameters = value.substring(sep+1); + String parameters = value.substring(sep + 1); int sep2 = parameters.indexOf("|"); if (sep2 > 0) { @@ -286,13 +293,17 @@ void loopHandler() { } -bool NextPrev(bool next=false) { +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) { @@ -340,7 +351,7 @@ bool NextPrev(bool next=false) { strip.None(); matrix.ScrollText("CTDO"); break; - } + } } @@ -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); @@ -382,7 +401,7 @@ void setup() { homieNode.advertise("icon").settable(onSetIcon); homieNode.advertise("command").settable(onSetCommand); 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.clear(); @@ -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)); + + } + }