ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
adc_sensor_esp8266.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
2
3#include "adc_sensor.h"
5#include "esphome/core/log.h"
6
7#ifdef USE_ADC_SENSOR_VCC
8#include <Esp.h>
9ADC_MODE(ADC_VCC)
10#else
11#include <Arduino.h>
12#endif // USE_ADC_SENSOR_VCC
13
14namespace esphome {
15namespace adc {
16
17static const char *const TAG = "adc.esp8266";
18
19void ADCSensor::setup() {
20#ifndef USE_ADC_SENSOR_VCC
21 this->pin_->setup();
22#endif
23}
24
26 LOG_SENSOR("", "ADC Sensor", this);
27#ifdef USE_ADC_SENSOR_VCC
28 ESP_LOGCONFIG(TAG, " Pin: VCC");
29#else
30 LOG_PIN(" Pin: ", this->pin_);
31#endif // USE_ADC_SENSOR_VCC
32 ESP_LOGCONFIG(TAG,
33 " Samples: %i\n"
34 " Sampling mode: %s",
35 this->sample_count_, LOG_STR_ARG(sampling_mode_to_str(this->sampling_mode_)));
36 LOG_UPDATE_INTERVAL(this);
37}
38
39float ADCSensor::sample() {
40 auto aggr = Aggregator<uint32_t>(this->sampling_mode_);
41
42 for (uint8_t sample = 0; sample < this->sample_count_; sample++) {
43 uint32_t raw = 0;
44#ifdef USE_ADC_SENSOR_VCC
45 raw = ESP.getVcc(); // NOLINT(readability-static-accessed-through-instance)
46#else
47 raw = analogRead(this->pin_->get_pin()); // NOLINT
48#endif // USE_ADC_SENSOR_VCC
49 aggr.add_sample(raw);
50 }
51
52 if (this->output_raw_) {
53 return aggr.aggregate();
54 }
55 return aggr.aggregate() / 1024.0f;
56}
57
58} // namespace adc
59} // namespace esphome
60
61#endif // USE_ESP8266
ADC_MODE(ADC_VCC) namespace esphome
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