(求酷)比较适合实际应用的按键程序,用了状态机的思想

2019-10-11 13:35发布

废话不多说,直接上程序吧。大家可以讨论讨论,多聊聊程序架构的问题,希望不要断掉哦。。。
直接贴一部分程序,没办法字数限制。。。
[mw_shl_code=c,true]/********************************************************************************************** * File Name : bsp_key.c * Required Perconditions : * Called Functions : TIM_IsExpired(),TIM_GetTimestamp() * Author : Abner * Created : 02/09/2014 * Updated : 28/09/2014 * * =========================================================================================== * @attention * KeyValue : [15..13]->KeyState,[12..9]->Reserved,[8..3]->KeyID,[2..0]->KeyEvent * KeyState = keyval & 0xE000,KeyID = keyval & 0x01F8,KeyEvent = keyval & 0x0007, * bsp_timer.h and bsp_key.h ought to be included * Call Key_Init() before Key_GetValue() * * * <------ Add to Group BSP/HAL/SRC -------> * =========================================================================================== * @updated * 1.Deleted some Key_State * 2.Add the Current KeyState(KEY_STATE_INIT or KEY_STATE_UNRELEASED) to rtnval * 3.Replace defines for KeyEventime(in bsp_key.h) with Function(Key_SetEventTime) * 4.Define KEY_x_EffectiveLevel in header_file to make it convenient to modify it * so it can apply to other boards. * 5.Support 6 Independent_Keys(Max upto 10,but need modify Key_Init() & Key_GetID()) * 6.MultiKeyID were Deleted * 7.Fix bug : When you release other key during MultiPressed state,return KeyID won't * reflect this changes. * *********************************************************************************************/ /* ********************************************************************************************************* * INCLUDE FILES ********************************************************************************************************* */ #include "bsp_key.h" #include "bsp_timer.h" /* ********************************************************************************************************* * LOCAL CONSTANTS ********************************************************************************************************* */ /* ********************************************************************************************************* * LOCAL TABLES ********************************************************************************************************* */ /* ********************************************************************************************************* * LOCAL GLOBAL VARIABLES ********************************************************************************************************* */ static u32 KeyTime_Jitter = 5 ; //Time for excluding Jiteter interference static u32 KeyTime_Interval = 400 ; //Time for confirming status had swtiched static u32 KeyTime_Long = 800 ; //Time for confirming long pressed static u32 KeyTime_Double = 300 ; //Time between double pressed /* ********************************************************************************************************* ********************************************************************************************************* ** GLOBAL FUNCTIONS ********************************************************************************************************* ********************************************************************************************************* */ /* ********************************************************************************************************* * Key_Init() * * Description : Initialize GPIO Pin,Speed,Mode for Key. * * Argument(s) : None. * * Return(s) : None. * * Note(s) : (1) Key's GPIO(Pin,Speed,Mode) is defined in the bsp_key.h. ********************************************************************************************************* */ void Key_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; #if (KEY_SUPPORTNUM > 0) RCC_APB2PeriphClockCmd (KEY_0_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_0_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_0_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_0_GPIO_Mode; GPIO_Init (KEY_0_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 1) RCC_APB2PeriphClockCmd (KEY_1_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_1_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_1_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_1_GPIO_Mode; GPIO_Init (KEY_1_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 2) RCC_APB2PeriphClockCmd (KEY_2_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_2_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_2_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_2_GPIO_Mode; GPIO_Init (KEY_2_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 3) RCC_APB2PeriphClockCmd (KEY_3_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_3_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_3_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_3_GPIO_Mode; GPIO_Init (KEY_3_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 4) RCC_APB2PeriphClockCmd (KEY_4_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_4_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_4_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_4_GPIO_Mode; GPIO_Init (KEY_4_GPIO_Port,&GPIO_InitStructure ); #endif #if (KEY_SUPPORTNUM > 5) RCC_APB2PeriphClockCmd (KEY_0_GPIO_RCC_Periph ,ENABLE ); GPIO_InitStructure .GPIO_Pin = KEY_5_GPIO_Pin ; GPIO_InitStructure .GPIO_Speed = KEY_5_GPIO_Speed ; GPIO_InitStructure .GPIO_Mode = KEY_5_GPIO_Mode ; GPIO_Init (KEY_5_GPIO_Port,&GPIO_InitStructure ); #endif } /* ********************************************************************************************************* * Key_GetID() * * Description : Get pressed Key(s)'(s) ID(s). * * Argument(s) : None. * * Return(s) : Pressed Key(s)'(s) ID(s). * * Note(s) : (1) Key's EffectiveLevel is defined in the bsp_key.h. ********************************************************************************************************* */ KeyID_Enum_t Key_GetID(void) { KeyID_Enum_t KeyIDTmp = KEY_ID_NONE ; #if (KEY_SUPPORTNUM > 0)&&(USE_WKUP_PA0 == 0) if(GPIO_ReadInputDataBit (KEY_0_GPIO_Port ,KEY_0_GPIO_Pin ) == KEY_0_EffectiveLevel) KeyIDTmp |= KEY_ID_0; #endif #if (KEY_SUPPORTNUM > 1) if(GPIO_ReadInputDataBit (KEY_1_GPIO_Port ,KEY_1_GPIO_Pin ) == KEY_1_EffectiveLevel) KeyIDTmp |= KEY_ID_1; #endif #if (KEY_SUPPORTNUM > 2) if(GPIO_ReadInputDataBit (KEY_2_GPIO_Port ,KEY_2_GPIO_Pin ) == KEY_2_EffectiveLevel) KeyIDTmp |= KEY_ID_2; #endif #if (KEY_SUPPORTNUM > 3) if(GPIO_ReadInputDataBit (KEY_3_GPIO_Port ,KEY_3_GPIO_Pin ) == KEY_3_EffectiveLevel) KeyIDTmp |= KEY_ID_3; #endif #if (KEY_SUPPORTNUM > 4) if(GPIO_ReadInputDataBit (KEY_4_GPIO_Port ,KEY_4_GPIO_Pin ) == KEY_4_EffectiveLevel) KeyIDTmp |= KEY_ID_4; #endif #if (KEY_SUPPORTNUM > 5) if(GPIO_ReadInputDataBit (KEY_5_GPIO_Port ,KEY_5_GPIO_Pin ) == KEY_5_EffectiveLevel) KeyIDTmp |= KEY_ID_5; #endif return KeyIDTmp; } /* ********************************************************************************************************* * Key_GetValue() * * Description : Get pressed Key's ID,and the first event key's event and status. * * Argument(s) : None. * * Return(s) : @ Description. * * Note(s) : (1) wait to update to support for getting each key's event and status independently. * (2) KeyState = keyval & 0xE000,KeyID = keyval & 0x01F8,KeyEvent = keyval & 0x0007. * (3) the time for Jitter and others is based on set Timestamp period to 1 ms (@bsp_timer) ********************************************************************************************************* */ u16 Key_GetValue(void) { static KeyAction_Struct_t KeyAction = {KEY_ID_NONE ,KEY_STATE_INIT ,KEY_EVENT_NONE ,0,0,0}; static u16 KeyValue = KEY_STATE_INIT | KEY_ID_NONE | KEY_EVENT_NONE; static KeyID_Enum_t KeyIDTmp = KEY_ID_NONE ; static u16 LastKeyValue = KEY_STATE_INIT | KEY_ID_NONE | KEY_EVENT_NONE ; KeyAction .KeyID = Key_GetID() ; switch(KeyAction .KeyState ) { case KEY_STATE_INIT : { if(KeyAction .KeyID != KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_JITTER ; KeyAction .KeyJitterTime = TIM_GetSTime (); } }break; case KEY_STATE_JITTER : { if(TIM_IsExpired (KeyAction .KeyJitterTime + KeyTime_Jitter )) { if(KeyAction .KeyID != KEY_ID_NONE ) { KeyIDTmp = KeyAction .KeyID ; KeyAction .KeyState = KEY_STATE_PRESSED ; KeyAction .KeyPressedTime = TIM_GetSTime (); } else { KeyAction .KeyID = KEY_ID_NONE ; KeyAction .KeyState = KEY_STATE_INIT ; } } }break; case KEY_STATE_PRESSED : { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INTERIM1 ; KeyAction .KeyReleasedTime = TIM_GetSTime (); } if(TIM_IsExpired (KeyAction .KeyPressedTime + KeyTime_Long ) ) { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INIT ; } else { KeyAction .KeyState = KEY_STATE_UNRELEASED ; } LastKeyValue = KeyAction .KeyState | KeyIDTmp | KEY_EVENT_LONG ; return LastKeyValue ; } }break; case KEY_STATE_INTERIM1 : { if(TIM_IsExpired (KeyAction .KeyReleasedTime + KeyTime_Interval )) { KeyAction .KeyState = KEY_STATE_JITTER ; } if(KeyAction .KeyID != KEY_ID_NONE ) { KeyIDTmp = KeyAction .KeyID ; KeyAction .KeyPressedTime = TIM_GetSTime (); KeyAction .KeyState = KEY_STATE_INTERIM2 ; } else { if(TIM_IsExpired (KeyAction .KeyReleasedTime + KeyTime_Double )) { KeyAction .KeyState = KEY_STATE_INIT ; LastKeyValue = KEY_STATE_INIT | KeyIDTmp | KEY_EVENT_SHORT ; return LastKeyValue ; } } }break; case KEY_STATE_INTERIM2 : { if(KeyAction .KeyID == KEY_ID_NONE ) { KeyAction .KeyState = KEY_STATE_INIT ; LastKeyValue = KEY_STATE_INIT | KeyIDTmp | KEY_EVENT_DOUBLESHORT ; return LastKeyValue ; } else { if(TIM_IsExpired (KeyAction .KeyPressedTime + KeyTime_Long )) { KeyAction .KeyState = KEY_STATE_UNRELEASED ; LastKeyValue = KEY_STATE_UNRELEASED | KeyIDTmp | KEY_EVENT_SHORTLONG ; return LastKeyValue ; } } }break; case KEY_STATE_UNRELEASED : { if(KeyAction .KeyID == KEY_ID_NONE) { KeyAction .KeyState = KEY_STATE_INIT ; } else { KeyIDTmp = KeyAction .KeyID ; return KEY_STATE_UNRELEASED | KeyIDTmp | (LastKeyValue & 0x0007 ) ; } }break; default : break; } return KeyValue ; } /********************************************************************************************** * Function Name : Key_SetEventTime * Function Prototype : void Key_SetEventTime(u32 NewKeyTime_Jitter , * u32 NewKeyTime_Interval , * u32 NewKeyTime_Long , * u32 NewKeyTime_Double ) * Behavior Description : Set KeyTime_Jitter,KeyTime_Interval,KeyTime_Long,Time_Double * Input Parameter : NewKeyTime_Jitter,NewKeyTime_Interval, * NewKeyTime_Long,NewTime_Double * * Output Parameter : None * Return Parameter : None * Required Perconditions : None * Called Functions : None * * =========================================================================================== * @ attention * * * * *********************************************************************************************/ /* ********************************************************************************************************* * Key_SetEventTime(KeyTime_Jitter,...) * * Description : Set JitterTimer, IntervalTime, LongTime, and DoubleTime. * * Argument(s) : NewKeyTime_Jitter,NewKeyTime_Interval,NewKeyTime_Long,NewKeyTime_Double. * * Return(s) : None. * * Note(s) : (1) you'd better not to call this function ********************************************************************************************************* */ void Key_SetEventTime(u32 NewKeyTime_Jitter , u32 NewKeyTime_Interval , u32 NewKeyTime_Long , u32 NewKeyTime_Double ) { KeyTime_Jitter = NewKeyTime_Jitter ; KeyTime_Interval = NewKeyTime_Interval ; KeyTime_Long = NewKeyTime_Long ; KeyTime_Double = NewKeyTime_Double ; }[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。