state_manager: Reset alarm state when threshold changes
Previously, the alarm would stay active even if you decreased the
threshold past the current air quality score.
Change-Id: I5a544498ef15a57f6d8834fd69ead3f55a2a32fc
Reviewed-on: https://pigweed-internal-review.git.corp.google.com/c/pigweed/showcase/rp2/+/73628
Reviewed-by: Aaron Green <aarongreen@google.com>
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Presubmit-Verified: CQ Bot Account <pigweed-internal-scoped@luci-project-accounts.iam.gserviceaccount.com>
Commit-Queue: Auto-Submit <auto-submit@pw-internal-service-accounts.iam.gserviceaccount.com>
diff --git a/modules/state_manager/state_manager.cc b/modules/state_manager/state_manager.cc
index 1b1763e..93d3db7 100644
--- a/modules/state_manager/state_manager.cc
+++ b/modules/state_manager/state_manager.cc
@@ -107,15 +107,21 @@
}
void StateManager::SetAlarmThreshold(uint16_t alarm_threshold) {
+ alarm_ = false; // Reset the alarm whenever the threshold changes.
+
alarm_threshold_ = alarm_threshold;
auto silence_threshold =
static_cast<uint16_t>(alarm_threshold_ + kThresholdIncrement);
+
+ // Set the thresholds and reset the edge detector to a good air quality state.
edge_detector_.set_low_and_high_thresholds(alarm_threshold_,
silence_threshold);
+ edge_detector_.Update(AirSensor::kMaxScore);
+
PW_LOG_INFO("Air quality thresholds set: alarm at %u, silence at %u",
alarm_threshold_,
silence_threshold);
- edge_detector_.Update(AirSensor::kMaxScore);
+
}
void StateManager::UpdateAirQuality(uint16_t score) {