⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 7寸TFT电视的源程序
💻 C
字号:
//#define DEBUG_MSG

#include "target.h"

#ifdef __KEY__

#define KEYMAX				24
#define REPEATKEYTIME		40
#define HOLDTIME			50

#define read_k1_1() Get_P1_0()
#define read_k1_2() Get_P1_1()
#define read_k1_3() Get_P1_2()

#define k1_1_high() p10_high()
#define k1_2_high() p11_high()
#define k1_3_high() p12_high()

#define k2_1_low()  p13_low()
#define k2_1_high() p13_high()
#define k2_2_low()  p14_low()
#define k2_2_high() p14_high()
#define k2_3_low()  p15_low()
#define k2_3_high() p15_high()

extern unsigned char ucSystemMode;

unsigned char idata keystatus[3];

unsigned char code FpKey[KEYMAX] = {
	ENTER_KEY,PREV_KEY,FR_KEY,POWER_KEY,
	NEXT_KEY,FF_KEY,UP_KEY,BAND_KEY,
	VOL_UP_KEY,LEFT_KEY,SETUP_KEY,VOL_DOWN_KEY,
	DOWN_KEY,TITLE_KEY,NULL_KEY,RIGHT_KEY,
	MUTE_KEY,NULL_KEY,MODE_KEY,STOP_KEY,
	NULL_KEY,PAUSE_KEY,EJECT_KEY,NULL_KEY
};

char code FpKeyAllowRepeat[KEYMAX] = {
	TONE_KEY,M3_KEY,M5_KEY,0,
	M4_KEY,M6_KEY,SEEKUP_KEY,0,
	-1,0,M1_KEY,-1,
	SEEKDOWN_KEY,M2_KEY,0,0,
	0,0,0,0,
	0,0,0,0
};

void WriteColumnValue(unsigned char ucvalue)
{
	if(ucvalue & 0x01)
		k2_1_high();
	else
		k2_1_low();

	if(ucvalue & 0x02)
		k2_2_high();
	else
		k2_2_low();

	if(ucvalue & 0x04)
		k2_3_high();
	else
		k2_3_low();
}

void ReadRowValue()
{
	unsigned char ucvalue = 0;

	k1_1_high();
	k1_2_high();
	k1_3_high();

	if(read_k1_1())
		ucvalue |= 1;
	if(read_k1_2())
		ucvalue |= 2;
	if(read_k1_3())
		ucvalue |= 4;

	keystatus[2] = (keystatus[2]<<3)|(keystatus[1]>>5);
	keystatus[1] = (keystatus[1]<<3)|(keystatus[0]>>5);
	keystatus[0] = (keystatus[0]<<3)|(ucvalue);
}

unsigned char GetKeyIndex()
{
	unsigned char i,j,mask;
	for(i=0;i<3;i++){
		if(keystatus[i]!=0xff){
			mask = 1;
			for(j=0;j<8;j++){
				if(!(keystatus[i]&mask)){
					return (i<<3)+j;
				}
				mask <<= 1;
			}
		}
	}
	return 0xff;
}

unsigned char FpMultiKey(unsigned char key)
{
	unsigned char mulkey = key;
//muti-key for tuner.
	if(ucSystemMode==TUNER_MODE){
		switch(key){
			case SETUP_KEY:
				mulkey = P1_KEY;
			break;
			case TITLE_KEY:
				mulkey = P2_KEY;
			break;
			case PREV_KEY:
				mulkey = P3_KEY;
			break;
			case NEXT_KEY:
				mulkey = P4_KEY;
			break;
			case FR_KEY:
				mulkey = P5_KEY;
			break;
			case FF_KEY:
				mulkey = P6_KEY;
			break;
			case STOP_KEY:
				mulkey = LOCAL_KEY;
			break;
			case PAUSE_KEY:
				mulkey = ST_KEY;
			break;
		}
	}
	return mulkey;
}

void TimerReadKey20ms()
{
	unsigned char i,index;
	static unsigned char lastkey = 0xff;

	static unsigned char KeyCatchTime=0;

	for(i = 0;i < 8;i++)
	{
		WriteColumnValue(i);
		ReadRowValue();
	}
	index = GetKeyIndex();
	ShowLcdHex(0x4a,KeyCatchTime);
	if(index!=0xff){
		if(index!=lastkey){
			if(FpKeyAllowRepeat[index]<=0)
				KeyCatchTime = 0;
			else
				KeyCatchTime = 1;
			if(FpKeyAllowRepeat[index]==0){
				if(FpKey[index]){
					i = FpMultiKey(FpKey[index]);
					SaveKeyToBuff(i);
				}
			}
			lastkey = index;
		}
		else{
			if(FpKeyAllowRepeat[index]<0){
				if( ++KeyCatchTime >= REPEATKEYTIME ) {        //repeat key
					KeyCatchTime = REPEATKEYTIME-6;
					SaveKeyToBuff(FpKey[index]);
				}
			}
			else if(FpKeyAllowRepeat[index]>0){					//hold key
				if(KeyCatchTime){
					if( ++KeyCatchTime >= HOLDTIME ) {
						KeyCatchTime = 0;
						SaveKeyToBuff(FpKeyAllowRepeat[index]);
					}
				}
			}
			else
				if( ++KeyCatchTime >= REPEATKEYTIME ) {
					KeyCatchTime = 0;
				}
		}
	}
	else{
		if(lastkey!=0xff){
			if(KeyCatchTime){
				if(FpKeyAllowRepeat[lastkey]>0){
					if(KeyCatchTime<HOLDTIME){
						i = FpMultiKey(FpKey[lastkey]);
						SaveKeyToBuff(i);
					}
				}
				else if(FpKeyAllowRepeat[lastkey]<0){
					if(KeyCatchTime<REPEATKEYTIME){
						i = FpMultiKey(FpKey[lastkey]);
						SaveKeyToBuff(i);
					}
				}
			}
			KeyCatchTime = 0;
			lastkey = index;
		}
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -