esp-wemos-schild/esp-wemos-schild/esp-wemos-schild.ino

227 lines
5.8 KiB
C++

#include <Arduino.h>
#include <Homie.h>
#include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h>
#include "NeoPatterns.h"
#include <math.h>
#define PIN D1
#define NUMPIXELS 30
#define FW_NAME "esp-schild"
#define FW_VERSION "1.0.4"
HomieNode homieNode("strip", "strip");
void StripComplete() {
return;
}
void DebugOutput(String value) {
homieNode.setProperty("DEBUG").send(value);
}
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete, &DebugOutput);
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 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 onSetSpeed(const HomieRange& range, const String& value) {
long speed = value.toInt();
if (speed < 0 || speed > 255) {
return false;
}
strip.setInterval(speed);
strip.show();
homieNode.setProperty("speed").send(value);
}
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), 4, 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), 50);
}
else if (effect == "bvb") {
strip.BVBChase(strip.Color(255, 185, 0), strip.Color(0, 0, 0), 50);
}
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 == "fire") {
strip.Fire();
}
else if (effect == "fireworks") {
strip.Fireworks();
}
else if (effect == "drop") {
strip.Drop();
}
else if (effect == "scannerrandom") {
strip.ScannerRandom(strip.Color(255, 0, 0), 4, true);
}
else if (effect == "ring") {
strip.Rings();
} 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 if (command.equals("randomscanner")) {
int sepparam = parameters.indexOf("|");
int p1 = parameters.substring(0, sepparam).toInt();
if (p1 <= 0) {
p1 = 5;
}
homieNode.setProperty("effect").send(String(p1));
strip.Scanner(strip.Color(255, 0, 0), p1, true);
}
else {
strip.None();
digitalWrite(PIN, LOW); // D4 ist auch gleichzeitig der LED-Pin, daher abschalten... (TODO: TEST: FIXME)
}
}
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);
}
void loopHandler() {
strip.Update();
}
void setup() {
Serial.begin(115200);
Homie_setFirmware(FW_NAME, FW_VERSION);
Homie_setBrand(FW_NAME);
Homie.setLoopFunction(loopHandler);
homieNode.advertiseRange("pixel", 0, NUMPIXELS - 1).settable(onSetPixel);
homieNode.advertiseRange("color", 0, 1).settable(onSetColor);
homieNode.advertise("brightness").settable(onSetBrightness);
homieNode.advertise("speed").settable(onSetSpeed);
homieNode.advertise("effect").settable(onSetEffect);
homieNode.advertise("clear").settable(onSetClear);
homieNode.advertise("length").settable(onSetLength);
strip.begin();
strip.clear();
strip.setBrightness(30);
strip.show();
Homie.setup();
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
ArduinoOTA.begin();
ArduinoOTA.onStart([]() {
strip.clear();
});
ArduinoOTA.onEnd([]() {
strip.clear();
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
strip.setPixelColor(progress / (total / NUMPIXELS), strip.Color(100, 0, 0));
strip.show();
});
ArduinoOTA.begin();
}
void loop() {
Homie.loop();
ArduinoOTA.handle();
}