key.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #include "key.h"
  2. #include "usart.h"
  3. #include "interface.h"
  4. //结构体
  5. LockInfo lockinfo;
  6. extern Menu_table menu;
  7. extern TipsFlag tipsflag;
  8. struct keys key[1]={0};
  9. //布尔
  10. bool sent1_key = false;
  11. bool sent2_key = false;
  12. bool sent3_key = false;
  13. bool sent4_key = false;
  14. uint32_t enterInterface_time =0;
  15. uint8_t preworkUp = 0;
  16. uint8_t onOff_Num=0;
  17. uint8_t KEY_1_Scan_PDA(void)
  18. {
  19. static uint8_t key_state = 0;
  20. static uint8_t last_key_state = 0;
  21. static uint32_t debounce_time = 0;
  22. key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3);
  23. if (key_state != last_key_state) // 检测按键状态变化
  24. {
  25. last_key_state = key_state;
  26. debounce_time = HAL_GetTick(); // 记录当前时间
  27. }
  28. else
  29. {
  30. if (HAL_GetTick() - debounce_time > 30) // 去抖时间
  31. {
  32. if (key_state == GPIO_PIN_RESET) // 按键按下
  33. {
  34. if(!sent1_key)
  35. {
  36. enterInterface_time = HAL_GetTick();
  37. switch(menu.current)
  38. {
  39. case 0:
  40. {
  41. SET_MENU_STATUS(0,0,0,0);
  42. break;
  43. }
  44. case 1:
  45. {
  46. SET_MENU_STATUS(1,1,0,1);
  47. break;
  48. }
  49. case 2:
  50. case 3:
  51. {
  52. SWITCH_MENU_UP4();
  53. break;
  54. }
  55. case 8://L绑定
  56. {
  57. if(menu.up != macsetptr->macCount) { \
  58. menu.up += 1; \
  59. menu.next -= 1; \
  60. } else if(menu.up == macsetptr->macCount) { \
  61. menu.up = 0; \
  62. menu.next = macsetptr->macCount; \
  63. } \
  64. refresh = true; \
  65. break;
  66. }
  67. case 4:
  68. case 5:
  69. case 6:
  70. case 7:
  71. {
  72. SWITCH_MENU_UP2();//上翻2选项
  73. break;
  74. }
  75. }
  76. #if debugkeyinfo == 1
  77. PRINT_MENU_STATUS("K1");
  78. #endif
  79. sent1_key = true;
  80. }
  81. return 1;
  82. }
  83. else // 按键释放
  84. {
  85. sent1_key = false;
  86. return 0;
  87. }
  88. }
  89. }
  90. return 0;
  91. }
  92. uint8_t KEY_2_Scan_PDA(void)
  93. {
  94. static uint8_t key_state = 0;
  95. static uint8_t last_key_state = 0;
  96. static uint32_t debounce_time = 0;
  97. key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4);
  98. if (key_state != last_key_state) // 检测按键状态变化
  99. {
  100. last_key_state = key_state;
  101. debounce_time = HAL_GetTick(); // 记录当前时间
  102. }
  103. else
  104. {
  105. if (HAL_GetTick() - debounce_time > 30) // 去抖时间
  106. {
  107. if (key_state == GPIO_PIN_RESET) // 按键按下
  108. {
  109. if (!sent2_key)
  110. {
  111. enterInterface_time = HAL_GetTick();
  112. switch (menu.current)
  113. {
  114. case 0:
  115. {
  116. //SET_MENU_STATUS(1,0,0,0);
  117. break;
  118. }
  119. case 1:
  120. {
  121. SET_MENU_STATUS(1,0,1,1);
  122. break;
  123. }
  124. case 2:
  125. {
  126. SWITCH_MENU_DOWN4();//四选项选择
  127. break;
  128. }
  129. case 3:
  130. case 8://L绑定
  131. {
  132. if(menu.next!=macsetptr->macCount){
  133. menu.up-=1;
  134. menu.next+=1;
  135. } else if(menu.next==macsetptr->macCount) {
  136. menu.up=macsetptr->macCount;
  137. menu.next=0;
  138. }
  139. refresh=true;
  140. break;
  141. }
  142. case 4:
  143. case 5:
  144. case 6:
  145. case 7:
  146. {
  147. SWITCH_MENU_DOWN2();//下翻2选项
  148. break;
  149. }
  150. }
  151. #if debugkeyinfo == 1
  152. PRINT_MENU_STATUS("K2");
  153. #endif
  154. sent2_key = true;
  155. }
  156. return 1;
  157. }
  158. else // 按键释放
  159. {
  160. sent2_key = false;
  161. return 0;
  162. }
  163. }
  164. }
  165. return 0;
  166. }
  167. uint8_t preCurrent = 0;
  168. uint8_t preBind = 0;
  169. bool L1work = false;
  170. bool L2work = false;
  171. bool L3work = false;
  172. bool L4work = false;
  173. char macStr[20][13]; // 长度为13的字符串加上字符串结束符'\0'
  174. uint8_t KEY_3_Scan_PDA(void)
  175. {
  176. static uint8_t key_state = 0;
  177. static uint8_t last_key_state = 0;
  178. static uint32_t debounce_time = 0;
  179. key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5);
  180. if (key_state != last_key_state) // 检测按键状态变化
  181. {
  182. last_key_state = key_state;
  183. debounce_time = HAL_GetTick(); // 记录当前时间
  184. }
  185. else
  186. {
  187. if (HAL_GetTick() - debounce_time > 30) // 去抖时间
  188. {
  189. if (key_state == GPIO_PIN_RESET) // 按键按下
  190. {
  191. if (!sent3_key) {
  192. enterInterface_time = HAL_GetTick();
  193. switch (menu.current)
  194. {
  195. case 0://跳转到菜单界面
  196. {
  197. SET_MENU_STATUS(1,1,0,1);//跳转操作界面
  198. break;
  199. }
  200. case 1://跳转到L绑定界面
  201. {
  202. if(menu.up==1)
  203. {
  204. SET_MENU_STATUS(2,3,0,2);
  205. }
  206. else
  207. {
  208. SET_MENU_STATUS(3,3,0,2);
  209. }
  210. break;
  211. }
  212. case 2:
  213. {
  214. preBind = menu.up;
  215. Flash_ReadBytes(&macsetptr->macCount,MacDbHeadAddr,1);//获取共存多少MAC
  216. printf("%04X\r\n",macsetptr->macCount);
  217. char mac[13*macsetptr->macCount];//char数组需要预留足够的空间,也就是13个字符,因为还需要包含字符串结尾的'\0'
  218. Flash_ReadBytes_mac((uint16_t*)macsetptr->mac,MacDbBodyAddr,(3*macsetptr->macCount));//读MAC
  219. bin2hex(mac, (unsigned char *)macsetptr->mac, (6*macsetptr->macCount));//将uint16数组转换成char数组 (FLASH中的字符串)
  220. // 分割mac并放入macStr数组中
  221. for (int i = 0; i < macsetptr->macCount; i++) {
  222. strncpy(macStr[i], mac + i * 12, 12); // 复制13个字符到macStr中
  223. macStr[i][12] = '\0'; // 添加字符串结束符
  224. }
  225. // 打印分割后的字符串
  226. for (int i = 0; i < macsetptr->macCount; i++) {
  227. printf("macStr[%d]: %s\n", i, macStr[i]);
  228. }
  229. SET_MENU_STATUS(8,macsetptr->macCount,0,2);
  230. break;
  231. }
  232. case 8:
  233. {
  234. if(preBind==3)//L1确认绑定
  235. {
  236. L1bd=true;
  237. sprintf(lockinfo.SN1name, "%s", macStr[menu.next]+8);
  238. Flash_WriteBytes(macsetptr->mac+menu.next*6,Sn1InfoAddr,3);
  239. break;
  240. }
  241. if(preBind==2)//L2确认绑定
  242. {
  243. L2bd=true;
  244. sprintf(lockinfo.SN2name, "%s", macStr[menu.next]+8);
  245. Flash_WriteBytes(macsetptr->mac+menu.next*6,Sn2InfoAddr,3);
  246. break;
  247. }
  248. if(preBind==1)//L3确认绑定
  249. {
  250. L3bd=true;
  251. sprintf(lockinfo.SN3name, "%s", macStr[menu.next]+8);
  252. Flash_WriteBytes(macsetptr->mac+menu.next*6,Sn3InfoAddr,3);
  253. break;
  254. }
  255. if(preBind==0)//L4确认绑定
  256. {
  257. L4bd=true;
  258. sprintf(lockinfo.SN4name, "%s", macStr[menu.next]+8);
  259. Flash_WriteBytes(macsetptr->mac+menu.next*6,Sn4InfoAddr,3);
  260. break;
  261. }
  262. }
  263. case 3:
  264. {
  265. if(menu.up==3)//L1操作
  266. {
  267. preworkUp=menu.up;
  268. SET_MENU_STATUS(4,1,0,3);
  269. break;
  270. }
  271. if(menu.up==2)//2操作
  272. {
  273. preworkUp=menu.up;
  274. SET_MENU_STATUS(5,1,0,3);
  275. break;
  276. }
  277. if(menu.up==1)//3操作
  278. {
  279. preworkUp=menu.up;
  280. SET_MENU_STATUS(6,1,0,3);
  281. break;
  282. }
  283. if(menu.up==0)//4操作
  284. {
  285. preworkUp=menu.up;
  286. SET_MENU_STATUS(7,1,0,3);
  287. break;
  288. }
  289. }
  290. case 4:
  291. case 5:
  292. case 6:
  293. case 7:
  294. {
  295. preCurrent = menu.current;
  296. onOff_Num = menu.up;//1:施封 0:解封
  297. printf("%d\r\n",onOff_Num);
  298. if(preworkUp == 3) //L1
  299. {
  300. }
  301. else if(preworkUp == 2) //L2
  302. {
  303. }
  304. else if(preworkUp == 1) //L3
  305. {
  306. }
  307. else if(preworkUp == 0) //L4
  308. {
  309. }
  310. }
  311. }
  312. #if debugkeyinfo == 1
  313. PRINT_MENU_STATUS("K3");
  314. #endif
  315. sent3_key = true;
  316. }
  317. return 1;
  318. }
  319. else // 按键释放
  320. {
  321. sent3_key = false;
  322. return 0;
  323. }
  324. }
  325. }
  326. return 0;
  327. }
  328. uint8_t KEY_4_Scan_PDA(void)
  329. {
  330. static uint8_t key_state = 0;
  331. static uint8_t last_key_state = 0;
  332. static uint32_t debounce_time = 0;
  333. key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6);
  334. if (key_state != last_key_state) // 检测按键状态变化
  335. {
  336. last_key_state = key_state;
  337. debounce_time = HAL_GetTick(); // 记录当前时间
  338. }
  339. else
  340. {
  341. if (HAL_GetTick() - debounce_time > 30) // 去抖时间
  342. {
  343. if (key_state == GPIO_PIN_RESET) // 按键按下
  344. {
  345. enterInterface_time = HAL_GetTick();
  346. if (!sent4_key) {//返回
  347. switch (menu.current)
  348. {
  349. case 0:
  350. case 1:
  351. {
  352. SET_MENU_STATUS(0,0,0,0);
  353. break;
  354. }
  355. case 2:
  356. case 3:
  357. {
  358. SET_MENU_STATUS(1,1,0,1);
  359. break;
  360. }
  361. case 4:
  362. case 5:
  363. case 6:
  364. case 7:
  365. {
  366. USART_SendString(&huart5, "AT+DISCONN=0\r\n");//断开连接
  367. SET_MENU_STATUS(3,3,0,2);
  368. break;
  369. }
  370. case 8:
  371. {
  372. SET_MENU_STATUS(2,3,0,2);
  373. break;
  374. }
  375. }
  376. #if debugkeyinfo == 1
  377. PRINT_MENU_STATUS("K4");
  378. #endif
  379. sent4_key = true;
  380. }
  381. return 1;
  382. }
  383. else // 按键释放
  384. {
  385. sent4_key = false;
  386. return 0;
  387. }
  388. }
  389. }
  390. return 0;
  391. }
  392. void updatekeystate(void)
  393. {
  394. KEY_1_Scan_PDA();
  395. KEY_2_Scan_PDA();
  396. KEY_3_Scan_PDA();
  397. KEY_4_Scan_PDA();
  398. }