modules: Use [[nodicard]]; include cleanups
- [[nodiscard]] on HysteresisEdgeDetector::Update() to help avoid missed
edges.
- Include and deps fixes
- Use static_cast instead of C-style cast.
Change-Id: I1083608d92e5e8d551727b45ca065fb617a67125
Reviewed-on: https://pigweed-internal-review.git.corp.google.com/c/pigweed/showcase/rp2/+/73609
Pigweed-Auto-Submit: Wyatt Hepler <hepler@google.com>
Commit-Queue: Auto-Submit <auto-submit@pw-internal-service-accounts.iam.gserviceaccount.com>
Reviewed-by: Aaron Green <aarongreen@google.com>
diff --git a/device/bme688.cc b/device/bme688.cc
index a8d0597..9fc5d21 100644
--- a/device/bme688.cc
+++ b/device/bme688.cc
@@ -44,7 +44,7 @@
void* context) {
PW_LOG_INFO("Write(reg_address=0x%02x, data=%p, length=%u, context=%p)",
reg_address,
- (const void*)data,
+ static_cast<const void*>(data),
length,
context);
auto i2c_device = static_cast<pw::i2c::RegisterDevice*>(context);
@@ -63,7 +63,7 @@
void* context) {
PW_LOG_INFO("Read(reg_address=0x%02x, data=%p, length=%u, context=%p)",
reg_address,
- (const void*)data,
+ static_cast<const void*>(data),
length,
context);
auto i2c_device = static_cast<pw::i2c::RegisterDevice*>(context);
diff --git a/modules/edge_detector/BUILD.bazel b/modules/edge_detector/BUILD.bazel
index 181b07b..1d64557 100644
--- a/modules/edge_detector/BUILD.bazel
+++ b/modules/edge_detector/BUILD.bazel
@@ -24,7 +24,7 @@
srcs = ["hysteresis_edge_detector.cc"],
hdrs = ["hysteresis_edge_detector.h"],
implementation_deps = ["@pigweed//pw_log"],
- deps = ["@pigweed//pw_function"],
+ deps = ["@pigweed//pw_assert"],
)
cc_library(
diff --git a/modules/edge_detector/hysteresis_edge_detector.h b/modules/edge_detector/hysteresis_edge_detector.h
index 5bd1fb4..f7cca26 100644
--- a/modules/edge_detector/hysteresis_edge_detector.h
+++ b/modules/edge_detector/hysteresis_edge_detector.h
@@ -14,7 +14,6 @@
#pragma once
#include "pw_assert/assert.h"
-#include "pw_function/function.h"
namespace sense {
@@ -77,15 +76,18 @@
PW_ASSERT(low_threshold_ <= high_threshold_);
}
- /// Sets the low and high thresholds, inclusive.
- void set_low_and_high_thresholds(Sample low_threshold, Sample high_threshold) {
+ /// Sets the low and high thresholds, inclusive. Resets the internal state.
+ void set_low_and_high_thresholds(Sample low_threshold,
+ Sample high_threshold) {
PW_ASSERT(low_threshold_ <= high_threshold_);
low_threshold_ = low_threshold;
high_threshold_ = high_threshold;
ResetState();
}
- Edge Update(Sample sample) {
+ /// Adds a new sample to the edge detector. Returns whether the sample crossed
+ /// below the lower threshold or above the upper threshold.
+ [[nodiscard]] Edge Update(Sample sample) {
if (sample <= low_threshold_) {
return UpdateLowSample();
}
diff --git a/modules/state_manager/state_manager.cc b/modules/state_manager/state_manager.cc
index 93d3db7..eddc5d4 100644
--- a/modules/state_manager/state_manager.cc
+++ b/modules/state_manager/state_manager.cc
@@ -15,6 +15,7 @@
#include "modules/state_manager/state_manager.h"
#include <cmath>
+#include <variant>
#include "pw_assert/check.h"
#include "pw_log/log.h"
@@ -116,12 +117,11 @@
// 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);
+ std::ignore = edge_detector_.Update(AirSensor::kMaxScore);
PW_LOG_INFO("Air quality thresholds set: alarm at %u, silence at %u",
alarm_threshold_,
silence_threshold);
-
}
void StateManager::UpdateAirQuality(uint16_t score) {
@@ -153,7 +153,7 @@
void StateManager::SilenceAlarms() {
alarm_ = false;
alarm_silenced_ = true;
- edge_detector_.Update(AirSensor::kMaxScore);
+ std::ignore = edge_detector_.Update(AirSensor::kMaxScore);
pubsub_.Publish(TimerRequest{
.token = kSilenceAlarmToken,
.timeout_s = kSilenceAlarmTimeout,
diff --git a/modules/state_manager/state_manager.h b/modules/state_manager/state_manager.h
index a1079dd..82fa58c 100644
--- a/modules/state_manager/state_manager.h
+++ b/modules/state_manager/state_manager.h
@@ -13,7 +13,9 @@
// the License.
#pragma once
-#include <cmath>
+#include <optional>
+#include <string_view>
+#include <utility>
#include "modules/air_sensor/air_sensor.h"
#include "modules/edge_detector/hysteresis_edge_detector.h"