mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 16:14:11 +08:00
[ESP32] ADC auto-range setting (#2541)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -18,6 +18,7 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||
#ifdef USE_ESP32
|
||||
/// Set the attenuation for this pin. Only available on the ESP32.
|
||||
void set_attenuation(adc_atten_t attenuation);
|
||||
void set_autorange(bool autorange);
|
||||
#endif
|
||||
|
||||
/// Update adc values.
|
||||
@@ -36,9 +37,12 @@ class ADCSensor : public sensor::Sensor, public PollingComponent, public voltage
|
||||
|
||||
protected:
|
||||
InternalGPIOPin *pin_;
|
||||
uint16_t read_raw_();
|
||||
uint32_t raw_to_microvolts_(uint16_t raw);
|
||||
|
||||
#ifdef USE_ESP32
|
||||
adc_atten_t attenuation_{ADC_ATTEN_DB_0};
|
||||
bool autorange_{false};
|
||||
#endif
|
||||
};
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ ATTENUATION_MODES = {
|
||||
"2.5db": cg.global_ns.ADC_ATTEN_DB_2_5,
|
||||
"6db": cg.global_ns.ADC_ATTEN_DB_6,
|
||||
"11db": cg.global_ns.ADC_ATTEN_DB_11,
|
||||
"auto": "auto",
|
||||
}
|
||||
|
||||
|
||||
@@ -92,4 +93,7 @@ async def to_code(config):
|
||||
cg.add(var.set_pin(pin))
|
||||
|
||||
if CONF_ATTENUATION in config:
|
||||
cg.add(var.set_attenuation(config[CONF_ATTENUATION]))
|
||||
if config[CONF_ATTENUATION] == "auto":
|
||||
cg.add(var.set_autorange(cg.global_ns.true))
|
||||
else:
|
||||
cg.add(var.set_attenuation(config[CONF_ATTENUATION]))
|
||||
|
||||
Reference in New Issue
Block a user