[ESP32] ADC auto-range setting (#2541)

This commit is contained in:
Carlos Garcia Saura
2021-10-20 19:44:51 +02:00
committed by GitHub
parent bcc77c73e1
commit e79f7ce290
3 changed files with 110 additions and 60 deletions
File diff suppressed because it is too large Load Diff
+4
View File
@@ -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
};
+5 -1
View File
@@ -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]))