2929#include "py/runtime.h"
3030#include "supervisor/shared/translate.h"
3131
32- #include "driver/adc .h"
33- // TODO: Add when ESP-IDF is updated latest version
34- // #include "esp-idf/ components/esp_adc_cal/include/esp_adc_cal.h"
32+ #include "components/ driver/include/driver/adc_common .h"
33+
34+ #include "components/esp_adc_cal/include/esp_adc_cal.h"
3535
3636#include "shared-bindings/microcontroller/Pin.h"
3737
@@ -69,10 +69,9 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
6969 adc2_config_channel_atten ((adc2_channel_t )self -> pin -> adc_channel , ATTENUATION );
7070 }
7171
72- // TODO: esp_adc_cal is only available in the latest version of the esp-idf. Enable when we update.
7372 // Automatically select calibration process depending on status of efuse
74- // esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
75- // esp_adc_cal_characterize(self->pin->adc_index, ATTENUATION, DATA_WIDTH, DEFAULT_VREF, adc_chars);
73+ esp_adc_cal_characteristics_t * adc_chars = calloc (1 , sizeof (esp_adc_cal_characteristics_t ));
74+ esp_adc_cal_characterize (self -> pin -> adc_index , ATTENUATION , DATA_WIDTH , DEFAULT_VREF , adc_chars );
7675
7776 uint32_t adc_reading = 0 ;
7877 //Multisampling
@@ -91,11 +90,8 @@ uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
9190 adc_reading /= NO_OF_SAMPLES ;
9291
9392 // This corrects non-linear regions of the ADC range with a LUT, so it's a better reading than raw
94- // Enable when ESP-IDF is updated
95- //uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars);
96- //return voltage * ((1 << 16) - 1)/3300;
97-
98- return adc_reading ;
93+ uint32_t voltage = esp_adc_cal_raw_to_voltage (adc_reading , adc_chars );
94+ return voltage * ((1 << 16 ) - 1 )/3300 ;
9995}
10096
10197float common_hal_analogio_analogin_get_reference_voltage (analogio_analogin_obj_t * self ) {
0 commit comments