ESPHome 2026.5.0b1
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::adc {
7
8static const char *const TAG = "adc.libretiny";
9
10void ADCSensor::setup() {
11#ifndef USE_ADC_SENSOR_VCC
12 this->pin_->setup();
13#endif // !USE_ADC_SENSOR_VCC
14}
15
17 LOG_SENSOR("", "ADC Sensor", this);
18#ifdef USE_ADC_SENSOR_VCC
19 ESP_LOGCONFIG(TAG, " Pin: VCC");
20#else // USE_ADC_SENSOR_VCC
21 LOG_PIN(" Pin: ", this->pin_);
22#endif // USE_ADC_SENSOR_VCC
23 ESP_LOGCONFIG(TAG,
24 " Samples: %i\n"
25 " Sampling mode: %s",
26 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
27 LOG_UPDATE_INTERVAL(this);
28}
29
30float ADCSensor::sample() {
31 uint32_t raw = 0;
32 auto aggr = Aggregator<uint32_t>(this->sampling_mode_);
33
34 if (this->output_raw_) {
35 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
36 raw = analogRead(this->pin_->get_pin()); // NOLINT
37 aggr.add_sample(raw);
38 }
39 return aggr.aggregate();
40 }
41
42 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
43 raw = analogReadVoltage(this->pin_->get_pin()); // NOLINT
44 aggr.add_sample(raw);
45 }
46
47 return aggr.aggregate() / 1000.0f;
48}
49
50} // namespace esphome::adc
51
52#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:133
void dump_config() override
Output the configuration details of the ADC sensor for debugging purposes.
SamplingMode sampling_mode_
Definition adc_sensor.h:134
const LogString * sampling_mode_to_str(SamplingMode mode)
static void uint32_t