[number] Clean up NumberCall::perform() increment/decrement logic

Extract cycle_or_clamp_ helper and simplify the increment/decrement
branches by flattening nested conditionals. Use LOG_STR_LITERAL for
cycling log strings.
This commit is contained in:
J. Nick Koston
2026-03-19 20:52:03 -10:00
parent 02ada93ea5
commit 310b50ffc6
2 changed files with 9 additions and 16 deletions
+6 -16
View File
@@ -80,35 +80,25 @@ void NumberCall::perform() {
target_value = max_value;
}
} else if (this->operation_ == NUMBER_OP_INCREMENT) {
ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out");
ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("out"));
if (!parent->has_state()) {
this->log_perform_warning_(LOG_STR("Can't increment, no state"));
return;
}
auto step = traits.get_step();
target_value = parent->state + (std::isnan(step) ? 1 : step);
if (target_value > max_value) {
if (this->cycle_ && !std::isnan(min_value)) {
target_value = min_value;
} else {
target_value = max_value;
}
}
if (target_value > max_value)
target_value = this->cycle_or_clamp_(max_value, min_value);
} else if (this->operation_ == NUMBER_OP_DECREMENT) {
ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out");
ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("out"));
if (!parent->has_state()) {
this->log_perform_warning_(LOG_STR("Can't decrement, no state"));
return;
}
auto step = traits.get_step();
target_value = parent->state - (std::isnan(step) ? 1 : step);
if (target_value < min_value) {
if (this->cycle_ && !std::isnan(max_value)) {
target_value = max_value;
} else {
target_value = min_value;
}
}
if (target_value < min_value)
target_value = this->cycle_or_clamp_(min_value, max_value);
}
if (target_value < min_value) {
+3
View File
@@ -33,6 +33,9 @@ class NumberCall {
NumberCall &with_cycle(bool cycle);
protected:
float cycle_or_clamp_(float clamp, float opposite) const {
return (this->cycle_ && !std::isnan(opposite)) ? opposite : clamp;
}
void log_perform_warning_(const LogString *message);
void log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val,
float limit);