ESPHome  2024.12.2
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  LOG_UPDATE_INTERVAL(this);
27 }
28 
29 float ADCSensor::sample() {
30  uint32_t raw = 0;
31  if (this->output_raw_) {
32  for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
33  raw += analogRead(this->pin_->get_pin()); // NOLINT
34  }
35  raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
36  return raw;
37  }
38  for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
39  raw += analogReadVoltage(this->pin_->get_pin()); // NOLINT
40  }
41  raw = (raw + (this->sample_count_ >> 1)) / this->sample_count_; // NOLINT(clang-analyzer-core.DivideZero)
42  return raw / 1000.0f;
43 }
44 
45 } // namespace adc
46 } // namespace esphome
47 
48 #endif // USE_LIBRETINY
uint8_t raw[35]
Definition: bl0939.h:19
virtual void setup()=0
void setup() override
Setup ADC.
virtual uint8_t get_pin() const =0
InternalGPIOPin * pin_
Definition: adc_sensor.h:68
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
const StringRef & get_name() const
Definition: entity_base.cpp:10