395 lines
10 KiB
C++
395 lines
10 KiB
C++
#include <Homie.h>
|
|
#include <ArduinoOTA.h>
|
|
#include <Arduino.h>
|
|
#include <SoftwareSerial.h>
|
|
#include "NeoPatterns.h"
|
|
|
|
#define PIN D7
|
|
// #define BIGPIXELS 20
|
|
#define NUMPIXELS 70 // 3 LEDs form one pixel
|
|
#define NUMBOXS 25
|
|
// #define NUMPIXELS (BIGPIXELS * 3) // 3 LEDs form one pixel
|
|
// #define NUMPIXELS 60
|
|
|
|
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_BRG + NEO_KHZ400, &StripComplete);
|
|
HomieNode homieNode("pixel", "commands");
|
|
|
|
// Timeout, wann die Verbindung als "zurückgesetzt" angesehen werden soll und wieder auf das erste Byte gewartet wird
|
|
#define TIMEOUT 500
|
|
|
|
#define FW_NAME "esp-videoswitcher"
|
|
#define FW_VERSION "1.0.3"
|
|
|
|
SoftwareSerial swSer(D2, D1);
|
|
int currentnumber = 0;
|
|
int inputbytes[4];
|
|
int commandbytes[4];
|
|
String output = "";
|
|
String output2 = "";
|
|
|
|
unsigned long lastMillis = 0;
|
|
|
|
|
|
/*
|
|
* 25 Boxen insgesamt
|
|
* Box 0, 1, 2
|
|
* 0-29 je 3
|
|
*
|
|
* 40-
|
|
* PS1 = 13: 21, 22, 23
|
|
* SW Pixel
|
|
* 1 55, 56, 57
|
|
* 2 52, 53, 54
|
|
* 3 49, 50, 51
|
|
* 4 46, 47, 48
|
|
*
|
|
*/
|
|
|
|
HomieNode switchNode("switch", "switch");
|
|
|
|
void StripComplete(){
|
|
return;
|
|
}
|
|
|
|
bool onSetColor(const HomieRange& range, const String& value) {
|
|
if (!range.isRange || range.index < 0 || range.index > 1) {
|
|
return false;
|
|
}
|
|
switch (range.index) {
|
|
case 0:
|
|
strip.SetColor1(value.toInt());
|
|
break;
|
|
case 1:
|
|
strip.SetColor2(value.toInt());
|
|
break;
|
|
}
|
|
homieNode.setProperty("color_" + String(range.index)).send(value);
|
|
}
|
|
|
|
bool onSetBox(const HomieRange& range, const String& value) {
|
|
if (range.index < 0 || range.index > (NUMBOXS)) {
|
|
return false;
|
|
}
|
|
strip.colorBox(range.index, value.toInt());
|
|
homieNode.setProperty("box_" + String(range.index)).send(value);
|
|
return true;
|
|
}
|
|
|
|
|
|
bool onSetPixel(const HomieRange& range, const String& value) {
|
|
if (!range.isRange) {
|
|
strip.None();
|
|
strip.ColorSet(value.toInt());
|
|
homieNode.setProperty("pixel").send(value);
|
|
return true;
|
|
}
|
|
if (range.index < 0 || range.index > strip.numPixels() - 1) {
|
|
return false;
|
|
}
|
|
strip.None();
|
|
strip.setPixelColor(range.index, value.toInt());
|
|
strip.show();
|
|
homieNode.setProperty("pixel_" + String(range.index)).send(value);
|
|
}
|
|
|
|
bool onSetBrightness(const HomieRange& range, const String& value) {
|
|
long brightness = value.toInt();
|
|
if (brightness < 0 || brightness > 255) {
|
|
return false;
|
|
}
|
|
strip.setBrightness(brightness);
|
|
strip.show();
|
|
homieNode.setProperty("brightness").send(value);
|
|
}
|
|
|
|
bool onSetPixels(const HomieRange& range, const String& value) {
|
|
|
|
String remaining = value;
|
|
int i = 0;
|
|
// Kein Effekt
|
|
strip.Stop();
|
|
do {
|
|
String current = remaining.substring(0, 7);
|
|
Homie.getLogger() << i << ":" << current << endl;
|
|
uint32_t currentcolor = strip.parseColor(current);
|
|
|
|
strip.setPixelColor(strip.numToPos(i), currentcolor);
|
|
i++;
|
|
|
|
remaining = remaining.substring(7);
|
|
|
|
} while (remaining.length() > 2 && (i < strip.numPixels()));
|
|
Homie.getLogger() << " filling rest with black" << endl;
|
|
while (i < strip.numPixels()) {
|
|
strip.setPixelColor(strip.numToPos(i), strip.Color(0, 0, 0));
|
|
i++;
|
|
}
|
|
|
|
strip.show();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool onSetBoxs(const HomieRange& range, const String& value) {
|
|
|
|
String remaining = value;
|
|
int i = 0;
|
|
// Kein Effekt
|
|
strip.Stop();
|
|
do {
|
|
String current = remaining.substring(0, 7);
|
|
Homie.getLogger() << i << ":" << current << endl;
|
|
uint32_t currentcolor = strip.parseColor(current);
|
|
|
|
strip.setPixelColor(i*3, currentcolor);
|
|
strip.setPixelColor(i*3+1, currentcolor);
|
|
strip.setPixelColor(i*3+2, currentcolor);
|
|
i++;
|
|
|
|
remaining = remaining.substring(7);
|
|
|
|
} while (remaining.length() > 2 && (i < strip.numPixels()));
|
|
Homie.getLogger() << " filling rest with black" << endl;
|
|
while (i < strip.numPixels()) {
|
|
strip.setPixelColor(strip.numToPos(i), strip.Color(0, 0, 0));
|
|
i++;
|
|
}
|
|
|
|
strip.show();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
bool onSetEffect(const HomieRange& range, const String& value) {
|
|
String effect = value;
|
|
effect.toLowerCase();
|
|
if (effect == "scanner") {
|
|
strip.Scanner(strip.Color(255, 0, 0));
|
|
}
|
|
else if (effect == "randomscanner") {
|
|
strip.Scanner(strip.Color(255, 0, 0), 40, true);
|
|
}
|
|
else if (effect == "larsonspiral") {
|
|
strip.Scanner(strip.Color(255, 0, 0), 40, true, true);
|
|
}
|
|
else if (effect == "rainbowcycle") {
|
|
strip.RainbowCycle(50);
|
|
}
|
|
else if (effect == "theaterchase" || effect == "chase") {
|
|
strip.TheaterChase(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 100);
|
|
}
|
|
else if (effect == "fade") {
|
|
strip.Fade(strip.Color(255, 0, 0), strip.Color(0, 0, 255), 200, 100);
|
|
}
|
|
else if (effect == "randomfade") {
|
|
strip.RandomFade();
|
|
}
|
|
else if (effect == "random") {
|
|
strip.Random();
|
|
}
|
|
else if (effect == "smooth") { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
|
|
strip.Smooth(16, 80, 50, 40);
|
|
}
|
|
else if (effect == "plasma") {
|
|
strip.Plasma();
|
|
}
|
|
else if (effect == "radar") {
|
|
strip.Radar();
|
|
}
|
|
else {
|
|
// Test whether command with parameters was sent
|
|
int sep = value.indexOf("|");
|
|
String command = value.substring(0, sep);
|
|
String parameters = value.substring(sep + 1);
|
|
if (command.equals("fill")) {
|
|
strip.ColorSetParameters(parameters);
|
|
}
|
|
else if (command.equals("randomfade")) {
|
|
int sepparam = parameters.indexOf("|");
|
|
int p1 = parameters.substring(0, sepparam).toInt();
|
|
if (p1 <= 0) {
|
|
p1 = 5;
|
|
}
|
|
strip.RandomFadeSingle(p1);
|
|
}
|
|
else {
|
|
strip.None();
|
|
}
|
|
}
|
|
homieNode.setProperty("effect").send(value);
|
|
}
|
|
|
|
bool onSetClear(const HomieRange& range, const String& value) {
|
|
strip.None();
|
|
strip.clear();
|
|
strip.show();
|
|
homieNode.setProperty("clear").send(value);
|
|
}
|
|
|
|
bool onSetLength(const HomieRange& range, const String& value) {
|
|
strip.None();
|
|
strip.clear();
|
|
strip.show();
|
|
int newLength = value.toInt();
|
|
if (newLength > 0) {
|
|
strip.updateLength(newLength);
|
|
}
|
|
homieNode.setProperty("length").send(value);
|
|
}
|
|
|
|
|
|
|
|
bool switchHandler(const HomieRange& range, const String& value) {
|
|
Homie.getLogger() << "switch " << ": " << value << endl;
|
|
int localByte = 0;
|
|
|
|
int lastspace = 0;
|
|
int nextspace = 0;
|
|
while (nextspace < value.length()-1)
|
|
{
|
|
// Nur solange wir noch Platz für Inputbytes haben... :)
|
|
if (localByte <= 3) {
|
|
nextspace = value.indexOf(' ', lastspace);
|
|
commandbytes[localByte] = (value.substring(lastspace,nextspace)).toInt();
|
|
lastspace = nextspace+1;
|
|
localByte++;
|
|
}
|
|
}
|
|
|
|
if (localByte ==4)
|
|
{
|
|
// Nur wenn wirklich 4 Werte übernommen wurden
|
|
switchNode.setProperty("switch").send(value);
|
|
// Sende an das Gerät
|
|
swSer.write(commandbytes[0]);
|
|
swSer.write(commandbytes[1]);
|
|
swSer.write(commandbytes[2]);
|
|
swSer.write(commandbytes[3]);
|
|
}
|
|
else
|
|
{
|
|
commandbytes[0] = 0;
|
|
commandbytes[1] = 0;
|
|
commandbytes[2] = 0;
|
|
commandbytes[3] = 0;
|
|
switchNode.setProperty("switch").send("ERROR");
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void loopHandler() {
|
|
strip.Update();
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
swSer.begin(9600);
|
|
Serial.println("\nKramer VS-162AV");
|
|
|
|
Homie_setFirmware(FW_NAME, FW_VERSION);
|
|
Homie_setBrand(FW_NAME);
|
|
|
|
Homie.setLoopFunction(loopHandler);
|
|
|
|
switchNode.advertise("switch").settable(switchHandler);
|
|
|
|
homieNode.advertiseRange("pixel", 0, NUMPIXELS - 1).settable(onSetPixel);
|
|
homieNode.advertiseRange("box", 0, NUMBOXS - 1).settable(onSetBox);
|
|
homieNode.advertiseRange("color", 0, 1).settable(onSetColor);
|
|
homieNode.advertise("brightness").settable(onSetBrightness);
|
|
homieNode.advertise("effect").settable(onSetEffect);
|
|
homieNode.advertise("clear").settable(onSetClear);
|
|
homieNode.advertise("length").settable(onSetLength);
|
|
// homieNode.advertiseRange("pixels", 0, (NUMPIXELS - 1)*7).settable(onSetPixels);
|
|
// homieNode.advertiseRange("boxs", 0, (BIGPIXELS - 1)*7).settable(onSetBoxs);
|
|
|
|
|
|
Homie.setup();
|
|
|
|
strip.begin();
|
|
strip.clear();
|
|
strip.show();
|
|
|
|
// Default effect
|
|
strip.RainbowCycle(50);
|
|
|
|
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
|
|
ArduinoOTA.onStart([]() {
|
|
Serial.println("\nArduinoOTA.onStart()");
|
|
strip.clear();
|
|
strip.setBrightness(64);
|
|
});
|
|
ArduinoOTA.onEnd([]() {
|
|
Serial.println("\nArduinoOTA.onEnd()");
|
|
strip.clear();
|
|
});
|
|
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
|
Serial.println("\nArduinoOTA.onProgress()");
|
|
// strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0));
|
|
// strip.show();
|
|
});
|
|
|
|
ArduinoOTA.begin();
|
|
}
|
|
|
|
void loop() {
|
|
Homie.loop();
|
|
ArduinoOTA.handle();
|
|
|
|
while (swSer.available() > 0) {
|
|
lastMillis = millis();
|
|
int r1 = swSer.read();
|
|
Serial.print(currentnumber);
|
|
Serial.print(": ");
|
|
Serial.print(r1);
|
|
Serial.print(" ");
|
|
inputbytes[currentnumber] = r1;
|
|
|
|
output += String(r1, DEC);
|
|
output += " ";
|
|
if (currentnumber == 3) {
|
|
switchNode.setProperty("data").send(output);
|
|
output2 = "";
|
|
output2 += "OUTPUT ";
|
|
output2 += String(inputbytes[2]-128, DEC);
|
|
output2 += " INPUT ";
|
|
output2 += String(inputbytes[1]-128, DEC);
|
|
switchNode.setProperty("switchevent").send(output2);
|
|
switchNode.setProperty("input").send(String(inputbytes[1]-128, DEC));
|
|
switchNode.setProperty("output").send(String(inputbytes[2]-128, DEC));
|
|
|
|
|
|
// Zeige aktuelle Quelle auf dem Strip an
|
|
strip.None();
|
|
for (int j=0;j<NUMPIXELS;j++)
|
|
{
|
|
strip.setPixelColor(j, strip.Color(0, 0, 0));
|
|
}
|
|
strip.show();
|
|
strip.colorBox(inputbytes[1]-128, strip.Color(255, 255, 255));
|
|
// strip.setPixelColor((inputbytes[1]-128)*3, strip.Color(255, 0, 0));
|
|
// strip.setPixelColor((inputbytes[1]-128)*3+1, strip.Color(255, 0, 0));
|
|
// strip.setPixelColor((inputbytes[1]-128)*3+2, strip.Color(255, 0, 0));
|
|
strip.show();
|
|
|
|
currentnumber = 0;
|
|
inputbytes[0] = 0;
|
|
inputbytes[1] = 0;
|
|
inputbytes[2] = 0;
|
|
inputbytes[3] = 0;
|
|
output = "";
|
|
|
|
} else {
|
|
Serial.print(">");
|
|
Serial.print(currentnumber);
|
|
Serial.print("<");
|
|
currentnumber++;
|
|
}
|
|
if (millis() > lastMillis + 1000)
|
|
{
|
|
currentnumber = 0;
|
|
}
|
|
}
|
|
}
|