SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
median.cpp
Go to the documentation of this file.
1#include "median.h"
2
3namespace sensesp {
4
6 : FloatTransform(config_path), sample_size_{sample_size} {
8 buf_.reserve(sample_size_);
9 buf_.clear();
10}
11
13 buf_.push_back(input);
14 if (buf_.size() >= sample_size_) {
15 // Its time to output a value
16 sort(buf_.begin(), buf_.end());
17 int mid = sample_size_ / 2;
18 output = buf_[mid];
19 buf_.clear();
20 notify();
21 }
22}
23
25 root["sample_size"] = sample_size_;
26}
27
28static const char SCHEMA[] PROGMEM = R"({
29 "type": "object",
30 "properties": {
31 "sample_size": { "title": "Sample size", "description": "Number of samples to take before outputing a value", "type": "integer" }
32 }
33 })";
34
36
38 String expected[] = {"sample_size"};
39 for (auto str : expected) {
40 if (!config.containsKey(str)) {
41 return false;
42 }
43 }
44 unsigned int sample_size_new = config["sample_size"];
45 if (sample_size_ != sample_size_new) {
46 sample_size_ = sample_size_new;
47 buf_.reserve(sample_size_);
48 buf_.clear();
49 }
50 return true;
51}
52
53} // namespace sensesp
virtual void load_configuration()
Construct a new transform based on a single function.
virtual void get_configuration(JsonObject &doc) override
Definition median.cpp:24
virtual String get_config_schema() override
Definition median.cpp:35
Median(unsigned int sample_size=10, String config_path="")
Definition median.cpp:5
virtual bool set_configuration(const JsonObject &config) override
Definition median.cpp:37
virtual void set_input(float input, uint8_t input_channel=0) override
Definition median.cpp:12
const uint8_t PAGE_css_bootstrap[] PROGMEM