2017-01-28 22:29:51 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
#include <Homie.h>
|
|
|
|
#include <ArduinoOTA.h>
|
|
|
|
#include <Adafruit_NeoPixel.h>
|
|
|
|
|
|
|
|
#define PIN D1
|
2017-01-29 13:18:23 +00:00
|
|
|
#define NUMPIXELS 144
|
2017-01-28 22:29:51 +00:00
|
|
|
|
|
|
|
uint16_t i=0,j=0,wait = 50;
|
|
|
|
unsigned long lastCall = 0;
|
|
|
|
byte wPos = 0;
|
|
|
|
|
|
|
|
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
|
|
|
|
|
|
|
|
HomieNode stripNode("strip", "strip");
|
|
|
|
|
|
|
|
String effect = "none";
|
|
|
|
|
|
|
|
// Input a value 0 to 255 to get a color value.
|
|
|
|
// The colours are a transition r - g - b - back to r.
|
|
|
|
uint32_t wheel(byte wheelPos) {
|
|
|
|
wheelPos = 255 - wheelPos;
|
|
|
|
if(wheelPos < 85) {
|
|
|
|
return pixels.Color(255 - wheelPos * 3, 0, wheelPos * 3);
|
|
|
|
}
|
|
|
|
if(wheelPos < 170) {
|
|
|
|
wheelPos -= 85;
|
|
|
|
return pixels.Color(0, wheelPos * 3, 255 - wheelPos * 3);
|
|
|
|
}
|
|
|
|
wheelPos -= 170;
|
|
|
|
return pixels.Color(wheelPos * 3, 255 - wheelPos * 3, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onSetPixel(const HomieRange& range, const String& value){
|
|
|
|
if(!range.isRange){
|
|
|
|
for(int i=0;i<pixels.numPixels();i++){
|
|
|
|
pixels.setPixelColor(i, value.toInt());
|
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
stripNode.setProperty("pixel").send(value);
|
|
|
|
return true;
|
|
|
|
}
|
2017-01-29 13:18:23 +00:00
|
|
|
if (range.index < 0 || range.index > pixels.numPixels()-1) {
|
2017-01-28 22:29:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-01-29 13:18:23 +00:00
|
|
|
effect = "none";
|
2017-01-28 22:29:51 +00:00
|
|
|
pixels.setPixelColor(range.index, value.toInt());
|
|
|
|
pixels.show();
|
|
|
|
stripNode.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;
|
|
|
|
}
|
|
|
|
pixels.setBrightness(brightness);
|
|
|
|
pixels.show();
|
|
|
|
stripNode.setProperty("brightness").send(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onSetEffect(const HomieRange& range, const String& value){
|
|
|
|
effect = value;
|
2017-01-28 23:43:06 +00:00
|
|
|
effect.toLowerCase();
|
2017-01-28 22:29:51 +00:00
|
|
|
i=0,j=0,wait=50;
|
|
|
|
stripNode.setProperty("effect").send(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool onSetClear(const HomieRange& range, const String& value){
|
|
|
|
effect = "none";
|
|
|
|
pixels.clear();
|
|
|
|
pixels.show();
|
|
|
|
stripNode.setProperty("clear").send(value);
|
|
|
|
}
|
|
|
|
|
2017-01-29 13:18:23 +00:00
|
|
|
bool onSetLength(const HomieRange& range, const String& value){
|
|
|
|
effect = "none";
|
|
|
|
pixels.clear();
|
|
|
|
pixels.show();
|
|
|
|
int newLength = value.toInt();
|
|
|
|
if(newLength > 0){
|
|
|
|
pixels.updateLength(newLength);
|
|
|
|
}
|
|
|
|
stripNode.setProperty("length").send(value);
|
|
|
|
}
|
|
|
|
|
2017-01-28 22:29:51 +00:00
|
|
|
|
|
|
|
void loopHandler() {
|
|
|
|
if (effect == "none"){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (effect == "larson") {
|
|
|
|
int SpeedDelay = 20;
|
|
|
|
int ReturnDelay = 50;
|
|
|
|
int EyeSize = 5;
|
|
|
|
for(int i = 0; i < pixels.numPixels()-EyeSize-2; i++) {
|
|
|
|
pixels.clear();
|
|
|
|
pixels.show();
|
|
|
|
pixels.setPixelColor(i, 255/10, 0, 0);
|
|
|
|
for(int j = 1; j <= EyeSize; j++) {
|
|
|
|
//pixels.setPixelColor(i+j, 255, 0, 0);
|
|
|
|
pixels.setPixelColor(i+j, wheel(wPos++));
|
|
|
|
}
|
|
|
|
pixels.setPixelColor(i+EyeSize+1, 255/10, 0, 0);
|
|
|
|
pixels.show();
|
|
|
|
delay(SpeedDelay);
|
|
|
|
}
|
|
|
|
delay(wait);
|
|
|
|
|
|
|
|
for(int i = pixels.numPixels()-EyeSize-2; i > 0; i--) {
|
|
|
|
pixels.clear();
|
|
|
|
pixels.show();
|
|
|
|
pixels.setPixelColor(i, 255/10, 0, 0);
|
|
|
|
for(int j = 1; j <= EyeSize; j++) {
|
|
|
|
//pixels.setPixelColor(i+j, 255, 0, 0);
|
|
|
|
pixels.setPixelColor(i+j, wheel(wPos++));
|
|
|
|
}
|
|
|
|
pixels.setPixelColor(i+EyeSize+1, 255/10, 0, 0);
|
|
|
|
pixels.show();
|
|
|
|
delay(SpeedDelay);
|
|
|
|
}
|
|
|
|
delay(wait);
|
|
|
|
}
|
|
|
|
else if (effect == "randomfade") {
|
|
|
|
if(lastCall + wait > millis()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastCall = millis();
|
|
|
|
for(int i=0;i<pixels.numPixels();i++){
|
|
|
|
pixels.setPixelColor(i,wheel(wPos));
|
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
wPos++;
|
|
|
|
}
|
|
|
|
else if (effect == "rainbow") {
|
|
|
|
if(wait + lastCall >= millis()){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(j<256) {
|
|
|
|
if(i<pixels.numPixels()) {
|
|
|
|
pixels.setPixelColor(i, wheel((i+j) & 255));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i=0;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
lastCall = millis();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
j=0;
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 23:43:06 +00:00
|
|
|
else if (effect == "rainbowcycle") {
|
2017-01-28 22:29:51 +00:00
|
|
|
if(j<256*5) {
|
|
|
|
if(i<pixels.numPixels()) {
|
|
|
|
pixels.setPixelColor(i, wheel(((i * 256 / pixels.numPixels()) + j) & 255));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
i=0;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
pixels.show();
|
|
|
|
lastCall = millis();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
j=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
Serial.begin(115200);
|
|
|
|
Homie_setFirmware("schild", "1.0.0");
|
|
|
|
Homie.setLoopFunction(loopHandler);
|
|
|
|
|
|
|
|
stripNode.advertiseRange("pixel", 0, NUMPIXELS-1).settable(onSetPixel);
|
|
|
|
stripNode.advertise("brightness").settable(onSetBrightness);
|
|
|
|
stripNode.advertise("effect").settable(onSetEffect);
|
|
|
|
stripNode.advertise("clear").settable(onSetClear);
|
2017-01-29 13:18:23 +00:00
|
|
|
stripNode.advertise("length").settable(onSetLength);
|
2017-01-28 22:29:51 +00:00
|
|
|
|
|
|
|
pixels.begin();
|
|
|
|
pixels.clear();
|
|
|
|
pixels.setBrightness(64);
|
|
|
|
pixels.show();
|
|
|
|
|
|
|
|
Homie.setup();
|
|
|
|
|
|
|
|
ArduinoOTA.setHostname(Homie.getConfiguration().deviceId);
|
|
|
|
ArduinoOTA.setPassword((const char *)"ctdo2342");
|
|
|
|
ArduinoOTA.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
Homie.loop();
|
|
|
|
ArduinoOTA.handle();
|
|
|
|
}
|