#include #include "Decection.h" #include "esp_log.h" #include "esp_adc/adc_oneshot.h" #include "esp_adc/adc_cali.h" #include "esp_adc/adc_cali_scheme.h" #include "esp_heap_caps.h" #include "hal/adc_hal.h" #include "driver/gpio.h" #include "esp_sleep.h" const static char *TAG = "Decection"; // #include "LED.h" static int adc_raw[2][10]; static int voltage[2][10]; // 0度的电池电量映射 batt_map batt_map_0Du[] = { // {2225, 100}, {2162, 99}, {2156, 98}, {2151, 97}, {2146, 95}, {2142, 94}, {2137, 93}, {2131, 92}, {2125, 91}, // {2120, 90}, {2113, 89}, {2107, 87}, {2101, 86}, {2094, 85}, {2088, 84}, {2081, 83}, {2075, 82}, // {2068, 80}, {2061, 79}, {2054, 78}, {2047, 77}, {2040, 76}, {2033, 75}, {2027, 74}, {2020, 72}, {2013, 71}, // {2006, 70}, {2000, 69}, {1993, 68}, {1987, 67}, {1981, 66}, {1974, 64}, {1967, 63}, {1961, 62}, {1955, 61}, // {1949, 60}, {1943, 59}, {1937, 58}, {1931, 56}, {1926, 55}, {1920, 54}, {1915, 53}, {1910, 52}, {1906, 51}, // {1902, 50}, {1899, 48}, {1894, 47}, {1891, 46}, {1887, 45}, {1884, 44}, {1880, 43}, {1878, 41}, // {1875, 40}, {1872, 39}, {1869, 38}, {1867, 37}, {1864, 36}, {1861, 35}, {1859, 33}, {1857, 32}, {1854, 31}, // {1852, 30}, {1850, 29}, {1849, 28}, {1847, 27}, {1845, 25}, {1843, 24}, {1841, 23}, {1838, 22}, {1835, 21}, // {1832, 20}, {1828, 19}, {1823, 17}, {1818, 16}, {1813, 15}, {1807, 14}, {1801, 13}, {1794, 12}, // {1789, 10}, {1783, 9}, {1778, 8}, {1771, 7}, {1762, 6}, {1756, 5}, {1691, 4}, {1634, 2}, {1541, 1}, {1400, 0}, }; #define USER_KEY_CHANNEL ADC_CHANNEL_7 #define USER_ADC_CHANNEL ADC_CHANNEL_2 #define USER_ADC_UNIT ADC_UNIT_1 static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle); static void adc_calibration_deinit(adc_cali_handle_t handle); adc_cali_handle_t adc2_cali_handle = NULL; // void adc1_deinit(adc_oneshot_unit_handle_t adc_handle) // { // ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle)); // if (do_calibration) { // adc_calibration_deinit(adc2_cali_handle); // } // } static int get_batt_pre(int vol) { for (int i = 0; i <= 100; i++) { if (vol >= batt_map_0Du[i].vol) { ESP_LOGI(TAG, "vol = %d ,pre = %d", vol, batt_map_0Du[i].pre); return batt_map_0Du[i].pre; } } ESP_LOGE(TAG, "ERR : get_batt_pre vol = %d",vol); return 0; } int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle) { bool do_calibration; adc_oneshot_chan_cfg_t config = { .bitwidth = ADC_BITWIDTH_DEFAULT, .atten = ADC_ATTEN_DB_11, }; do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_11, &adc2_cali_handle); //-------------ADC Config---------------// adc_oneshot_config_channel(adc_handle, USER_ADC_CHANNEL, &config); // adc_ll_set_power_manage(ADC_POWER_BY_FSM); adc_oneshot_read(adc_handle, USER_ADC_CHANNEL, &adc_raw[0][0]); // adc_ll_set_power_manage(ADC_POWER_SW_OFF); ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, adc_raw[0][0]); if (do_calibration) { ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0])); ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, voltage[0][0]); adc_calibration_deinit(adc2_cali_handle); } return get_batt_pre(voltage[0][0]); } int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle) { bool do_calibration; adc_oneshot_chan_cfg_t config = { .bitwidth = ADC_BITWIDTH_DEFAULT, .atten = ADC_ATTEN_DB_0, }; do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_0, &adc2_cali_handle); //-------------ADC Config---------------// ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, USER_KEY_CHANNEL, &config)); // adc_ll_set_power_manage(ADC_POWER_BY_FSM); ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, USER_KEY_CHANNEL, &adc_raw[0][0])); // adc_ll_set_power_manage(ADC_POWER_SW_OFF); ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, adc_raw[0][0]); if (do_calibration) { ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0])); ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, voltage[0][0]); adc_calibration_deinit(adc2_cali_handle); } int batt_percent; #if 0 //电量范围映射 float result_voltage = (float)(voltage[0][0]) / 1000.0/*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V float battery_percentage = ((result_voltage - 1.5) / (2.2 - 1.5)) * 100.0; // 限制电量范围在0%到100%之间 if (battery_percentage < 0) { battery_percentage = 0; } else if (battery_percentage > 100) { battery_percentage = 100; } batt_percent = (int )battery_percentage; #if 0 ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]); #else printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage); #endif #else printf("left key voltage:%d\r\n", voltage[0][0]); batt_percent = voltage[0][0]; #endif return batt_percent; } extern adc_oneshot_unit_handle_t adc1_handle; int read_battery_voltage() { // printf("读电量\n"); return adc_read_power_pin(adc1_handle); } /*--------------------------------------------------------------- ADC Calibration ---------------------------------------------------------------*/ static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle) { adc_cali_handle_t handle = NULL; esp_err_t ret = ESP_FAIL; bool calibrated = false; if (!calibrated) { ESP_LOGD(TAG, "calibration scheme version is %s", "Curve Fitting"); adc_cali_curve_fitting_config_t cali_config = { .unit_id = unit, .atten = atten, .bitwidth = ADC_BITWIDTH_DEFAULT, }; ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle); if (ret == ESP_OK) { calibrated = true; } } *out_handle = handle; if (ret == ESP_OK) { ESP_LOGD(TAG, "Calibration Success"); } else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) { ESP_LOGW(TAG, "eFuse not burnt, skip software calibration"); } else { ESP_LOGE(TAG, "Invalid arg or no memory"); } return calibrated; } static void adc_calibration_deinit(adc_cali_handle_t handle) { ESP_LOGD(TAG, "deregister %s calibration scheme", "Curve Fitting"); ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle)); } // 返回充电中状态 int decection_state_0(void) { return gpio_get_level(2); } // 返回充满状态 int decection_state_1(void) { return 0; // gpio_get_level(38); } // #include "freertos/FreeRTOS.h" // #include "freertos/task.h" // #include "freertos/queue.h" // #include "SPIFFS.h" // extern RTC_FAST_ATTR Machine_info_t Machine_info; // static void IRAM_ATTR gpio_isr_handler(void *arg) // { // uint32_t gpio_num = (uint32_t)arg; // switch (gpio_num) // { // case 2: // // beep_blink(100,3); // // printf("----------------gpio_num%ld中断------------------------\r\n",gpio_num); // break; // case 38: // // beep_blink(100,4);//rintf("----------------gpio_num%ld中断------------------------\r\n",gpio_num); // break; // default: // break; // } // } // void decection_charging_init(void) // { // // zero-initialize the config structure. // gpio_config_t io_conf = {}; // // interrupt of rising edge // io_conf.intr_type = GPIO_INTR_NEGEDGE; // 下降沿 // // bit mask of the pins // io_conf.pin_bit_mask = ((1ULL << 2) | (1ULL << 38)); // // set as input mode // io_conf.mode = GPIO_MODE_INPUT; // io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 禁用上拉电阻 // io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 禁用下拉电阻 // // configure GPIO with the given settings // gpio_config(&io_conf); // ESP_LOGW(TAG, "decection_charging_init"); // // install gpio isr service // gpio_install_isr_service(ESP_INTR_FLAG_EDGE); // gpio_isr_handler_add(2, gpio_isr_handler, (void *)2); // gpio_isr_handler_add(38, gpio_isr_handler, (void *)38); // } // void decection_charging_deinit(void) // { // gpio_isr_handler_remove(2); // gpio_isr_handler_remove(38); // }