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

📄 add_keypad.c

📁 Nulceus下的键盘驱动程序
💻 C
📖 第 1 页 / 共 5 页
字号:
   \return none   \retval none*//*----------------------------------------------------------------------- */void Keyp_SwitchSingleKeyMode(void){    mg_u8KeypadMode = eSingleKeyMode;    return;}/*-----------------------------------------------------------------------*//**    Switch Keypad mode to double key detection.       \param  none   \return none   \retval none*//*----------------------------------------------------------------------- */void Keyp_SwitchDoubleKeyMode(void){    mg_u8KeypadMode = eDoubleKeyMode;    return;}/*-----------------------------------------------------------------------*//**   Keypad HISR.   This function calls the keypad state machine: Keyp_HandleKey(u8KeyType).   The HISR is activated during the Keypad-LISRs.      \param  none   \return none   \retval none*//*----------------------------------------------------------------------- */void Keyp_HisrHandler(void){    UINT8 u8KeyType;    if(PddKpd_InterruptIsDisabled() == FALSE)    {   // HISR is called but the keypad interrupts are enabled.       PddKpd_DisableKeypadInt0_3();       SEXIT(KEYPAD_INTERRUPS_NOT_DISABLED);     }    u8KeyType = Keyp_GetKeyType();  // read scancodes from keypad peripheral    if(u8KeyType == eApplicationStarbitProcessNotPresent)    {   // Startbit process not created -> do nothing        PddKpd_EnableKeypadInt0_3();    // enable keypad interrupts        return;    }        Keyp_HandleKey(u8KeyType);      // call keypad state machine    Keyp_UpdateHistory();    PddKpd_EnableKeypadInt0_3();    // enable keypad interrupts    return;}/*-----------------------------------------------------------------------*//**   This function is called from HISR. The function returns the key type   of the current pressed or released key. For this the function compares   the scancode from the last call, with current value. It starts with   the special keys.   \param   none   \return  UINT8 Key type   \retval  0x0 = eSpecialKey   \retval  0x1 = eStandardKey   \retval  0xFD= eApplicationStarbitProcessNotPresent   \retval  0xFE= eStandardKeyPressedFirst   \retval  0xFF= eNoKeyPressed*//*----------------------------------------------------------------------- */UINT8 Keyp_GetKeyType(void){    UINT8 u3KeyType;//---- save scancodes -----------------------------------------------------    mg_u32CurrentKenum3 = PddKpd_GetScancodeKeynum3();     // get current scancode for special keys    mg_u32CurrentKenum1 = PddKpd_GetScancodeKeynum1();     // get current scancode for special keys    if(GbsExtProcessExists( KBD_KEYPADMESSAGES ) == FALSE)    {   // Startbit process not created -> do nothing        u3KeyType = eApplicationStarbitProcessNotPresent;        return (u3KeyType);    }    //---- test special key ---------------------------------------------------    if(mg_u32CurrentKenum3 == mg_u32OldKenum3 && mg_u32CurrentKenum3 == NO_KEY_SCANCODE)    {   // no special key pressed => test standard key        u3KeyType = eNoKeyPressed;    }    else if(mg_u32CurrentKenum3 == mg_u32OldKenum3)    {   // no new special key, but standard key was pressed or released. Ignore standard key.        u3KeyType = eSpecialKey;        return(u3KeyType);    }    else if(mg_u32CurrentKenum3 != mg_u32OldKenum3)    {   // special key pressed or released        u3KeyType = eSpecialKey;                if(mg_u8KeyState == eFirstStandardKey ||           mg_u8KeyState == eSecondStandardKey ||           mg_u8KeyState == eMultipleStandardKey)        {   // standard key was pressed first            if(mg_u32CurrentKenum3 == NO_KEY_SCANCODE)            {   // all special keys are released, we have to test the status of the standard keys                u3KeyType = eStandardKey;            }            else            {   // standard key was detected first -> ignore special key                // in this case it is not possible to detect a standard key                u3KeyType = eStandardKeyPressedFirst;            }        }        return(u3KeyType);    }//---- test standard key --------------------------------------------------    if(mg_u32CurrentKenum1 != mg_u32OldKenum1)    {   // standard key pressed or released        u3KeyType = eStandardKey;        return (u3KeyType);    }    else    {//---- no key pressed -----------------------------------------------------        if(mg_u32CurrentKenum3 == NO_KEY_SCANCODE)        {   // no special key pressed            if(mg_u32CurrentKenum1 == NO_KEY_SCANCODE)            {   // no key pressed                u3KeyType = eNoKeyPressed;            }            // standard key pressed            u3KeyType = eStandardKey;        }        else        {   // special key pressed            u3KeyType = eSpecialKey;        }        return (u3KeyType);    }}/*-----------------------------------------------------------------------*//**   This function has to be called after every call of  Keyp_GetKeyType().   This function updates the "scancode history".   \param  none   \return none*//*----------------------------------------------------------------------- */void Keyp_UpdateHistory(void){//---- update scancode history ----------------------------------------------    mg_u32OldKenum1 = mg_u32CurrentKenum1;         // update scancode history    mg_u32OldKenum3 = mg_u32CurrentKenum3;         // update scancode history    return;}/*-----------------------------------------------------------------------*//**   Keypad state machine.   This function is called from HISR after Keyp_GetKeyType().   There exist the following states:   - eIdle: wait for keypad event,   - eFirstStandardKey: one standard key was pressed,   - eFirstSpecialKey: one special key was pressed,   - eSecondStandardKey: two standard key are pressed,   - eSecondSpecialKey: two special keys are pressed,   - eMultipleStandardKey: more then two standard keys are pressed,   - eMultipleSpecialKey: more then two special keys are pressed   \param   UINT8 Key type     \return  keypad state*//*----------------------------------------------------------------------- */void Keyp_HandleKey(UINT8 u8KeyType){    UINT8 i;    UINT8 u8KeyCtr;    UINT8 u8Key1Flag;    UINT32 u32Scan;    UINT32 u32KeyDummy;        TKey KeyDetectionArray[MULTIPLE_KEY_BUFFER_SIZE]; ///< buffer for key detection,                                                      ///< used in case if more then                                                      ///< one key is pressed at the same time    for(i=0; i<MULTIPLE_KEY_BUFFER_SIZE; i++)    {   // init array        KeyDetectionArray[i].u32Scancode = 0;        KeyDetectionArray[i].u8Type = 0;    }        u32KeyDummy = 0x1;    u8KeyCtr = 0;    u8Key1Flag = 0;// find out which keys are pressed, count and save them    switch (u8KeyType)    {        case eStandardKey:            u32Scan = mg_u32CurrentKenum1;            for(i=0; i<32; i++)                // loop for standard key detection            {                if((u32Scan & 0x01) == 0)   // zero detection                {   // save detected single keycode                    KeyDetectionArray[u8KeyCtr].u32Scancode = 0xFFFFFFFF ^ u32KeyDummy;                    KeyDetectionArray[u8KeyCtr].u8Type = eStandardKey;                    u8KeyCtr++;                    if(u8KeyCtr >= (MULTIPLE_KEY_BUFFER_SIZE))                    {   // buffer overflow                        SEXIT(MULTIPLE_KEY_DETECTION_BUFFER_OVERFLOW);                        break;                    }                }                u32Scan = u32Scan >> 1; // right shift of scancode                u32KeyDummy = u32KeyDummy << 1;            }            break;        case eSpecialKey:            u32Scan = mg_u32CurrentKenum3;            for(i=0; i<8; i++)                 // loop for special key detection            {                if((u32Scan & 0x01) == 0)   // zero detection                {                    KeyDetectionArray[u8KeyCtr].u32Scancode = 0xFFFFFFFF ^ u32KeyDummy;                    KeyDetectionArray[u8KeyCtr].u8Type = eSpecialKey;                    u8KeyCtr++;                }                u32Scan = u32Scan >> 1; // right shift of scancode                u32KeyDummy = u32KeyDummy << 1;            }            break;        case eNoKeyPressed:            u8KeyCtr = 0;            break;                    case eStandardKeyPressedFirst:            // ignore special keys do not switch state            return;        default:            break;    }// now all pressed keys are known and saved // compare the pressed keys with the already known one    switch (mg_u8KeyState)    {        case eIdle:            if(u8KeyType == eStandardKey || u8KeyType == eSpecialKey)            {   // standard key was pressed                switch (u8KeyCtr)                {                    case 0:                        // no key pressed                        // check if an old key is in buffer                        if(Key2.u32Scancode != 0)                        {                            Keyp_WriteReleaseKey(Key2.u8Type, Key2.u32Scancode);                            Key2.u32Scancode = 0;                            Key2.u8Type = 0;                        }                        if(Key1.u32Scancode != 0)                        {                            Keyp_WriteReleaseKey(Key1.u8Type, Key1.u32Scancode);                            Key1.u32Scancode = 0;                            Key1.u8Type = 0;                        }                    //---- switch state -------                        mg_u8KeyState = eIdle;                        return;                                            case 1:                        // one key is pressed                        Keyp_WriteAndSaveKey1(KeyDetectionArray[0].u8Type, KeyDetectionArray[0].u32Scancode);                    //---- switch state -------                        if(u8KeyType == eSpecialKey)                        {   // special key detected => switch to special key state                            mg_u8KeyState = eFirstSpecialKey;                        }                        else                        {   // standard key detected => switch to standard key state                            mg_u8KeyState = eFirstStandardKey;                        }                        return;                                            case 2:                        // two keys are pressed                        Keyp_WriteAndSaveKey1(KeyDetectionArray[0].u8Type, KeyDetectionArray[0].u32Scancode);                        if(mg_u8KeypadMode == eSingleKeyMode)                        {   // for single key mode                        //---- switch state -------                            if(u8KeyType == eSpecialKey)                            {   // special key detected => switch to special key state                                mg_u8KeyState = eMultipleSpecialKey;                            }                            else                            {   // standard key detected => switch to standard key state                                mg_u8KeyState = eMultipleStandardKey;                            }                            return;                        }                        // for double key detection                        Keyp_WriteAndSaveKey2(KeyDetectionArray[1].u8Type, KeyDetectionArray[1].u32Scancode);                    //---- switch state -------                        if(u8KeyType == eSpecialKey)                        {   // special key detected => switch to special key state                            mg_u8KeyState = eSecondSpecialKey;                        }                        else                        {   // standard key detected => switch to standard key state

⌨️ 快捷键说明

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