Skip to content

Commit

Permalink
Updating code to cross compile for 4.2 and 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Nov 22, 2020
1 parent cbd6a58 commit a50f16f
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions ESP32AnalogRead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ uint32_t ESP32AnalogRead::readMiliVolts() {
return 0;
analogRead(myPin);
// Configure ADC

adc_unit_t unit;
if (myPin > 27) {
adc1_config_width(ADC_WIDTH_12Bit);
adc1_channel_t chan= ADC1_CHANNEL_0;
unit=ADC_UNIT_1;
switch (myPin) {

case 36:
Expand Down Expand Up @@ -90,6 +91,7 @@ uint32_t ESP32AnalogRead::readMiliVolts() {
adc1_config_channel_atten(chan, ADC_ATTEN_11db);
} else {
adc2_channel_t chan= ADC2_CHANNEL_0;
unit=ADC_UNIT_2;
switch (myPin) {
case 4:
chan = ADC2_CHANNEL_0;
Expand Down Expand Up @@ -124,10 +126,21 @@ uint32_t ESP32AnalogRead::readMiliVolts() {
}
adc2_config_channel_atten(chan, ADC_ATTEN_11db);
}

// Calculate ADC characteristics i.e. gain and offset factors
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12,
#ifdef ESP_IDF_VERSION
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
esp_adc_cal_characterize(unit,
ADC_ATTEN_DB_11,
ADC_WIDTH_BIT_12,
V_REF,
&characteristics);
#endif
#else
esp_adc_cal_get_characteristics(V_REF, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12,&characteristics);
#endif



uint32_t voltage = 0;
// Read ADC and obtain result in mV
esp_adc_cal_get_voltage(channel, &characteristics, &voltage);
Expand Down

0 comments on commit a50f16f

Please sign in to comment.