//key.c
#include "key.h"
#include "mcu.h"
#include "RTT/SEGGER_RTT.h"

//KEY1:PB11
#define KEY1	11
//KEY2:PB08
#define KEY2	8
//KEY3:PB07
#define KEY3	7
//KEY4:PB10
#define KEY4	10
//KEY5:PB06
#define KEY5	9
//KEY6:PB06
#define KEY6	6

unsigned int key1_delay_count,key2_delay_count,key3_delay_count,key4_delay_count,key5_delay_count,key6_delay_count;

unsigned char r_switch_state=0;//one bit corresponding a key's state,two bit reserved

extern McuType mcu;
extern FlexCanDrv_MsgObjType txMsgObj;
uint8_t roll_count = 0;


Encoder_t encoder1;
unsigned char encoder_state=0;
signed char up_down=0;//encoder up_down count
unsigned char send_state=0;//encoder can message send flag


unsigned char button_stick=0;
unsigned char initflag[6]={1,1,1,1,1,1};//vioce,phone,back,left,ok,right



unsigned char key_buf[16];

void key_init(void)
{
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY1, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY1, 0);//input
	
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY2, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY2, 0);//input
	
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY3, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY3, 0);//input
	
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY4, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY4, 0);//input
	
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY5, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY5, 0);//input
	
	PinsDrv_SetMuxModeSel(&mcu.ptb, KEY6, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.ptb, KEY6, 0);//input
}


/* GPIO init  */
void encoder_init(void)
{
	/*pta1:KIT3033S enable pin*/
	/*GPIO mode*/
	PinsDrv_SetMuxModeSel(&mcu.pta, 1, PINSDRV_MUX_AS_GPIO);
	/*GPIO direction out*/
	PinsDrv_SetPinDirection(&mcu.pta, 1, 1);/*output*/
	/*out high*/
	PinsDrv_WritePin(&mcu.pta, 1, 1);
	
	/*pta2:KIT3033S OUT 0*/
	PinsDrv_SetMuxModeSel(&mcu.pta, 2, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.pta, 2, 0);/*input*/
	
	/*pta3:KIT3033S OUT 1*/
	PinsDrv_SetMuxModeSel(&mcu.pta, 3, PINSDRV_MUX_AS_GPIO);
	PinsDrv_SetPinDirection(&mcu.pta, 3, 0);
}


/*pin1,pin2:light high,blocked low*/
/*ret:0:none,1:roll-up,2:roll-down*/
static unsigned char encoder_deal(Encoder_t *e,unsigned char pin1,unsigned char pin2)
{
	unsigned char ret=0;
	
	switch (e->state)
	{
	case 0:
		if(pin1 == 1 && pin2 == 1)//default 
		{return 0;}
		if(pin1 == 0 && pin2 == 1)//rolling step1(roll-up):pin1 blocked,pin2 not blocked yet
		{
			e->state=1;//step1 detected
			e->rise_state=1;//roll-up rise
		}
		else if (pin1 == 1 && pin2 == 0)//rolling step1(roll-down):pin2 blocked first
		{
			e->state=1;//step1 detected
			e->rise_state=2;//roll-down
		}
		break;
		
	case 1:
		if(pin1 == 1 && pin2 == 1)//give up rolling
		{
			e->state=0;
			e->rise_state=0;
		}
		else if(pin1 == 0 && pin2 == 0){e->state = 2;}//rolling to step2:both blocked
		break;
		
	case 2:
		if(pin1 == 1 && pin2 == 0){e->fall_state = 1;}//rolling to step3(roll-up):pin1 light first
		else if(pin1 == 0 && pin2 == 1){e->fall_state = 2;}//rolling to step3(roll-down):pin2 light first
		else if(pin1 == 1 && pin2 == 1)//finish
		{
			if(e->rise_state == 1 && e->fall_state == 1)
			{
				ret =1;				
			}//slide-up	
			else if (e->rise_state == 2 && e->fall_state == 2)
			{
				ret = 2;				
			}//slide-down
			else {ret =0;}

			e->state=0;
			e->rise_state=0;
			e->fall_state=0;
		}
	}
	return ret;
}

void Encoder_process()
{
	unsigned char ret_val,button_up,button_down;

	button_up = PinsDrv_ReadPin(&mcu.pta,2);
	button_down = PinsDrv_ReadPin(&mcu.pta,3);
	
	ret_val=encoder_deal(&encoder1,button_up,button_down);
	if(ret_val == 1)
	{
		up_down++;
	}
	if(ret_val == 2)
	{
		up_down--;
	}
	
	if(up_down !=0)
		{
			if(up_down>0)
			{
				encoder_state = 1;
				up_down--;
				roll_count++;
				roll_count = (roll_count << 4) & 0xf0;
			}
			if(up_down<0)
			{
				encoder_state = 2;
				up_down++;
				roll_count++;
				roll_count = (roll_count & 0x0f);
			}
		}
	else{
		if(send_state == 1)
		{
			send_state=0;
			encoder_state = 0;
		}
	}
	
}

void button_detect(unsigned char button_state,unsigned char *init_code,unsigned char button_bit,unsigned int *delay_count)
{
	if(button_state == 0)
	{
		
		if(*init_code != 0)
		{return;}
		(*delay_count)++;
		if(*delay_count >=3 && *delay_count <12000)
		{
			r_switch_state |= 1<<(button_bit -1);
		}
		else if(*delay_count >=12000)
		{
			*delay_count=12000;
			button_stick=1;
		}
	}
	else
	{
		*delay_count=0;
		r_switch_state &= ~(1<<(button_bit -1));
		*init_code=0;
	}
	
}

void button_scan(void)
{
	button_stick=0;
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY1),&initflag[1],1,&key1_delay_count);
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY2),&initflag[2],2,&key2_delay_count);
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY3),&initflag[3],3,&key3_delay_count);
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY4),&initflag[4],4,&key4_delay_count);
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY5),&initflag[5],5,&key5_delay_count);
	button_detect(PinsDrv_ReadPin(&mcu.ptb,KEY6),&initflag[6],6,&key6_delay_count);
	button_stick=0;
}
