diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index 8e5d7c3..f8bc21b 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -135,7 +135,6 @@ WS2812FX* strip = NULL; #endif void initDMA(uint16_t stripSize = NUMLEDS){ - if (dma) delete dma; uint8_t ledcolors = 3; if (strstr(WS2812FXStripSettings.RGBOrder, "W") != NULL) { ledcolors = 4; @@ -267,18 +266,23 @@ void saveConfigCallback () { #include "mode_custom_ws2812fx_animations.h" // Add animations in this file #endif - // function to Initialize the strip void initStrip(uint16_t stripSize = WS2812FXStripSettings.stripSize, char RGBOrder[5] = WS2812FXStripSettings.RGBOrder, uint8_t pin = WS2812FXStripSettings.pin, uint8_t fxoptions = WS2812FXStripSettings.fxoptions ){ - if (strip != NULL) { - DBG_OUTPUT_PORT.println("Deleting strip!"); + DBG_OUTPUT_PORT.println("Initializing strip!"); + if (dma != NULL) { + delete(dma); + } + if (strip != NULL) { //second one created second to delete delete(strip); WS2812FXStripSettings.stripSize = stripSize; strcpy(WS2812FXStripSettings.RGBOrder, RGBOrder); WS2812FXStripSettings.pin = pin; WS2812FXStripSettings.fxoptions = fxoptions; } - DBG_OUTPUT_PORT.println("Initializing strip!"); + if (ledstates != NULL) { + delete(ledstates); + } + ledstates = new uint8_t [WS2812FXStripSettings.stripSize]; #if !defined(LED_TYPE_WS2811) strip = new WS2812FX(stripSize, pin, checkRGBOrder(RGBOrder) + NEO_KHZ800); #else @@ -297,24 +301,6 @@ void initStrip(uint16_t stripSize = WS2812FXStripSettings.stripSize, char RGBOrd // and minimize distance between Arduino and first pixel. Avoid connecting // on a live circuit...if you must, connect GND first. -#if defined(CUSTOM_WS2812FX_ANIMATIONS) - if (heat != NULL) { - delete(heat); - } - heat = new byte [WS2812FXStripSettings.stripSize]; -#endif -#if defined(ENABLE_TV) - if (ledStates != NULL) { - delete(ledStates); - } - ledStates = new uint8_t [WS2812FXStripSettings.stripSize]; -#endif -/* - if (ledstates != NULL) { - delete(ledstates); - } - ledStates = new LEDState ledstates[WS2812FXStripSettings.stripSize]; -*/ strip->init(); #if defined(USE_WS2812FX_DMA) initDMA(stripSize); @@ -325,7 +311,20 @@ void initStrip(uint16_t stripSize = WS2812FXStripSettings.stripSize, char RGBOrd strip->setSegment(0, 0, stripSize - 1, ws2812fx_mode, hex_colors, convertSpeed(ws2812fx_speed), fxoptions); #if defined(CUSTOM_WS2812FX_ANIMATIONS) strip->setCustomMode(0, F("Fire 2012"), myCustomEffect0); -//strip->setCustomMode(1, F("CustEffect"), myCustomEffect1); +//strip->setCustomMode(1, F("CustEffect"), myCustomEffect1); +#endif +#if defined(ENABLE_E131) +/* + uint8_t universe_leds = 170.0; // a universe has only 512 (0..511) channels: 3*170 or 4*128 <= 512 + if (strstr(WS2812FXStripSettings.RGBOrder, "W") != NULL) { + universe_leds = 128.0; + } +*/ + float float_enduni = stripSize/170.0; + uint8_t END_UNIVERSE = stripSize/170.0; + if (float_enduni > END_UNIVERSE) { + END_UNIVERSE = END_UNIVERSE +1; + } #endif } @@ -532,7 +531,7 @@ void setup() { strcpy(mqtt_pass, custom_mqtt_pass.getValue()); #endif strcpy(tmp_strip_size, custom_strip_size.getValue()); - WS2812FXStripSettings.stripSize = atoi(custom_strip_size.getValue()); + WS2812FXStripSettings.stripSize = constrain(atoi(custom_strip_size.getValue()), 0, MAXLEDS); #if !defined(USE_WS2812FX_DMA) checkPin(atoi(custom_led_pin.getValue())); #endif @@ -557,8 +556,6 @@ void setup() { } #endif #endif - - initStrip(); //if you get here you have connected to the WiFi DBG_OUTPUT_PORT.println("connected...yeey :)"); @@ -618,6 +615,8 @@ void setup() { initMqtt(); #endif + initStrip(); + #if ENABLE_MQTT == 1 wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect); wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect); @@ -667,7 +666,7 @@ void setup() { DBG_OUTPUT_PORT.println(F("*** e131.begin failed ***")); } #endif - DBG_OUTPUT_PORT.println("You are here!: 1117"); + prevmode = mode; #if defined(ENABLE_BUTTON_GY33) diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index e2154e4..2d82946 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -2,7 +2,8 @@ // Neopixel #define LED_PIN 3 // PIN (15 / D8) where neopixel / WS2811 strip is attached; is configurable, if USE_WS2812FX_DMA is not defined. Just for the start -#define NUMLEDS 144 // Number of leds in the; is configurable just for the start +#define NUMLEDS 50 // Number of leds in the; is configurable just for the start +#define MAXLEDS 700 // due to memory limit of esp8266 at the moment only 700 leds are supported. #define RGBORDER "GRBW" // RGBOrder; is configurable just for the start #define FX_OPTIONS 56 // ws2812fx Options 56 = SIZE_SMALL + FADE_MEDIUM + GAMMA is configurable just for the start; for WS2812FX setSegment OPTIONS, see: https://github.com/kitesurfer1404/WS2812FX/blob/master/extras/WS2812FX%20Users%20Guide.md //#define LED_TYPE_WS2811 // Uncomment, if LED type uses 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) @@ -30,8 +31,8 @@ char HOSTNAME[65] = "McLightingRGBW_02"; // Friedly hostname is configurable #if defined(ENABLE_E131) #define MULTICAST false #define START_UNIVERSE 1 // First DMX Universe to listen for - #define END_UNIVERSE 2 // Total number of Universes to listen for, starting at UNIVERSE - // MUST: END_UNIVERSE >= START_UNIVERSE + uint8_t END_UNIVERSE = 1; // Total number of Universes to listen for, starting at UNIVERSE + #endif #if defined(ENABLE_REMOTE) @@ -140,17 +141,17 @@ int ws2812fx_mode = 0; // Global variable for storing the WS2812FX modes bool shouldSaveConfig = false; // For WiFiManger custom config -uint32_t hex_colors[3] = {}; // Color array for setting WS2812FX +uint32_t hex_colors[3] = {}; // Color array for setting WS2812FX struct ledstate // Data structure to store a state of a single led { uint8_t red; uint8_t green; uint8_t blue; - uint8_t white; + uint8_t white; // is abused for tv_mode and custom animations as it is already here and white channel is not used there. }; -typedef struct ledstate LEDState; // Define the datatype LEDState -LEDState ledstates[NUMLEDS]; // Get an array of led states to store the state of the whole strip +typedef struct ledstate LEDState; // Define the datatype LEDState +uint8_t* ledstates; // Set a pointer to get an array of led states to store the state of the whole strip LEDState main_color = { 255, 0, 0, 0 }; // Store the "main color" of the strip used in single color modes LEDState back_color = { 0, 0, 0, 0 }; // Store the "2nd color" of the strip used in single color modes LEDState xtra_color = { 0, 0, 0, 0 }; // Store the "3rd color" of the strip used in single color modes diff --git a/Arduino/McLighting/mode_custom_ws2812fx_animations.h b/Arduino/McLighting/mode_custom_ws2812fx_animations.h index 66db560..460f50b 100644 --- a/Arduino/McLighting/mode_custom_ws2812fx_animations.h +++ b/Arduino/McLighting/mode_custom_ws2812fx_animations.h @@ -9,7 +9,7 @@ More info on how to create custom aniamtions for WS2812FX: https://github.com/ki #include //https://github.com/FastLED/FastLED -byte* heat; +//byte* heat; /* * paste in the Fire2012 code with a small edit at the end which uses the @@ -61,30 +61,30 @@ void Fire2012() { // Step 1. Cool down every cell a little for( int i = 0; i < WS2812FXStripSettings.stripSize; i++) { - heat[i] = qsub8( heat[i], random8(0, ((COOLING * 10) / WS2812FXStripSettings.stripSize) + 2)); + ledstates[i] = qsub8( ledstates[i], random8(0, ((COOLING * 10) / WS2812FXStripSettings.stripSize) + 2)); } // Step 2. Heat from each cell drifts 'up' and diffuses a little for( int k= WS2812FXStripSettings.stripSize - 1; k >= 2; k--) { - heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2] ) / 3; + ledstates[k] = (ledstates[k - 1] + ledstates[k - 2] + ledstates[k - 2]) / 3; } // Step 3. Randomly ignite new 'sparks' of heat near the bottom if( random8() < SPARKING ) { int y = random8(7); - heat[y] = qadd8( heat[y], random8(160,255) ); + ledstates[y] = qadd8(ledstates[y], random8(160,255) ); } // Step 4. Map from heat cells to LED colors for( int j = 0; j < WS2812FXStripSettings.stripSize; j++) { - CRGB color = HeatColor( heat[j]); + CRGB color = HeatColor( ledstates[j]); int pixelnumber; if( gReverseDirection ) { pixelnumber = (WS2812FXStripSettings.stripSize - 1) - j; } else { pixelnumber = j; } - strip->setPixelColor(pixelnumber, color.red, color.green, color.blue); + strip->setPixelColor(pixelnumber, color.red, color.green, color.blue, 0); } } diff --git a/Arduino/McLighting/mode_tv.h b/Arduino/McLighting/mode_tv.h index a069b42..466769a 100644 --- a/Arduino/McLighting/mode_tv.h +++ b/Arduino/McLighting/mode_tv.h @@ -15,7 +15,6 @@ uint8_t twitch = 50; uint8_t dipCount = 0; uint8_t analogLevel = 100; boolean timeToDip = false; -uint8_t* ledStates; void hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright, uint8_t myled) { @@ -29,10 +28,10 @@ void hsb2rgbAN1(uint16_t index, uint8_t sat, uint8_t bright, uint8_t myled) { void updateLed (uint16_t led, uint8_t brightness) { - ledStates[led] = brightness; + ledstates[led] = brightness; for (int i=0; ishow(); } @@ -50,7 +49,7 @@ void handleTV() { dipCount = dipCount++; } if(currentMillis-previousMillisnumPixels()-1)); + led=random(0, WS2812FXStripSettings.stripSize - 1); analogLevel=random(50,255);// set the range of the 3 pwm leds ledState = ledState == LOW ? HIGH: LOW; // if the LED is off turn it on and vice-versa: updateLed(led, (ledState) ? 255 : 0); @@ -67,7 +66,7 @@ void handleTV() { DBG_OUTPUT_PORT.println("Dip Time"); currentDipTime = millis(); if (currentDipTime - dipStartTime < darkTime) { - for (int i=3; inumPixels(); i++) { + for (int i=3; i %s)\r\n", led, strip->numPixels(), mypayload ); - if (led <= strip->numPixels()) { + DBG_OUTPUT_PORT.printf("led value: [%i]. Entry threshold: <= [%i] (=> %s)\r\n", led, WS2812FXStripSettings.stripSize, mypayload ); + if (led <= WS2812FXStripSettings.stripSize) { char redhex[3]; char greenhex[3]; char bluehex[3]; @@ -223,6 +224,8 @@ void handleSetSingleLED(uint8_t * mypayload, uint8_t firstChar = 0) { redhex[2] = 0x00; greenhex[2] = 0x00; bluehex[2] = 0x00; + whitehex[2] = 0x00; + /* ledstates[led].red = strtol(redhex, NULL, 16); ledstates[led].green = strtol(greenhex, NULL, 16); ledstates[led].blue = strtol(bluehex, NULL, 16); @@ -231,6 +234,16 @@ void handleSetSingleLED(uint8_t * mypayload, uint8_t firstChar = 0) { DBG_OUTPUT_PORT.printf("rgb.red: [%i] rgb.green: [%i] rgb.blue: [%i] rgb.white: [%i]\r\n", strtol(redhex, NULL, 16), strtol(greenhex, NULL, 16), strtol(bluehex, NULL, 16), strtol(whitehex, NULL, 16)); DBG_OUTPUT_PORT.printf("WS: Set single led [%i] to [%i] [%i] [%i] [%i] (%s)!\r\n", led, ledstates[led].red, ledstates[led].green, ledstates[led].blue, ledstates[led].white, mypayload); strip->setPixelColor(led, ledstates[led].red, ledstates[led].green, ledstates[led].blue, ledstates[led].white); + */ + LEDState color; + color.red = strtol(redhex, NULL, 16); + color.green = strtol(greenhex, NULL, 16); + color.blue = strtol(bluehex, NULL, 16); + color.white = strtol(whitehex, NULL, 16); + //DBG_OUTPUT_PORT.printf("rgb.red: [%s] rgb.green: [%s] rgb.blue: [%s] rgb.white: [%s]\r\n", redhex, greenhex, bluehex, whitehex); + //DBG_OUTPUT_PORT.printf("rgb.red: [%i] rgb.green: [%i] rgb.blue: [%i] rgb.white: [%i]\r\n", strtol(redhex, NULL, 16), strtol(greenhex, NULL, 16), strtol(bluehex, NULL, 16), strtol(whitehex, NULL, 16)); + //DBG_OUTPUT_PORT.printf("WS: Set single led [%i] to [%i] [%i] [%i] [%i] (%s)!\r\n", led, color.red, color.green, color.blue, color.white, mypayload); + strip->setPixelColor(led, color.red, color.green, color.blue, color.white); strip->show(); } mode = CUSTOM; @@ -252,26 +265,22 @@ void handleRangeDifferentColors(uint8_t * mypayload) { while (nextCommand) { // Loop for each LED. - char startled[4] = { 0, 0, 0 }; - char endled[4] = { 0, 0, 0 }; - char colorval[9] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + char startled[4]; + char endled[4]; + char colorval[9]; strncpy ( startled, (const char *) &nextCommand[0], 2 ); + startled[3] = 0x00; strncpy ( endled, (const char *) &nextCommand[2], 2 ); + endled[3] = 0x00; strncpy ( colorval, (const char *) &nextCommand[4], 8 ); - int rangebegin = atoi(startled); - int rangeend = atoi(endled); + colorval[8] = 0x00; + uint8_t rangebegin = atoi(startled); + uint8_t rangeend = atoi(endled); DBG_OUTPUT_PORT.printf("Setting RANGE from [%i] to [%i] as color [%s] \r\n", rangebegin, rangeend, colorval); while ( rangebegin <= rangeend ) { - char rangeData[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - if ( rangebegin < 10 ) { - // Create the valid 'nextCommand' structure - snprintf(rangeData, sizeof(rangeData), "0%d%s", rangebegin, colorval); - } - if ( rangebegin >= 10 ) { - // Create the valid 'nextCommand' structure - snprintf(rangeData, sizeof(rangeData), "%d%s", rangebegin, colorval); - } + char rangeData[11]; + snprintf(rangeData, sizeof(rangeData), "%02d%s", rangebegin, colorval); // Set one LED handleSetSingleLED((uint8_t*) rangeData, 0); rangebegin++; @@ -374,7 +383,7 @@ bool setConfByConfString(String saved_conf_string) { getValue(saved_conf_string, '|', 4).toCharArray(mqtt_user, 32); getValue(saved_conf_string, '|', 5).toCharArray(mqtt_pass, 32); #endif - WS2812FXStripSettings.stripSize = getValue(saved_conf_string, '|', 6).toInt(); + WS2812FXStripSettings.stripSize = constrain(getValue(saved_conf_string, '|', 6).toInt(), 0, MAXLEDS); checkPin(getValue(saved_conf_string, '|', 7).toInt()); char tmp_rgbOrder[5]; getValue(saved_conf_string, '|', 8).toCharArray(tmp_rgbOrder, 4); @@ -816,18 +825,21 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { if (payload[2] == 'c') { char tmp_count[6]; snprintf(tmp_count, sizeof(tmp_count), "%s", &payload[3]); - WS2812FXStripSettings.stripSize = constrain(atoi(tmp_count), 0, 65535); + tmp_count[5] = 0x00; + WS2812FXStripSettings.stripSize = constrain(atoi(tmp_count), 0, MAXLEDS); updateStrip = true; } if (payload[2] == 'r') { char tmp_rgbOrder[5]; snprintf(tmp_rgbOrder, sizeof(tmp_rgbOrder), "%s", &payload[3]); + tmp_rgbOrder[4] = 0x00; checkRGBOrder(tmp_rgbOrder); updateStrip=true; } #if !defined(USE_WS2812FX_DMA) if (payload[2] == 'p') { char tmp_pin[3]; + tmp_pin[2] = 0x00; snprintf(tmp_pin, sizeof(tmp_pin), "%s", &payload[3]); checkPin(atoi(tmp_pin)); updateStrip = true; @@ -837,6 +849,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { if (payload[2] == 'o') { char tmp_fxoptions[4]; snprintf(tmp_fxoptions, sizeof(tmp_fxoptions), "%s", &payload[3]); + tmp_fxoptions[3] = 0x00; WS2812FXStripSettings.fxoptions = constrain(atoi(tmp_fxoptions), 0, 255); updateStrip = true; } @@ -886,6 +899,7 @@ void checkpayload(uint8_t * payload, bool mqtt = false, uint8_t num = 0) { #else snprintf(last_conf, sizeof(last_conf), "CNF|%64s|%64s|%5d|%32s|%32s|%4d|%2d|%4s|%3d", HOSTNAME, "", "", "", "", WS2812FXStripSettings.stripSize, WS2812FXStripSettings.pin, WS2812FXStripSettings.RGBOrder, WS2812FXStripSettings.fxoptions); #endif + last_conf[sizeof(last_conf)-1]= 0x00; writeEEPROM(0, 222, last_conf); EEPROM.commit(); } @@ -1627,7 +1641,7 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght strcpy(mqtt_user, json["mqtt_user"]); strcpy(mqtt_pass, json["mqtt_pass"]); #endif - WS2812FXStripSettings.stripSize = (uint8_t) json["ws_cnt"]; + WS2812FXStripSettings.stripSize = constrain ((uint8_t) json["ws_cnt"],0, MAXLEDS); char tmp_rgbOrder[5]; strcpy(tmp_rgbOrder, json["ws_rgbo"]); checkRGBOrder(tmp_rgbOrder); diff --git a/Arduino/McLighting/version_info.ino b/Arduino/McLighting/version_info.ino index ddd88b9..dc0dd17 100644 --- a/Arduino/McLighting/version_info.ino +++ b/Arduino/McLighting/version_info.ino @@ -129,4 +129,7 @@ * 17 Mar 2019 * adressed issue: #2 * adressed issue: #3 + * + * 18 Mar 2019 + * adressed issue: #6 (possibly affects R[r_start][r_end][hexrgb] [...]; +[numled][hexrgb]+[numled][hexrgb]+[numled][hexrgb] !) */