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
#if !defined(ENABLE_HOMEASSISTANT) and defined(ENABLE_STATE_SAVE_SPIFFS)
#include <ArduinoJson.h> //
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#endif
// MQTT
#ifdef ENABLE_MQTT
#include <PubSubClient.h>
#ifdef ENABLE_HOMEASSISTANT
#include <ArduinoJson.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson
#endif
WiFiClient espClient;
@ -220,7 +220,9 @@ void saveConfigCallback () {
// ***************************************************************************
// Include: Color modes
// ***************************************************************************
#include "colormodes.h"
#ifdef ENABLE_LEGACY_ANIMATIONS
#include "colormodes.h"
#endif
// ***************************************************************************
// MAIN
@ -530,18 +532,8 @@ void setup() {
// Setup: SPIFFS Webserver handler
// ***************************************************************************
server.on("/set_brightness", []() {
if (server.arg("c").toInt() > 0) {
brightness = (int) server.arg("c").toInt() * 2.55;
} else {
brightness = server.arg("p").toInt();
}
if (brightness > 255) {
brightness = 255;
}
if (brightness < 0) {
brightness = 0;
}
strip.setBrightness(brightness);
getArgs();
mode = BRIGHTNESS;
#ifdef ENABLE_MQTT
mqtt_client.publish(mqtt_outtopic, String(String("OK %") + String(brightness)).c_str());
#endif
@ -597,7 +589,8 @@ void setup() {
});
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 );
DBG_OUTPUT_PORT.print("/get_color: ");
DBG_OUTPUT_PORT.println(rgbcolor);
@ -608,7 +601,9 @@ void setup() {
});
server.on("/off", []() {
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true;
#endif
mode = OFF;
getArgs();
getStatusJSON();
@ -627,8 +622,12 @@ void setup() {
});
server.on("/all", []() {
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true;
mode = ALL;
#endif
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
//mode = ALL;
getArgs();
getStatusJSON();
#ifdef ENABLE_MQTT
@ -645,6 +644,7 @@ void setup() {
#endif
});
#ifdef ENABLE_LEGACY_ANIMATIONS
server.on("/wipe", []() {
exit_func = true;
mode = WIPE;
@ -777,6 +777,7 @@ void setup() {
if(!spiffs_save_state.active()) spiffs_save_state.once(3, tickerSpiffsSaveState);
#endif
});
#endif
server.on("/get_modes", []() {
getModesJSON();
@ -872,31 +873,27 @@ void loop() {
if (mode == SET_MODE) {
DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode);
strip.setMode(ws2812fx_mode);
mode = SETSPEED;
prevmode = SET_MODE;
mode = SETCOLOR;
}
if (mode == OFF) {
// strip.setColor(0,0,0);
// strip.setMode(FX_MODE_STATIC);
if(strip.isRunning()) strip.stop(); //should clear memory
// 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) {
strip.setColor(main_color.red, main_color.green, main_color.blue);
mode = HOLD;
mode = (prevmode == SET_MODE) ? SETSPEED : HOLD;
}
if (mode == SETSPEED) {
strip.setSpeed(convertSpeed(ws2812fx_speed));
mode = HOLD;
mode = (prevmode == SET_MODE) ? BRIGHTNESS : HOLD;
}
if (mode == BRIGHTNESS) {
strip.setBrightness(brightness);
if (prevmode == SET_MODE) prevmode == HOLD;
mode = HOLD;
}
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode == WIPE) {
strip.setColor(main_color.red, main_color.green, main_color.blue);
strip.setMode(FX_MODE_COLOR_WIPE);
@ -924,19 +921,28 @@ void loop() {
strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW);
mode = HOLD;
}
#endif
if (mode == HOLD || mode == CUSTOM) {
if(!strip.isRunning()) strip.start();
#ifdef ENABLE_LEGACY_ANIMATIONS
if (exit_func) {
exit_func = false;
}
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode == TV) {
if(!strip.isRunning()) strip.start();
tv();
}
#endif
// Only for modes with WS2812FX functionality
#ifdef ENABLE_LEGACY_ANIMATIONS
if (mode != TV && mode != CUSTOM) {
#else
if (mode != CUSTOM) {
#endif
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_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 ENABLE_LEGACY_ANIMATIONS
#if defined(USE_NEOANIMATIONFX) and defined(USE_WS2812FX)
#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
// 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
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 mode = RAINBOW; // Standard mode that is active when software starts
MODE prevmode = mode;
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 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
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);
}
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.green = constrain(main_color.green, 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.green = ((rgb >> 8) & 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) {
@ -85,13 +92,16 @@ void handleSetAllMode(uint8_t * mypayload) {
main_color.green = ((rgb >> 8) & 0xFF);
main_color.blue = ((rgb >> 0) & 0xFF);
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue);
}
strip.show();
// for (int i = 0; i < strip.numPixels(); i++) {
// strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue);
// }
// 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);
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true;
mode = ALL;
#endif
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
}
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.show();
}
#ifdef ENABLE_LEGACY_ANIMATIONS
exit_func = true;
#endif
mode = CUSTOM;
}
@ -198,7 +210,8 @@ void setModeByStateString(String saved_state_string) {
strip.setColor(main_color.red, main_color.green, main_color.blue);
}
void handleSetNamedMode(String str_mode) {
#ifdef ENABLE_LEGACY_ANIMATIONS
void handleSetNamedMode(String str_mode) {
exit_func = true;
if (str_mode.startsWith("=off")) {
@ -208,7 +221,8 @@ void handleSetNamedMode(String str_mode) {
#endif
}
if (str_mode.startsWith("=all")) {
mode = ALL;
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
#ifdef ENABLE_HOMEASSISTANT
stateOn = true;
#endif
@ -255,7 +269,8 @@ void handleSetNamedMode(String str_mode) {
stateOn = true;
#endif
}
}
}
#endif
void handleSetWS2812FXMode(uint8_t * mypayload) {
mode = HOLD;
@ -270,10 +285,16 @@ char* listStatusJSON() {
char json[255];
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]}",
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;
}
@ -503,6 +524,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode
if (payload[0] == '=') {
// 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);
#endif
}
#endif
// $ ==> Get status Info.
if (payload[0] == '$') {
@ -597,13 +620,15 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght
}
}
void checkForRequests() {
#ifdef ENABLE_LEGACY_ANIMATIONS
void checkForRequests() {
webSocket.loop();
server.handleClient();
#ifdef ENABLE_MQTT
mqtt_client.loop();
#endif
}
}
#endif
// ***************************************************************************
@ -724,7 +749,8 @@ void checkForRequests() {
const char* state_in = root["state"];
if (strcmp(state_in, on_cmd) == 0 and !(animation_on)) {
stateOn = true;
mode = ALL;
ws2812fx_mode = FX_MODE_STATIC;
mode = SET_MODE;
}
else if (strcmp(state_in, off_cmd) == 0) {
stateOn = false;
@ -927,6 +953,7 @@ void checkForRequests() {
#endif
}
#ifdef ENABLE_LEGACY_ANIMATIONS
// = ==> Activate named mode
if (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);
#endif
}
#endif
// $ ==> Get status Info.
if (payload[0] == '$') {