注:宏 应包括 HAL_KEY=TRUE
1.注册按键服务
/*********************************************************************
* Keyboard Register function
*
* The keyboard handler is setup to send all keyboard changes to
* one task (if a task is registered).
*
* If a task registers, it will get all the keys. You can change this
* to register for individual keys.
*********************************************************************/
uint8 RegisterForKeys( uint8 task_id )
{
// Allow only the first task
if ( registeredKeysTaskID == NO_TASK_ID )
{
registeredKeysTaskID = task_id;
return ( true );
}
else
return ( false );
}
2.在应用程序的事件处理函数中调用按键事件系统信息处理函数(按下按键时系统会产生对应的osal事件信息)
// SYS_EVENT_MSG 这是系统事件比如按键事件蓝牙读写事件处理,都会置这个事件
if ( events & SYS_EVENT_MSG )
{
uint8 *pMsg;
if ( (pMsg = osal_msg_receive( SimpleBLETest_TaskID )) != NULL )
{
simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); //调用按键事件系统信息处理函数
// Release the OSAL message
VOID osal_msg_deallocate( pMsg );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
3.按键事件系统信息处理函数调用对按键信息具体实现函数
/*********************************************************************
* @fn simpleBLECentral_ProcessOSALMsg
*
* @brief Process an incoming task message.
*
* @param pMsg - message to process
*
* @return none
*/
static void simpleBLECentral_ProcessOSALMsg( osal_event_hdr_t *pMsg )
{
switch ( pMsg->event )
{
case KEY_CHANGE:
simpleBLECentral_HandleKeys( ((keyChange_t *)pMsg)->state, ((keyChange_t *)pMsg)->keys );//调用具体实现函数
break;
default:
break;
}
}
4.按键事件具体实现函数
/*********************************************************************
* @fn simpleBLECentral_HandleKeys
*
* @brief Handles all key events for this device.
*
* @param shift - true if in shift/alt.
* @param keys - bit field for key events. Valid entries:
* HAL_KEY_SW_2
* HAL_KEY_SW_1
*
* @return none
*/
static void simpleBLECentral_HandleKeys( uint8 shift, uint8 keys )
{
(void)shift; // Intentionally unreferenced parameter
if ( keys & HAL_KEY_SW_6 )
{
printf("按键S1\n");
}
if ( keys & HAL_KEY_UP )
{
printf("按键 上\n");
}
if ( keys & HAL_KEY_LEFT )
{
printf("按键 左\n");
}
if ( keys & HAL_KEY_RIGHT )
{
printf("按键 右\n");
}
if ( keys & HAL_KEY_CENTER )
{
printf("按键 中间\n");
}
if ( keys & HAL_KEY_DOWN )
{
printf("按键 下\n");
}
}