1#ifdef LOOK_AT_THIS_TO_SEE_IF_CAN_USE
2#include "driver/gpio.h"
4#include "esp_adc_cal.h"
5#include "soc/adc_channel.h"
8#define BAT_OUTPUT_HOLD_PIN 33
11#define BAT_ADC_CHANNEL ADC1_GPIO38_CHANNEL
13#define BASE_VOLATAGE 3600
15#define ADC_FILTER_SAMPLE 64
17static esp_adc_cal_characteristics_t *adc_chars;
21 gpio_pad_select_gpio(BAT_OUTPUT_HOLD_PIN);
22 gpio_set_direction(BAT_OUTPUT_HOLD_PIN, GPIO_MODE_OUTPUT);
23 gpio_set_level(BAT_OUTPUT_HOLD_PIN, 1);
26 adc1_config_width(ADC_WIDTH_BIT_12);
27 adc1_config_channel_atten(BAT_ADC_CHANNEL, ADC_ATTEN_DB_11);
28 adc_chars = calloc(1,
sizeof(esp_adc_cal_characteristics_t));
29 esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, BASE_VOLATAGE, adc_chars);
32void bat_hold_output() {
33 gpio_set_level(BAT_OUTPUT_HOLD_PIN, 1);
36void bat_disable_output() {
37 gpio_set_level(BAT_OUTPUT_HOLD_PIN, 0);
40uint32_t bat_get_adc_raw() {
41 return adc1_get_raw(BAT_ADC_CHANNEL);
44uint32_t bat_get_voltage() {
45 uint32_t adc_raw_value = 0;
46 for (uint16_t i = 0; i < ADC_FILTER_SAMPLE; i++) {
47 adc_raw_value += adc1_get_raw(BAT_ADC_CHANNEL);
50 adc_raw_value = adc_raw_value / ADC_FILTER_SAMPLE;
51 uint32_t voltage = (uint32_t)(esp_adc_cal_raw_to_voltage(adc_raw_value, adc_chars) / SCALE);