static void AppTaskKbd(void *p_arg) { #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0; #endif
volatile int i,k; int Key_ADValue; int KeyTempValue[4]; int KeyValue[4]; int KeyDownInterval[4];
(void)p_arg;
KBD_Init();
Kbd_Status.KeyStatus=KeyStatus_Idle; Kbd_Status.KeyValue=0; for(i=0;i<4;i++) { KeyValue=0; KeyTempValue=0; KeyDownInterval=0; } while(1) { for(i=0;i<4;i++) { k=0; OS_ENTER_CRITICAL(); Key_ADValue=do_ADC(i+4); OS_EXIT_CRITICAL(); if((Key_ADValue>=AD_K1_Min)&&(Key_ADValue<=AD_K1_Max)) k=i*5+1; if((Key_ADValue>=AD_K2_Min)&&(Key_ADValue<=AD_K2_Max)) k=i*5+2; if((Key_ADValue>=AD_K3_Min)&&(Key_ADValue<=AD_K3_Max)) k=i*5+3; if((Key_ADValue>=AD_K4_Min)&&(Key_ADValue<=AD_K4_Max)) k=i*5+4; if((Key_ADValue>=AD_K5_Min)&&(Key_ADValue<=AD_K5_Max)) k=i*5+5;
if(k) //有键按下,重复按键的判断 { if(k==KeyTempValue) //上次的按键 { KeyDownInterval+=20; if(KeyValue) //已处于按下状态 { if(KeyDownInterval>400) { KeyDownInterval=0; Kbd_Status.KeyStatus=KeyStatus_Repeat; Kbd_Status.KeyValue=KeyValue; OSMboxPost(MBox_Key,(void*)&Kbd_Status); //sprintf(TxBuf,"%02x>
",KeyValue); //AT91F_US_SendFrame(AT91C_BASE_US1,TxBuf,5 ,0,0); } } else //尚没有键按下 { if(KeyDownInterval>40) { KeyValue=KeyTempValue; KeyDownInterval=0; Kbd_Status.KeyStatus=KeyStatus_Down; Kbd_Status.KeyValue=KeyValue; OSMboxPost(MBox_Key,(void *)&Kbd_Status); //sprintf(TxBuf,"%02x>
",KeyValue); //AT91F_US_SendFrame(AT91C_BASE_US1,TxBuf,5 ,0,0); } } } else //新按键或组合按键(认为错误) { if(KeyValue) //已经认定有键按下,新键值不对? { KeyTempValue=KeyStatus_Idle; KeyDownInterval=0; Kbd_Status.KeyStatus=KeyStatus_Up; Kbd_Status.KeyValue=KeyValue; OSMboxPost(MBox_Key,(void *)&Kbd_Status); KeyValue=KeyStatus_Idle;
} else //认定新键按下 { KeyTempValue=k; KeyValue=KeyStatus_Idle; KeyDownInterval=0; //Kbd_Status.KeyStatus=Key_Error; //Kbd_Status.KeyValue=KeyValue; //OSMboxPost(MBox_Key,(void *)&Kbd_Status); }
} } else //没有键按下或已抬起 { if(KeyValue) { Kbd_Status.KeyStatus=KeyStatus_Up; Kbd_Status.KeyValue=KeyValue; OSMboxPost(MBox_Key,(void *)&Kbd_Status); } KeyDownInterval=0; KeyTempValue=KeyStatus_Idle; KeyValue=KeyStatus_Idle; } }
OSTimeDly(20); }
} |