user_button.c 18 KB

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