#include "key.h"
//#include "delay.h"
#include "ht16c23.h"
#include "delay.h"
#include "string.h"
//#define  u8  unsigned char
//#define  u16  unsigned int
int MENUnum	=0;
int KEY2_ENTER=0;
int KEY2_ENTER_First_sel=0;
int KEY2_ENTER_Second=0;
int KEY2_ENTER_ADDR_selLocation=1;
u8 BOUNDSEL=1;
int KEY2_CT_selLocation=1;
int KEY2_PT_selLocation=1;

u8 ADDR_Num_Single=0;
u8 ADDR_Num_Tens=0;

u8 CT_Single=1;
u8 CT_Tens=0;
u8 CT_Huns=0;

u8 PT_Single=1;
u8 PT_Tens=0;
u8 PT_Huns=0;

extern u8 tempArr12[16];

extern u8 tempArr34[16];

extern u8 KeyPressFlag[4];
extern void USART1SendData();

void KEY_Init(void)
{

	
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStruct);

	 /*Configure GPIO pins : PA5 */
  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStruct);
	

	
}


/*
*********************************************************************************************************
*	函 数 名:  void keyHandle()
*	功能说明: 处理按键响应
*	形    参：
*	返 回 值: 
*********************************************************************************************************
*/
void keyHandle(void)
{
	if(KeyPressFlag[0]==1)//right 按下
	{
		if(KEY2_ENTER==0)
		{
			MENUnum+=1;
			if(MENUnum>16)	
				MENUnum=0;
			ht16c23_clear();
			memset(tempArr12,0,16);
			memset(tempArr34,0,16);
						
		}
		else if(KEY2_ENTER==1)
		{
			KEY2_ENTER_First_sel+=1;
			if(KEY2_ENTER_First_sel>3) KEY2_ENTER_First_sel=0;	
			ht16c23_clear();							
		}
		else if(KEY2_ENTER_First_sel==0 && KEY2_ENTER==2)//设置ADDR时
		{
			KEY2_ENTER_ADDR_selLocation++;
			if(KEY2_ENTER_ADDR_selLocation>2)
				KEY2_ENTER_ADDR_selLocation=1;// 选择3 4位	
			ht16c23_clear();						
		}
		else if(KEY2_ENTER_First_sel==1 && KEY2_ENTER==2 )  ////设置Bound时
		{
			BOUNDSEL++;
			if(BOUNDSEL>4) BOUNDSEL=1;//选择1-4级的波特率 	
			ht16c23_clear();						
		}
		else if(KEY2_ENTER_First_sel==2 && KEY2_ENTER==2) // 设置CT时
		{
			KEY2_CT_selLocation++;
			if(KEY2_CT_selLocation>3) KEY2_CT_selLocation=1;//选择 2 3 4位
			ht16c23_clear();
		}
		else if(KEY2_ENTER_First_sel==3 && KEY2_ENTER==2 )  ////设置PT时
		{
			KEY2_PT_selLocation++;
			if(KEY2_PT_selLocation>3) KEY2_PT_selLocation=1;//选择 2 3 4位
			ht16c23_clear();
		}	
		KeyPressFlag[0]=0;
	}		
	else if(KeyPressFlag[1]==1)//menu
	{
		KEY2_ENTER+=1;//回车键进入设置的不同层级
		 if(KEY2_ENTER>2)
		 {
			 USART1SendData();
			 KEY2_ENTER=0;
			 KEY2_ENTER_First_sel=0;
		 }				
		 ht16c23_clear();	
		 KeyPressFlag[1]=0;
	}		
	else if(KeyPressFlag[2]==1)//left
	{
		if(KEY2_ENTER==0)
		{
			if (MENUnum == 0)
				MENUnum = 16;
			else
				MENUnum -= 1; //翻页	 
				
			ht16c23_clear();
			memset(tempArr12,0,16);
			memset(tempArr34,0,16);
		}					
	 	else if(KEY2_ENTER==1)
		{
			KEY2_ENTER_First_sel--;
			if(KEY2_ENTER_First_sel<0) KEY2_ENTER_First_sel=3;	//上下选择设置的选项
			ht16c23_clear();						
		}
	 	else if(KEY2_ENTER_First_sel==0 && KEY2_ENTER==2&&KEY2_ENTER_ADDR_selLocation==1)  
		{
			ADDR_Num_Single++;
			if(ADDR_Num_Single>9) ADDR_Num_Single=0;//设置位上面的数值
		}
		
	 	else if(KEY2_ENTER_First_sel==0 && KEY2_ENTER==2&&KEY2_ENTER_ADDR_selLocation==2)  
		{
			ADDR_Num_Tens++;
			if(ADDR_Num_Tens>9) ADDR_Num_Tens=0;//设置位上面的数值

			ht16c23_clear();						
		}
		else if(KEY2_ENTER_First_sel==1 && KEY2_ENTER==2) 
		{
			BOUNDSEL--;
			if(BOUNDSEL<1) BOUNDSEL=4;	
			ht16c23_clear();					
		}
		else if(KEY2_ENTER_First_sel==2 && KEY2_ENTER==2 ) 
		{
			if(KEY2_CT_selLocation==1)
			{
				CT_Single+=1;
				if(CT_Single>9)
					CT_Single=0;              							
			}
			if(KEY2_CT_selLocation==2)
			{
				CT_Tens+=1;							
				if(CT_Tens>9) 
					CT_Tens=0;							

			}
			if(KEY2_CT_selLocation==3)
			{
				CT_Huns+=1;																				
				if(CT_Huns>9) 
					CT_Huns=0;
			}
			
			ht16c23_clear();
		}
		else if(KEY2_ENTER_First_sel==3 && KEY2_ENTER==2 )
		{
			if(KEY2_PT_selLocation==1)
			{
				PT_Single+=1;
				if(PT_Single>9)	
					PT_Single=0; 							
			}
			if(KEY2_PT_selLocation==2)
			{		
				PT_Tens+=1;				
				if(PT_Tens>9) 
					PT_Tens=0;							
			}
			if(KEY2_PT_selLocation==3)
			{			
				PT_Huns+=1;													
				if(PT_Huns>9) 
					PT_Huns=0;
			}			
			ht16c23_clear();					
		}		
		KeyPressFlag[2]=0;
	}		
	else if(KeyPressFlag[3]==1)//enter
	{
		if(KEY2_ENTER>0)
			KEY2_ENTER-=1;

		ht16c23_clear();
		KeyPressFlag[3]=0;
	}
}



