ESPHome 2025.8.0b2
Loading...
Searching...
No Matches
adc_sensor_common.cpp
Go to the documentation of this file.
1#include "adc_sensor.h"
2#include "esphome/core/log.h"
3
4namespace esphome {
5namespace adc {
6
7static const char *const TAG = "adc.common";
8
10 switch (mode) {
12 return LOG_STR("average");
14 return LOG_STR("minimum");
16 return LOG_STR("maximum");
17 }
18 return LOG_STR("unknown");
19}
20
22 this->mode_ = mode;
23 // set to max uint if mode is "min"
24 if (mode == SamplingMode::MIN) {
25 this->aggr_ = std::numeric_limits<T>::max();
26 }
27}
28
29template<typename T> void Aggregator<T>::add_sample(T value) {
30 this->samples_ += 1;
31
32 switch (this->mode_) {
34 this->aggr_ += value;
35 break;
36
38 if (value < this->aggr_) {
39 this->aggr_ = value;
40 }
41 break;
42
44 if (value > this->aggr_) {
45 this->aggr_ = value;
46 }
47 }
48}
49
50template<typename T> T Aggregator<T>::aggregate() {
51 if (this->mode_ == SamplingMode::AVG) {
52 if (this->samples_ == 0) {
53 return this->aggr_;
54 }
55
56 return (this->aggr_ + (this->samples_ >> 1)) / this->samples_; // NOLINT(clang-analyzer-core.DivideZero)
57 }
58
59 return this->aggr_;
60}
61
62#ifdef USE_ZEPHYR
63template class Aggregator<int32_t>;
64#else
65template class Aggregator<uint32_t>;
66#endif
67
69 float value_v = this->sample();
70 ESP_LOGV(TAG, "'%s': Voltage=%.4fV", this->get_name().c_str(), value_v);
71 this->publish_state(value_v);
72}
73
74void ADCSensor::set_sample_count(uint8_t sample_count) {
75 if (sample_count != 0) {
76 this->sample_count_ = sample_count;
77 }
78}
79
80void ADCSensor::set_sampling_mode(SamplingMode sampling_mode) { this->sampling_mode_ = sampling_mode; }
81
83
84} // namespace adc
85} // namespace esphome
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
void set_sampling_mode(SamplingMode sampling_mode)
Set the sampling mode for how multiple ADC samples are combined into a single measurement.
void set_sample_count(uint8_t sample_count)
Set the number of samples to be taken for ADC readings to improve accuracy.
float sample() override
Perform a single ADC sampling operation and return the measured value.
void update() override
Update the sensor's state by reading the current ADC value.
SamplingMode sampling_mode_
Definition adc_sensor.h:139
float get_setup_priority() const override
Return the setup priority for this component.
Aggregator(SamplingMode mode)
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:45
const LogString * sampling_mode_to_str(SamplingMode mode)
const float DATA
For components that import data from directly connected sensors like DHT.
Definition component.cpp:50
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7