user_button.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "user_button.h"
  4. #include "iot_button.h"
  5. #include "esp_log.h"
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "freertos/queue.h"
  9. #include "freertos/semphr.h"
  10. #include "freertos/timers.h"
  11. #include "../EPD//include/EPD.h"
  12. static const char *LOG_TAG = "user button";
  13. extern QueueHandle_t button_Data_queue;
  14. extern SemaphoreHandle_t button_semaphore;
  15. static button_handle_t user_button_handle[KEY_NUM] = {0};
  16. static button_handle_t user_powerbutton_handle[3] = {0};
  17. TimerHandle_t btn_timer_handle;
  18. bool is_setting = false;
  19. bool is_reset_net = false;//配网
  20. uint16_t adc_value[][2]=
  21. {
  22. {BAOYANG_MIN_ADC,BAOYANG_MAX_ADC},
  23. {FENGCUN_MIN_ADC,FENGCUN_MAX_ADC},
  24. {GUZHUANG_MIN_ADC,GUZHANG_MAX_ADC},
  25. {DAILIAO_MIN_ADC,DAILIAO_MAX_ADC},
  26. {TINGJI_MIN_ADC,TINGJI_MAX_ADC},
  27. {YUNXING_MIN_ADC,YUNXING_MAX_ADC},
  28. };
  29. uint16_t power_adc_value[][2]=
  30. {
  31. {0,100},
  32. {1000,1900},
  33. {2000,3063},
  34. };
  35. int find_key_value(int value)
  36. {
  37. int key = 0xff;
  38. for(int i =0 ;i<6;i++)
  39. {
  40. if(
  41. (adc_value[i][1]>=value)&&
  42. (adc_value[i][0]<=value)
  43. )
  44. {
  45. key = i;
  46. printf("key vaule %d\r\n",i);
  47. break;
  48. }
  49. }
  50. return key;
  51. }
  52. static void user_button_single_click_cb(void *arg,void *usr_data);
  53. static void power_long_press_start_cb(void *arg,void *usr_data);
  54. static void power_long_press_hold_cb(void *arg,void *usr_data);
  55. static void power_long_press_up_cb(void *arg,void *usr_data);
  56. static void power_single_press_cb(void *arg,void *usr_data);
  57. static void user_button_single_UP_cb(void *arg,void *usr_data);
  58. static void power_button_multi_press_cb(void *arg,void *usr_data);
  59. static void power_left_single_press_cb(void *arg,void *usr_data)
  60. {
  61. printf("%s\r\n",__FUNCTION__);
  62. }
  63. static void power_right_single_press_cb(void *arg,void *usr_data)
  64. {
  65. printf("%s\r\n",__FUNCTION__);
  66. }
  67. int button_idx = 0;
  68. int power_button_idx = 0;
  69. int power_button_mult_time = 0;
  70. void btn_timer_Callback(TimerHandle_t xTimer)
  71. {
  72. ESP_LOGW(LOG_TAG, "btn_timer_Callback");
  73. if(is_setting)
  74. {
  75. is_setting = false;
  76. }
  77. else if (is_reset_net)
  78. {
  79. is_reset_net = false;
  80. }
  81. is_setting = 0;
  82. //xTimerStop(btn_timer_handle,0);
  83. }
  84. void button_init(adc_oneshot_unit_handle_t adc_handle)
  85. {
  86. int i;
  87. printf("button_init\n");
  88. #if 1
  89. button_config_t adc_btn_cfg = {
  90. .type = BUTTON_TYPE_ADC,
  91. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  92. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  93. .adc_button_config = {
  94. .adc_channel = 7,
  95. .adc_handle = &adc_handle,
  96. },
  97. };
  98. //左边6个按键初始化
  99. for(i = 0;i < KEY_NUM ;i++)
  100. {
  101. adc_btn_cfg.adc_button_config.button_index = i;
  102. adc_btn_cfg.adc_button_config.min = adc_value[i][0] ;
  103. adc_btn_cfg.adc_button_config.max = adc_value[i][1] ;
  104. user_button_handle[i] = iot_button_create(&adc_btn_cfg);
  105. if(NULL == user_button_handle[i]) {
  106. printf( "Button create failed");
  107. }
  108. button_idx = i;
  109. int ret = iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_DOWN, user_button_single_click_cb,button_idx);
  110. //iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  111. //iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb,button_idx);
  112. }
  113. #endif
  114. }
  115. void button_deinit(void)
  116. {
  117. printf("button_deinit\n");
  118. for(int i=0;i<KEY_NUM;i++)
  119. {
  120. iot_button_delete(user_button_handle[i]);
  121. }
  122. //iot_button_delete(user_powerbutton_handle[0]);
  123. }
  124. void power_button_init(adc_oneshot_unit_handle_t adc_handle)
  125. {
  126. btn_timer_handle = xTimerCreate(
  127. "btn_timer_handle",
  128. pdMS_TO_TICKS(10*1000),
  129. pdFALSE,
  130. 0,
  131. btn_timer_Callback
  132. );
  133. int i = 0;
  134. #if 0
  135. //右边3个按键 目前只是用一个按键 POWER
  136. button_config_t power_btn_cfg = {
  137. .type = BUTTON_TYPE_GPIO,
  138. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  139. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  140. .adc_button_config = {
  141. .adc_channel = 3,
  142. .adc_handle = &adc_handle,
  143. },
  144. };
  145. for(int i = 0;i < 1 ;i++)
  146. {
  147. power_btn_cfg.adc_button_config.button_index = i;
  148. power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
  149. power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
  150. user_powerbutton_handle[i] = iot_button_create(&power_btn_cfg);
  151. if(NULL == user_powerbutton_handle[i]) {
  152. printf( "Button create failed");
  153. }
  154. button_idx = i;
  155. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
  156. #if 0
  157. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  158. #else
  159. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
  160. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
  161. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
  162. #endif
  163. //注册按键多次按键回调
  164. button_event_config_t btn_cfg;
  165. btn_cfg.event = BUTTON_MULTIPLE_CLICK;
  166. // btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_OTA;
  167. // power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  168. // iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  169. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_RESET;
  170. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  171. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  172. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_BLE_OTA_MODE;
  173. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  174. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  175. // //注册按键长按按键回调
  176. // button_event_config_t btn_long_press_cfg;
  177. // btn_cfg.event = BUTTON_LONG_PRESS_START;
  178. }
  179. #else
  180. button_config_t power_btn_cfg = {
  181. .type = BUTTON_TYPE_GPIO,
  182. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  183. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  184. .gpio_button_config = {
  185. .gpio_num = 4,
  186. .active_level = 0,
  187. },
  188. };
  189. for(int i = 0;i < 1 ;i++)
  190. {
  191. //power_btn_cfg.adc_button_config.button_index = i;
  192. //power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
  193. //power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
  194. user_powerbutton_handle[i] = iot_button_create(&power_btn_cfg);
  195. if(NULL == user_powerbutton_handle[i]) {
  196. printf( "Button create failed");
  197. }
  198. button_idx = i;
  199. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
  200. #if 0
  201. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  202. #else
  203. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
  204. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
  205. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
  206. #endif
  207. //注册按键多次按键回调
  208. button_event_config_t btn_cfg;
  209. btn_cfg.event = BUTTON_MULTIPLE_CLICK;
  210. // btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_OTA;
  211. // power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  212. // iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  213. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_RESET;
  214. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  215. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  216. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_DIS_RIGHT;
  217. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  218. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  219. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_BLE_OTA_MODE;
  220. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  221. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  222. // //注册按键长按按键回调
  223. // button_event_config_t btn_long_press_cfg;
  224. // btn_cfg.event = BUTTON_LONG_PRESS_START;
  225. }
  226. #endif
  227. }
  228. void power_button_deinit(void)
  229. {
  230. for(int i = 0;i < 3;i++)
  231. iot_button_delete(user_powerbutton_handle[i]);
  232. }
  233. uint8_t button_info = 0;
  234. #include "SPIFFS.h"
  235. #if 0
  236. static void power_long_press_cb(void *arg,void *usr_data)
  237. {
  238. switch ((int)usr_data)
  239. {
  240. case 0:
  241. printf("power key long press start\r\n");
  242. button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
  243. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  244. {
  245. ESP_LOGE(LOG_TAG,"queue send is fail");
  246. return;
  247. }
  248. break;
  249. case 1:
  250. printf("power left key long press\r\n");
  251. break;
  252. }
  253. }
  254. iot_button_get_long_press_hold_cnt((button_handle_t)arg)
  255. #else
  256. static void power_long_press_start_cb(void *arg,void *usr_data)
  257. {
  258. // printf("power_long_press_start_cb\n");
  259. }
  260. static void power_long_press_hold_cb(void *arg,void *usr_data)
  261. {
  262. //设置的长按开始时间后开始计算hold_cnt 长按开始时间设置为1s,cnt计算时间间隔(SERIAL_TICKS)也设置为1s
  263. uint16_t hold_cnt = iot_button_get_long_press_hold_cnt((button_handle_t)arg);
  264. ESP_LOGE(LOG_TAG," BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
  265. switch ((int)hold_cnt)
  266. {
  267. case 2://设置模式,长按3s进入
  268. if(is_setting && is_reset_net)
  269. {
  270. ESP_LOGE(LOG_TAG,"err:OTA 和 重新配网 同时");
  271. return;
  272. }
  273. if(is_setting)
  274. {
  275. button_info = POWER_ON_INTO_OTA_VALUE;
  276. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  277. {
  278. ESP_LOGE(LOG_TAG,"queue send is fail");
  279. return;
  280. }
  281. }
  282. if(is_reset_net)
  283. {
  284. button_info = POWER_ON_INTO_RESET_VALUE;
  285. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  286. {
  287. ESP_LOGE(LOG_TAG,"queue send is fail");
  288. return;
  289. }
  290. }
  291. break;
  292. case 4://5s开关机
  293. button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
  294. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  295. {
  296. ESP_LOGE(LOG_TAG,"queue send is fail");
  297. return;
  298. }
  299. break;
  300. default:
  301. //printf(" BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
  302. break;
  303. }
  304. }
  305. static void power_long_press_up_cb(void *arg,void *usr_data)
  306. {
  307. // printf("power_long_press_up_cb\n");
  308. }
  309. #endif
  310. #include "esp_sleep.h"
  311. static void power_single_press_cb(void *arg,void *usr_data)
  312. {
  313. switch ((int)usr_data)
  314. {
  315. case 0:
  316. printf("power sigle press\r\n");
  317. if(Machine_info.power_status == 1) //开机
  318. {
  319. button_info = POWER_ON_PRESS_VALUE;
  320. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  321. {
  322. ESP_LOGE(LOG_TAG,"queue send is fail");
  323. return;
  324. }
  325. }else
  326. {
  327. button_info = POWER_OFF_PRESS_VALUE; //关机状态下单击按键
  328. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  329. {
  330. ESP_LOGE(LOG_TAG,"queue send is fail");
  331. return;
  332. }
  333. }
  334. break;
  335. case 1:
  336. printf("power left single press\r\n");
  337. break;
  338. case 2:
  339. printf("power right single press\r\n");
  340. break;
  341. }
  342. }
  343. static void user_button_single_click_cb(void *arg,void *usr_data)
  344. {
  345. is_setting = false;
  346. is_reset_net = false;
  347. ESP_LOGW(LOG_TAG," single_click_cb[%d]", iot_button_get_ticks_time((button_handle_t)arg));
  348. switch ((int)usr_data)
  349. {
  350. case BAOYANG_KEY:
  351. button_info = 6;
  352. ESP_LOGD(LOG_TAG,"bao yang");
  353. break;
  354. case FENGCUN_KEY:
  355. button_info = 5;
  356. ESP_LOGD(LOG_TAG,"feng cun");
  357. break;
  358. case GUZHUANG_KEY:
  359. button_info = 4;
  360. ESP_LOGD(LOG_TAG,"gu zhang");
  361. break;
  362. case DAILIAO_KEY:
  363. button_info = 3;
  364. ESP_LOGD(LOG_TAG,"dai liao");
  365. break;
  366. case TINGJI_KEY:
  367. button_info = 2;
  368. ESP_LOGD(LOG_TAG,"ting ji");
  369. break;
  370. case YUNXING_KEY:
  371. button_info = 1;
  372. ESP_LOGD(LOG_TAG,"yun xing");
  373. break;
  374. default:
  375. break;
  376. }
  377. #if 0
  378. if(xSemaphoreTake(button_semaphore, 0) != true)
  379. {
  380. ESP_LOGE(LOG_TAG,"button mutex is fail,not send lora queue");
  381. return;
  382. }
  383. xSemaphoreGive(button_semaphore);
  384. #endif
  385. if(Machine_info.power_status == 0) //关机
  386. {
  387. printf("power off not refresh\r\n");
  388. return;
  389. }
  390. if(left_refresh_timer_isActive() == true)
  391. {
  392. return;
  393. }
  394. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  395. {
  396. ESP_LOGE(LOG_TAG,"queue send is fail");
  397. return;
  398. }
  399. }
  400. static void power_button_multi_press_cb(void *arg,void *usr_data)
  401. {
  402. if(Machine_info.power_status == 0) //关机
  403. {
  404. printf("关机状态\r\n");
  405. return;
  406. }
  407. switch ((int)usr_data)
  408. {
  409. case POWER_KEY_PRSSS_BLE_OTA_MODE:
  410. if(!is_setting && !is_reset_net) //当前非配置模式
  411. {
  412. is_setting = true;
  413. xTimerStart(btn_timer_handle, 0);
  414. printf("短按2次\r\n");
  415. }
  416. break;
  417. case POWER_KEY_PRSSS_DIS_RIGHT:
  418. printf("短按3次\r\n");
  419. send_button_key_queue((uint8_t)POWER_ON_INTO_DIS_RIGHT);
  420. break;
  421. case POWER_KEY_PRSSS_COUNT_RESET:
  422. if(!is_setting && !is_reset_net)
  423. {
  424. is_reset_net = true;
  425. xTimerStart(btn_timer_handle, 0);
  426. printf("短按4次\r\n");
  427. }
  428. break;
  429. }
  430. }
  431. static void user_button_single_UP_cb(void *arg,void *usr_data)
  432. {
  433. }
  434. void send_button_key_queue(uint8_t value)
  435. {
  436. button_info = value;
  437. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  438. {
  439. ESP_LOGE(LOG_TAG,"queue send is fail");
  440. return;
  441. }
  442. }