ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
adc_sensor_libretiny.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
3#include "adc_sensor.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace adc {
8
9static const char *const TAG = "adc.libretiny";
10
11void ADCSensor::setup() {
12#ifndef USE_ADC_SENSOR_VCC
13 this->pin_->setup();
14#endif // !USE_ADC_SENSOR_VCC
15}
16
18 LOG_SENSOR("", "ADC Sensor", this);
19#ifdef USE_ADC_SENSOR_VCC
20 ESP_LOGCONFIG(TAG, " Pin: VCC");
21#else // USE_ADC_SENSOR_VCC
22 LOG_PIN(" Pin: ", this->pin_);
23#endif // USE_ADC_SENSOR_VCC
24 ESP_LOGCONFIG(TAG,
25 " Samples: %i\n"
26 " Sampling mode: %s",
27 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
28 LOG_UPDATE_INTERVAL(this);
29}
30
31float ADCSensor::sample() {
32 uint32_t raw = 0;
33 auto aggr = Aggregator<uint32_t>(this->sampling_mode_);
34
35 if (this->output_raw_) {
36 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
37 raw = analogRead(this->pin_->get_pin()); // NOLINT
38 aggr.add_sample(raw);
39 }
40 return aggr.aggregate();
41 }
42
43 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
44 raw = analogReadVoltage(this->pin_->get_pin()); // NOLINT
45 aggr.add_sample(raw);
46 }
47
48 return aggr.aggregate() / 1000.0f;
49}
50
51} // namespace adc
52} // namespace esphome
53
54#endif // USE_LIBRETINY
uint8_t raw[35]
Definition bl0939.h:0
virtual void setup()=0
virtual uint8_t get_pin() const =0
float sample() override
Perform a single ADC sampling operation and return the measured value.
void setup() override
Set up the ADC sensor by initializing hardware and calibration parameters.
InternalGPIOPin * pin_
Definition adc_sensor.h:138
void dump_config() override
Output the configuration details of the ADC sensor for debugging purposes.
SamplingMode sampling_mode_
Definition adc_sensor.h:139
const LogString * sampling_mode_to_str(SamplingMode mode)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7