rotary encode (alt): avoid starvation

* for installations with >1000 LEDs, strip.isUpdating() is true almost all the time. This fix ensures that rotary are polled at least every 4 ms (250Hz)
* fixing a few potential string buffer overruns.
This commit is contained in:
Frank
2022-11-19 19:59:37 +01:00
parent c57f75c759
commit 6404bb55ee
2 changed files with 19 additions and 15 deletions

View File

@@ -54,8 +54,8 @@
class RotaryEncoderUIUsermod : public Usermod {
private:
int fadeAmount = 10; // Amount to change every step (brightness)
unsigned long currentTime;
unsigned long loopTime;
unsigned long currentTime = 0;
unsigned long loopTime = 0;
int8_t pinA = ENCODER_DT_PIN; // DT from encoder
int8_t pinB = ENCODER_CLK_PIN; // CLK from encoder
int8_t pinC = ENCODER_SW_PIN; // SW from encoder
@@ -107,6 +107,7 @@ public:
// tracking the owner tags....
pinA = pinB = pinC = -1;
enabled = false;
DEBUG_PRINTLN(F("Failed to alocate GPIO pins for Usermod Rotary Encoder.")); //WLEDMM add debug info
return;
}

View File

@@ -116,7 +116,7 @@ static int re_qstringCmp(const void *ap, const void *bp) {
class RotaryEncoderUIUsermod : public Usermod {
private:
int8_t fadeAmount = 5; // Amount to change every step (brightness)
unsigned long loopTime;
unsigned long loopTime = 0;
unsigned long buttonPressedTime = 0;
unsigned long buttonWaitTime = 0;
@@ -275,6 +275,7 @@ public:
// tracking the owner tags....
pinA = pinB = pinC = -1;
enabled = false;
DEBUG_PRINTLN(F("Failed to alocate GPIO pins for Usermod Rotary Encoder (ALT).")); //WLEDMM add debug info
return;
}
@@ -327,9 +328,11 @@ public:
*/
void loop()
{
if (!enabled || strip.isUpdating()) return;
if (!enabled) return;
unsigned long currentTime = millis(); // get the current elapsed time
if (strip.isUpdating() && (currentTime - loopTime < 4)) return; // WLEDMM: be nice, but not too nice
// Initialize effectCurrentIndex and effectPaletteIndex to
// current state. We do it here as (at least) effectCurrent
// is not yet initialized when setup is called.
@@ -342,7 +345,7 @@ public:
currentEffectAndPaletteInitialized = false;
}
if (currentTime >= (loopTime + 2)) // 2ms since last check of encoder = 500Hz
if (currentTime - loopTime >= 2) // 2ms since last check of encoder = 500Hz
{
loopTime = currentTime; // Updates loopTime
@@ -642,8 +645,8 @@ public:
}
lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY
char lineBuffer[64];
sprintf(lineBuffer, "%d", val);
char lineBuffer[64] = { '\0' };
snprintf(lineBuffer, 63, "%d", val); // WLEDMM: avoid string buffer overflow
display->overlay(lineBuffer, 500, 10); // use star
#endif
}
@@ -702,8 +705,8 @@ public:
}
lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY
char lineBuffer[64];
sprintf(lineBuffer, "%d", currentHue1);
char lineBuffer[64] = { '\0' };
snprintf(lineBuffer, 63, "%d", currentHue1); // WLEDMM: avoid string buffer overflow
display->overlay(lineBuffer, 500, 7); // use brush
#endif
}
@@ -731,8 +734,8 @@ public:
}
lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY
char lineBuffer[64];
sprintf(lineBuffer, "%d", currentSat1);
char lineBuffer[64] = { '\0' };
snprintf(lineBuffer, 63, "%d", currentSat1); // WLEDMM: avoid string buffer overflow
display->overlay(lineBuffer, 500, 8); // use contrast
#endif
}
@@ -748,7 +751,7 @@ public:
#endif
if (presetHigh && presetLow && presetHigh > presetLow) {
StaticJsonDocument<64> root;
char str[64];
char str[64] = { '\0' };
sprintf_P(str, PSTR("%d~%d~%s"), presetLow, presetHigh, increase?"":"-");
root[F("ps")] = str;
deserializeState(root.as<JsonObject>(), CALL_MODE_BUTTON_PRESET);
@@ -763,7 +766,7 @@ public:
*/
lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY
sprintf(str, "%d", currentPreset);
snprintf(str, 63, "%d", currentPreset); // WLEDMM: avoid string buffer overflow
display->overlay(str, 500, 11); // use heart
#endif
}
@@ -791,8 +794,8 @@ public:
// }
lampUdated();
#ifdef USERMOD_FOUR_LINE_DISPLAY
char lineBuffer[64];
sprintf(lineBuffer, "%d", currentCCT);
char lineBuffer[64] = { '\0' };
snprintf(lineBuffer, 63, "%d", currentCCT); // WLEDMM: avoid string buffer overflow
display->overlay(lineBuffer, 500, 10); // use star
#endif
}