zigbee 是怎样注册,产生,处理 按键事件的?(转)
当硬件有按键按下时调用
uint8 OnBoard_SendKeys( uint8 keys, uint8 state )
{
keyChange_t *msgPtr;
if ( registeredKeysTaskID != NO_TASK_ID )
{
// Send the address to the task
msgPtr = (keyChange_t *)osal_msg_allocate( sizeof(keyChange_t) );
if ( msgPtr )
{
msgPtr->hdr.event = KEY_CHANGE;
msgPtr->state = state;
msgPtr->keys = keys;
osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr );
}
return ( ZSuccess );
}
else
return ( ZFailure );
}
调用osal_msg_send( registeredKeysTaskID, (uint8 *)msgPtr ); 将消息发送个任务registeredKeysTaskID;
按键事件能且只能在一个任务中注册;我们看看registeredKeysTaskID他是在被注册到哪个任务中去了;
uint8 RegisterForKeys( uint8 task_id )
{
// Allow only the first task
if ( registeredKeysTaskID == NO_TASK_ID )
{
registeredKeysTaskID = task_id;
return ( true );
}
else
return ( false );
}
GenericApp.c中我们看到 按键事件被注册到APP任务中;
RegisterForKeys( GenericApp_TaskID );
总结下按键事件:
在应用程序中,用户将按键事件注册到应用层任务中, 当硬件检测到有按键事件发生时,将消息发送个应用层的任务ID交由应用程序处理;
UINT16 GenericApp_ProcessEvent( byte task_id, UINT16 events )
{if ( events & SYS_EVENT_MSG )
{
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
while ( MSGpkt )
{
switch ( MSGpkt->hdr.event )
{
case KEY_CHANGE:
GenericApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
break;
osal_msg_deallocate( (uint8 *)MSGpkt );
// Next
MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( GenericApp_TaskID );
}
// return unprocessed events
return (events ^ SYS_EVENT_MSG);
}
我们在zstack 中常见到这样的定义
#define HAL_KEY_JOY_MOVE_BIT BV(0)
BV的定义如下:
#ifndef BV
#define BV(n) (1 << (n))
#end
(1 << (n)) 即1左移N位 相当于1乘以2的N 次方; 1 X (2^n)
所以:
/* SW_6 interrupts */
#define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
#define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
HAL_KEY_SW_6_IENBIT 0010 0000H
感谢分享