From 2e58d55eec917d08b672e4f0b98d49669a2d5634 Mon Sep 17 00:00:00 2001 From: Tobias Blum Date: Sat, 16 Dec 2017 23:43:57 +0100 Subject: [PATCH] Convert new WS2812FX spped format to McLighting Keep McLighting speeds (0-255) unchanged and convert to WS2812FX speeds. (65535-0) --- Arduino/McLighting/McLighting.ino | 4 ++-- Arduino/McLighting/definitions.h | 4 ++-- Arduino/McLighting/request_handlers.h | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 040b800..45e20b9 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -166,7 +166,7 @@ void setup() { // *************************************************************************** strip.init(); strip.setBrightness(brightness); - strip.setSpeed(ws2812fx_speed); + strip.setSpeed(convertSpeed(ws2812fx_speed)); //strip.setMode(FX_MODE_RAINBOW_CYCLE); strip.setColor(main_color.red, main_color.green, main_color.blue); strip.start(); @@ -450,7 +450,7 @@ void setup() { if (server.arg("d").toInt() >= 0) { ws2812fx_speed = server.arg("d").toInt(); ws2812fx_speed = constrain(ws2812fx_speed, 0, 255); - strip.setSpeed(ws2812fx_speed); + strip.setSpeed(convertSpeed(ws2812fx_speed)); } getStatusJSON(); diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index bd0ddae..4f50f97 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -44,8 +44,8 @@ enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE, MODE mode = RAINBOW; // Standard mode that is active when software starts -int ws2812fx_speed = 10; // Global variable for storing the delay between color changes --> smaller == faster -int brightness = 192; // Global variable for storing the brightness (255 == 100%) +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 diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index ef72cd2..e79007b 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -14,7 +14,7 @@ void getArgs() { } ws2812fx_speed = constrain(server.arg("s").toInt(), 0, 255); if (server.arg("s") == "") { - ws2812fx_speed = 128; + ws2812fx_speed = 196; } ws2812fx_mode = constrain(server.arg("m").toInt(), 0, strip.getModeCount()-1); @@ -38,6 +38,19 @@ void getArgs() { } +long convertSpeed(int mcl_speed) { + long ws2812_speed = mcl_speed * 256; + ws2812_speed = SPEED_MAX - ws2812_speed; + if (ws2812_speed < SPEED_MIN) { + ws2812_speed = SPEED_MIN; + } + if (ws2812_speed > SPEED_MAX) { + ws2812_speed = SPEED_MAX; + } + return ws2812_speed; +} + + // *************************************************************************** // Handler functions for WS and MQTT // *************************************************************************** @@ -260,7 +273,7 @@ int autoCount = 0; void autoTick() { strip.setColor(autoParams[autoCount][0]); - strip.setSpeed((uint8_t)autoParams[autoCount][1]); + strip.setSpeed(convertSpeed((uint8_t)autoParams[autoCount][1])); strip.setMode((uint8_t)autoParams[autoCount][2]); autoTicker.once((float)autoParams[autoCount][3], autoTick); DBG_OUTPUT_PORT.print("autoTick "); @@ -313,7 +326,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght if (payload[0] == '?') { uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); ws2812fx_speed = constrain(d, 0, 255); - strip.setSpeed(ws2812fx_speed); + strip.setSpeed(convertSpeed(ws2812fx_speed)); DBG_OUTPUT_PORT.printf("WS: Set speed to: [%u]\n", ws2812fx_speed); webSocket.sendTXT(num, "OK"); } @@ -428,7 +441,7 @@ void checkForRequests() { if (payload[0] == '?') { uint8_t d = (uint8_t) strtol((const char *) &payload[1], NULL, 10); ws2812fx_speed = constrain(d, 0, 255); - strip.setSpeed(ws2812fx_speed); + strip.setSpeed(convertSpeed(ws2812fx_speed)); DBG_OUTPUT_PORT.printf("MQTT: Set speed to [%u]\n", ws2812fx_speed); mqtt_client.publish(mqtt_outtopic, String(String("OK ") + String((char *)payload)).c_str()); }