From 27d300f2273821dad2705c72a540b910f975786a Mon Sep 17 00:00:00 2001 From: Martin Brodbeck Date: Wed, 6 Mar 2024 10:53:16 +0100 Subject: [PATCH] smoothing calcWPM --- src/utils.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/utils.cpp b/src/utils.cpp index 617b7ba..cd1a112 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -8,9 +8,23 @@ uint8_t calcWPM(float percent, uint8_t wpmMin, uint8_t wpmMax) { + static float lastPercent {0}; + static float lastResult {0}; + auto wpm = (percent * (wpmMax - wpmMin) / 100) + wpmMin; uint8_t result = static_cast(std::round(wpm)); - return result; + + float amountChanged = std::abs(percent - lastPercent); + + // Only change WPM if poti value change was big enough + // (Preventing rapid WPM jumps back and forth.) + if (amountChanged > 0.6) { + lastPercent = percent; + lastResult = result; + return result; + } else { + return lastResult; + }; } float potiRead()