//PMC153
//==============================================================================
//         PB5 |1    14| PB2
//         PB6 |2    13| PB1
//         PB7 |3    12| PB0/INT1
//         VDD |4    11| GND
//         PA7 |5    10| PA0
//         PA6 |6     9| PA4
//   PA5/PRST# |7     8| PA3
//==============================================================================
#include	"extern.h"
#include 	"WirlessApp.h"
#include	"KeyControl.h"

//==============================外边变量========================================
extern	bit 	b_sleep;
extern	byte   	ColorCnt;
//==============================局部变量========================================
byte    KeyCmd;						//命令
byte	KeyMessage;					//按键值
byte    KeyIdleCnt;					//按键空闲状态计数器
byte    KeyHoldCnt;					//按键Down状态计数器

byte	DriverID0;
byte	DriverID1;
byte	DriverID2;
byte	DriverID3;
byte	LightID;


//==============================函数主体========================================

/*******************************************************************************
* 名称: void	PinInit(void)
* 功能: IO初始化
* 形参:       
* 返回: 无
* 说明: 
*******************************************************************************/
/*    7    6   5    4        3   2    1   0
//PB: SS  CLK MISO NSS      NSS CE    R3  R2

//PA: L1   L2 LED  L3/MOSI  R4  NSS   NSS R1//L1-L2-L3-其中接地的按键不能共用到SPI

L1---------上拉输入
L2---------上拉输入
L3---------上拉输入

R1---------输出0
R2---------输出0
R3---------输出0
R4---------输出0

*/
void	PinInit(void)
{
	//PB: SS CLK MISO NSS -- NSS CE  R3  R2
	PBC  = 0B1100_0111;//输出
	PBPH|= 0B0010_0000;//MISO上拉
	PB  &= 0B1111_1100;//RST R3 R2 输出0

	//PA: L1 L2  LED  L3/MOSI  R4  NSS   NSS R1
	PAC  = 0b0010_1001;//L1 L2 L3输入,其他输出
	PAPH = 0B1101_0000;//L1 L2 L3上拉
	PA  &= 0b1101_0110;//LED输出0 R4,R1输出0

	$ padier 0b1101_1001;//L1 L2 L3---睡眠中断
}

/*******************************************************************************
* 名称: KeyScanf
* 功能: 按键查看
* 形参:       
* 返回: 无
* 说明: 
*******************************************************************************/
void KeyScanf(void)
{
  BYTE Rval,Lval,val;

  //PA: L1 L2  LED  L3  R4  NSS   NSS R1
  Rval = PA & 0b1101_0000;//读取L1 L2 L3数值
  if(Rval == 0b1101_0000) //没有按键按下
  {
	LED_OFF();
	if(KeyIdleCnt < KEYSR_500MS)
    {
      KeyIdleCnt ++;
      KeyHoldCnt =0;
 	}
	else
	{
		b_sleep = 1;//进入睡眠
	}
	return;
  }

  //PA: L1 L2  LED  L3/MOSI  R4  NSS   NSS R1
  PAC  = 0b1111_0000;//L1 L2 L3/MOSI输出,R4,R1输入,LED输出
  PAPH = 0B0000_1001;//R4,R1上拉
  PA  &= 0b0010_1111;//L1 L2 L3/MOSI输出0

  //PB: SS CLK MISO NSS -- NSS MISO  R3  R2
  PBC &= 0B1111_1100;//R3,R2输入
  PBPH|= 0B0000_0011;//R3,R2上拉

  Lval  =   PA & 0B0000_1001;//读取R值
  Lval |= ((PB & 0B0000_0011)<<1);

  val = Lval|Rval;

  //PA: L1 L2  LED  L3/MOSI  R4  NSS   NSS R1
  PAC  = 0b0010_1001;//L1 L2 L3/MOSI输入,其他输出
  PAPH = 0B1101_0000;//L1 L2 L3上拉
  PA  &= 0b1111_0110;//R4,R1输出0

  //PB: SS CLK MISO NSS NSS MISO  R3  R2
  PBC |= 0B0000_0011;//R3 R2输出
  PB  &= 0B1111_1100;//R3 R2输出0

  b_sleep    = 0;
  KeyIdleCnt = 0;
  if (KeyHoldCnt == KEYSR_12MS)
  {
	KeyBit = 0;
    KeyMessage = val;
    KeyHoldCnt++;
  } 
  else if (KeyHoldCnt >= KEYSR_600MS)
  {
	KeyMessage = val;
    if(KeyMessage == KEY_CON_ADD ||
	   KeyMessage == KEY_CON_DEV ||
	   KeyMessage == KEY_COL_ADD ||
	   KeyMessage == KEY_COL_DEV)
	{
		KeyBit 	   = 0;//没有长按按键
		KeyHoldCnt = KEYSR_540MS;
	}
	else
	{
		KeyBit 	   = 1;//长按按键标志
		KeyHoldCnt = KEYSR_100MS;
	}
  }
  else
  {
    KeyHoldCnt ++;
  }

  
}
/*******************************************************************************
* 名称: KeyTaskProcess
* 功能: 按键任务
* 形参:       
* 返回: 无
* 说明: 
*******************************************************************************/
/*
void 	KeyTaskProcess(void)
{
	KeyCmd = LCMD_NULL;
	switch(KeyMessage)
	{
	case KEY_ON:
		LightID= LIGHTNAME_LED0;//所有绑定LED开
		KeyCmd = LCMD_ON;
	break;
	case KEY_OFF:
		LightID= LIGHTNAME_LED0;//所有绑定LED关
		KeyCmd = LCMD_OFF;
	break;
	case KEY_CON_ADD:
		KeyCmd = LCMD_CON_ADD;		//亮度加
	break;
	case KEY_CON_DEV:
		KeyCmd = LCMD_CON_DEC;		//亮度减
	break;
	case KEY_COL_ADD:
		KeyCmd = LCMD_COL_ADD;		//色温+
	break;
	case KEY_COL_DEV:				//色温-
		KeyCmd = LCMD_COL_DEC;		
	break;
	case KEY1_ON:
		LightID= LIGHTNAME_LED1;//LED1开
		KeyCmd = LCMD_ON;
	break;
	case KEY1_OFF:
		LightID= LIGHTNAME_LED1;//LED1关
		KeyCmd = LCMD_OFF;
	break;
	case KEY2_ON:
		LightID= LIGHTNAME_LED2;//LED2开
		KeyCmd = LCMD_ON;
	break;
	case KEY2_OFF:
		LightID= LIGHTNAME_LED2;//LED2关
		KeyCmd = LCMD_OFF;
	break;
	case KEY_COL_WARM://暖光
		ColorCnt = 0;
		KeyCmd = LCMD_COLORSEL;
	break;
	case KEY_COL_COOL://冷光
		ColorCnt = 4;
		KeyCmd = LCMD_COLORSEL;
	break;
	case KEY_COL_NIGHT://日光
		KeyCmd = LCMD_NIGHT;
	break;
	case KEY_COL_NOR://常规
		ColorCnt = 2;
		KeyCmd = LCMD_COLORSEL;
	break;
	default:
	break;
	}
	if(KeyCmd != LCMD_NULL)
	{
		LED_ON();
		Wirless_SentKey();
	}
	else
	{
		LED_OFF();
	}
	KeyMessage = NOKEY;
}
*/