ESPHome 2025.5.0
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 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
30float 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
uint8_t raw[35]
Definition bl0939.h:0
const StringRef & get_name() const
virtual void setup()=0
virtual uint8_t get_pin() const =0
void setup() override
Setup ADC.
InternalGPIOPin * pin_
Definition adc_sensor.h:84
SamplingMode sampling_mode_
Definition adc_sensor.h:87
const LogString * sampling_mode_to_str(SamplingMode mode)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7