Decection.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // 0度的电池电量映射
  16. batt_map batt_map_0Du[] = {
  17. //
  18. {2225, 100},
  19. {2162, 99},
  20. {2156, 98},
  21. {2151, 97},
  22. {2146, 95},
  23. {2142, 94},
  24. {2137, 93},
  25. {2131, 92},
  26. {2125, 91},
  27. //
  28. {2120, 90},
  29. {2113, 89},
  30. {2107, 87},
  31. {2101, 86},
  32. {2094, 85},
  33. {2088, 84},
  34. {2081, 83},
  35. {2075, 82},
  36. //
  37. {2068, 80},
  38. {2061, 79},
  39. {2054, 78},
  40. {2047, 77},
  41. {2040, 76},
  42. {2033, 75},
  43. {2027, 74},
  44. {2020, 72},
  45. {2013, 71},
  46. //
  47. {2006, 70},
  48. {2000, 69},
  49. {1993, 68},
  50. {1987, 67},
  51. {1981, 66},
  52. {1974, 64},
  53. {1967, 63},
  54. {1961, 62},
  55. {1955, 61},
  56. //
  57. {1949, 60},
  58. {1943, 59},
  59. {1937, 58},
  60. {1931, 56},
  61. {1926, 55},
  62. {1920, 54},
  63. {1915, 53},
  64. {1910, 52},
  65. {1906, 51},
  66. //
  67. {1902, 50},
  68. {1899, 48},
  69. {1894, 47},
  70. {1891, 46},
  71. {1887, 45},
  72. {1884, 44},
  73. {1880, 43},
  74. {1878, 41},
  75. //
  76. {1875, 40},
  77. {1872, 39},
  78. {1869, 38},
  79. {1867, 37},
  80. {1864, 36},
  81. {1861, 35},
  82. {1859, 33},
  83. {1857, 32},
  84. {1854, 31},
  85. //
  86. {1852, 30},
  87. {1850, 29},
  88. {1849, 28},
  89. {1847, 27},
  90. {1845, 25},
  91. {1843, 24},
  92. {1841, 23},
  93. {1838, 22},
  94. {1835, 21},
  95. //
  96. {1832, 20},
  97. {1828, 19},
  98. {1823, 17},
  99. {1818, 16},
  100. {1813, 15},
  101. {1807, 14},
  102. {1801, 13},
  103. {1794, 12},
  104. //
  105. {1789, 10},
  106. {1783, 9},
  107. {1778, 8},
  108. {1771, 7},
  109. {1762, 6},
  110. {1756, 5},
  111. {1691, 4},
  112. {1634, 2},
  113. {1541, 1},
  114. {1400, 0},
  115. };
  116. #define USER_KEY_CHANNEL ADC_CHANNEL_7
  117. #define USER_ADC_CHANNEL ADC_CHANNEL_2
  118. #define USER_ADC_UNIT ADC_UNIT_1
  119. static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle);
  120. static void adc_calibration_deinit(adc_cali_handle_t handle);
  121. adc_cali_handle_t adc2_cali_handle = NULL;
  122. // void adc1_deinit(adc_oneshot_unit_handle_t adc_handle)
  123. // {
  124. // ESP_ERROR_CHECK(adc_oneshot_del_unit(adc_handle));
  125. // if (do_calibration) {
  126. // adc_calibration_deinit(adc2_cali_handle);
  127. // }
  128. // }
  129. static int get_batt_pre(int vol)
  130. {
  131. for (int i = 0; i <= 100; i++)
  132. {
  133. if (vol >= batt_map_0Du[i].vol)
  134. {
  135. ESP_LOGI(TAG, "vol = %d ,pre = %d", vol, batt_map_0Du[i].pre);
  136. return batt_map_0Du[i].pre;
  137. }
  138. }
  139. ESP_LOGE(TAG, "ERR : get_batt_pre vol = %d",vol);
  140. return 0;
  141. }
  142. int adc_read_power_pin(adc_oneshot_unit_handle_t adc_handle)
  143. {
  144. bool do_calibration;
  145. adc_oneshot_chan_cfg_t config = {
  146. .bitwidth = ADC_BITWIDTH_DEFAULT,
  147. .atten = ADC_ATTEN_DB_11,
  148. };
  149. do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_11, &adc2_cali_handle);
  150. //-------------ADC Config---------------//
  151. adc_oneshot_config_channel(adc_handle, USER_ADC_CHANNEL, &config);
  152. // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
  153. adc_oneshot_read(adc_handle, USER_ADC_CHANNEL, &adc_raw[0][0]);
  154. // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
  155. ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, adc_raw[0][0]);
  156. if (do_calibration)
  157. {
  158. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
  159. ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_ADC_CHANNEL, voltage[0][0]);
  160. adc_calibration_deinit(adc2_cali_handle);
  161. }
  162. return get_batt_pre(voltage[0][0]);
  163. }
  164. int adc_read_left_key_pin(adc_oneshot_unit_handle_t adc_handle)
  165. {
  166. bool do_calibration;
  167. adc_oneshot_chan_cfg_t config = {
  168. .bitwidth = ADC_BITWIDTH_DEFAULT,
  169. .atten = ADC_ATTEN_DB_0,
  170. };
  171. do_calibration = adc_calibration_init(USER_ADC_UNIT, ADC_ATTEN_DB_0, &adc2_cali_handle);
  172. //-------------ADC Config---------------//
  173. ESP_ERROR_CHECK(adc_oneshot_config_channel(adc_handle, USER_KEY_CHANNEL, &config));
  174. // adc_ll_set_power_manage(ADC_POWER_BY_FSM);
  175. ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, USER_KEY_CHANNEL, &adc_raw[0][0]));
  176. // adc_ll_set_power_manage(ADC_POWER_SW_OFF);
  177. ESP_LOGD(TAG, "ADC%d Channel[%d] Raw Data: %d", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, adc_raw[0][0]);
  178. if (do_calibration)
  179. {
  180. ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc2_cali_handle, adc_raw[0][0], &voltage[0][0]));
  181. ESP_LOGD(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", USER_ADC_UNIT + 1, USER_KEY_CHANNEL, voltage[0][0]);
  182. adc_calibration_deinit(adc2_cali_handle);
  183. }
  184. int batt_percent;
  185. #if 0
  186. //电量范围映射
  187. float result_voltage = (float)(voltage[0][0]) / 1000.0/*4095.0 * 3.3*/; // 假设ADC参考电压为3.3V
  188. float battery_percentage = ((result_voltage - 1.5) / (2.2 - 1.5)) * 100.0;
  189. // 限制电量范围在0%到100%之间
  190. if (battery_percentage < 0) {
  191. battery_percentage = 0;
  192. } else if (battery_percentage > 100) {
  193. battery_percentage = 100;
  194. }
  195. batt_percent = (int )battery_percentage;
  196. #if 0
  197. ESP_LOGI(TAG,"batt_percent = [%d],voltage[0][0] = [%d]",batt_percent, voltage[0][0]);
  198. #else
  199. printf("batt_percent = [%d],voltage[0][0] = [%d],adc value %d\r\n",batt_percent, voltage[0][0],(int)battery_percentage);
  200. #endif
  201. #else
  202. printf("left key voltage:%d\r\n", voltage[0][0]);
  203. batt_percent = voltage[0][0];
  204. #endif
  205. return batt_percent;
  206. }
  207. extern adc_oneshot_unit_handle_t adc1_handle;
  208. int read_battery_voltage()
  209. {
  210. // printf("读电量\n");
  211. return adc_read_power_pin(adc1_handle);
  212. }
  213. /*---------------------------------------------------------------
  214. ADC Calibration
  215. ---------------------------------------------------------------*/
  216. static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
  217. {
  218. adc_cali_handle_t handle = NULL;
  219. esp_err_t ret = ESP_FAIL;
  220. bool calibrated = false;
  221. if (!calibrated)
  222. {
  223. ESP_LOGD(TAG, "calibration scheme version is %s", "Curve Fitting");
  224. adc_cali_curve_fitting_config_t cali_config = {
  225. .unit_id = unit,
  226. .atten = atten,
  227. .bitwidth = ADC_BITWIDTH_DEFAULT,
  228. };
  229. ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
  230. if (ret == ESP_OK)
  231. {
  232. calibrated = true;
  233. }
  234. }
  235. *out_handle = handle;
  236. if (ret == ESP_OK)
  237. {
  238. ESP_LOGD(TAG, "Calibration Success");
  239. }
  240. else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated)
  241. {
  242. ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
  243. }
  244. else
  245. {
  246. ESP_LOGE(TAG, "Invalid arg or no memory");
  247. }
  248. return calibrated;
  249. }
  250. static void adc_calibration_deinit(adc_cali_handle_t handle)
  251. {
  252. ESP_LOGD(TAG, "deregister %s calibration scheme", "Curve Fitting");
  253. ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
  254. }
  255. // 返回充电中状态
  256. int decection_state_0(void)
  257. {
  258. return gpio_get_level(2);
  259. }
  260. // 返回充满状态
  261. int decection_state_1(void)
  262. {
  263. return 0; // gpio_get_level(38);
  264. }
  265. // #include "freertos/FreeRTOS.h"
  266. // #include "freertos/task.h"
  267. // #include "freertos/queue.h"
  268. // #include "SPIFFS.h"
  269. // extern RTC_FAST_ATTR Machine_info_t Machine_info;
  270. // static void IRAM_ATTR gpio_isr_handler(void *arg)
  271. // {
  272. // uint32_t gpio_num = (uint32_t)arg;
  273. // switch (gpio_num)
  274. // {
  275. // case 2:
  276. // // beep_blink(100,3);
  277. // // printf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
  278. // break;
  279. // case 38:
  280. // // beep_blink(100,4);//rintf("----------------gpio_num%ld中断------------------------\r\n",gpio_num);
  281. // break;
  282. // default:
  283. // break;
  284. // }
  285. // }
  286. // void decection_charging_init(void)
  287. // {
  288. // // zero-initialize the config structure.
  289. // gpio_config_t io_conf = {};
  290. // // interrupt of rising edge
  291. // io_conf.intr_type = GPIO_INTR_NEGEDGE; // 下降沿
  292. // // bit mask of the pins
  293. // io_conf.pin_bit_mask = ((1ULL << 2) | (1ULL << 38));
  294. // // set as input mode
  295. // io_conf.mode = GPIO_MODE_INPUT;
  296. // io_conf.pull_up_en = GPIO_PULLUP_DISABLE; // 禁用上拉电阻
  297. // io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE; // 禁用下拉电阻
  298. // // configure GPIO with the given settings
  299. // gpio_config(&io_conf);
  300. // ESP_LOGW(TAG, "decection_charging_init");
  301. // // install gpio isr service
  302. // gpio_install_isr_service(ESP_INTR_FLAG_EDGE);
  303. // gpio_isr_handler_add(2, gpio_isr_handler, (void *)2);
  304. // gpio_isr_handler_add(38, gpio_isr_handler, (void *)38);
  305. // }
  306. // void decection_charging_deinit(void)
  307. // {
  308. // gpio_isr_handler_remove(2);
  309. // gpio_isr_handler_remove(38);
  310. // }