From bc249379f070ece81f7b75da680d477875296e56 Mon Sep 17 00:00:00 2001 From: Frank <91616163+softhack007@users.noreply.github.com> Date: Tue, 9 Apr 2024 15:43:46 +0200 Subject: [PATCH] minor improvements from upstream * add checkSettingsPIN() and get_random_wheel_index() functions * add on/off state to UDP data * small robustness improvements --- wled00/fcn_declare.h | 2 ++ wled00/led.cpp | 8 +++++-- wled00/udp.cpp | 1 + wled00/util.cpp | 55 ++++++++++++++++++++++++++++++++++---------- wled00/wled.h | 2 +- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index dcf4019e..f9dbd8ac 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -370,9 +370,11 @@ void releaseJSONBufferLock(); uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen); uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxLen, uint8_t *var = nullptr); int16_t extractModeDefaults(uint8_t mode, const char *segVar); +void checkSettingsPIN(const char *pin); uint16_t __attribute__((pure)) crc16(const unsigned char* data_p, size_t length); // WLEDMM: added attribute pure um_data_t* simulateSound(uint8_t simulationId); // WLEDMM enumerateLedmaps(); moved to FX.h +uint8_t get_random_wheel_index(uint8_t pos); CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal); //WLEDMM netmindz ar palette char *cleanUpName(char *in); // to clean up a name that was read from file diff --git a/wled00/led.cpp b/wled00/led.cpp index fd334872..0e245c9d 100644 --- a/wled00/led.cpp +++ b/wled00/led.cpp @@ -105,6 +105,7 @@ void stateUpdated(byte callMode) { if (stateChanged) currentPreset = 0; //something changed, so we are no longer in the preset if (callMode != CALL_MODE_NOTIFICATION && callMode != CALL_MODE_NO_NOTIFY) notify(callMode); + if (bri != briOld && nodeBroadcastEnabled) sendSysInfoUDP(); // update on state //set flag to update ws and mqtt interfaceUpdateCallMode = callMode; @@ -193,12 +194,15 @@ void handleTransitions() if (tper >= 1.0f) { strip.setTransitionMode(false); + // restore (global) transition time if not called from UDP notifier or single/temporary transition from JSON (also playlist) + if (jsonTransitionOnce) strip.setTransition(transitionDelay); transitionActive = false; + jsonTransitionOnce = false; tperLast = 0; applyFinalBri(); return; } - if (tper - tperLast < 0.004) return; + if (tper - tperLast < 0.004f) return; tperLast = tper; briT = briOld + ((bri - briOld) * tper); @@ -208,7 +212,7 @@ void handleTransitions() // legacy method, applies values from col, effectCurrent, ... to selected segments -void colorUpdated(byte callMode){ +void colorUpdated(byte callMode) { applyValuesToSelectedSegs(); stateUpdated(callMode); } diff --git a/wled00/udp.cpp b/wled00/udp.cpp index c2395357..0a2d5332 100644 --- a/wled00/udp.cpp +++ b/wled00/udp.cpp @@ -712,6 +712,7 @@ void sendSysInfoUDP() #else data[38] = NODE_TYPE_ID_UNDEFINED; #endif + if (bri) data[38] |= 0x80U; // add on/off state data[39] = ip[3]; // unit ID == last IP number uint32_t build = VERSION; diff --git a/wled00/util.cpp b/wled00/util.cpp index 31f9716c..ceefcc2f 100644 --- a/wled00/util.cpp +++ b/wled00/util.cpp @@ -204,7 +204,7 @@ bool requestJSONBufferLock(uint8_t module) { unsigned long now = millis(); - while (jsonBufferLock && millis()-now < 1200) delay(1); // wait for fraction for buffer lock + while (jsonBufferLock && millis()-now < 1100) delay(1); // wait for fraction for buffer lock if (jsonBufferLock) { USER_PRINT(F("ERROR: Locking JSON buffer failed! (still locked by ")); @@ -239,9 +239,10 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe { if (src == JSON_mode_names || src == nullptr) { if (mode < strip.getModeCount()) { - char lineBuffer[256]; + char lineBuffer[256] = { '\0' }; //strcpy_P(lineBuffer, (const char*)pgm_read_dword(&(WS2812FX::_modeData[mode]))); - strcpy_P(lineBuffer, strip.getModeData(mode)); + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string size_t len = strlen(lineBuffer); size_t j = 0; for (; j < maxLen && j < len; j++) { @@ -253,6 +254,12 @@ uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLe } else return 0; } + if (src == JSON_palette_names && mode > GRADIENT_PALETTE_COUNT) { + snprintf_P(dest, maxLen, PSTR("~ Custom %d~"), 255-mode); + dest[maxLen-1] = '\0'; + return strlen(dest); + } + uint8_t qComma = 0; bool insideQuotes = false; uint8_t printedChars = 0; @@ -363,9 +370,9 @@ uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxL int16_t extractModeDefaults(uint8_t mode, const char *segVar) { if (mode < strip.getModeCount()) { - char lineBuffer[128] = ""; - strncpy_P(lineBuffer, strip.getModeData(mode), 127); - lineBuffer[127] = '\0'; // terminate string + char lineBuffer[256] = { '\0' }; + strncpy_P(lineBuffer, strip.getModeData(mode), sizeof(lineBuffer)/sizeof(char)-1); + lineBuffer[sizeof(lineBuffer)/sizeof(char)-1] = '\0'; // terminate string if (lineBuffer[0] != 0) { char* startPtr = strrchr(lineBuffer, ';'); // last ";" in FX data if (!startPtr) return -1; @@ -381,6 +388,16 @@ int16_t extractModeDefaults(uint8_t mode, const char *segVar) } +void checkSettingsPIN(const char* pin) { + if (!pin) return; + if (!correctPIN && millis() - lastEditTime < PIN_RETRY_COOLDOWN) return; // guard against PIN brute force + bool correctBefore = correctPIN; + correctPIN = (strlen(settingsPIN) == 0 || strncmp(settingsPIN, pin, 4) == 0); + if (correctBefore != correctPIN) createEditHandler(correctPIN); + lastEditTime = millis(); +} + + uint16_t crc16(const unsigned char* data_p, size_t length) { uint8_t x; uint16_t crc = 0xFFFF; @@ -401,9 +418,9 @@ uint16_t crc16(const unsigned char* data_p, size_t length) { // (only 2 used as stored in 1 bit in segment options, consider switching to a single global simulation type) typedef enum UM_SoundSimulations { UMS_BeatSin = 0, - UMS_WeWillRockYou - //UMS_10_13, - //UMS_14_3 + UMS_WeWillRockYou, + UMS_10_13, + UMS_14_3 } um_soundSimulations_t; um_data_t* simulateSound(uint8_t simulationId) @@ -491,7 +508,7 @@ um_data_t* simulateSound(uint8_t simulationId) fftResult[i] = 0; } break; - /*case UMS_10_3: + case UMS_10_13: for (int i = 0; i<16; i++) fftResult[i] = inoise8(beatsin8(90 / (i+1), 0, 200)*15 + (ms>>10), ms>>3); volumeSmth = fftResult[8]; @@ -500,11 +517,11 @@ um_data_t* simulateSound(uint8_t simulationId) for (int i = 0; i<16; i++) fftResult[i] = inoise8(beatsin8(120 / (i+1), 10, 30)*10 + (ms>>14), ms>>3); volumeSmth = fftResult[8]; - break;*/ + break; } samplePeak = random8() > 250; - FFT_MajorPeak = 21 + (volumeSmth*volumeSmth) / 8.0f; // WLEDMM 21hz...8200hz + FFT_MajorPeak = 21 + (volumeSmth*volumeSmth) / 8.0f; // walk thru full range of 21hz...8200hz maxVol = 31; // this gets feedback fro UI binNum = 8; // this gets feedback fro UI volumeRaw = volumeSmth; @@ -545,6 +562,20 @@ CRGB getCRGBForBand(int x, uint8_t *fftResult, int pal) { return value; } +/* + * Returns a new, random color wheel index with a minimum distance of 42 from pos. + */ +uint8_t get_random_wheel_index(uint8_t pos) { + uint8_t r = 0, x = 0, y = 0, d = 0; + while (d < 42) { + r = random8(); + x = abs(pos - r); + y = 255 - x; + d = MIN(x, y); + } + return r; +} + // WLEDMM extended "trim string" function to support enumerateLedmaps // The function takes char* as input, and removes all leading and trailing "decorations" like spaces, tabs, line endings, quotes, colons // The conversion is "in place" (destructive). diff --git a/wled00/wled.h b/wled00/wled.h index 5008c1af..30772955 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2402252 +#define VERSION 2404090 // WLEDMM - you can check for this define in usermods, to only enabled WLEDMM specific code in the "right" fork. Its not defined in AC WLED. #define _MoonModules_WLED_