mirror of
https://github.com/esphome/esphome.git
synced 2026-06-01 09:25:09 +08:00
[sensor] Use std::array in CalibrateLinearFilter (#15263)
This commit is contained in:
@@ -770,7 +770,9 @@ async def calibrate_linear_filter_to_code(config, filter_id):
|
||||
linear_functions = [[k, b, float("NaN")]]
|
||||
elif config[CONF_METHOD] == "exact":
|
||||
linear_functions = map_linear(x, y)
|
||||
return cg.new_Pvariable(filter_id, linear_functions)
|
||||
return cg.new_Pvariable(
|
||||
filter_id, cg.TemplateArguments(len(linear_functions)), linear_functions
|
||||
)
|
||||
|
||||
|
||||
CONF_DEGREE = "degree"
|
||||
|
||||
@@ -385,13 +385,10 @@ void HeartbeatFilter::setup() {
|
||||
|
||||
float HeartbeatFilter::get_setup_priority() const { return setup_priority::HARDWARE; }
|
||||
|
||||
CalibrateLinearFilter::CalibrateLinearFilter(std::initializer_list<std::array<float, 3>> linear_functions)
|
||||
: linear_functions_(linear_functions) {}
|
||||
|
||||
optional<float> CalibrateLinearFilter::new_value(float value) {
|
||||
for (const auto &f : this->linear_functions_) {
|
||||
if (!std::isfinite(f[2]) || value < f[2])
|
||||
return (value * f[0]) + f[1];
|
||||
optional<float> calibrate_linear_compute(const std::array<float, 3> *functions, size_t count, float value) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (!std::isfinite(functions[i][2]) || value < functions[i][2])
|
||||
return (value * functions[i][0]) + functions[i][1];
|
||||
}
|
||||
return NAN;
|
||||
}
|
||||
|
||||
@@ -528,13 +528,21 @@ template<size_t N> class OrFilter : public Filter {
|
||||
bool has_value_{false};
|
||||
};
|
||||
|
||||
class CalibrateLinearFilter : public Filter {
|
||||
/// Non-template helper for linear calibration (implementation in filter.cpp)
|
||||
optional<float> calibrate_linear_compute(const std::array<float, 3> *functions, size_t count, float value);
|
||||
|
||||
/// N is set by code generation to match the exact number of calibration segments.
|
||||
template<size_t N> class CalibrateLinearFilter : public Filter {
|
||||
public:
|
||||
explicit CalibrateLinearFilter(std::initializer_list<std::array<float, 3>> linear_functions);
|
||||
optional<float> new_value(float value) override;
|
||||
explicit CalibrateLinearFilter(std::initializer_list<std::array<float, 3>> linear_functions) {
|
||||
init_array_from(this->linear_functions_, linear_functions);
|
||||
}
|
||||
optional<float> new_value(float value) override {
|
||||
return calibrate_linear_compute(this->linear_functions_.data(), N, value);
|
||||
}
|
||||
|
||||
protected:
|
||||
FixedVector<std::array<float, 3>> linear_functions_;
|
||||
std::array<std::array<float, 3>, N> linear_functions_{};
|
||||
};
|
||||
|
||||
/// Non-template helper for polynomial calibration (implementation in filter.cpp)
|
||||
|
||||
Reference in New Issue
Block a user