esp-pixelbox/esp-pixelbox.ino

264 lines
6.9 KiB
Arduino
Raw Normal View History

2017-07-05 00:05:34 +00:00
#include <Homie.h>
#include <Adafruit_NeoPixel.h>
#include <ArduinoOTA.h>
#include "NeoPatterns.h"
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN D2 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2) // Für pixelpad: Pin2
#define NUMPIXELS 64
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete);
bool stopAfterCompletion;
void StripComplete() {
if (stopAfterCompletion)
{
strip.IconComplete();
}
return;
}
HomieNode homieNode("pixel", "commands");
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 onSetVU(const HomieRange& range, const String& value) {
strip.Stop(); // Do not show any "effects" anymore
strip.clear(); // All pixels to black
String remaining = value;
String current;
int i = 0;
do
{
if (remaining.length() == 1)
{
current = remaining.substring(0,1);
} else {
current = remaining.substring(0,2);
}
int currentvalue = (int) strtol(&current[0], NULL, 10);
// White peak
strip.setPixelColor(strip.numToPos(56+i-currentvalue*8), 16777215); // White dot
// Shaded bar "below" the peak
if (currentvalue>0)
{
for (int j=currentvalue-1;j>-1;j--)
{
strip.setPixelColor(strip.numToPos(56+i-j*8), 4276545); // Greyscale
}
}
i++;
remaining = remaining.substring(2);
} while (remaining.length()>0); // TODO: Not bigger than strip
strip.show();
homieNode.setProperty("VU_" + 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 onSetEffect(const HomieRange& range, const String& value) {
stopAfterCompletion = false;
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 {
// 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 onSetIcon(const HomieRange& range, const String& value) {
stopAfterCompletion = true;
String _iconname = value;
if (value[0] == '#') { //color given
strip.Icon(value.substring(7)[0], value.substring(0, 6));
}
else {
strip.Icon(value[0]);
}
homieNode.setProperty("icon").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("pixelbox", "1.0.0");
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("effect").settable(onSetEffect);
homieNode.advertise("clear").settable(onSetClear);
homieNode.advertise("length").settable(onSetLength);
homieNode.advertise("icon").settable(onSetIcon);
homieNode.advertiseRange("pixels", 0, (NUMPIXELS - 1)*7).settable(onSetPixels);
homieNode.advertiseRange("vu", 0, sqrt(NUMPIXELS)-1).settable(onSetVU);
Homie.setup();
strip.begin();
strip.clear();
// strip.setBrightness(64);
strip.setBrightness(255); // HEEELLLLLLL :)
strip.show();
stopAfterCompletion = false; // Default
// strip.Plasma(); // Default effect
ArduinoOTA.setHostname("pixelbox");
ArduinoOTA.onStart([]() {
strip.clear();
strip.setBrightness(64);
});
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();
}