diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 6ff1286..6f32b19 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -26,12 +26,11 @@ WebSocketsServer webSocket = WebSocketsServer(81); // *************************************************************************** -// Load libraries / Instanciate Neopixel +// Load libraries / Instanciate WS2812FX library // *************************************************************************** -#include -#ifdef __AVR__ -#include -#endif +// https://github.com/kitesurfer1404/WS2812FX +#include +WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800); // Parameter 1 = number of pixels in strip // Parameter 2 = Arduino pin number (most are valid) @@ -40,7 +39,6 @@ WebSocketsServer webSocket = WebSocketsServer(81); // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) -Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800); // IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across // pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input @@ -76,7 +74,7 @@ void configModeCallback (WiFiManager *myWiFiManager) { uint16_t i; for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, 0, 255, 0); + strip.setPixelColor(i, 0, 0, 255); } strip.show(); } @@ -114,9 +112,11 @@ void setup() { // *************************************************************************** // Setup: Neopixel // *************************************************************************** - strip.begin(); + strip.init(); strip.setBrightness(brightness); - strip.show(); // Initialize all pixels to 'off' + strip.setSpeed(200); + strip.setMode(FX_MODE_RAINBOW_CYCLE); + strip.start(); // *************************************************************************** // Setup: WiFiManager @@ -334,6 +334,15 @@ void setup() { getStatusJSON(); }); + server.on("/get_modes", []() { + getModesJSON(); + }); + + server.on("/set_mode", []() { + getArgs(); + mode = SET_MODE; + }); + server.begin(); } @@ -342,37 +351,42 @@ void loop() { webSocket.loop(); // Simple statemachine that handles the different modes + if (mode == SET_MODE) { + DBG_OUTPUT_PORT.printf("SET_MODE: %d %d\n", ws2812fx_mode, mode); + strip.setMode(ws2812fx_mode); + mode = HOLD; + } if (mode == OFF) { - //colorWipe(strip.Color(0, 0, 0), 50); - uint16_t i; - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, 0, 0, 0); - } - strip.show(); - //mode = HOLD; + strip.setColor(0,0,0); + strip.setMode(FX_MODE_STATIC); + mode = HOLD; } if (mode == ALL) { - uint16_t i; - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, main_color.red, main_color.green, main_color.blue); - } - strip.show(); - //mode = HOLD; + strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.setMode(FX_MODE_STATIC); + mode = HOLD; } if (mode == WIPE) { - colorWipe(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); + strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.setMode(FX_MODE_COLOR_WIPE); + mode = HOLD; } if (mode == RAINBOW) { - rainbow(delay_ms); + strip.setMode(FX_MODE_RAINBOW); + mode = HOLD; } if (mode == RAINBOWCYCLE) { - rainbowCycle(delay_ms); + strip.setMode(FX_MODE_RAINBOW_CYCLE); + mode = HOLD; } if (mode == THEATERCHASE) { - theaterChase(strip.Color(main_color.red, main_color.green, main_color.blue), delay_ms); + strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.setMode(FX_MODE_THEATER_CHASE); + mode = HOLD; } if (mode == THEATERCHASERAINBOW) { - theaterChaseRainbow(delay_ms); + strip.setMode(FX_MODE_THEATER_CHASE_RAINBOW); + mode = HOLD; } if (mode == HOLD) { if (exit_func) { @@ -382,4 +396,8 @@ void loop() { if (mode == TV) { tv(); } + + if (mode != TV) { + strip.service(); + } } diff --git a/Arduino/McLighting/colormodes.h b/Arduino/McLighting/colormodes.h index f05191f..33d7bf9 100644 --- a/Arduino/McLighting/colormodes.h +++ b/Arduino/McLighting/colormodes.h @@ -40,22 +40,6 @@ void updateLed (int led, int brightness) { } -// 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 strip.Color(255 - WheelPos * 3, 0, WheelPos * 3); - } - if (WheelPos < 170) { - WheelPos -= 85; - return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3); - } - WheelPos -= 170; - return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0); -} - - // See: http://forum.mysensors.org/topic/85/phoneytv-for-vera-is-here/13 void tv() { checkForRequests(); @@ -114,111 +98,5 @@ void tv() { } -// Fill the dots one after the other with a color -void colorWipe(uint32_t c, uint8_t wait) { - uint16_t i; - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, 0, 0, 0); - } - strip.show(); - - for (uint16_t i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, c); - strip.show(); - - checkForRequests(); - if (exit_func) { - exit_func = false; - return; - } - - delay(wait); - } - mode = HOLD; -} - -void rainbow(uint8_t wait) { - uint16_t i, j; - - for (j = 0; j < 256; j++) { - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, Wheel((i + j) & 255)); - } - strip.show(); - - checkForRequests(); - if (exit_func) { - exit_func = false; - return; - } - - delay(wait); - } -} - -// Slightly different, this makes the rainbow equally distributed throughout -void rainbowCycle(uint8_t wait) { - uint16_t i, j; - - for (j = 0; j < 256; j++) { // 1 cycle of all colors on wheel - for (i = 0; i < strip.numPixels(); i++) { - strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255)); - } - strip.show(); - - checkForRequests(); - if (exit_func) { - exit_func = false; - return; - } - - delay(wait); - } -} - -// Theatre-style crawling lights. -void theaterChase(uint32_t c, uint8_t wait) { - for (int q = 0; q < 3; q++) { - for (int i = 0; i < strip.numPixels(); i = i + 3) { - strip.setPixelColor(i + q, c); //turn every third pixel on - } - strip.show(); - - checkForRequests(); - if (exit_func) { - exit_func = false; - return; - } - delay(wait); - - for (int i = 0; i < strip.numPixels(); i = i + 3) { - strip.setPixelColor(i + q, 0); //turn every third pixel off - } - } -} - -// Theatre-style crawling lights with rainbow effect -void theaterChaseRainbow(uint8_t wait) { - for (int j = 0; j < 256; j++) { // cycle all 256 colors in the wheel - for (int q = 0; q < 3; q++) { - for (int i = 0; i < strip.numPixels(); i = i + 3) { - strip.setPixelColor(i + q, Wheel( (i + j) % 255)); //turn every third pixel on - } - strip.show(); - - checkForRequests(); - if (exit_func) { - exit_func = false; - return; - } - delay(wait); - - for (int i = 0; i < strip.numPixels(); i = i + 3) { - strip.setPixelColor(i + q, 0); //turn every third pixel off - } - } - } -} - diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 9a4d2fe..44687d0 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -1,9 +1,9 @@ // Neopixel #define PIN 5 // PIN where neopixel / WS2811 strip is attached -#define NUMLEDS 12 // Number of leds in the strip +#define NUMLEDS 24 // Number of leds in the strip -#define HOSTNAME "ESP8266_01" // Friedly hostname +#define HOSTNAME "ESP8266_02" // Friedly hostname // *************************************************************************** @@ -12,12 +12,14 @@ #define DBG_OUTPUT_PORT Serial // Set debug output port // List of all color modes -enum MODE { HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, THEATERCHASERAINBOW, TV }; +enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, THEATERCHASERAINBOW, TV }; -MODE mode = RAINBOWCYCLE; // Standard mode that is active when software starts +MODE mode = RAINBOW; // Standard mode that is active when software starts -int delay_ms = 50; // Global variable for storing the delay between color changes --> smaller == faster -int brightness = 128; // Global variable for storing the brightness (255 == 100%) +int ws2812fx_speed = 128; // Global variable for storing the delay between color changes --> smaller == faster +int brightness = 192; // 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 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 78affa6..86a511f 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -12,31 +12,16 @@ void getArgs() { main_color.green = server.arg("g").toInt(); main_color.blue = server.arg("b").toInt(); } - delay_ms = server.arg("d").toInt(); - - if (main_color.red > 255) { - main_color.red = 255; - } - if (main_color.green > 255) { - main_color.green = 255; - } - if (main_color.blue > 255) { - main_color.blue = 255; + ws2812fx_speed = constrain(server.arg("s").toInt(), 0, 255); + if (server.arg("s") == "") { + ws2812fx_speed = 128; } - if (main_color.red < 0) { - main_color.red = 0; - } - if (main_color.green < 0) { - main_color.green = 0; - } - if (main_color.blue < 0) { - main_color.blue = 0; - } - - if (server.arg("d") == "") { - delay_ms = 20; - } + ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount()-1); + + 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); DBG_OUTPUT_PORT.print("Mode: "); DBG_OUTPUT_PORT.print(mode); @@ -46,8 +31,8 @@ void getArgs() { DBG_OUTPUT_PORT.print(main_color.green); DBG_OUTPUT_PORT.print(", "); DBG_OUTPUT_PORT.print(main_color.blue); - DBG_OUTPUT_PORT.print(", Delay:"); - DBG_OUTPUT_PORT.print(delay_ms); + DBG_OUTPUT_PORT.print(", Speed:"); + DBG_OUTPUT_PORT.print(ws2812fx_speed); DBG_OUTPUT_PORT.print(", Brightness:"); DBG_OUTPUT_PORT.println(brightness); } @@ -97,15 +82,31 @@ void handleNotFound() { char* listStatusJSON() { char json[255]; - snprintf(json, sizeof(json), "{\"mode\":%d, \"delay_ms\":%d, \"brightness\":%d, \"color\":[%d, %d, %d]}", mode, delay_ms, brightness, main_color.red, main_color.green, main_color.blue); + 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); return json; } - void getStatusJSON() { server.send ( 200, "application/json", listStatusJSON() ); } +String listModesJSON() { + String modes = "["; + for(uint8_t i=0; i < strip.getModeCount(); i++) { + modes += "{\"mode\":"; + modes += i; + modes += ", \"name\":\""; + modes += strip.getModeName(i); + modes += "\"},"; + } + modes += "{}]"; + return modes; +} + +void getModesJSON() { + server.send ( 200, "application/json", listModesJSON() ); +} + void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) { switch (type) { case WStype_DISCONNECTED: @@ -131,16 +132,17 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght 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); DBG_OUTPUT_PORT.printf("Set main color to: [%u] [%u] [%u]\n", main_color.red, main_color.green, main_color.blue); webSocket.sendTXT(num, "OK"); } - // # ==> Set delay + // # ==> Set speed if (payload[0] == '?') { - // decode delay data uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); - delay_ms = ((d >> 0) & 0xFF); - DBG_OUTPUT_PORT.printf("WS: Set delay to: [%u]\n", delay_ms); + ws2812fx_speed = constrain(d, 0, 255); + strip.setSpeed(ws2812fx_speed); + DBG_OUTPUT_PORT.printf("WS: Set speed to: [%u]\n", ws2812fx_speed); webSocket.sendTXT(num, "OK"); } @@ -239,6 +241,28 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght DBG_OUTPUT_PORT.println(json); webSocket.sendTXT(num, json); } + + // $ ==> Get WS2812 modes. + if (payload[0] == '~') { + DBG_OUTPUT_PORT.printf("Get WS2812 modes."); + + String json = listModesJSON(); + DBG_OUTPUT_PORT.println(json); + webSocket.sendTXT(num, json); + } + + // $ ==> Set WS2812 mode. + if (payload[0] == '/') { + mode = HOLD; + uint8_t ws2812fx_mode = (uint8_t) strtol((const char *) &payload[1], NULL, 10); + ws2812fx_mode = constrain(ws2812fx_mode, 0, 255); + strip.setColor(main_color.red, main_color.green, main_color.blue); + strip.setMode(ws2812fx_mode); + + //String json = listStatusJSON(); + //DBG_OUTPUT_PORT.println(json); + webSocket.sendTXT(num, "OK"); + } break; } } diff --git a/clients/web/build/index.htm b/clients/web/build/index.htm index 91c5bac..a69c4c7 100644 --- a/clients/web/build/index.htm +++ b/clients/web/build/index.htm @@ -11,8 +11,8 @@ - -