user_button.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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_SETTING_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_SETTING_MODE;
  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. // //注册按键长按按键回调
  220. // button_event_config_t btn_long_press_cfg;
  221. // btn_cfg.event = BUTTON_LONG_PRESS_START;
  222. }
  223. #endif
  224. }
  225. void power_button_deinit(void)
  226. {
  227. for(int i = 0;i < 3;i++)
  228. iot_button_delete(user_powerbutton_handle[i]);
  229. }
  230. uint8_t button_info = 0;
  231. #include "SPIFFS.h"
  232. #if 0
  233. static void power_long_press_cb(void *arg,void *usr_data)
  234. {
  235. switch ((int)usr_data)
  236. {
  237. case 0:
  238. printf("power key long press start\r\n");
  239. button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
  240. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  241. {
  242. ESP_LOGE(LOG_TAG,"queue send is fail");
  243. return;
  244. }
  245. break;
  246. case 1:
  247. printf("power left key long press\r\n");
  248. break;
  249. }
  250. }
  251. iot_button_get_long_press_hold_cnt((button_handle_t)arg)
  252. #else
  253. static void power_long_press_start_cb(void *arg,void *usr_data)
  254. {
  255. // printf("power_long_press_start_cb\n");
  256. }
  257. static void power_long_press_hold_cb(void *arg,void *usr_data)
  258. {
  259. //设置的长按开始时间后开始计算hold_cnt 长按开始时间设置为1s,cnt计算时间间隔(SERIAL_TICKS)也设置为1s
  260. uint16_t hold_cnt = iot_button_get_long_press_hold_cnt((button_handle_t)arg);
  261. switch ((int)hold_cnt)
  262. {
  263. case 0:
  264. {
  265. ESP_LOGE(LOG_TAG,"case 0:");
  266. button_info = POWER_ON_INTO_DIS_RIGHT;
  267. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  268. {
  269. ESP_LOGE(LOG_TAG,"queue send is fail");
  270. return;
  271. }
  272. }
  273. case 2://设置模式,长按3s进入
  274. if(is_setting && is_reset_net)
  275. {
  276. ESP_LOGE(LOG_TAG,"err:OTA 和 重新配网 同时");
  277. return;
  278. }
  279. if(is_setting)
  280. {
  281. button_info = POWER_ON_INTO_OTA_VALUE;
  282. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  283. {
  284. ESP_LOGE(LOG_TAG,"queue send is fail");
  285. return;
  286. }
  287. }
  288. if(is_reset_net)
  289. {
  290. button_info = POWER_ON_INTO_RESET_VALUE;
  291. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  292. {
  293. ESP_LOGE(LOG_TAG,"queue send is fail");
  294. return;
  295. }
  296. }
  297. break;
  298. case 4://5s开关机
  299. button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
  300. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  301. {
  302. ESP_LOGE(LOG_TAG,"queue send is fail");
  303. return;
  304. }
  305. break;
  306. default:
  307. //printf(" BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
  308. break;
  309. }
  310. }
  311. static void power_long_press_up_cb(void *arg,void *usr_data)
  312. {
  313. // printf("power_long_press_up_cb\n");
  314. }
  315. #endif
  316. #include "esp_sleep.h"
  317. static void power_single_press_cb(void *arg,void *usr_data)
  318. {
  319. switch ((int)usr_data)
  320. {
  321. case 0:
  322. printf("power sigle press\r\n");
  323. if(Machine_info.power_status == 1) //开机
  324. {
  325. button_info = POWER_ON_PRESS_VALUE;
  326. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  327. {
  328. ESP_LOGE(LOG_TAG,"queue send is fail");
  329. return;
  330. }
  331. }else
  332. {
  333. button_info = POWER_OFF_PRESS_VALUE; //关机状态下单击按键
  334. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  335. {
  336. ESP_LOGE(LOG_TAG,"queue send is fail");
  337. return;
  338. }
  339. }
  340. break;
  341. case 1:
  342. printf("power left single press\r\n");
  343. break;
  344. case 2:
  345. printf("power right single press\r\n");
  346. break;
  347. }
  348. }
  349. static void user_button_single_click_cb(void *arg,void *usr_data)
  350. {
  351. is_setting = false;
  352. is_reset_net = false;
  353. switch ((int)usr_data)
  354. {
  355. case BAOYANG_KEY:
  356. button_info = 6;
  357. ESP_LOGD(LOG_TAG,"bao yang");
  358. break;
  359. case FENGCUN_KEY:
  360. button_info = 5;
  361. ESP_LOGD(LOG_TAG,"feng cun");
  362. break;
  363. case GUZHUANG_KEY:
  364. button_info = 4;
  365. ESP_LOGD(LOG_TAG,"gu zhang");
  366. break;
  367. case DAILIAO_KEY:
  368. button_info = 3;
  369. ESP_LOGD(LOG_TAG,"dai liao");
  370. break;
  371. case TINGJI_KEY:
  372. button_info = 2;
  373. ESP_LOGD(LOG_TAG,"ting ji");
  374. break;
  375. case YUNXING_KEY:
  376. button_info = 1;
  377. ESP_LOGD(LOG_TAG,"yun xing");
  378. break;
  379. default:
  380. break;
  381. }
  382. #if 0
  383. if(xSemaphoreTake(button_semaphore, 0) != true)
  384. {
  385. ESP_LOGE(LOG_TAG,"button mutex is fail,not send lora queue");
  386. return;
  387. }
  388. xSemaphoreGive(button_semaphore);
  389. #endif
  390. if(Machine_info.power_status == 0) //关机
  391. {
  392. printf("power off not refresh\r\n");
  393. return;
  394. }
  395. if(left_refresh_timer_isActive() == true)
  396. {
  397. return;
  398. }
  399. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  400. {
  401. ESP_LOGE(LOG_TAG,"queue send is fail");
  402. return;
  403. }
  404. }
  405. static void power_button_multi_press_cb(void *arg,void *usr_data)
  406. {
  407. if(Machine_info.power_status == 0) //关机
  408. {
  409. printf("关机状态\r\n");
  410. return;
  411. }
  412. switch ((int)usr_data)
  413. {
  414. case POWER_KEY_PRSSS_SETTING_MODE:
  415. if(!is_setting && !is_reset_net) //当前非配置模式
  416. {
  417. is_setting = true;
  418. xTimerStart(btn_timer_handle, 0);
  419. printf("短按2次\r\n");
  420. }
  421. break;
  422. case POWER_KEY_PRSSS_COUNT_RESET:
  423. if(!is_setting && !is_reset_net)
  424. {
  425. is_reset_net = true;
  426. xTimerStart(btn_timer_handle, 0);
  427. printf("短按4次\r\n");
  428. }
  429. break;
  430. }
  431. }
  432. static void user_button_single_UP_cb(void *arg,void *usr_data)
  433. {
  434. }
  435. void send_button_key_queue(uint8_t value)
  436. {
  437. button_info = value;
  438. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  439. {
  440. ESP_LOGE(LOG_TAG,"queue send is fail");
  441. return;
  442. }
  443. }