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

232 lines
5.6 KiB
Arduino
Raw Normal View History

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-31 19:13:45 +00:00
#define NUMPIXELS 30
2017-01-28 22:29:51 +00:00
2017-01-29 13:59:12 +00:00
uint16_t effectI=0,effectJ=0,wait = 50;
2017-01-28 22:29:51 +00:00
unsigned long lastCall = 0;
byte wPos = 0;
2017-02-14 19:39:52 +00:00
uint8_t state = 0;
2017-01-28 22:29:51 +00:00
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);
}
2017-01-31 19:13:45 +00:00
void getRGBValues(uint32_t *rgbBuffer,uint32_t const color){
rgbBuffer[0] = color >> 16 & 255;
rgbBuffer[1] = color >> 8 & 255;
rgbBuffer[2] = color & 255;
return ;
}
2017-01-28 22:29:51 +00:00
bool onSetPixel(const HomieRange& range, const String& value){
if(!range.isRange){
2017-02-16 19:51:48 +00:00
effect = "none";
pixels.clear();
2017-01-28 22:29:51 +00:00
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) {
2017-01-28 22:29:51 +00:00
return false;
}
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-29 13:59:12 +00:00
effectI=0,effectJ=0,wait=50;
2017-01-28 22:29:51 +00:00
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){
2017-01-29 13:41:26 +00:00
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 EyeSize = 5;
2017-01-31 19:13:45 +00:00
uint32_t rgb[3] = {0};
2017-02-14 19:39:52 +00:00
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;
2017-01-28 22:29:51 +00:00
}
}
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;
}
2017-01-29 13:59:12 +00:00
if(effectJ<256) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel((effectI+effectJ) & 255));
effectI++;
2017-01-28 22:29:51 +00:00
}
else {
2017-01-29 13:59:12 +00:00
effectI=0;
effectJ++;
2017-01-28 22:29:51 +00:00
}
pixels.show();
lastCall = millis();
}
else {
2017-01-29 13:59:12 +00:00
effectJ=0;
2017-01-28 22:29:51 +00:00
}
}
2017-01-28 23:43:06 +00:00
else if (effect == "rainbowcycle") {
2017-01-29 13:59:12 +00:00
if(effectJ<256*5) {
if(effectI<pixels.numPixels()) {
pixels.setPixelColor(effectI, wheel(((effectI * 256 / pixels.numPixels()) + effectJ) & 255));
effectI++;
2017-01-28 22:29:51 +00:00
}
else {
2017-01-29 13:59:12 +00:00
effectI=0;
effectJ++;
2017-01-28 22:29:51 +00:00
}
pixels.show();
lastCall = millis();
}
else {
2017-01-29 13:59:12 +00:00
effectJ=0;
2017-01-28 22:29:51 +00:00
}
}
}
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);
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();
}