Decection.c 8.0 KB

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