2017-02-27 16:51:18 +00:00
|
|
|
#include <Homie.h>
|
2016-12-18 21:17:09 +00:00
|
|
|
#include <Adafruit_NeoPixel.h>
|
2017-02-27 16:51:18 +00:00
|
|
|
#include <ArduinoOTA.h>
|
2017-02-28 21:57:30 +00:00
|
|
|
#include "NeoPatterns.h"
|
2016-12-18 21:17:09 +00:00
|
|
|
#ifdef __AVR__
|
2017-02-27 16:51:18 +00:00
|
|
|
#include <avr/power.h>
|
2016-12-18 21:17:09 +00:00
|
|
|
#endif
|
2016-12-18 20:15:32 +00:00
|
|
|
|
2017-02-27 16:51:18 +00:00
|
|
|
#define PIN D1 //data pin for ws2812 (pixelprojektor @ ctdo: PIN 2)
|
2016-12-22 23:05:38 +00:00
|
|
|
#define NUMPIXELS 64
|
|
|
|
|
2017-03-01 20:41:27 +00:00
|
|
|
NeoPatterns strip = NeoPatterns(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800, &StripComplete);
|
|
|
|
|
|
|
|
bool stopAfterCompletion;
|
|
|
|
|
2017-02-28 23:02:17 +00:00
|
|
|
void StripComplete() {
|
2017-03-01 20:41:27 +00:00
|
|
|
if (stopAfterCompletion)
|
|
|
|
{
|
|
|
|
strip.IconComplete();
|
|
|
|
}
|
2017-02-28 23:02:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HomieNode homieNode("pixel", "commands");
|
|
|
|
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
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);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
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);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
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);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
bool onSetEffect(const HomieRange& range, const String& value) {
|
2017-03-01 20:41:27 +00:00
|
|
|
stopAfterCompletion = false;
|
2017-03-01 15:21:34 +00:00
|
|
|
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);
|
|
|
|
}
|
2017-03-01 20:41:27 +00:00
|
|
|
else if (effect == "theaterchase" || effect == "chase") {
|
2017-03-01 15:21:34 +00:00
|
|
|
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();
|
|
|
|
}
|
2017-03-01 20:41:27 +00:00
|
|
|
else if (effect == "random") {
|
|
|
|
strip.Random();
|
|
|
|
} else if (effect == "smooth") { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
|
2017-03-01 15:21:34 +00:00
|
|
|
strip.Smooth(16, 80, 50, 40);
|
|
|
|
}
|
|
|
|
else {
|
2017-03-01 16:37:20 +00:00
|
|
|
// Test whether command with parameters was sent
|
|
|
|
int sep = value.indexOf("|");
|
|
|
|
String command = value.substring(0, sep);
|
|
|
|
String parameters = value.substring(sep + 1);
|
2017-03-01 20:41:27 +00:00
|
|
|
if (command.equals("fill")) {
|
2017-03-01 16:37:20 +00:00
|
|
|
strip.ColorSetParameters(parameters);
|
2017-03-01 20:41:27 +00:00
|
|
|
}
|
|
|
|
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 {
|
2017-03-01 16:37:20 +00:00
|
|
|
strip.None();
|
|
|
|
}
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
|
|
|
homieNode.setProperty("effect").send(value);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
2016-12-22 23:05:38 +00:00
|
|
|
|
2017-03-01 20:41:27 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
bool onSetClear(const HomieRange& range, const String& value) {
|
|
|
|
strip.None();
|
|
|
|
strip.clear();
|
|
|
|
strip.show();
|
|
|
|
homieNode.setProperty("clear").send(value);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
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);
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loopHandler() {
|
2017-03-01 15:21:34 +00:00
|
|
|
strip.Update();
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
2017-03-01 15:21:34 +00:00
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
Homie_setFirmware("pixelprojektor", "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);
|
2017-03-01 20:41:27 +00:00
|
|
|
homieNode.advertise("icon").settable(onSetIcon);
|
2017-03-01 16:37:20 +00:00
|
|
|
|
|
|
|
Homie.setup();
|
2017-03-01 20:41:27 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
strip.begin();
|
|
|
|
strip.clear();
|
|
|
|
strip.setBrightness(64);
|
|
|
|
strip.show();
|
2017-03-01 20:41:27 +00:00
|
|
|
stopAfterCompletion = false; // Default
|
2017-03-01 15:21:34 +00:00
|
|
|
|
|
|
|
ArduinoOTA.setHostname("pixelprojektor");
|
|
|
|
ArduinoOTA.onStart([]() {
|
|
|
|
strip.clear();
|
2017-03-01 20:41:27 +00:00
|
|
|
strip.setBrightness(64);
|
2017-03-01 15:21:34 +00:00
|
|
|
});
|
|
|
|
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();
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2017-03-01 15:21:34 +00:00
|
|
|
Homie.loop();
|
|
|
|
ArduinoOTA.handle();
|
2017-02-28 23:02:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Diese Effekte müssen nach dem Umbau wieder vorhanden sein:
|
|
|
|
/*
|
|
|
|
case EFFECT_SPIRAL:
|
|
|
|
led_spiral();
|
|
|
|
break;
|
|
|
|
case EFFECT_RANDOMFADE:
|
|
|
|
led_randomfade();
|
|
|
|
break;
|
|
|
|
case EFFECT_CHASE:
|
|
|
|
led_chase();
|
|
|
|
break;
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************ Old stuff ************/
|
|
|
|
/*
|
2017-03-01 15:21:34 +00:00
|
|
|
int fadespeedmax = 5; //1 to 255
|
2016-12-18 20:15:32 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
void led_random()
|
|
|
|
{
|
2017-02-27 16:51:18 +00:00
|
|
|
for (int i = 0; i < strip.numPixels(); i++) {
|
|
|
|
strip.setPixelColor(i, wheel(random(0, 255)));
|
2016-12-18 22:37:20 +00:00
|
|
|
}
|
|
|
|
strip.show();
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
2016-12-18 21:17:09 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
void led_radar()
|
|
|
|
{
|
2017-02-27 16:51:18 +00:00
|
|
|
// "Sweep" in cirles...
|
|
|
|
// line(0,0,950*cos(radians(iAngle)),-950*sin(radians(iAngle)));
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
2017-02-27 16:51:18 +00:00
|
|
|
|
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
void led_spiral()
|
|
|
|
{
|
2017-02-27 16:51:18 +00:00
|
|
|
int every = 4;
|
|
|
|
wheelPos++;
|
|
|
|
int qp = Index % every;
|
|
|
|
Index++;
|
2017-02-28 23:02:17 +00:00
|
|
|
if (Index >= strip.numPixels() - 1) {
|
2017-02-27 16:51:18 +00:00
|
|
|
Index = 0;
|
|
|
|
}
|
|
|
|
int q = Index % every;
|
|
|
|
for (uint16_t i = 0; i < strip.numPixels(); i = i + every) {
|
|
|
|
strip.setPixelColor(numToSpiralPos(i + q), wheel( (i + Index * 4) % 255)); //turn every "every" pixel on
|
|
|
|
}
|
|
|
|
for (uint16_t i = 0; i < strip.numPixels(); i = i + every) {
|
|
|
|
strip.setPixelColor(numToSpiralPos(i + qp), 0); //turn every "every" pixel off
|
2016-12-22 23:05:38 +00:00
|
|
|
}
|
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
2016-12-22 23:05:38 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
bool effectHandler(const HomieRange& range, const String& value) {
|
2016-12-18 22:37:20 +00:00
|
|
|
Homie.getLogger() << "-> " << value << endl;
|
|
|
|
int sep = value.indexOf("|");
|
|
|
|
|
2017-02-27 16:51:18 +00:00
|
|
|
String command = value.substring(0, sep);
|
|
|
|
String parameters = value.substring(sep + 1);
|
2016-12-18 22:37:20 +00:00
|
|
|
Homie.getLogger() << "command=" << command << " parameters=" << parameters << endl;
|
2017-02-27 16:51:18 +00:00
|
|
|
|
|
|
|
if (command.equals("fill")) {
|
|
|
|
effect = EFFECT_NONE;
|
2016-12-18 22:37:20 +00:00
|
|
|
led_fill(parseColor(parameters));
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("off")) {
|
|
|
|
effect = EFFECT_NONE;
|
2016-12-18 22:37:20 +00:00
|
|
|
led_fill(strip.Color(0, 0, 0));
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("random")) {
|
|
|
|
effect = EFFECT_NONE;
|
2016-12-18 22:37:20 +00:00
|
|
|
led_random();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("set")) { //example: set|37#ff003a
|
|
|
|
effect = EFFECT_NONE;
|
|
|
|
int x = parameters.substring(0, 1).toInt();
|
|
|
|
int y = parameters.substring(1, 2).toInt();
|
|
|
|
String cstr = parameters.substring(2, 9);
|
|
|
|
strip.setPixelColor(xyToPos(x, y), parseColor(cstr));
|
2016-12-18 22:37:20 +00:00
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("smooth")) { //example: smooth|[wheelspeed]|[smoothing]|[strength] wheelspeed=1-255, smoothing=0-100, strength=1-255
|
2016-12-23 00:43:06 +00:00
|
|
|
int sepparam = parameters.indexOf("|");
|
2017-02-27 16:51:18 +00:00
|
|
|
int p1 = parameters.substring(0, sepparam).toInt();
|
|
|
|
String parameters_part2 = parameters.substring(sepparam + 1);
|
2016-12-23 00:43:06 +00:00
|
|
|
sepparam = parameters_part2.indexOf("|");
|
2017-02-27 16:51:18 +00:00
|
|
|
int p2 = parameters_part2.substring(0, sepparam).toInt();
|
|
|
|
int p3 = parameters_part2.substring(sepparam + 1).toInt();
|
|
|
|
wheelSpeed = 16; //default, speed=+1 /frame
|
|
|
|
if (p1 > 0) {
|
|
|
|
wheelSpeed = p1;
|
2016-12-23 00:43:06 +00:00
|
|
|
}
|
2017-02-27 16:51:18 +00:00
|
|
|
smoothing = 80;
|
|
|
|
if (p2 > 0) {
|
|
|
|
smoothing = p2;
|
2016-12-23 00:43:06 +00:00
|
|
|
}
|
2017-02-27 16:51:18 +00:00
|
|
|
strength = 50;
|
|
|
|
if (p3 > 0) {
|
|
|
|
strength = p3;
|
2016-12-23 00:43:06 +00:00
|
|
|
}
|
|
|
|
Homie.getLogger() << "-- p1=" << p1 << " p2=" << p2 << " p3=" << p3 << endl;
|
2017-02-27 16:51:18 +00:00
|
|
|
effect = EFFECT_SMOOTH;
|
2016-12-22 23:05:38 +00:00
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("spiral")) {
|
|
|
|
effect = EFFECT_SPIRAL;
|
|
|
|
Index = 0;
|
2016-12-22 23:05:38 +00:00
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("clearbuffer")) {
|
2016-12-23 00:43:06 +00:00
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("randomfade")) { //example: randomfade|5
|
2016-12-23 00:43:06 +00:00
|
|
|
int sepparam = parameters.indexOf("|");
|
2017-02-27 16:51:18 +00:00
|
|
|
int p1 = parameters.substring(0, sepparam).toInt();
|
|
|
|
fadespeedmax = 5;
|
|
|
|
if (p1 > 0) {
|
|
|
|
fadespeedmax = p1;
|
2016-12-23 00:43:06 +00:00
|
|
|
}
|
2017-02-27 16:51:18 +00:00
|
|
|
effect = EFFECT_RANDOMFADE;
|
2016-12-23 00:43:06 +00:00
|
|
|
set_randomBuffer(); //initialize random
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("randombuffer")) {
|
2016-12-23 00:43:06 +00:00
|
|
|
set_randomBuffer(); //set random
|
|
|
|
showBuffer();
|
2017-02-27 16:51:18 +00:00
|
|
|
} else if (command.equals("chase")) {
|
|
|
|
effect = EFFECT_CHASE;
|
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
|
|
|
} else if (command.equals("radar")) {
|
|
|
|
effect = EFFECT_RADAR;
|
|
|
|
Index = 0;
|
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
|
|
|
} else if (command.equals("larson")) {
|
|
|
|
effect = EFFECT_LARSON;
|
|
|
|
Index = 0;
|
|
|
|
bufferClear();
|
|
|
|
showBuffer();
|
|
|
|
strip.show();
|
2016-12-18 20:15:32 +00:00
|
|
|
}
|
|
|
|
return true;
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
2016-12-18 20:15:32 +00:00
|
|
|
|
2017-03-01 15:21:34 +00:00
|
|
|
bool pixelsHandler(const HomieRange& range, const String& value) {
|
2017-02-27 16:51:18 +00:00
|
|
|
|
|
|
|
String remaining = value;
|
|
|
|
int i = 0;
|
|
|
|
effect = EFFECT_NONE;
|
|
|
|
do {
|
|
|
|
String current = remaining.substring(0, 7);
|
2016-12-18 22:37:20 +00:00
|
|
|
Homie.getLogger() << i << ":" << current << endl;
|
2017-02-27 16:51:18 +00:00
|
|
|
uint32_t currentcolor = parseColor(current);
|
2016-12-18 22:37:20 +00:00
|
|
|
|
|
|
|
strip.setPixelColor(i, currentcolor);
|
|
|
|
i++;
|
2017-02-27 16:51:18 +00:00
|
|
|
|
|
|
|
remaining = remaining.substring(7);
|
|
|
|
|
|
|
|
} while (remaining.length() > 2 && (i < strip.numPixels()));
|
2016-12-18 22:37:20 +00:00
|
|
|
Homie.getLogger() << " filling rest with black" << endl;
|
2017-02-27 16:51:18 +00:00
|
|
|
while (i < strip.numPixels()) {
|
|
|
|
strip.setPixelColor(numToPos(i), strip.Color(0, 0, 0));
|
|
|
|
i++;
|
2016-12-18 20:15:32 +00:00
|
|
|
}
|
2016-12-18 22:37:20 +00:00
|
|
|
|
|
|
|
strip.show();
|
2017-02-27 16:51:18 +00:00
|
|
|
|
2016-12-18 20:15:32 +00:00
|
|
|
return true;
|
2017-03-01 15:21:34 +00:00
|
|
|
}
|
2016-12-18 20:15:32 +00:00
|
|
|
|
|
|
|
|
2017-02-28 23:02:17 +00:00
|
|
|
*/
|