ESPHome  2025.2.0
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 
6 namespace esphome {
7 namespace adc {
8 
9 static const char *const TAG = "adc.libretiny";
10 
11 void ADCSensor::setup() {
12  ESP_LOGCONFIG(TAG, "Setting up ADC '%s'...", this->get_name().c_str());
13 #ifndef USE_ADC_SENSOR_VCC
14  this->pin_->setup();
15 #endif // !USE_ADC_SENSOR_VCC
16 }
17 
19  LOG_SENSOR("", "ADC Sensor", this);
20 #ifdef USE_ADC_SENSOR_VCC
21  ESP_LOGCONFIG(TAG, " Pin: VCC");
22 #else // USE_ADC_SENSOR_VCC
23  LOG_PIN(" Pin: ", this->pin_);
24 #endif // USE_ADC_SENSOR_VCC
25  ESP_LOGCONFIG(TAG, " Samples: %i", this->sample_count_);
26  ESP_LOGCONFIG(TAG, " Sampling mode: %s", LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
27  LOG_UPDATE_INTERVAL(this);
28 }
29 
30 float ADCSensor::sample() {
31  uint32_t raw = 0;
32  auto aggr = Aggregator(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 adc
51 } // namespace esphome
52 
53 #endif // USE_LIBRETINY
const LogString * sampling_mode_to_str(SamplingMode mode)
uint8_t raw[35]
Definition: bl0939.h:19
SamplingMode sampling_mode_
Definition: adc_sensor.h:87
virtual void setup()=0
void setup() override
Setup ADC.
virtual uint8_t get_pin() const =0
InternalGPIOPin * pin_
Definition: adc_sensor.h:84
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const StringRef & get_name() const
Definition: entity_base.cpp:10