#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;
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)
				{
					switch(menu.current)
					{
							case 0:
							{
								SET_MENU_STATUS(0,0,0,0);
								break;
							}
							case 1:
							{
								SET_MENU_STATUS(1,1,0,1);
								break;
							}
							case 2:
							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:
							{
								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 8:
							case 9:
							case 0xa:
							case 0xb:
							case 0x0D:
							case 0xA1:
							{
								if(menu.up!=0xF)
								{
									menu.up+=1;
									menu.next-=1;
								}
								else if(menu.up==0xF)
								{
									menu.up=0;
									menu.next=0xF;
								}
								refresh=true;
								break;
							}

								
					}
					//PRINT_MENU_STATUS("K1");
					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) // 去抖时间大于50ms
        {
            if (key_state == GPIO_PIN_RESET) // 按键按下
            {
				if (!sent2_key)
				{
					switch (menu.current)
					{
						case 0:
						{
							SET_MENU_STATUS(0,0,0,0);
							break;
						}
						case 1:
						{
							SET_MENU_STATUS(1,0,1,1);
							break;
						}
						case 2:
						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:
						{
							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 8:
						case 9:
						case 0xa:
						case 0xb:
						case 0x0D:
						case 0xA1:
						{
							if(menu.next!=0xF)
							{
								menu.up-=1;
								menu.next+=1;
							}
							else if(menu.next==0xF)
							{
								menu.up=0xF;
								menu.next=0;
							}
							refresh=true;
							break;
						}
					}
					//PRINT_MENU_STATUS("K2");
					sent2_key = true;
				}
				return 1;
            }
            else // 按键释放
            {
				sent2_key = false;
                return 0;
            }
        }
    }

    return 0;
}

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) // 去抖时间大于50ms
        {
            if (key_state == GPIO_PIN_RESET) // 按键按下
            {
				if (!sent3_key) {
					switch (menu.current) {
						case 0://跳转到菜单界面
						{
							SET_MENU_STATUS(1,1,0,1);
							break;
						}
						case 1://跳转到L绑定界面
						{
							if(menu.up==1)
							{
								SET_MENU_STATUS(2,3,0,2);
							}
							else
							{
								scanMac();
								SET_MENU_STATUS(3,3,0,2);
							}
							break;
						}
						case 2:
						{
							menu.PreNext=menu.next;
							SET_MENU_STATUS(0xA1,0xF,0,3);
							//printf("%04x",menu.PreNext);
							break;
						}
						case 3:
						{
							if(menu.up==3)//L1操作
							{
//								if(sendCmd_BT(menu.SN1,menu.SN1F,2,5))//L1连接
//								{
//									//HAL_Delay(1200);
//									printf("success\r\n");
//									SET_MENU_STATUS(4,1,0,3);
//									break;
//								}
//								if(sendCmd_BT(menu.SN1,menu.SN1D,2,5))
//								{
//									sendCmd_BT(menu.SN1,menu.SN1F,2,5);
//									SET_MENU_STATUS(4,1,0,3);
//									break;
//								}
//								if(sendCmd_BT(menu.SN1,timeout,2,5))
//								{
//									sendCmd_BT(menu.SN1,menu.SN1F,2,5);
//									SET_MENU_STATUS(4,1,0,3);
//									break;
//								}
//								else
//								{
//									printf("fail\r\n");
//									break;
//								}
								sendCmd_BT(menu.SN1,menu.SN1F,3,3);
								HAL_Delay(500);
								SET_MENU_STATUS(4,1,0,3);
								break;
							}
							if(menu.up==2)//2操作
							{
								if(sendCmd_BT(menu.SN2,menu.SN2F,2,3))//L2连接
								{
									//HAL_Delay(1200);
									printf("success\r\n");
									SET_MENU_STATUS(5,1,0,3);
								}
								else
								{
									printf("fail\r\n");
								}
								break;
							}
//							if(menu.up==1)//3操作
//							{
//								if(sendCmd_BT(menu.SN3,menu.SN3F,2,3))//L3连接
//								{
//									HAL_Delay(1200);
//									printf("success\r\n");
//									SET_MENU_STATUS(6,1,0,3);
//								}
//								else
//								{
//									printf("fail\r\n");
//								}
//								break;
//							}						
//							if(menu.up==0)//3操作
//							{
//								if(sendCmd_BT(menu.SN4,menu.SN4F,2,3))//L4连接
//								{
//									HAL_Delay(1200);
//									printf("success\r\n");
//									SET_MENU_STATUS(7,1,0,3);
//								}
//								else
//								{
//									printf("fail\r\n");
//								}
//								break;
//							}			
						}
						case 4:
						case 5:
						case 6:
						case 7:
						{
							menu.enter=4;
							break;
						}
						case 8:
						case 9:
						case 0xa:
						case 0xb:
						case 0xd:
							
						case 0xA1:
						{
							if(menu.PreNext==0)//L1 写
							{
								if(menu.next==0)
								{
									menu.SN1="AT+CONN=101000000001\r\n";
									menu.SN1F="+CONNECTED:0,101000000001";
									menu.SN1D="+DISCONN:0,101000000001";
									Write_Information(Sn1Addr, 0x0001);
								}
								if(menu.next==1)
								{
									menu.SN1="AT+CONN=101000000002\r\n";
									menu.SN1F="+CONNECTED:0,101000000002";
									menu.SN1D="+DISCONN:0,101000000002";
									Write_Information(Sn1Addr, 0x0002);
								}
								if(menu.next==2)
								{
									menu.SN1="AT+CONN=101000000003\r\n";
									menu.SN1F="+CONNECTED:0,101000000003";
									menu.SN1D="+DISCONN:0,101000000003";
									Write_Information(Sn1Addr, 0x0003);
								}
								if(menu.next==3)
								{
									menu.SN1="AT+CONN=101000000004\r\n";
									menu.SN1F="+CONNECTED:0,101000000004";
									menu.SN1D="+DISCONN:0,101000000004";
									Write_Information(Sn1Addr, 0x0004);
								}
								if(menu.next==4)
								{
									menu.SN1="AT+CONN=101000000005\r\n";
									menu.SN1F="+CONNECTED:0,101000000005";
									menu.SN1D="+DISCONN:0,101000000005";
									Write_Information(Sn1Addr, 0x0005);
									
								}
								if(menu.next==5)
								{
									menu.SN1="AT+CONN=101000000006\r\n";
									menu.SN1F="+CONNECTED:0,101000000006";
									menu.SN1D="+DISCONN:0,101000000006";
									Write_Information(Sn1Addr, 0x0006);
								}
								if(menu.next==6)
								{
									menu.SN1="AT+CONN=101000000007\r\n";
									menu.SN1F="+CONNECTED:0,101000000007";
									menu.SN1D="+DISCONN:0,101000000007";
									Write_Information(Sn1Addr, 0x0007);
								}
								if(menu.next==7)
								{
									menu.SN1="AT+CONN=101000000008\r\n";
									menu.SN1F="+CONNECTED:0,101000000008";
									menu.SN1D="+DISCONN:0,101000000008";
									Write_Information(Sn1Addr, 0x0008);
								}
								if(menu.next==8)
								{
									menu.SN1="AT+CONN=101000000009\r\n";
									menu.SN1F="+CONNECTED:0,101000000009";
									menu.SN1D="+DISCONN:0,101000000009";
									Write_Information(Sn1Addr, 0x0009);
								}
								if(menu.next==9)
								{
									menu.SN1="AT+CONN=10100000000A\r\n";
									menu.SN1F="+CONNECTED:0,10100000000A";
									menu.SN1D="+DISCONN:0,10100000000A";
									Write_Information(Sn1Addr, 0x000A);
								}
								if(menu.next==0xa)
								{
									menu.SN1="AT+CONN=10100000000B\r\n";
									menu.SN1F="+CONNECTED:0,10100000000B";
									menu.SN1D="+DISCONN:0,10100000000B";
									Write_Information(Sn1Addr, 0x000B);
								}
								if(menu.next==0xb)
								{
									menu.SN1="AT+CONN=10100000000C\r\n";
									menu.SN1F="+CONNECTED:0,10100000000C";
									menu.SN1D="+DISCONN:0,10100000000C";
									Write_Information(Sn1Addr, 0x000C);
								}
								if(menu.next==0xc)
								{
									menu.SN1="AT+CONN=10100000000D\r\n";
									menu.SN1F="+CONNECTED:0,10100000000D";
									menu.SN1D="+DISCONN:0,10100000000D";
									Write_Information(Sn1Addr, 0x000D);
								}
								if(menu.next==0xd)
								{
									menu.SN1="AT+CONN=10100000000E\r\n";
									menu.SN1F="+CONNECTED:0,10100000000E";
									menu.SN1D="+DISCONN:0,10100000000E";
									Write_Information(Sn1Addr, 0x000E);
								}
								if(menu.next==0xe)
								{
									menu.SN1="AT+CONN=10100000000F\r\n";
									menu.SN1F="+CONNECTED:0,10100000000F";
									menu.SN1D="+DISCONN:0,10100000000F";
									Write_Information(Sn1Addr, 0x000F);
								}
								if(menu.next==0xf)
								{
									menu.SN1="AT+CONN=101000000010\r\n";
									menu.SN1F="+CONNECTED:0,101000000010";
									menu.SN1D="+DISCONN:0,101000000010";
									Write_Information(Sn1Addr, 0x0010);
								}
								
								L1bd=true;
							}
							if(menu.PreNext==1)//L2 写
							{
								if(menu.next==0)
								{
									menu.SN2="AT+CONN=101000000001\r\n";
									menu.SN2F="+CONNECTED:0,101000000001";
									menu.SN2D="+DISCONN:0,101000000001";
									Write_Information(Sn2Addr, 0x0001);
								}
								if(menu.next==1)
								{
									menu.SN2="AT+CONN=101000000002\r\n";
									menu.SN2F="+CONNECTED:0,101000000002";
									menu.SN2D="+DISCONN:0,101000000002";
									Write_Information(Sn2Addr, 0x0002);
								}
								if(menu.next==2)
								{
									menu.SN2="AT+CONN=101000000003\r\n";
									menu.SN2F="+CONNECTED:0,101000000003";
									menu.SN2D="+DISCONN:0,101000000003";
									Write_Information(Sn2Addr, 0x0003);
								}
								if(menu.next==3)
								{
									menu.SN2="AT+CONN=101000000004\r\n";
									menu.SN2F="+CONNECTED:0,101000000004";
									menu.SN2D="+DISCONN:0,101000000004";
									Write_Information(Sn2Addr, 0x0004);
								}
								if(menu.next==4)
								{
									menu.SN2="AT+CONN=101000000005\r\n";
									menu.SN2F="+CONNECTED:0,101000000005";
									menu.SN2D="+DISCONN:0,101000000005";
									Write_Information(Sn2Addr, 0x0005);
									
								}
								if(menu.next==5)
								{
									menu.SN2="AT+CONN=101000000006\r\n";
									menu.SN2F="+CONNECTED:0,101000000006";
									menu.SN2D="+DISCONN:0,101000000006";
									Write_Information(Sn2Addr, 0x0006);
								}
								if(menu.next==6)
								{
									menu.SN2="AT+CONN=101000000007\r\n";
									menu.SN2F="+CONNECTED:0,101000000007";
									menu.SN2D="+DISCONN:0,101000000007";
									Write_Information(Sn2Addr, 0x0007);
								}
								if(menu.next==7)
								{
									menu.SN2="AT+CONN=101000000008\r\n";
									menu.SN2F="+CONNECTED:0,101000000008";
									menu.SN2D="+DISCONN:0,101000000008";
									Write_Information(Sn2Addr, 0x0008);
								}
								if(menu.next==8)
								{
									menu.SN2="AT+CONN=101000000009\r\n";
									menu.SN2F="+CONNECTED:0,101000000009";
									menu.SN2D="+DISCONN:0,101000000009";
									Write_Information(Sn2Addr, 0x0009);
								}
								if(menu.next==9)
								{
									menu.SN2="AT+CONN=10100000000A\r\n";
									menu.SN2F="+CONNECTED:0,10100000000A";
									menu.SN2D="+DISCONN:0,10100000000A";
									Write_Information(Sn2Addr, 0x000A);
								}
								if(menu.next==0xa)
								{
									menu.SN2="AT+CONN=10100000000B\r\n";
									menu.SN2F="+CONNECTED:0,10100000000B";
									menu.SN2D="+DISCONN:0,10100000000B";
									Write_Information(Sn2Addr, 0x000B);
								}
								if(menu.next==0xb)
								{
									menu.SN2="AT+CONN=10100000000C\r\n";
									menu.SN2F="+CONNECTED:0,10100000000C";
									menu.SN2D="+DISCONN:0,10100000000C";
									Write_Information(Sn2Addr, 0x000C);
								}
								if(menu.next==0xc)
								{
									menu.SN2="AT+CONN=10100000000D\r\n";
									menu.SN2F="+CONNECTED:0,10100000000D";
									menu.SN2D="+DISCONN:0,10100000000D";
									Write_Information(Sn2Addr, 0x000D);
								}
								if(menu.next==0xd)
								{
									menu.SN2="AT+CONN=10100000000E\r\n";
									menu.SN2F="+CONNECTED:0,10100000000E";
									menu.SN2D="+DISCONN:0,10100000000E";
									Write_Information(Sn2Addr, 0x000E);
								}
								if(menu.next==0xe)
								{
									menu.SN2="AT+CONN=10100000000F\r\n";
									menu.SN2F="+CONNECTED:0,10100000000F";
									menu.SN2D="+DISCONN:0,10100000000F";
									Write_Information(Sn2Addr, 0x000F);
								}
								if(menu.next==0xf)
								{
									menu.SN2="AT+CONN=101000000010\r\n";
									menu.SN2F="+CONNECTED:0,101000000010";
									menu.SN2D="+DISCONN:0,101000000010";
									Write_Information(Sn2Addr, 0x0010);
								}
								L2bd=true;
							}
							if(menu.PreNext==2)//L3 写
							{
								if(menu.next==0)
								{
									menu.SN3="AT+CONN=101000000001\r\n";
									menu.SN3F="+CONNECTED:0,101000000001";
									menu.SN3D="+DISCONN:0,101000000001";
									Write_Information(Sn3Addr, 0x0001);
								}
								if(menu.next==1)
								{
									menu.SN3="AT+CONN=101000000002\r\n";
									menu.SN3F="+CONNECTED:0,101000000002";
									menu.SN3D="+DISCONN:0,101000000002";
									Write_Information(Sn3Addr, 0x0002);
								}
								if(menu.next==2)
								{
									menu.SN3="AT+CONN=101000000003\r\n";
									menu.SN3F="+CONNECTED:0,101000000003";
									menu.SN3D="+DISCONN:0,101000000003";
									Write_Information(Sn3Addr, 0x0003);
								}
								if(menu.next==3)
								{
									menu.SN3="AT+CONN=101000000004\r\n";
									menu.SN3F="+CONNECTED:0,101000000004";
									menu.SN3D="+DISCONN:0,101000000004";
									Write_Information(Sn3Addr, 0x0004);
								}
								if(menu.next==4)
								{
									menu.SN3="AT+CONN=101000000005\r\n";
									menu.SN3F="+CONNECTED:0,101000000005";
									menu.SN3D="+DISCONN:0,101000000005";
									Write_Information(Sn3Addr, 0x0005);
									
								}
								if(menu.next==5)
								{
									menu.SN3="AT+CONN=101000000006\r\n";
									menu.SN3F="+CONNECTED:0,101000000006";
									menu.SN3D="+DISCONN:0,101000000006";
									Write_Information(Sn3Addr, 0x0006);
								}
								if(menu.next==6)
								{
									menu.SN3="AT+CONN=101000000007\r\n";
									menu.SN3F="+CONNECTED:0,101000000007";
									menu.SN3D="+DISCONN:0,101000000007";
									Write_Information(Sn3Addr, 0x0007);
								}
								if(menu.next==7)
								{
									menu.SN3="AT+CONN=101000000008\r\n";
									menu.SN3F="+CONNECTED:0,101000000008";
									menu.SN3D="+DISCONN:0,101000000008";
									Write_Information(Sn3Addr, 0x0008);
								}
								if(menu.next==8)
								{
									menu.SN3="AT+CONN=101000000009\r\n";
									menu.SN3F="+CONNECTED:0,101000000009";
									menu.SN3D="+DISCONN:0,101000000009";
									Write_Information(Sn3Addr, 0x0009);
								}
								if(menu.next==9)
								{
									menu.SN3="AT+CONN=10100000000A\r\n";
									menu.SN3F="+CONNECTED:0,10100000000A";
									menu.SN3D="+DISCONN:0,10100000000A";
									Write_Information(Sn3Addr, 0x000A);
								}
								if(menu.next==0xa)
								{
									menu.SN3="AT+CONN=10100000000B\r\n";
									menu.SN3F="+CONNECTED:0,10100000000B";
									menu.SN3D="+DISCONN:0,10100000000B";
									Write_Information(Sn3Addr, 0x000B);
								}
								if(menu.next==0xb)
								{
									menu.SN3="AT+CONN=10100000000C\r\n";
									menu.SN3F="+CONNECTED:0,10100000000C";
									menu.SN3D="+DISCONN:0,10100000000C";
									Write_Information(Sn3Addr, 0x000C);
								}
								if(menu.next==0xc)
								{
									menu.SN3="AT+CONN=10100000000D\r\n";
									menu.SN3F="+CONNECTED:0,10100000000D";
									menu.SN3D="+DISCONN:0,10100000000D";
									Write_Information(Sn3Addr, 0x000D);
								}
								if(menu.next==0xd)
								{
									menu.SN3="AT+CONN=10100000000E\r\n";
									menu.SN3F="+CONNECTED:0,10100000000E";
									menu.SN3D="+DISCONN:0,10100000000E";
									Write_Information(Sn3Addr, 0x000E);
								}
								if(menu.next==0xe)
								{
									menu.SN3="AT+CONN=10100000000F\r\n";
									menu.SN3F="+CONNECTED:0,10100000000F";
									menu.SN3D="+DISCONN:0,10100000000F";
									Write_Information(Sn3Addr, 0x000F);
								}
								if(menu.next==0xf)
								{
									menu.SN3="AT+CONN=101000000010\r\n";
									menu.SN3F="+CONNECTED:0,101000000010";
									menu.SN3D="+DISCONN:0,101000000010";
									Write_Information(Sn3Addr, 0x0010);
								}
								L3bd=true;
							}
							if(menu.PreNext==3)//L4 写
							{
								if(menu.next==0)
								{
									menu.SN4="AT+CONN=101000000001\r\n";
									menu.SN4F="+CONNECTED:0,101000000001";
									menu.SN4D="+DISCONN:0,101000000001";
									Write_Information(Sn4Addr, 0x0001);
								}
								if(menu.next==1)
								{
									menu.SN4="AT+CONN=101000000002\r\n";
									menu.SN4F="+CONNECTED:0,101000000002";
									menu.SN4D="+DISCONN:0,101000000002";
									Write_Information(Sn4Addr, 0x0002);
								}
								if(menu.next==2)
								{
									menu.SN4="AT+CONN=101000000003\r\n";
									menu.SN4F="+CONNECTED:0,101000000003";
									menu.SN4D="+DISCONN:0,101000000003";
									Write_Information(Sn4Addr, 0x0003);
								}
								if(menu.next==3)
								{
									menu.SN4="AT+CONN=101000000004\r\n";
									menu.SN4F="+CONNECTED:0,101000000004";
									menu.SN4D="+DISCONN:0,101000000004";
									Write_Information(Sn4Addr, 0x0004);
								}
								if(menu.next==4)
								{
									menu.SN4="AT+CONN=101000000005\r\n";
									menu.SN4F="+CONNECTED:0,101000000005";
									menu.SN4D="+DISCONN:0,101000000005";
									Write_Information(Sn4Addr, 0x0005);
									
								}
								if(menu.next==5)
								{
									menu.SN4="AT+CONN=101000000006\r\n";
									menu.SN4F="+CONNECTED:0,101000000006";
									menu.SN4D="+DISCONN:0,101000000006";
									Write_Information(Sn4Addr, 0x0006);
								}
								if(menu.next==6)
								{
									menu.SN4="AT+CONN=101000000007\r\n";
									menu.SN4F="+CONNECTED:0,101000000007";
									menu.SN4D="+DISCONN:0,101000000007";
									Write_Information(Sn4Addr, 0x0007);
								}
								if(menu.next==7)
								{
									menu.SN4="AT+CONN=101000000008\r\n";
									menu.SN4F="+CONNECTED:0,101000000008";
									menu.SN4D="+DISCONN:0,101000000008";
									Write_Information(Sn4Addr, 0x0008);
								}
								if(menu.next==8)
								{
									menu.SN4="AT+CONN=101000000009\r\n";
									menu.SN4F="+CONNECTED:0,101000000009";
									menu.SN4D="+DISCONN:0,101000000009";
									Write_Information(Sn4Addr, 0x0009);
								}
								if(menu.next==9)
								{
									menu.SN4="AT+CONN=10100000000A\r\n";
									menu.SN4F="+CONNECTED:0,10100000000A";
									menu.SN4D="+DISCONN:0,10100000000A";
									Write_Information(Sn4Addr, 0x000A);
								}
								if(menu.next==0xa)
								{
									menu.SN4="AT+CONN=10100000000B\r\n";
									menu.SN4F="+CONNECTED:0,10100000000B";
									menu.SN4D="+DISCONN:0,10100000000B";
									Write_Information(Sn4Addr, 0x000B);
								}
								if(menu.next==0xb)
								{
									menu.SN4="AT+CONN=10100000000C\r\n";
									menu.SN4F="+CONNECTED:0,10100000000C";
									menu.SN4D="+DISCONN:0,10100000000C";
									Write_Information(Sn4Addr, 0x000C);
								}
								if(menu.next==0xc)
								{
									menu.SN4="AT+CONN=10100000000D\r\n";
									menu.SN4F="+CONNECTED:0,10100000000D";
									menu.SN4D="+DISCONN:0,10100000000D";
									Write_Information(Sn4Addr, 0x000D);
								}
								if(menu.next==0xd)
								{
									menu.SN4="AT+CONN=10100000000E\r\n";
									menu.SN4F="+CONNECTED:0,10100000000E";
									menu.SN4D="+DISCONN:0,10100000000E";
									Write_Information(Sn4Addr, 0x000E);
								}
								if(menu.next==0xe)
								{
									menu.SN4="AT+CONN=10100000000F\r\n";
									menu.SN4F="+CONNECTED:0,10100000000F";
									menu.SN4D="+DISCONN:0,10100000000F";
									Write_Information(Sn4Addr, 0x000F);
								}
								if(menu.next==0xf)
								{
									menu.SN4="AT+CONN=101000000010\r\n";
									menu.SN4F="+CONNECTED:0,101000000010";
									menu.SN4D="+DISCONN:0,101000000010";
									Write_Information(Sn4Addr, 0x0010);
								}
								L4bd=true;
							}

							
							SET_MENU_STATUS(2,3,0,2);
							
							break;
						}
					}

					//PRINT_MENU_STATUS("K3");
					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) // 去抖时间大于50ms
        {
            if (key_state == GPIO_PIN_RESET) // 按键按下
            {
				if (!sent4_key) {//返回
					switch (menu.current) {
						case 0:
						case 1:
						{
							SET_MENU_STATUS(0,0,0,0);
							break;
						}
						case 2:
						case 3:
						{
							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(300);
							S1 = read_flash_16(Sn1StatusAddr);
							S2 = read_flash_16(Sn2StatusAddr);
							SET_MENU_STATUS(3,3,0,2);
							break;
						}	
						case 8:
						case 9:
						case 0xa:
						case 0xb:
						case 0xd:
							break;
						case 0xA1:
						{
							S1 = read_flash_16(Sn1StatusAddr);
							S2 = read_flash_16(Sn2StatusAddr);
							SET_MENU_STATUS(2,3,0,2);
							break;
						}
					}
					//PRINT_MENU_STATUS("K4");
					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();
}


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);
	
//	printf("%04x\r\n",SN1);
//	printf("%04x\r\n",SN2);
//	printf("%04x\r\n",SN3);
//	printf("%04x\r\n",SN4);
	if(SN1==0x0001)
	{
		menu.SN1name="0001";
		menu.SN1="AT+CONN=101000000001\r\n";
		menu.SN1F="+CONNECTED:0,101000000001";
		L1bd=true;
	}
	if(SN1==0x0002)
	{
		menu.SN1name="0002";
		menu.SN1="AT+CONN=101000000002\r\n";
		menu.SN1F="+CONNECTED:0,101000000002";
		L1bd=true;
	}
	if(SN1==0x0003)
	{
		menu.SN1name="0003";
		menu.SN1="AT+CONN=101000000003\r\n";
		menu.SN1F="+CONNECTED:0,101000000003";
		L1bd=true;
	}
	if(SN1==0x0004)
	{
		menu.SN1name="0004";
		menu.SN1="AT+CONN=101000000004\r\n";
		menu.SN1F="+CONNECTED:0,101000000004";
		L1bd=true;
	}
	if(SN1==0x0005)
	{
		menu.SN1name="0005";
		menu.SN1="AT+CONN=101000000005\r\n";
		menu.SN1F="+CONNECTED:0,101000000005";
		L1bd=true;
	}
	if(SN1==0x0006)
	{
		menu.SN1name="0006";
		menu.SN1="AT+CONN=101000000006\r\n";
		menu.SN1F="+CONNECTED:0,101000000006";
		L1bd=true;
	}
	if(SN1==0x0007)
	{
		menu.SN1name="0007";
		menu.SN1="AT+CONN=101000000007\r\n";
		menu.SN1F="+CONNECTED:0,101000000007";
		L1bd=true;
	}
	if(SN1==0x0008)
	{
		menu.SN1name="0008";
		menu.SN1="AT+CONN=101000000008\r\n";
		menu.SN1F="+CONNECTED:0,101000000008";
		L1bd=true;
	}
	if(SN1==0x0009)
	{
		menu.SN1name="0009";
		menu.SN1="AT+CONN=101000000009\r\n";
		menu.SN1F="+CONNECTED:0,101000000009";
		L1bd=true;
	}
	if(SN1==0x000A)
	{
		menu.SN1name="000A";
		menu.SN1="AT+CONN=10100000000A\r\n";
		menu.SN1F="+CONNECTED:0,10100000000A";
		L1bd=true;
	}
	if(SN1==0x000B)
	{
		menu.SN1name="000B";
		menu.SN1="AT+CONN=10100000000B\r\n";
		menu.SN1F="+CONNECTED:0,10100000000B";
		L1bd=true;
	}
	if(SN1==0x000C)
	{
		menu.SN1name="000C";
		menu.SN1="AT+CONN=10100000000C\r\n";
		menu.SN1F="+CONNECTED:0,10100000000C";
		L1bd=true;
	}
	if(SN1==0x000D)
	{
		menu.SN1name="000D";
		menu.SN1="AT+CONN=10100000000D\r\n";
		menu.SN1F="+CONNECTED:0,10100000000D";
		L1bd=true;
	}
	if(SN1==0x000E)
	{
		menu.SN1name="000E";
		menu.SN1="AT+CONN=10100000000E\r\n";
		menu.SN1F="+CONNECTED:0,10100000000E";
		L1bd=true;
	}
	if(SN1==0x000F)
	{
		menu.SN1name="000F";
		menu.SN1="AT+CONN=10100000000F\r\n";
		menu.SN1F="+CONNECTED:0,10100000000F";
		L1bd=true;
	}
	if(SN1==0x0010)
	{
		menu.SN1name="0010";
		menu.SN1="AT+CONN=101000000010\r\n";
		menu.SN1F="+CONNECTED:0,101000000010";
		L1bd=true;
	}
	
	if(SN2==0x0001)
	{
		menu.SN2name="0001";
		menu.SN2="AT+CONN=101000000001\r\n";
		menu.SN2F="+CONNECTED:0,101000000001";
		L2bd=true;
	}
	if(SN2==0x0002)
	{
		menu.SN2name="0002";
		menu.SN2="AT+CONN=101000000002\r\n";
		menu.SN2F="+CONNECTED:0,101000000002";
		L2bd=true;
	}
	if(SN2==0x0003)
	{
		menu.SN2name="0003";
		menu.SN2="AT+CONN=101000000003\r\n";
		menu.SN2F="+CONNECTED:0,101000000003";
		L2bd=true;
	}
	if(SN2==0x0004)
	{
		menu.SN2name="0004";
		menu.SN2="AT+CONN=101000000004\r\n";
		menu.SN2F="+CONNECTED:0,101000000004";
		L2bd=true;
	}
	if(SN2==0x0005)
	{
		menu.SN2name="0005";
		menu.SN2="AT+CONN=101000000005\r\n";
		menu.SN2F="+CONNECTED:0,101000000005";
		L2bd=true;
	}
	if(SN2==0x0006)
	{
		menu.SN2name="0006";
		menu.SN2="AT+CONN=101000000006\r\n";
		menu.SN2F="+CONNECTED:0,101000000006";
		L2bd=true;
	}
	if(SN2==0x0007)
	{
		menu.SN2name="0007";
		menu.SN2="AT+CONN=101000000007\r\n";
		menu.SN2F="+CONNECTED:0,101000000007";
		L2bd=true;
	}
	if(SN2==0x0008)
	{
		menu.SN2name="0008";
		menu.SN2="AT+CONN=101000000008\r\n";
		menu.SN2F="+CONNECTED:0,101000000008";
		L2bd=true;
	}
	if(SN2==0x0009)
	{
		menu.SN2name="0009";
		menu.SN2="AT+CONN=101000000009\r\n";
		menu.SN2F="+CONNECTED:0,101000000009";
		L2bd=true;
	}
	if(SN2==0x000A)
	{
		menu.SN2name="000A";
		menu.SN2="AT+CONN=10100000000A\r\n";
		menu.SN2F="+CONNECTED:0,10100000000A";
		L2bd=true;
	}
	if(SN2==0x000B)
	{
		menu.SN2name="000B";
		menu.SN2="AT+CONN=10100000000B\r\n";
		menu.SN2F="+CONNECTED:0,10100000000B";
		L2bd=true;
	}
	if(SN2==0x000C)
	{
		menu.SN2name="000C";
		menu.SN2="AT+CONN=10100000000C\r\n";
		menu.SN2F="+CONNECTED:0,10100000000C";
		L2bd=true;
	}
	if(SN2==0x000D)
	{
		menu.SN2name="000D";
		menu.SN2="AT+CONN=10100000000D\r\n";
		menu.SN2F="+CONNECTED:0,10100000000D";
		L2bd=true;
	}
	if(SN2==0x000E)
	{
		menu.SN2name="000E";
		menu.SN2="AT+CONN=10100000000E\r\n";
		menu.SN2F="+CONNECTED:0,10100000000E";
		L2bd=true;
	}
	if(SN2==0x000F)
	{
		menu.SN2name="000F";
		menu.SN2="AT+CONN=10100000000F\r\n";
		menu.SN2F="+CONNECTED:0,10100000000F";
		L2bd=true;
	}
	if(SN2==0x0010)
	{
		menu.SN2name="0010";
		menu.SN2="AT+CONN=101000000010\r\n";
		menu.SN2F="+CONNECTED:0,101000000010";
		L2bd=true;
	}
	
	
	
	
	if(SN3==0x0001)
	{
		menu.SN3name="0001";
		menu.SN3="AT+CONN=101000000001\r\n";
		menu.SN3F="+CONNECTED:0,101000000001";
		L3bd=true;
	}
	if(SN3==0x0002)
	{
		menu.SN3name="0002";
		menu.SN3="AT+CONN=101000000002\r\n";
		menu.SN3F="+CONNECTED:0,101000000002";
		L3bd=true;
	}
	if(SN3==0x0003)
	{
		menu.SN3name="0003";
		menu.SN3="AT+CONN=101000000003\r\n";
		menu.SN3F="+CONNECTED:0,101000000003";
		L3bd=true;
	}
	if(SN3==0x0004)
	{
		menu.SN3name="0004";
		menu.SN3="AT+CONN=101000000004\r\n";
		menu.SN3F="+CONNECTED:0,101000000004";
		L3bd=true;
	}
	if(SN3==0x0005)
	{
		menu.SN3name="0005";
		menu.SN3="AT+CONN=101000000005\r\n";
		menu.SN3F="+CONNECTED:0,101000000005";
		L3bd=true;
	}
	if(SN3==0x0006)
	{
		menu.SN3name="0006";
		menu.SN3="AT+CONN=101000000006\r\n";
		menu.SN3F="+CONNECTED:0,101000000006";
		L3bd=true;
	}
	if(SN3==0x0007)
	{
		menu.SN3name="0007";
		menu.SN3="AT+CONN=101000000007\r\n";
		menu.SN3F="+CONNECTED:0,101000000007";
		L3bd=true;
	}
	if(SN3==0x0008)
	{
		menu.SN3name="0008";
		menu.SN3="AT+CONN=101000000008\r\n";
		menu.SN3F="+CONNECTED:0,101000000008";
		L3bd=true;
	}
	if(SN3==0x0009)
	{
		menu.SN3name="0009";
		menu.SN3="AT+CONN=101000000009\r\n";
		menu.SN3F="+CONNECTED:0,101000000009";
		L3bd=true;
	}
	if(SN3==0x000A)
	{
		menu.SN3name="000A";
		menu.SN3="AT+CONN=10100000000A\r\n";
		menu.SN3F="+CONNECTED:0,10100000000A";
		L3bd=true;
	}
	if(SN3==0x000B)
	{
		menu.SN3name="000B";
		menu.SN3="AT+CONN=10100000000B\r\n";
		menu.SN3F="+CONNECTED:0,10100000000B";
		L3bd=true;
	}
	if(SN3==0x000C)
	{
		menu.SN3name="000C";
		menu.SN3="AT+CONN=10100000000C\r\n";
		menu.SN3F="+CONNECTED:0,10100000000C";
		L3bd=true;
	}
	if(SN3==0x000D)
	{
		menu.SN3name="000D";
		menu.SN3="AT+CONN=10100000000D\r\n";
		menu.SN3F="+CONNECTED:0,10100000000D";
		L3bd=true;
	}
	if(SN3==0x000E)
	{
		menu.SN3name="000E";
		menu.SN3="AT+CONN=10100000000E\r\n";
		menu.SN3F="+CONNECTED:0,10100000000E";
		L3bd=true;
	}
	if(SN3==0x000F)
	{
		menu.SN3name="000F";
		menu.SN3="AT+CONN=10100000000F\r\n";
		menu.SN3F="+CONNECTED:0,10100000000F";
		L3bd=true;
	}
	if(SN3==0x0010)
	{
		menu.SN3name="0010";
		menu.SN3="AT+CONN=101000000010\r\n";
		menu.SN3F="+CONNECTED:0,101000000010";
		L3bd=true;
	}

	if(SN4==0x0001)
	{
		menu.SN4name="0001";
		menu.SN4="AT+CONN=101000000001\r\n";
		menu.SN4F="+CONNECTED:0,101000000001";
		L4bd=true;
	}
	if(SN4==0x0002)
	{
		menu.SN4name="0002";
		menu.SN4="AT+CONN=101000000002\r\n";
		menu.SN4F="+CONNECTED:0,101000000002";
		L4bd=true;
	}
	if(SN4==0x0003)
	{
		menu.SN4name="0003";
		menu.SN4="AT+CONN=101000000003\r\n";
		menu.SN4F="+CONNECTED:0,101000000003";
		L4bd=true;
	}
	if(SN4==0x0004)
	{
		menu.SN4name="0004";
		menu.SN4="AT+CONN=101000000004\r\n";
		menu.SN4F="+CONNECTED:0,101000000004";
		L4bd=true;
	}
	if(SN4==0x0005)
	{
		menu.SN4name="0005";
		menu.SN4="AT+CONN=101000000005\r\n";
		menu.SN4F="+CONNECTED:0,101000000005";
		L4bd=true;
	}
	if(SN4==0x0006)
	{
		menu.SN4name="0006";
		menu.SN4="AT+CONN=101000000006\r\n";
		menu.SN4F="+CONNECTED:0,101000000006";
		L4bd=true;
	}
	if(SN4==0x0007)
	{
		menu.SN4name="0007";
		menu.SN4="AT+CONN=101000000007\r\n";
		menu.SN4F="+CONNECTED:0,101000000007";
		L4bd=true;
	}
	if(SN4==0x0008)
	{
		menu.SN4name="0008";
		menu.SN4="AT+CONN=101000000008\r\n";
		menu.SN4F="+CONNECTED:0,101000000008";
		L4bd=true;
	}
	if(SN4==0x0009)
	{
		menu.SN4name="0009";
		menu.SN4="AT+CONN=101000000009\r\n";
		menu.SN4F="+CONNECTED:0,101000000009";
		L4bd=true;
	}
	if(SN4==0x000A)
	{
		menu.SN4name="000A";
		menu.SN4="AT+CONN=10100000000A\r\n";
		menu.SN4F="+CONNECTED:0,10100000000A";
		L4bd=true;
	}
	if(SN4==0x000B)
	{
		menu.SN4name="000B";
		menu.SN4="AT+CONN=10100000000B\r\n";
		menu.SN4F="+CONNECTED:0,10100000000B";
		L4bd=true;
	}
	if(SN4==0x000C)
	{
		menu.SN4name="000C";
		menu.SN4="AT+CONN=10100000000C\r\n";
		menu.SN4F="+CONNECTED:0,10100000000C";
		L4bd=true;
	}
	
	if(SN4==0x000D)
	{
		menu.SN4name="000D";
		menu.SN4="AT+CONN=10100000000D\r\n";
		menu.SN4F="+CONNECTED:0,10100000000D";
		L4bd=true;
	}
	if(SN4==0x000E)
	{
		menu.SN4name="000E";
		menu.SN4="AT+CONN=10100000000E\r\n";
		menu.SN4F="+CONNECTED:0,10100000000E";
		L4bd=true;
	}
	if(SN4==0x000F)
	{
		menu.SN4name="000F";
		menu.SN4="AT+CONN=10100000000F\r\n";
		menu.SN4F="+CONNECTED:0,10100000000F";
		L4bd=true;
	}
	if(SN4==0x0010)
	{
		menu.SN4name="0010";
		menu.SN4="AT+CONN=101000000010\r\n";
		menu.SN4F="+CONNECTED:0,101000000010";
		L4bd=true;
	}



}


