#include "key.h" #include "usart.h" #include "interface.h" extern Menu_table menu; extern TipsFlag tipsflag; bool sent1_key = false; bool sent2_key = false; bool sent3_key = false; bool sent4_key = false; struct keys key[1]={0}; char timeout[] = "+CONNECT TIMEOUT"; uint16_t S1 = 0; uint16_t S2 = 0; uint16_t S3 = 0; uint16_t S4 = 0; uint32_t enterInterface_time = 0; void ClearBtSnItem(void); uint8_t KEY_1_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_3); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间 { if (key_state == GPIO_PIN_RESET) // 按键按下 { if(!sent1_key) { enterInterface_time = HAL_GetTick(); switch(menu.current) { case 0: { scanMac(); SET_MENU_STATUS(0,0,0,0); break; } case 1: { SET_MENU_STATUS(1,1,0,1); break; } case 2: { if(menu.up!=4) { menu.up+=1; menu.next-=1; } else if(menu.up==4) { menu.up=0; menu.next=4; } refresh=true; break; } case 3: { if(menu.up!=3) { menu.up+=1; menu.next-=1; } else if(menu.up==3) { menu.up=0; menu.next=3; } refresh=true; break; } case 4: case 5: case 6: case 7: case 0xA0: { if(menu.up!=1) { menu.up+=1; menu.next-=1; } else if(menu.up==1) { menu.up=0; menu.next=1; } refresh=true; break; } case 0xA1: { if(menu.up!=0xE) { menu.up+=1; menu.next-=1; } else if(menu.up==0xE) { menu.up=0; menu.next=0xE; } refresh=true; break; } case 0xA2: { if(menu.up!=0xF) { menu.up+=1; menu.next-=1; } else if(menu.up==0xF) { menu.up=0; menu.next=0xF; } refresh=true; break; } } #if debugkeyinfo == 1 PRINT_MENU_STATUS("K1"); #endif sent1_key = true; } return 1; } else // 按键释放 { sent1_key = false; return 0; } } } return 0; } uint8_t KEY_2_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间 { if (key_state == GPIO_PIN_RESET) // 按键按下 { if (!sent2_key) { enterInterface_time = HAL_GetTick(); switch (menu.current) { case 0: { scanMac(); SET_MENU_STATUS(0,0,0,0); break; } case 1: { SET_MENU_STATUS(1,0,1,1); break; } case 2: { if(menu.next!=4) { menu.up-=1; menu.next+=1; } else if(menu.next==4) { menu.up=4; menu.next=0; } refresh=true; break; } case 3: { if(menu.next!=3) { menu.up-=1; menu.next+=1; } else if(menu.next==3) { menu.up=3; menu.next=0; } refresh=true; break; } case 4: case 5: case 6: case 7: case 0xA0: { if(menu.next!=1) { menu.up-=1; menu.next+=1; } else if(menu.next==1) { menu.up=1; menu.next=0; } refresh=true; break; } case 0xA1: { if(menu.next!=0xE) { menu.up-=1; menu.next+=1; } else if(menu.next==0xE) { menu.up=0xE; menu.next=0; } refresh=true; break; } case 0xA2: { if(menu.next!=0xF) { menu.up-=1; menu.next+=1; } else if(menu.next==0xF) { menu.up=0xF; menu.next=0; } refresh=true; break; } } #if debugkeyinfo == 1 PRINT_MENU_STATUS("K2"); #endif sent2_key = true; } return 1; } else // 按键释放 { sent2_key = false; return 0; } } } return 0; } // 定义menu_dict存储menu.next对应的信息 MenuInfo menu_dict[] = { {"AT+CONN=101000000001\r\n", "+CONNECTED:0,101000000001", "+DISCONN:0,101000000001", 0x0001}, {"AT+CONN=101000000002\r\n", "+CONNECTED:0,101000000002", "+DISCONN:0,101000000002", 0x0002}, {"AT+CONN=101000000003\r\n", "+CONNECTED:0,101000000003", "+DISCONN:0,101000000003", 0x0003}, {"AT+CONN=101000000004\r\n", "+CONNECTED:0,101000000004", "+DISCONN:0,101000000004", 0x0004}, {"AT+CONN=101000000005\r\n", "+CONNECTED:0,101000000005", "+DISCONN:0,101000000005", 0x0005}, {"AT+CONN=101000000006\r\n", "+CONNECTED:0,101000000006", "+DISCONN:0,101000000006", 0x0006}, {"AT+CONN=101000000007\r\n", "+CONNECTED:0,101000000007", "+DISCONN:0,101000000007", 0x0007}, {"AT+CONN=101000000008\r\n", "+CONNECTED:0,101000000008", "+DISCONN:0,101000000008", 0x0008}, {"AT+CONN=101000000009\r\n", "+CONNECTED:0,101000000009", "+DISCONN:0,101000000009", 0x0009}, {"AT+CONN=10100000000A\r\n", "+CONNECTED:0,10100000000A", "+DISCONN:0,10100000000A", 0x000A}, {"AT+CONN=10100000000B\r\n", "+CONNECTED:0,10100000000B", "+DISCONN:0,10100000000B", 0x000B}, {"AT+CONN=10100000000C\r\n", "+CONNECTED:0,10100000000C", "+DISCONN:0,10100000000C", 0x000C}, {"AT+CONN=10100000000D\r\n", "+CONNECTED:0,10100000000D", "+DISCONN:0,10100000000D", 0x000D}, {"AT+CONN=10100000000E\r\n", "+CONNECTED:0,10100000000E", "+DISCONN:0,10100000000E", 0x000E}, {"AT+CONN=10100000000F\r\n", "+CONNECTED:0,10100000000F", "+DISCONN:0,10100000000F", 0x000F}, {"AT+CONN=101000000010\r\n", "+CONNECTED:0,101000000010", "+DISCONN:0,101000000010", 0x0010}, {"AT+CONN=101000000011\r\n", "+CONNECTED:0,101000000011", "+DISCONN:0,101000000011", 0x0011}, {"AT+CONN=101000000012\r\n", "+CONNECTED:0,101000000012", "+DISCONN:0,101000000012", 0x0012}, {"AT+CONN=101000000013\r\n", "+CONNECTED:0,101000000013", "+DISCONN:0,101000000013", 0x0013}, {"AT+CONN=101000000014\r\n", "+CONNECTED:0,101000000014", "+DISCONN:0,101000000014", 0x0014}, {"AT+CONN=101000000015\r\n", "+CONNECTED:0,101000000015", "+DISCONN:0,101000000015", 0x0015}, {"AT+CONN=101000000016\r\n", "+CONNECTED:0,101000000016", "+DISCONN:0,101000000016", 0x0016}, {"AT+CONN=101000000017\r\n", "+CONNECTED:0,101000000017", "+DISCONN:0,101000000017", 0x0017}, {"AT+CONN=101000000018\r\n", "+CONNECTED:0,101000000018", "+DISCONN:0,101000000018", 0x0018}, {"AT+CONN=101000000019\r\n", "+CONNECTED:0,101000000019", "+DISCONN:0,101000000019", 0x0019}, {"AT+CONN=10100000001A\r\n", "+CONNECTED:0,10100000001A", "+DISCONN:0,10100000001A", 0x001A}, {"AT+CONN=10100000001B\r\n", "+CONNECTED:0,10100000001B", "+DISCONN:0,10100000001B", 0x001B}, {"AT+CONN=10100000001C\r\n", "+CONNECTED:0,10100000001C", "+DISCONN:0,10100000001C", 0x001C}, {"AT+CONN=10100000001D\r\n", "+CONNECTED:0,10100000001D", "+DISCONN:0,10100000001D", 0x001D}, {"AT+CONN=10100000001E\r\n", "+CONNECTED:0,10100000001E", "+DISCONN:0,10100000001E", 0x001E}, {"AT+CONN=10100000001F\r\n", "+CONNECTED:0,10100000001F", "+DISCONN:0,10100000001F", 0x001F}, // 其他项依次类推 }; uint8_t KEY_3_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_5); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间 { if (key_state == GPIO_PIN_RESET) // 按键按下 { if (!sent3_key) { enterInterface_time = HAL_GetTick(); switch (menu.current) { case 0://跳转到菜单界面 { SET_MENU_STATUS(1,1,0,1); break; } case 1://跳转到L绑定界面 { if(menu.up==1) { scanMac(); SET_MENU_STATUS(2,4,0,2); } else { scanMac(); SET_MENU_STATUS(3,3,0,2); } break; } case 2: { if(menu.up==0) { //清空SN ClearBtSnItem(); SET_MENU_STATUS(1,1,0,1); break; } else { menu.PreNext=menu.next;//L1 - L4 SET_MENU_STATUS(0xA0,1,0,3); break; } } case 3://施解封操作 { if(menu.up==3)//L1操作 { sendCmd_BT(menu.SN1,menu.SN1F,3,3); HAL_Delay(100); SET_MENU_STATUS(4,1,0,3); break; } if(menu.up==2)//2操作 { sendCmd_BT(menu.SN2,menu.SN2F,3,3); HAL_Delay(100); SET_MENU_STATUS(5,1,0,3); break; } if(menu.up==1)//3操作 { sendCmd_BT(menu.SN3,menu.SN3F,3,3); HAL_Delay(100); SET_MENU_STATUS(6,1,0,3); break; } if(menu.up==0)//4操作 { sendCmd_BT(menu.SN4,menu.SN4F,3,3); HAL_Delay(100); SET_MENU_STATUS(7,1,0,3); break; } } case 4: case 5: case 6: case 7: { menu.enter=4; break; } case 0xA0: { if(menu.up==1) { SET_MENU_STATUS(0xA1,0xE,0,3); } else if(menu.up==0) { SET_MENU_STATUS(0xA2,0xF,0,3); } break; } case 0xA1: case 0xA2: { if(menu.PreNext==0)//L1 写 { if(menu.current!=0xA2) { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if (menu.next == i) { erase_flash(Sn1StatusAddr); erase_flash(Sn1Addr); erase_flash(serialNum1Addr); menu.SN1 = menu_dict[i].command; menu.SN1F = menu_dict[i].connected_msg; menu.SN1D = menu_dict[i].disconnected_msg; Write_Information(Sn1Addr, menu_dict[i].value); L1bd=true; } } } else { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if ((menu.next+0xf) == i) { menu.SN1 = menu_dict[i].command; menu.SN1F = menu_dict[i].connected_msg; menu.SN1D = menu_dict[i].disconnected_msg; Write_Information(Sn1Addr, menu_dict[i].value); L1bd=true; } } } menu.home &= 0x0FFF; } if(menu.PreNext==1)//L2 写 { if(menu.current!=0xA2) { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if (menu.next == i) { menu.SN2 = menu_dict[i].command; menu.SN2F = menu_dict[i].connected_msg; menu.SN2D = menu_dict[i].disconnected_msg; Write_Information(Sn2Addr, menu_dict[i].value); L2bd=true; } } } else { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if ((menu.next+0xf) == i) { menu.SN2 = menu_dict[i].command; menu.SN2F = menu_dict[i].connected_msg; menu.SN2D = menu_dict[i].disconnected_msg; Write_Information(Sn2Addr, menu_dict[i].value); L2bd=true; } } } menu.home &= 0xF0FF; } if(menu.PreNext==2)//L3 写 { if(menu.current!=0xA2) { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if (menu.next == i) { menu.SN3 = menu_dict[i].command; menu.SN3F = menu_dict[i].connected_msg; menu.SN3D = menu_dict[i].disconnected_msg; Write_Information(Sn3Addr, menu_dict[i].value); L3bd=true; } } } else { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if ((menu.next+0xf) == i) { menu.SN3 = menu_dict[i].command; menu.SN3F = menu_dict[i].connected_msg; menu.SN3D = menu_dict[i].disconnected_msg; Write_Information(Sn3Addr, menu_dict[i].value); L3bd=true; } } } menu.home &= 0xFF0F; } if(menu.PreNext==3)//L4 写 { if(menu.current!=0xA2) { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if (menu.next == i) { menu.SN4 = menu_dict[i].command; menu.SN4F = menu_dict[i].connected_msg; menu.SN4D = menu_dict[i].disconnected_msg; Write_Information(Sn4Addr, menu_dict[i].value); L4bd=true; } } } else { for (int i = 0; i < sizeof(menu_dict) / sizeof(menu_dict[0]); i++) { if ((menu.next+0xf) == i) { menu.SN4 = menu_dict[i].command; menu.SN4F = menu_dict[i].connected_msg; menu.SN4D = menu_dict[i].disconnected_msg; Write_Information(Sn4Addr, menu_dict[i].value); L4bd=true; } } } menu.home &= 0xFFF0; } readLockStatusinit(); scanMac(); SET_MENU_STATUS(2,4,0,2); break; } } #if debugkeyinfo == 1 PRINT_MENU_STATUS("K3"); #endif sent3_key = true; } return 1; } else // 按键释放 { sent3_key = false; return 0; } } } return 0; } uint8_t KEY_4_Scan(void) { static uint8_t key_state = 0; static uint8_t last_key_state = 0; static uint32_t debounce_time = 0; key_state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6); if (key_state != last_key_state) // 检测按键状态变化 { last_key_state = key_state; debounce_time = HAL_GetTick(); // 记录当前时间 } else { if (HAL_GetTick() - debounce_time > 30) // 去抖时间 { if (key_state == GPIO_PIN_RESET) // 按键按下 { enterInterface_time = HAL_GetTick(); if (!sent4_key) {//返回 switch (menu.current) { case 0: case 1: { scanMac(); SET_MENU_STATUS(0,0,0,0); break; } case 2: case 3: { scanMac(); SET_MENU_STATUS(1,1,0,1); break; } case 4: case 5: case 6: case 7: { sendCmd_BT("AT+DISCONN=0\r\n","ERROR",1,1);//断开连接 HAL_Delay(30); S1 = read_flash_16(Sn1StatusAddr); S2 = read_flash_16(Sn2StatusAddr); S3 = read_flash_16(Sn3StatusAddr); S4 = read_flash_16(Sn4StatusAddr); scanMac(); SET_MENU_STATUS(3,3,0,2); break; } case 0xA0: { SET_MENU_STATUS(2,4,0,2); break; } case 0xA1: case 0xA2: { S1 = read_flash_16(Sn1StatusAddr); S2 = read_flash_16(Sn2StatusAddr); S3 = read_flash_16(Sn3StatusAddr); S4 = read_flash_16(Sn4StatusAddr); HAL_Delay(30); scanMac(); SET_MENU_STATUS(0xA0,1,0,3); break; } } #if debugkeyinfo == 1 PRINT_MENU_STATUS("K4"); #endif sent4_key = true; } return 1; } else // 按键释放 { sent4_key = false; return 0; } } } return 0; } void updatekeystate(void) { KEY_1_Scan(); KEY_2_Scan(); KEY_3_Scan(); KEY_4_Scan(); } // 定义存储信息的结构体数组 struct BtMenuItem btMenuItems[] = { {0x0001, "0001", "AT+CONN=101000000001\r\n", "+CONNECTED:0,101000000001"}, {0x0002, "0002", "AT+CONN=101000000002\r\n", "+CONNECTED:0,101000000002"}, {0x0003, "0003", "AT+CONN=101000000003\r\n", "+CONNECTED:0,101000000003"}, {0x0004, "0004", "AT+CONN=101000000004\r\n", "+CONNECTED:0,101000000004"}, {0x0005, "0005", "AT+CONN=101000000005\r\n", "+CONNECTED:0,101000000005"}, {0x0006, "0006", "AT+CONN=101000000006\r\n", "+CONNECTED:0,101000000006"}, {0x0007, "0007", "AT+CONN=101000000007\r\n", "+CONNECTED:0,101000000007"}, {0x0008, "0008", "AT+CONN=101000000008\r\n", "+CONNECTED:0,101000000008"}, {0x0009, "0009", "AT+CONN=101000000009\r\n", "+CONNECTED:0,101000000009"}, {0x000A, "000A", "AT+CONN=10100000000A\r\n", "+CONNECTED:0,10100000000A"}, {0x000B, "000B", "AT+CONN=10100000000B\r\n", "+CONNECTED:0,10100000000B"}, {0x000C, "000C", "AT+CONN=10100000000C\r\n", "+CONNECTED:0,10100000000C"}, {0x000D, "000D", "AT+CONN=10100000000D\r\n", "+CONNECTED:0,10100000000D"}, {0x000E, "000E", "AT+CONN=10100000000E\r\n", "+CONNECTED:0,10100000000E"}, {0x000F, "000F", "AT+CONN=10100000000F\r\n", "+CONNECTED:0,10100000000F"}, {0x0010, "0010", "AT+CONN=101000000010\r\n", "+CONNECTED:0,101000000010"}, {0x0011, "0011", "AT+CONN=101000000011\r\n", "+CONNECTED:0,101000000011"}, {0x0012, "0012", "AT+CONN=101000000012\r\n", "+CONNECTED:0,101000000012"}, {0x0013, "0013", "AT+CONN=101000000013\r\n", "+CONNECTED:0,101000000013"}, {0x0014, "0014", "AT+CONN=101000000014\r\n", "+CONNECTED:0,101000000014"}, {0x0015, "0015", "AT+CONN=101000000015\r\n", "+CONNECTED:0,101000000015"}, {0x0016, "0016", "AT+CONN=101000000016\r\n", "+CONNECTED:0,101000000016"}, {0x0017, "0017", "AT+CONN=101000000017\r\n", "+CONNECTED:0,101000000017"}, {0x0018, "0018", "AT+CONN=101000000018\r\n", "+CONNECTED:0,101000000018"}, {0x0019, "0019", "AT+CONN=101000000019\r\n", "+CONNECTED:0,101000000019"}, {0x001A, "001A", "AT+CONN=10100000001A\r\n", "+CONNECTED:0,10100000001A"}, {0x001B, "001B", "AT+CONN=10100000001B\r\n", "+CONNECTED:0,10100000001B"}, {0x001C, "001C", "AT+CONN=10100000001C\r\n", "+CONNECTED:0,10100000001C"}, {0x001D, "001D", "AT+CONN=10100000001D\r\n", "+CONNECTED:0,10100000001D"}, {0x001E, "001E", "AT+CONN=10100000001E\r\n", "+CONNECTED:0,10100000001E"}, {0x001F, "001F", "AT+CONN=10100000001F\r\n", "+CONNECTED:0,10100000001F"}, }; void scanMac(void) { uint16_t SN1 = read_flash_16(Sn1Addr); uint16_t SN2 = read_flash_16(Sn2Addr); uint16_t SN3 = read_flash_16(Sn3Addr); uint16_t SN4 = read_flash_16(Sn4Addr); if(SN1==0xffff) { SN1=0; } if(SN2==0xffff) { SN2=0; } if(SN3==0xffff) { SN3=0; } if(SN4==0xffff) { SN4=0; } locksnu8.L1snT8[0] = SN1>>8&0xff; locksnu8.L1snT8[1] = SN1&0xff; locksnu8.L2snT8[0] = SN2>>8&0xff; locksnu8.L2snT8[1] = SN2&0xff; locksnu8.L3snT8[0] = SN3>>8&0xff; locksnu8.L3snT8[1] = SN3&0xff; locksnu8.L4snT8[0] = SN4>>8&0xff; locksnu8.L4snT8[1] = SN4&0xff; // 匹配并设置属性 for (int i = 0; i < sizeof(btMenuItems)/sizeof(btMenuItems[0]); i++) { if (SN1 == (uint16_t)i + 1) { menu.SN1name = btMenuItems[i].name; menu.SN1 = btMenuItems[i].command; menu.SN1F = btMenuItems[i].feedback; L1bd = true; break; } } for (int i = 0; i < sizeof(btMenuItems)/sizeof(btMenuItems[0]); i++) { if (SN2 == (uint16_t)i + 1) { menu.SN2name = btMenuItems[i].name; menu.SN2 = btMenuItems[i].command; menu.SN2F = btMenuItems[i].feedback; L2bd = true; break; } } for (int i = 0; i < sizeof(btMenuItems)/sizeof(btMenuItems[0]); i++) { if (SN3 == (uint16_t)i + 1) { menu.SN3name = btMenuItems[i].name; menu.SN3 = btMenuItems[i].command; menu.SN3F = btMenuItems[i].feedback; L3bd = true; break; } } for (int i = 0; i < sizeof(btMenuItems)/sizeof(btMenuItems[0]); i++) { if (SN4 == (uint16_t)i + 1) { menu.SN4name = btMenuItems[i].name; menu.SN4 = btMenuItems[i].command; menu.SN4F = btMenuItems[i].feedback; L4bd = true; break; } } } void ClearBtSnItem(void) { menu.SN1name=NULL; menu.SN1=NULL; menu.SN1F=NULL; menu.SN1D=NULL; erase_flash(Sn1StatusAddr); erase_flash(Sn1Addr); erase_flash(serialNum1Addr); L1bd=false; menu.SN2name=NULL; menu.SN2=NULL; menu.SN2F=NULL; menu.SN2D=NULL; menu.SN3name=NULL; menu.SN3=NULL; menu.SN3F=NULL; menu.SN3D=NULL; menu.SN4name=NULL; menu.SN4=NULL; menu.SN4F=NULL; menu.SN4D=NULL; erase_flash(Sn2StatusAddr); erase_flash(Sn3StatusAddr); erase_flash(Sn4StatusAddr); erase_flash(Sn2Addr); erase_flash(Sn3Addr); erase_flash(Sn4Addr); erase_flash(serialNum2Addr); erase_flash(serialNum3Addr); erase_flash(serialNum4Addr); L2bd=false; L3bd=false; L4bd=false; menu.home=0x10000;//其他地址 }