Respect values over 11.1s.
This commit is contained in:
@@ -2,6 +2,30 @@
|
||||
|
||||
namespace trbc {
|
||||
|
||||
namespace {
|
||||
|
||||
uint16_t highestRepresentableAtOrBelow(uint16_t valueMs) {
|
||||
if (valueMs <= kTasmotaTenthsLimitMs) {
|
||||
return clampTimerMs((valueMs / kTimerStepMs) * kTimerStepMs);
|
||||
}
|
||||
if (valueMs < kTasmotaSecondsStartMs) {
|
||||
return kTasmotaTenthsLimitMs;
|
||||
}
|
||||
return clampTimerMs((valueMs / 1000) * 1000);
|
||||
}
|
||||
|
||||
uint16_t lowestRepresentableAtOrAbove(uint16_t valueMs) {
|
||||
if (valueMs <= kTasmotaTenthsLimitMs) {
|
||||
return clampTimerMs(((valueMs + kTimerStepMs - 1) / kTimerStepMs) * kTimerStepMs);
|
||||
}
|
||||
if (valueMs < kTasmotaSecondsStartMs) {
|
||||
return clampTimerMs(kTasmotaSecondsStartMs);
|
||||
}
|
||||
return clampTimerMs(((valueMs + 999) / 1000) * 1000);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
uint16_t clampTimerMs(int valueMs) {
|
||||
if (valueMs < kMinTimerMs) {
|
||||
return kMinTimerMs;
|
||||
@@ -12,13 +36,70 @@ uint16_t clampTimerMs(int valueMs) {
|
||||
return static_cast<uint16_t>(valueMs);
|
||||
}
|
||||
|
||||
uint16_t normalizeTimerMs(int valueMs) {
|
||||
const uint16_t clamped = clampTimerMs(valueMs);
|
||||
if (clamped <= kTasmotaTenthsLimitMs) {
|
||||
uint16_t rounded = ((clamped + (kTimerStepMs / 2)) / kTimerStepMs) * kTimerStepMs;
|
||||
if (rounded > kTasmotaTenthsLimitMs) {
|
||||
rounded = kTasmotaTenthsLimitMs;
|
||||
}
|
||||
return clampTimerMs(rounded);
|
||||
}
|
||||
|
||||
uint16_t rounded = ((clamped + 500) / 1000) * 1000;
|
||||
if (rounded < kTasmotaSecondsStartMs) {
|
||||
rounded = kTasmotaSecondsStartMs;
|
||||
}
|
||||
if (rounded > kMaxTimerMs) {
|
||||
return highestRepresentableAtOrBelow(kMaxTimerMs);
|
||||
}
|
||||
if (rounded < kMinTimerMs) {
|
||||
return lowestRepresentableAtOrAbove(kMinTimerMs);
|
||||
}
|
||||
return rounded;
|
||||
}
|
||||
|
||||
uint16_t adjustTimerMs(uint16_t currentMs, int ticks) {
|
||||
const int adjusted = static_cast<int>(currentMs) + ticks * kTimerStepMs;
|
||||
return clampTimerMs(adjusted);
|
||||
uint16_t adjusted = normalizeTimerMs(currentMs);
|
||||
const int direction = ticks < 0 ? -1 : 1;
|
||||
|
||||
for (int remaining = ticks < 0 ? -ticks : ticks; remaining > 0; remaining--) {
|
||||
uint16_t next = adjusted;
|
||||
if (direction > 0) {
|
||||
if (adjusted < kTasmotaTenthsLimitMs) {
|
||||
next = adjusted + kTimerStepMs;
|
||||
} else if (adjusted < kTasmotaSecondsStartMs) {
|
||||
next = kTasmotaSecondsStartMs;
|
||||
} else {
|
||||
next = adjusted + 1000;
|
||||
}
|
||||
if (next > kMaxTimerMs || next < adjusted) {
|
||||
return highestRepresentableAtOrBelow(kMaxTimerMs);
|
||||
}
|
||||
} else {
|
||||
if (adjusted > kTasmotaSecondsStartMs) {
|
||||
next = adjusted - 1000;
|
||||
} else if (adjusted > kTasmotaTenthsLimitMs) {
|
||||
next = kTasmotaTenthsLimitMs;
|
||||
} else {
|
||||
next = adjusted - kTimerStepMs;
|
||||
}
|
||||
if (next < kMinTimerMs || next > adjusted) {
|
||||
return lowestRepresentableAtOrAbove(kMinTimerMs);
|
||||
}
|
||||
}
|
||||
adjusted = next;
|
||||
}
|
||||
|
||||
return adjusted;
|
||||
}
|
||||
|
||||
uint16_t tasmotaPulseTimeValue(uint16_t timerMs) {
|
||||
return clampTimerMs(timerMs) / kTimerStepMs;
|
||||
const uint16_t normalized = normalizeTimerMs(timerMs);
|
||||
if (normalized <= kTasmotaTenthsLimitMs) {
|
||||
return normalized / kTimerStepMs;
|
||||
}
|
||||
return (normalized / 1000) + 100;
|
||||
}
|
||||
|
||||
} // namespace trbc
|
||||
|
||||
@@ -20,7 +20,7 @@ constexpr unsigned long kWifiRetryMs = 10000;
|
||||
constexpr unsigned long kButtonDebounceMs = 35;
|
||||
constexpr unsigned long kStatusHoldMs = 2500;
|
||||
|
||||
uint16_t timerMs = trbc::clampTimerMs(INITIAL_TIMER_MS);
|
||||
uint16_t timerMs = trbc::normalizeTimerMs(INITIAL_TIMER_MS);
|
||||
bool needsDisplayUpdate = true;
|
||||
unsigned long lastDisplayRefresh = 0;
|
||||
unsigned long lastWifiAttempt = 0;
|
||||
|
||||
Reference in New Issue
Block a user