Decection.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include <stdio.h>
  2. #include "Decection.h"
  3. #include "esp_log.h"
  4. #include "esp_adc/adc_oneshot.h"
  5. #include "esp_adc/adc_cali.h"
  6. #include "esp_adc/adc_cali_scheme.h"
  7. #include "esp_heap_caps.h"
  8. #include "hal/adc_hal.h"
  9. #include "driver/gpio.h"
  10. #include "esp_sleep.h"
  11. const static char *TAG = "Decection";
  12. // #include "LED.h"
  13. static int adc_raw[2][10];
  14. static int voltage[2][10];
  15. #define USER_KEY_CHANNEL ADC_CHANNEL_7
  16. #define USER_ADC_CHANNEL ADC_CHANNEL_2
  17. #define USER_ADC_UNIT ADC_UNIT_1
  18. static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
  19. static void adc_calibration_deinit(adc_cali_handle_t handle);
  20. adc_cali_handle_t adc2_cali_handle = NULL;
  21. // void adc1_deinit(adc_oneshot_unit_handle_t adc_handle)
  22. // {
  23. // ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle));
  24. // if (do_calibration) {
  25. // adc_calibration_deinit(adc2_cali_handle);
  26. // }
  27. // }
  28. int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle)
  29. {
  30. bool do_calibration;
  31. adc_oneshot_chan_cfg_t config = {
  32. .bitwidth = ADC_BITWIDTH_DEFAULT,
  33. .atten = ADC_ATTEN_DB_11,
  34. };
  35. do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_11, &adc2_cali_handle);
  36. //-------------ADC Config---------------//
  37. adc_oneshot_config_channel(adc_handle, USER_ADC_CHANNEL, &config);
  38. // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
  39. adc_oneshot_read(adc_handle, USER_ADC_CHANNEL, &adc_raw[0][0]);
  40. // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
  41. ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, adc_raw[0][0]);
  42. if (do_calibration)
  43. {
  44. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
  45. ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, voltage[0][0]);
  46. adc_calibration_deinit(adc2_cali_handle);
  47. }
  48. int batt_percent;
  49. // 电量范围映射
  50. float result_voltage = (float)(voltage[0][0]) / 1000.0 /*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
  51. float battery_percentage = ((result_voltage - 1.8) / (2.2 - 1.8)) * 100.0;
  52. // 限制电量范围在0%到100%之间
  53. if (battery_percentage <= 0)
  54. {
  55. battery_percentage = 0;
  56. }
  57. else if (battery_percentage >= 100)
  58. {
  59. battery_percentage = 100;
  60. }
  61. batt_percent = (int)battery_percentage;
  62. #if 0
  63. ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
  64. #else
  65. printf("voltage[0][0] = [%d],adc value %d\r\n",voltage[0][0],(int)battery_percentage);
  66. #endif
  67. return batt_percent;
  68. }
  69. int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle)
  70. {
  71. bool do_calibration;
  72. adc_oneshot_chan_cfg_t config = {
  73. .bitwidth = ADC_BITWIDTH_DEFAULT,
  74. .atten = ADC_ATTEN_DB_0,
  75. };
  76. do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_0, &adc2_cali_handle);
  77. //-------------ADC Config---------------//
  78. ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, USER_KEY_CHANNEL, &config));
  79. // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
  80. ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, USER_KEY_CHANNEL, &adc_raw[0][0]));
  81. // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
  82. ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, adc_raw[0][0]);
  83. if (do_calibration)
  84. {
  85. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
  86. ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, voltage[0][0]);
  87. adc_calibration_deinit(adc2_cali_handle);
  88. }
  89. int batt_percent;
  90. #if 0
  91. //电量范围映射
  92. float result_voltage = (float)(voltage[0][0]) / 1000.0/*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
  93. float battery_percentage = ((result_voltage - 1.5) / (2.2 - 1.5)) * 100.0;
  94. // 限制电量范围在0%到100%之间
  95. if (battery_percentage < 0) {
  96. battery_percentage = 0;
  97. } else if (battery_percentage > 100) {
  98. battery_percentage = 100;
  99. }
  100. batt_percent = (int )battery_percentage;
  101. #if 0
  102. ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
  103. #else
  104. printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage);
  105. #endif
  106. #else
  107. printf("left key voltage:%d\r\n", voltage[0][0]);
  108. batt_percent = voltage[0][0];
  109. #endif
  110. return batt_percent;
  111. }
  112. extern adc_oneshot_unit_handle_t adc1_handle;
  113. int read_battery_voltage()
  114. {
  115. // printf("读电量\n");
  116. return adc_read_power_pin(adc1_handle);
  117. }
  118. /*---------------------------------------------------------------
  119. ADC Calibration
  120. ---------------------------------------------------------------*/
  121. static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
  122. {
  123. adc_cali_handle_t handle = NULL;
  124. esp_err_t ret = ESP_FAIL;
  125. bool calibrated = false;
  126. if (!calibrated)
  127. {
  128. ESP_LOGD(TAG, "calibration scheme version is %s", "Curve Fitting");
  129. adc_cali_curve_fitting_config_t cali_config = {
  130. .unit_id = unit,
  131. .atten = atten,
  132. .bitwidth = ADC_BITWIDTH_DEFAULT,
  133. };
  134. ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
  135. if (ret == ESP_OK)
  136. {
  137. calibrated = true;
  138. }
  139. }
  140. *out_handle = handle;
  141. if (ret == ESP_OK)
  142. {
  143. ESP_LOGD(TAG, "Calibration Success");
  144. }
  145. else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated)
  146. {
  147. ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
  148. }
  149. else
  150. {
  151. ESP_LOGE(TAG, "Invalid arg or no memory");
  152. }
  153. return calibrated;
  154. }
  155. static void adc_calibration_deinit(adc_cali_handle_t handle)
  156. {
  157. ESP_LOGD(TAG, "deregister %s calibration scheme", "Curve Fitting");
  158. ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
  159. }
  160. // 返回充电中状态
  161. int decection_state_0(void)
  162. {
  163. return gpio_get_level(2);
  164. }
  165. // 返回充满状态
  166. int decection_state_1(void)
  167. {
  168. return 0; // gpio_get_level(38);
  169. }
  170. #include "freertos/FreeRTOS.h"
  171. #include "freertos/task.h"
  172. #include "freertos/queue.h"
  173. #include "SPIFFS.h"
  174. extern RTC_FAST_ATTR Machine_info_t Machine_info;
  175. static void IRAM_ATTR gpio_isr_handler(void *arg)
  176. {
  177. uint32_t gpio_num = (uint32_t)arg;
  178. switch (gpio_num)
  179. {
  180. case 2:
  181. // beep_blink(100,3);
  182. // printf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
  183. break;
  184. case 38:
  185. // beep_blink(100,4);//rintf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. void decection_charging_init(void)
  192. {
  193. // zero-initialize the config structure.
  194. gpio_config_t io_conf = {};
  195. // interrupt of rising edge
  196. io_conf.intr_type = GPIO_INTR_NEGEDGE; // 下降沿
  197. // bit mask of the pins
  198. io_conf.pin_bit_mask = ((1ULL << 2) | (1ULL << 38));
  199. // set as input mode
  200. io_conf.mode = GPIO_MODE_INPUT;
  201. io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 禁用上拉电阻
  202. io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 禁用下拉电阻
  203. // configure GPIO with the given settings
  204. gpio_config(&io_conf);
  205. ESP_LOGW(TAG, "decection_charging_init");
  206. // install gpio isr service
  207. gpio_install_isr_service(ESP_INTR_FLAG_EDGE);
  208. gpio_isr_handler_add(2, gpio_isr_handler, (void *)2);
  209. gpio_isr_handler_add(38, gpio_isr_handler, (void *)38);
  210. }
  211. // void decection_charging_deinit(void)
  212. // {
  213. // gpio_isr_handler_remove(2);
  214. // gpio_isr_handler_remove(38);
  215. // }