|
本帖最后由 lhdzwkk 于 2011-12-12 09:41 编辑
单个按键长按:
多定义一个变量:- static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */
- static uint8 halKeySameKeyCnt; /* used to store previous key same state cnt in polling mode *///20110621
复制代码 初始化:- void HalKeyInit( void )
- {
- /* Initialize previous key to 0 */
- halKeySavedKeys = 0;
- halKeySameKeyCnt =0;
复制代码 修改poll函数:- void HalKeyPoll (void)
- {
- //20110621对轮询的做了修改,支持长按
- //都改为低电平触发
- //不对中断方式有影响和改变
- //20111026对中断做了修改,支持单键的长按
- uint8 keys = 0;
- #if (0)
- if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */
- {
- keys = halGetJoyKeyInput();
- }
- #endif
- /* If interrupts are not enabled, previous key status and current key status
- * are compared to find out if a key has changed status.
- */
- if (HAL_PUSH_BUTTON1())
- {
- keys |= HAL_KEY_SW_6;
- }
- if (!Hal_KeyIntEnable)
- {
- if (keys == halKeySavedKeys)
- {
- /* Exit - since no keys have changed */
- //keke//check no change cnt
- if(keys)
- {
- halKeySameKeyCnt++;//再次轮询后keys值不变
- HalLcdWriteStringValue("KEYDOWN=",halKeySameKeyCnt,10,3);
- }
- else
- {
- halKeySameKeyCnt=0;
- }
- //--
- if(halKeySameKeyCnt==20)
- {
- if(pHalKeyProcessFunction)
- {
- (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_SHIFT);//区别于短按的传递参数
- //(pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);//短按对应HAL_KEY_STATE_NORMAL
- }
- //halKeySameKeyCnt=0;
- //halKeySavedKeys=0;
- keys=0;
- }
- return;
- }
- else
- {
- if(halKeySavedKeys && (pHalKeyProcessFunction)&&(halKeySameKeyCnt<20))//我们认为halKeySavedKeys为0,而keys不为0是最初按下状态,需要等按键弹起
- {
- (pHalKeyProcessFunction) (halKeySavedKeys, HAL_KEY_STATE_NORMAL);
- }
- halKeySameKeyCnt=0;
- }
- /* Store the current keys for comparation next time */
- halKeySavedKeys = keys;
- return;
- }
- else
- {
- HalLcdWriteStringValue("KEYDOWN=",halKeySameKeyCnt,10,3);
- /* Key interrupt handled here */
- if((halKeySavedKeys==0)&&(keys))//按下
- {
- osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);
- halKeySavedKeys = keys;
- return;
- }
- else if((keys)&&(keys == halKeySavedKeys))//按着
- {
- halKeySameKeyCnt++;
- osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);
- if(halKeySameKeyCnt==20)
- {
- if(pHalKeyProcessFunction)
- {
- (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_SHIFT);
- //(pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
- }
- //halKeySameKeyCnt=0;
- //halKeySavedKeys=0;
- //keys=0;
- //osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT); /* Cancel polling if active */
- }
- return;
- }
- else if((halKeySavedKeys)&&(keys==0))//弹起
- {
- if((pHalKeyProcessFunction)&&(halKeySameKeyCnt<20))
- {
- (pHalKeyProcessFunction) (halKeySavedKeys, HAL_KEY_STATE_NORMAL);
- }
- halKeySameKeyCnt=0;
- halKeySavedKeys = keys;
- //osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT); /* Cancel polling if active */
- return;
- }
- }
- /* Invoke Callback if new keys were depressed */
- if (keys && (pHalKeyProcessFunction))
- {
- (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
- }
- }
复制代码 应用层代码:- void DEMO_HandleKeys( byte shift, byte keys )
- {
- if ( shift )
- {
- if ( keys & HAL_KEY_SW_6 )
- {
- #if defined ( LCD_SUPPORTED )
- HalLcdWriteString( "KEY=long", 3 );
- //HalLcdWriteStringValue("netid",_NIB.nwkPanId,16,2);//实时的panid
- #endif
- }
- }
- else
- {
- if ( keys & HAL_KEY_SW_6 )
- {
- #if defined ( LCD_SUPPORTED )
- HalLcdWriteString( "KEY=short", 3 );
- #endif
- }
- }
- }
复制代码 |
|