user_button.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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_LOGD(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. struct tm* tmp = localtime(&now_time.tv_sec);
  109. ESP_LOGI(LOG_TAG,"时间: %04d-%02d-%02d %02d:%02d:%02d\n",
  110. tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
  111. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  112. if(((now_time.tv_sec - btn_last_time.tv_sec) > 30)&&(is_send_lora))
  113. {
  114. ESP_LOGW(LOG_TAG,"发送-----现在按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
  115. tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
  116. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  117. is_send_lora = false;
  118. return true;
  119. }
  120. return false;
  121. }
  122. void reset_btn_last_time(void)
  123. {
  124. if(!is_send_lora)
  125. {
  126. is_send_lora = true;
  127. }
  128. gettimeofday(&btn_last_time, NULL);
  129. struct tm* tmp = localtime(&btn_last_time.tv_sec);
  130. // printf("btn_last_time: %lld\n", btn_last_time.tv_sec);
  131. ESP_LOGW(LOG_TAG,"最后按键时间: %04d-%02d-%02d %02d:%02d:%02d\n",
  132. tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
  133. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  134. }
  135. void button_init(adc_oneshot_unit_handle_t adc_handle)
  136. {
  137. int i;
  138. printf("button_init\n");
  139. #if 1
  140. button_config_t adc_btn_cfg = {
  141. .type = BUTTON_TYPE_ADC,
  142. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  143. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  144. .adc_button_config = {
  145. .adc_channel = 7,
  146. .adc_handle = &adc_handle,
  147. },
  148. };
  149. //左边6个按键初始化
  150. for(i = 0;i < KEY_NUM ;i++)
  151. {
  152. adc_btn_cfg.adc_button_config.button_index = i;
  153. adc_btn_cfg.adc_button_config.min = adc_value[i][0] ;
  154. adc_btn_cfg.adc_button_config.max = adc_value[i][1] ;
  155. user_button_handle[i] = iot_button_create(&adc_btn_cfg);
  156. if(NULL == user_button_handle[i]) {
  157. printf( "Button create failed");
  158. }
  159. button_idx = i;
  160. int ret = iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_DOWN, user_button_single_click_cb,button_idx);
  161. //iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  162. //iot_button_register_cb( user_button_handle[i], BUTTON_PRESS_UP, user_button_single_click_cb,button_idx);
  163. }
  164. #endif
  165. //电源按键
  166. btn_timer_handle = xTimerCreate(
  167. "btn_timer_handle",
  168. pdMS_TO_TICKS(10*1000),
  169. pdFALSE,
  170. 0,
  171. btn_timer_Callback
  172. );
  173. #if 0
  174. //右边3个按键 目前只是用一个按键 POWER
  175. button_config_t power_btn_cfg = {
  176. .type = BUTTON_TYPE_GPIO,
  177. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  178. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  179. .adc_button_config = {
  180. .adc_channel = 3,
  181. .adc_handle = &adc_handle,
  182. },
  183. };
  184. for(int i = 0;i < 1 ;i++)
  185. {
  186. power_btn_cfg.adc_button_config.button_index = i;
  187. power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
  188. power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
  189. user_powerbutton_handle[i] = iot_button_create(&power_btn_cfg);
  190. if(NULL == user_powerbutton_handle[i]) {
  191. printf( "Button create failed");
  192. }
  193. button_idx = i;
  194. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
  195. #if 0
  196. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  197. #else
  198. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
  199. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
  200. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
  201. #endif
  202. //注册按键多次按键回调
  203. button_event_config_t btn_cfg;
  204. btn_cfg.event = BUTTON_MULTIPLE_CLICK;
  205. // btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_OTA;
  206. // power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  207. // iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  208. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_RESET;
  209. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  210. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  211. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_BLE_OTA_MODE;
  212. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  213. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  214. // //注册按键长按按键回调
  215. // button_event_config_t btn_long_press_cfg;
  216. // btn_cfg.event = BUTTON_LONG_PRESS_START;
  217. }
  218. #else
  219. button_config_t power_btn_cfg = {
  220. .type = BUTTON_TYPE_GPIO,
  221. .long_press_time = CONFIG_BUTTON_LONG_PRESS_TIME_MS,
  222. .short_press_time = CONFIG_BUTTON_SHORT_PRESS_TIME_MS,
  223. .gpio_button_config = {
  224. .gpio_num = 4,
  225. .active_level = 0,
  226. },
  227. };
  228. for( i = 0;i < 1 ;i++)
  229. {
  230. //power_btn_cfg.adc_button_config.button_index = i;
  231. //power_btn_cfg.adc_button_config.min = power_adc_value[i][0] ;
  232. //power_btn_cfg.adc_button_config.max = power_adc_value[i][1] ;
  233. user_powerbutton_handle[i] = iot_button_create(&power_btn_cfg);
  234. if(NULL == user_powerbutton_handle[i]) {
  235. printf( "Button create failed");
  236. }
  237. button_idx = i;
  238. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_PRESS_DOWN, power_single_press_cb,button_idx);
  239. #if 0
  240. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_cb,button_idx);
  241. #else
  242. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_START, power_long_press_start_cb,button_idx);
  243. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_HOLD, power_long_press_hold_cb,button_idx);
  244. iot_button_register_cb( user_powerbutton_handle[i], BUTTON_LONG_PRESS_UP, power_long_press_up_cb,button_idx);
  245. #endif
  246. //注册按键多次按键回调
  247. button_event_config_t btn_cfg;
  248. btn_cfg.event = BUTTON_MULTIPLE_CLICK;
  249. // btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_OTA;
  250. // power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  251. // iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  252. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_COUNT_RESET;
  253. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  254. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  255. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_DIS_RIGHT;
  256. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  257. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  258. btn_cfg.event_data.multiple_clicks.clicks = POWER_KEY_PRSSS_BLE_OTA_MODE;
  259. power_button_mult_time = btn_cfg.event_data.multiple_clicks.clicks ;
  260. iot_button_register_event_cb(user_powerbutton_handle[i], btn_cfg, power_button_multi_press_cb, power_button_mult_time);
  261. // //注册按键长按按键回调
  262. // button_event_config_t btn_long_press_cfg;
  263. // btn_cfg.event = BUTTON_LONG_PRESS_START;
  264. }
  265. #endif
  266. }
  267. void button_deinit(void)
  268. {
  269. printf("button_deinit\n");
  270. for(int i=0;i<KEY_NUM;i++)
  271. {
  272. iot_button_delete(user_button_handle[i]);
  273. }
  274. //iot_button_delete(user_powerbutton_handle[0]);
  275. }
  276. void power_button_init(adc_oneshot_unit_handle_t adc_handle)
  277. {
  278. }
  279. void power_button_deinit(void)
  280. {
  281. for(int i = 0;i < 3;i++)
  282. iot_button_delete(user_powerbutton_handle[i]);
  283. }
  284. uint8_t button_info = 0;
  285. #include "SPIFFS.h"
  286. #if 0
  287. static void power_long_press_cb(void *arg,void *usr_data)
  288. {
  289. switch ((int)usr_data)
  290. {
  291. case 0:
  292. printf("power key long press start\r\n");
  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. case 1:
  301. printf("power left key long press\r\n");
  302. break;
  303. }
  304. }
  305. iot_button_get_long_press_hold_cnt((button_handle_t)arg)
  306. #else
  307. static void power_long_press_start_cb(void *arg,void *usr_data)
  308. {
  309. // printf("power_long_press_start_cb\n");
  310. }
  311. static void power_long_press_hold_cb(void *arg,void *usr_data)
  312. {
  313. //设置的长按开始时间后开始计算hold_cnt 长按开始时间设置为1s,cnt计算时间间隔(SERIAL_TICKS)也设置为1s
  314. uint16_t hold_cnt = iot_button_get_long_press_hold_cnt((button_handle_t)arg);
  315. ESP_LOGE(LOG_TAG," BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
  316. switch ((int)hold_cnt)
  317. {
  318. case 2://设置模式,长按3s进入
  319. if(is_setting && is_reset_net)
  320. {
  321. ESP_LOGE(LOG_TAG,"err:OTA 和 重新配网 同时");
  322. return;
  323. }
  324. if(is_setting)
  325. {
  326. button_info = POWER_ON_INTO_OTA_VALUE;
  327. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  328. {
  329. ESP_LOGE(LOG_TAG,"queue send is fail");
  330. return;
  331. }
  332. }
  333. if(is_reset_net)
  334. {
  335. button_info = POWER_ON_INTO_RESET_VALUE;
  336. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  337. {
  338. ESP_LOGE(LOG_TAG,"queue send is fail");
  339. return;
  340. }
  341. }
  342. break;
  343. case 4://5s开关机
  344. button_info = POWER_ON_INTO_STATUS_CHANGE_VALUE;
  345. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  346. {
  347. ESP_LOGE(LOG_TAG,"queue send is fail");
  348. return;
  349. }
  350. break;
  351. default:
  352. //printf(" BUTTON_LONG_PRESS_HOLD[%d],count is [%d]\n", iot_button_get_ticks_time((button_handle_t)arg), hold_cnt);
  353. break;
  354. }
  355. }
  356. static void power_long_press_up_cb(void *arg,void *usr_data)
  357. {
  358. // printf("power_long_press_up_cb\n");
  359. }
  360. #endif
  361. #include "esp_sleep.h"
  362. static void power_single_press_cb(void *arg,void *usr_data)
  363. {
  364. switch ((int)usr_data)
  365. {
  366. case 0:
  367. printf("power sigle press\r\n");
  368. if(Machine_info.power_status == 1) //开机
  369. {
  370. button_info = POWER_ON_PRESS_VALUE;
  371. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  372. {
  373. ESP_LOGE(LOG_TAG,"queue send is fail");
  374. return;
  375. }
  376. }else
  377. {
  378. button_info = POWER_OFF_PRESS_VALUE; //关机状态下单击按键
  379. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  380. {
  381. ESP_LOGE(LOG_TAG,"queue send is fail");
  382. return;
  383. }
  384. }
  385. break;
  386. case 1:
  387. printf("power left single press\r\n");
  388. break;
  389. case 2:
  390. printf("power right single press\r\n");
  391. break;
  392. }
  393. }
  394. static void user_button_single_click_cb(void *arg,void *usr_data)
  395. {
  396. is_setting = false;
  397. is_reset_net = false;
  398. ESP_LOGW(LOG_TAG," single_click_cb[%d]", iot_button_get_ticks_time((button_handle_t)arg));
  399. switch ((int)usr_data)
  400. {
  401. case BAOYANG_KEY:
  402. button_info = 6;
  403. ESP_LOGD(LOG_TAG,"bao yang");
  404. break;
  405. case FENGCUN_KEY:
  406. button_info = 5;
  407. ESP_LOGD(LOG_TAG,"feng cun");
  408. break;
  409. case GUZHUANG_KEY:
  410. button_info = 4;
  411. ESP_LOGD(LOG_TAG,"gu zhang");
  412. break;
  413. case DAILIAO_KEY:
  414. button_info = 3;
  415. ESP_LOGD(LOG_TAG,"dai liao");
  416. break;
  417. case TINGJI_KEY:
  418. button_info = 2;
  419. ESP_LOGD(LOG_TAG,"ting ji");
  420. break;
  421. case YUNXING_KEY:
  422. button_info = 1;
  423. ESP_LOGD(LOG_TAG,"yun xing");
  424. break;
  425. default:
  426. break;
  427. }
  428. #if 0
  429. if(xSemaphoreTake(button_semaphore, 0) != true)
  430. {
  431. ESP_LOGE(LOG_TAG,"button mutex is fail,not send lora queue");
  432. return;
  433. }
  434. xSemaphoreGive(button_semaphore);
  435. #endif
  436. if(Machine_info.power_status == 0) //关机
  437. {
  438. printf("power off not refresh\r\n");
  439. return;
  440. }
  441. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  442. {
  443. ESP_LOGE(LOG_TAG,"queue send is fail");
  444. return;
  445. }
  446. }
  447. static void power_button_multi_press_cb(void *arg,void *usr_data)
  448. {
  449. if(Machine_info.power_status == 0) //关机
  450. {
  451. printf("关机状态\r\n");
  452. return;
  453. }
  454. switch ((int)usr_data)
  455. {
  456. case POWER_KEY_PRSSS_BLE_OTA_MODE:
  457. if(!is_setting && !is_reset_net) //当前非配置模式
  458. {
  459. is_setting = true;
  460. xTimerStart(btn_timer_handle, 0);
  461. printf("短按2次\r\n");
  462. }
  463. break;
  464. case POWER_KEY_PRSSS_DIS_RIGHT:
  465. printf("短按3次\r\n");
  466. send_button_key_queue((uint8_t)POWER_ON_INTO_DIS_RIGHT);
  467. break;
  468. case POWER_KEY_PRSSS_COUNT_RESET:
  469. if(!is_setting && !is_reset_net)
  470. {
  471. is_reset_net = true;
  472. xTimerStart(btn_timer_handle, 0);
  473. printf("短按4次\r\n");
  474. }
  475. break;
  476. }
  477. }
  478. static void user_button_single_UP_cb(void *arg,void *usr_data)
  479. {
  480. }
  481. void send_button_key_queue(uint8_t value)
  482. {
  483. button_info = value;
  484. if(xQueueSend(button_Data_queue,&button_info,0) != true)
  485. {
  486. ESP_LOGE(LOG_TAG,"queue send is fail");
  487. return;
  488. }
  489. }