From bc2984cffc0730c035aa5618960aafb8dbc3a452 Mon Sep 17 00:00:00 2001 From: Keith Lord Date: Tue, 19 Sep 2017 18:31:03 -0400 Subject: [PATCH] Added auto cycle feature --- Arduino/McLighting/McLighting.ino | 5 +- Arduino/McLighting/definitions.h | 8 +++ Arduino/McLighting/request_handlers.h | 84 ++++++++++++++++++++------- clients/web/build/index.htm | 30 +++++++--- 4 files changed, 96 insertions(+), 31 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 57b3f33..941c967 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -348,7 +348,10 @@ void setup() { size_t fileSize = dir.fileSize(); DBG_OUTPUT_PORT.printf("FS File: %s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str()); } - DBG_OUTPUT_PORT.printf("\n"); + + FSInfo fs_info; + SPIFFS.info(fs_info); + DBG_OUTPUT_PORT.printf("FS Usage: %d/%d bytes\n\n", fs_info.usedBytes, fs_info.totalBytes); } // *************************************************************************** diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 1f69202..617e3d4 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -7,6 +7,14 @@ const char HOSTNAME[] = "ESP8266_01"; // Friedly hostname #define ENABLE_OTA // If defined, enable Arduino OTA code. #define ENABLE_MQTT // If defined, enable MQTT client code. + +// parameters for automatically cycling favorite patterns +uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds) + {0xff0000, 200, 1, 5.0}, // blink red for 5 seconds + {0x00ff00, 200, 3, 10.0}, // wipe green for 10 seconds + {0x0000ff, 200, 11, 5.0} // dual scan blue for 5 seconds +}; + #ifdef ENABLE_MQTT #define MQTT_MAX_PACKET_SIZE 256 #define MQTT_MAX_RECONNECT_TRIES 4 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 9261e26..ed40453 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -123,11 +123,17 @@ void handleSetWS2812FXMode(uint8_t * mypayload) { ws2812fx_mode = constrain(ws2812fx_mode, 0, 255); strip.setColor(main_color.red, main_color.green, main_color.blue); strip.setMode(ws2812fx_mode); + strip.start(); } char* listStatusJSON() { char json[255]; - snprintf(json, sizeof(json), "{\"mode\":%d, \"ws2812fx_mode\":%d, \"ws2812fx_mode_name\":\"%s\", \"speed\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, strip.getMode(), strip.getModeName(strip.getMode()), ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue); + + char modeName[30]; + 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; } @@ -158,29 +164,25 @@ void getModesJSON() { // *************************************************************************** void handleMinimalUpload() { char temp[1500]; - int sec = millis() / 1000; - int min = sec / 60; - int hr = min / 60; snprintf ( temp, 1500, - "\ - \ - \ - ESP8266 Upload\ - \ - \ - \ - \ - \ -
\ - \ - \ - \ -
\ - \ - ", - hr, min % 60, sec % 60 - ); + "\ + \ + \ + ESP8266 Upload\ + \ + \ + \ + \ + \ +
\ + \ + \ + \ +
\ + \ + " + ); server.send ( 200, "text/html", temp ); } @@ -199,6 +201,32 @@ void handleNotFound() { server.send ( 404, "text/plain", message ); } +// automatic cycling +Ticker autoTicker; +int autoCount = 0; + +void autoTick() { + strip.setColor(autoParams[autoCount][0]); + strip.setSpeed((uint8_t)autoParams[autoCount][1]); + strip.setMode((uint8_t)autoParams[autoCount][2]); + autoTicker.once((float)autoParams[autoCount][3], autoTick); + DBG_OUTPUT_PORT.print("autoTick "); + DBG_OUTPUT_PORT.println(autoCount); + + autoCount++; + if(autoCount >= (sizeof(autoParams) / sizeof(autoParams[0]))) autoCount=0; +} + +void handleAutoStart() { + autoCount=0; + autoTick(); + strip.start(); +} + +void handleAutoStop() { + autoTicker.detach(); + strip.stop(); +} // *************************************************************************** // WS request handlers @@ -292,6 +320,18 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght handleSetWS2812FXMode(payload); webSocket.sendTXT(num, "OK"); } + + // start auto cycling + if (strcmp((char *)payload, "start") == 0 ) { + handleAutoStart(); + webSocket.sendTXT(num, "OK"); + } + + // stop auto cycling + if (strcmp((char *)payload, "stop") == 0 ) { + handleAutoStop(); + webSocket.sendTXT(num, "OK"); + } break; } } diff --git a/clients/web/build/index.htm b/clients/web/build/index.htm index 8f50305..14b2c9e 100644 --- a/clients/web/build/index.htm +++ b/clients/web/build/index.htm @@ -14,7 +14,8 @@