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

231 lines
5.6 KiB
C++

#include <Arduino.h>
#include <Homie.h>
#include <ArduinoOTA.h>
#include <Adafruit_NeoPixel.h>
#define PIN D1
#define NUMPIXELS 30
uint16_t effectI=0,effectJ=0,wait = 50;
unsigned long lastCall = 0;
byte wPos = 0;
uint8_t state = 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);
}
void getRGBValues(uint32_t *rgbBuffer,uint32_t const color){
rgbBuffer[0] = color >> 16 & 255;
rgbBuffer[1] = color >> 8 & 255;
rgbBuffer[2] = color & 255;
return ;
}
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;
}
if (range.index < 0 || range.index > pixels.numPixels()-1) {
return false;
}
effect = "none";
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;
effect.toLowerCase();
effectI=0,effectJ=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);
}
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);
}
void loopHandler() {
if (effect == "none"){
return;
}
else if (effect == "larson") {
int SpeedDelay = 20;
int EyeSize = 5;
uint32_t rgb[3] = {0};
switch(state){
case 0:
if(lastCall + wait < millis()){
state++;
}
break;
case 1:
for(int i = 0; i < pixels.numPixels()-EyeSize-2; i++) {
pixels.clear();
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i, rgb[0]/10, rgb[1]/10, rgb[2]/10);
for(int j = 1; j <= EyeSize; j++) {
pixels.setPixelColor(i+j, wheel(wPos++));
}
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i+EyeSize+1, rgb[0]/10, rgb[1]/10, rgb[2]/10);
pixels.show();
delay(SpeedDelay);
}
lastCall = millis();
state++;
break;
case 2:
if(lastCall + wait < millis()){
state++;
}
break;
case 3:
for(int i = pixels.numPixels()-EyeSize-2; i > 0; i--) {
pixels.clear();
getRGBValues(rgb,wheel(wPos));
pixels.setPixelColor(i, rgb[0]/10, rgb[1]/10, rgb[2]/10);
for(int j = 1; j <= EyeSize; j++) {
pixels.setPixelColor(i+j, wheel(wPos++));
}
pixels.setPixelColor(i+EyeSize+1, rgb[0]/10, rgb[1]/10, rgb[2]/10);
pixels.show();
delay(SpeedDelay);
}
lastCall = millis();
state++;
break;
default:
state = 0;
}
}
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(effectJ<256) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel((effectI+effectJ) & 255));
effectI++;
}
else {
effectI=0;
effectJ++;
}
pixels.show();
lastCall = millis();
}
else {
effectJ=0;
}
}
else if (effect == "rainbowcycle") {
if(effectJ<256*5) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel(((effectI * 256 / pixels.numPixels()) + effectJ) & 255));
effectI++;
}
else {
effectI=0;
effectJ++;
}
pixels.show();
lastCall = millis();
}
else {
effectJ=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);
stripNode.advertise("length").settable(onSetLength);
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();
}