Merge pull request #136 from debsahu/master

fixes and added legacy option
This commit is contained in:
Tobias Blum 2018-04-15 11:17:05 +02:00 committed by GitHub
commit 265a1c9437
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 358 additions and 319 deletions

View file

@ -26,14 +26,14 @@
//SPIFFS Save //SPIFFS Save
#if !defined(ENABLE_HOMEASSISTANT) and defined(ENABLE_STATE_SAVE_SPIFFS) #if !defined(ENABLE_HOMEASSISTANT) and defined(ENABLE_STATE_SAVE_SPIFFS)
#include <ArduinoJson.h> // #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#endif #endif
// MQTT // MQTT
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
#include <PubSubClient.h> #include <PubSubClient.h>
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
#include <ArduinoJson.h> #include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#endif #endif
WiFiClient espClient; WiFiClient espClient;
@ -220,7 +220,9 @@ void saveConfigCallback () {
// *************************************************************************** // ***************************************************************************
// Include: Color modes // Include: Color modes
// *************************************************************************** // ***************************************************************************
#ifdef ENABLE_LEGACY_ANIMATIONS
#include "colormodes.h" #include "colormodes.h"
#endif
// *************************************************************************** // ***************************************************************************
// MAIN // MAIN
@ -530,18 +532,8 @@ void setup() {
// Setup: SPIFFS Webserver handler // Setup: SPIFFS Webserver handler
// *************************************************************************** // ***************************************************************************
server.on("/set_brightness", []() { server.on("/set_brightness", []() {
if (server.arg("c").toInt() > 0) { getArgs();
brightness = (int) server.arg("c").toInt() * 2.55; mode = BRIGHTNESS;
} else {
brightness = server.arg("p").toInt();
}
if (brightness > 255) {
brightness = 255;
}
if (brightness < 0) {
brightness = 0;
}
strip.setBrightness(brightness);
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK %") + String(brightness)).c_str()); mqtt_client.publish(mqtt_outtopic, String(String("OK %") + String(brightness)).c_str());
#endif #endif
@ -597,7 +589,8 @@ void setup() {
}); });
server.on("/get_color", []() { server.on("/get_color", []() {
String rgbcolor = String(main_color.red, HEX) + String(main_color.green, HEX) + String(main_color.blue, HEX); char rgbcolor[7];
snprintf(rgbcolor, sizeof(rgbcolor), "%02X%02X%02X", main_color.red, main_color.green, main_color.blue);
server.send(200, "text/plain", rgbcolor ); server.send(200, "text/plain", rgbcolor );
DBG_OUTPUT_PORT.print("/get_color: "); DBG_OUTPUT_PORT.print("/get_color: ");
DBG_OUTPUT_PORT.println(rgbcolor); DBG_OUTPUT_PORT.println(rgbcolor);
@ -608,7 +601,9 @@ void setup() {
}); });
server.on("/off", []() { server.on("/off", []() {
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true; exit_func = true;
#endif
mode = OFF; mode = OFF;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
@ -627,8 +622,12 @@ void setup() {
}); });
server.on("/all", []() { server.on("/all", []() {
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true; exit_func = true;
mode = ALL; #endif
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
//mode = ALL;
getArgs(); getArgs();
getStatusJSON(); getStatusJSON();
#ifdef ENABLE_MQTT #ifdef ENABLE_MQTT
@ -645,6 +644,7 @@ void setup() {
#endif #endif
}); });
#ifdef ENABLE_LEGACY_ANIMATIONS
server.on("/wipe", []() { server.on("/wipe", []() {
exit_func = true; exit_func = true;
mode = WIPE; mode = WIPE;
@ -777,6 +777,7 @@ void setup() {
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif #endif
}); });
#endif
server.on("/get_modes", []() { server.on("/get_modes", []() {
getModesJSON(); getModesJSON();
@ -872,31 +873,27 @@ void loop() {
if (mode == SET_MODE) { if (mode == SET_MODE) {
DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode);
strip.setMode(ws2812fx_mode); strip.setMode(ws2812fx_mode);
mode = SETSPEED; prevmode = SET_MODE;
mode = SETCOLOR;
} }
if (mode == OFF) { if (mode == OFF) {
// strip.setColor(0,0,0);
// strip.setMode(FX_MODE_STATIC);
if(strip.isRunning()) strip.stop(); //should clear memory if(strip.isRunning()) strip.stop(); //should clear memory
// mode = HOLD; // mode = HOLD;
} }
if (mode == ALL) {
strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_STATIC);
mode = HOLD;
}
if (mode == SETCOLOR) { if (mode == SETCOLOR) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
mode = HOLD; mode = (prevmode == SET_MODE) ? SETSPEED : HOLD;
} }
if (mode == SETSPEED) { if (mode == SETSPEED) {
strip.setSpeed(convertSpeed(ws2812fx_speed)); strip.setSpeed(convertSpeed(ws2812fx_speed));
mode = HOLD; mode = (prevmode == SET_MODE) ? BRIGHTNESS : HOLD;
} }
if (mode == BRIGHTNESS) { if (mode == BRIGHTNESS) {
strip.setBrightness(brightness); strip.setBrightness(brightness);
if (prevmode == SET_MODE) prevmode == HOLD;
mode = HOLD; mode = HOLD;
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode == WIPE) { if (mode == WIPE) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_COLOR_WIPE); strip.setMode(FX_MODE_COLOR_WIPE);
@ -924,19 +921,28 @@ void loop() {
strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW); strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
mode = HOLD; mode = HOLD;
} }
#endif
if (mode == HOLD || mode == CUSTOM) { if (mode == HOLD || mode == CUSTOM) {
if(!strip.isRunning()) strip.start(); if(!strip.isRunning()) strip.start();
#ifdef ENABLE_LEGACY_ANIMATIONS
if (exit_func) { if (exit_func) {
exit_func = false; exit_func = false;
} }
#endif
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode == TV) { if (mode == TV) {
if(!strip.isRunning()) strip.start(); if(!strip.isRunning()) strip.start();
tv(); tv();
} }
#endif
// Only for modes with WS2812FX functionality // Only for modes with WS2812FX functionality
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode != TV && mode != CUSTOM) { if (mode != TV && mode != CUSTOM) {
#else
if (mode != CUSTOM) {
#endif
strip.service(); strip.service();
} }

View file

@ -16,7 +16,7 @@ const char HOSTNAME[] = "McLighting01"; // Friedly hostname
#define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active #define ENABLE_HOMEASSISTANT // If defined, enable Homeassistant integration, ENABLE_MQTT must be active
#define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control #define ENABLE_BUTTON // If defined, enable button handling code, see: https://github.com/toblum/McLighting/wiki/Button-control
//#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth //#define MQTT_HOME_ASSISTANT_SUPPORT // If defined, use AMQTT and select Tools -> IwIP Variant -> Higher Bandwidth
#define ENABLE_LEGACY_ANIMATIONS
#if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX) #if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX)
#error "Cant have both NeoAnimationFX and WS2812FX enabled. Choose either one." #error "Cant have both NeoAnimationFX and WS2812FX enabled. Choose either one."
@ -85,17 +85,22 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
#define DBG_OUTPUT_PORT Serial // Set debug output port #define DBG_OUTPUT_PORT Serial // Set debug output port
// List of all color modes // List of all color modes
enum MODE { SET_MODE, HOLD, OFF, ALL, SETCOLOR, SETSPEED, BRIGHTNESS, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, TWINKLERANDOM, THEATERCHASERAINBOW, TV, CUSTOM }; #ifdef ENABLE_LEGACY_ANIMATIONS
enum MODE { SET_MODE, HOLD, OFF, SETCOLOR, SETSPEED, BRIGHTNESS, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, TWINKLERANDOM, THEATERCHASERAINBOW, TV, CUSTOM };
MODE mode = RAINBOW; // Standard mode that is active when software starts MODE mode = RAINBOW; // Standard mode that is active when software starts
bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
#else
enum MODE { SET_MODE, HOLD, OFF, SETCOLOR, SETSPEED, BRIGHTNESS, CUSTOM };
MODE mode = SET_MODE; // Standard mode that is active when software starts
#endif
MODE prevmode = mode;
int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster
int brightness = 196; // Global variable for storing the brightness (255 == 100%) int brightness = 196; // Global variable for storing the brightness (255 == 100%)
int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes
bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
bool shouldSaveConfig = false; // For WiFiManger custom config bool shouldSaveConfig = false; // For WiFiManger custom config
struct ledstate // Data structure to store a state of a single led struct ledstate // Data structure to store a state of a single led

View file

@ -32,6 +32,12 @@ void getArgs() {
ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount() - 1); ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount() - 1);
} }
if (server.arg("c").toInt() > 0) {
brightness = constrain((int) server.arg("c").toInt() * 2.55, 0, 255);
} else if (server.arg("p").toInt() > 0) {
brightness = constrain(server.arg("p").toInt(), 0, 255);
}
main_color.red = constrain(main_color.red, 0, 255); main_color.red = constrain(main_color.red, 0, 255);
main_color.green = constrain(main_color.green, 0, 255); main_color.green = constrain(main_color.green, 0, 255);
main_color.blue = constrain(main_color.blue, 0, 255); main_color.blue = constrain(main_color.blue, 0, 255);
@ -74,7 +80,8 @@ void handleSetMainColor(uint8_t * mypayload) {
main_color.red = ((rgb >> 16) & 0xFF); main_color.red = ((rgb >> 16) & 0xFF);
main_color.green = ((rgb >> 8) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF); main_color.blue = ((rgb >> 0) & 0xFF);
strip.setColor(main_color.red, main_color.green, main_color.blue); // strip.setColor(main_color.red, main_color.green, main_color.blue);
mode = SETCOLOR;
} }
void handleSetAllMode(uint8_t * mypayload) { void handleSetAllMode(uint8_t * mypayload) {
@ -85,13 +92,16 @@ void handleSetAllMode(uint8_t * mypayload) {
main_color.green = ((rgb >> 8) & 0xFF); main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF); main_color.blue = ((rgb >> 0) & 0xFF);
for (int i = 0; i < strip.numPixels(); i++) { // for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); // strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue);
} // }
strip.show(); // strip.show();
DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); DBG_OUTPUT_PORT.printf("WS: Set all leds to main color: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue);
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true; exit_func = true;
mode = ALL; #endif
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
} }
void handleSetSingleLED(uint8_t * mypayload, uint8_t firstChar = 0) { void handleSetSingleLED(uint8_t * mypayload, uint8_t firstChar = 0) {
@ -119,7 +129,9 @@ void handleSetSingleLED(uint8_t * mypayload, uint8_t firstChar = 0) {
strip.setPixelColor(led, ledstates[led].red, ledstates[led].green, ledstates[led].blue); strip.setPixelColor(led, ledstates[led].red, ledstates[led].green, ledstates[led].blue);
strip.show(); strip.show();
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true; exit_func = true;
#endif
mode = CUSTOM; mode = CUSTOM;
} }
@ -198,6 +210,7 @@ void setModeByStateString(String saved_state_string) {
strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setColor(main_color.red, main_color.green, main_color.blue);
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
void handleSetNamedMode(String str_mode) { void handleSetNamedMode(String str_mode) {
exit_func = true; exit_func = true;
@ -208,7 +221,8 @@ void handleSetNamedMode(String str_mode) {
#endif #endif
} }
if (str_mode.startsWith("=all")) { if (str_mode.startsWith("=all")) {
mode = ALL; ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
#ifdef ENABLE_HOMEASSISTANT #ifdef ENABLE_HOMEASSISTANT
stateOn = true; stateOn = true;
#endif #endif
@ -256,6 +270,7 @@ void handleSetNamedMode(String str_mode) {
#endif #endif
} }
} }
#endif
void handleSetWS2812FXMode(uint8_t * mypayload) { void handleSetWS2812FXMode(uint8_t * mypayload) {
mode = HOLD; mode = HOLD;
@ -270,10 +285,16 @@ char* listStatusJSON() {
char json[255]; char json[255];
char modeName[30]; char modeName[30];
strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem
// if (mode == SET_MODE) {
strncpy_P(modeName, (PGM_P)strip.getModeName((uint8_t) ws2812fx_mode), sizeof(modeName)); // copy from progmem
snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}",
mode, strip.getMode(), modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue); mode, ws2812fx_mode, modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
// }else {
// strncpy_P(modeName, (PGM_P)strip.getModeName(strip.getMode()), sizeof(modeName)); // copy from progmem
// snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}",
// mode, strip.getMode(), modeName, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
// }
return json; return json;
} }
@ -503,6 +524,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#endif #endif
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode // = ==> Activate named mode
if (payload[0] == '=') { if (payload[0] == '=') {
// we get mode data // we get mode data
@ -525,6 +547,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif #endif
} }
#endif
// $ ==> Get status Info. // $ ==> Get status Info.
if (payload[0] == '$') { if (payload[0] == '$') {
@ -597,6 +620,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
} }
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
void checkForRequests() { void checkForRequests() {
webSocket.loop(); webSocket.loop();
server.handleClient(); server.handleClient();
@ -604,6 +628,7 @@ void checkForRequests() {
mqtt_client.loop(); mqtt_client.loop();
#endif #endif
} }
#endif
// *************************************************************************** // ***************************************************************************
@ -724,7 +749,8 @@ void checkForRequests() {
const char* state_in = root["state"]; const char* state_in = root["state"];
if (strcmp(state_in, on_cmd) == 0 and !(animation_on)) { if (strcmp(state_in, on_cmd) == 0 and !(animation_on)) {
stateOn = true; stateOn = true;
mode = ALL; ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
} }
else if (strcmp(state_in, off_cmd) == 0) { else if (strcmp(state_in, off_cmd) == 0) {
stateOn = false; stateOn = false;
@ -927,6 +953,7 @@ void checkForRequests() {
#endif #endif
} }
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode // = ==> Activate named mode
if (payload[0] == '=') { if (payload[0] == '=') {
String str_mode = String((char *) &payload[0]); String str_mode = String((char *) &payload[0]);
@ -945,6 +972,7 @@ void checkForRequests() {
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState); if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif #endif
} }
#endif
// $ ==> Get status Info. // $ ==> Get status Info.
if (payload[0] == '$') { if (payload[0] == '$') {