BLE 协议栈中对于触发任务事件大可分为三种方式:
1 通过设置一个“软件定时器”,当其溢出时触发事件。osal_start_timerEx()—osalTimerUpdate()— osal_set_event() ;
2 通过调用系统消息传递机制触发事件。osal_msg_send()—osal_set_event()
3 直接调用osal_set_event()触发事件。
自定义消息的发送与接收
这三种划分方式算是前人之鉴吧,自己看的时候值分出了前两种,不过最终归到事件触发上都调用了osal_set_event()函数
看了两天的消息触发事件,终于看出头了 下面是发送消息的一个例子!
***************** 自定义 消息的发送 ****************************************/
#define king_come 0xFC // 定义消息标志位
注意 自定义消息的标志位的值只能是0xE0 到 0xFC 之间
static int right_come=0;
typedef struct
{
osal_event_hdr_t hdr;
uint8 mark;
}myUartMsg_t; // 定义消息的结构体
void xy_come(void) // 配置消息参数
{
myUartMsg_t *myUartMsg;
myUartMsg=(myUartMsg_t*)osal_msg_allocate(sizeof(myUartMsg_t));
myUartMsg->hdr.event=king_come;
myUartMsg->mark=right_come;
osal_msg_send(simpleBLETaskId,(uint8 *)myUartMsg);
}
消息的发送是通过置位SYS_EVENT_MSG来实现的(在 osal_msg_send中使用osal_set_event( destination_task, SYS_EVENT_MSG );置位)
在这个例子中将xy_come()放在了串口的回调函数中,这样一但受到了数据,就会发送一条消息给osal ,并被osal_msg_receive接收
/**************************** 消息的接收 *************************************/
在系统下一次轮询的时候,会进入SimpleBLECentral_ProcessEvent事件
并被 if ( events & SYS_EVENT_MSG )响应
通过osal_msg_receive( simpleBLETaskId )接收消息
uint16 SimpleBLECentral_ProcessEvent( uint8 task_id, uint16 events )
{
VOID task_id; // OSAL required parameter that isn't used in this function
if ( events & SYS_EVENT_MSG )
{
uint8 *pMsg;
if ( (pMsg = osal_msg_receive( simpleBLETaskId )) != NULL )//接收到消息
{
simpleBLECentral_ProcessOSALMsg( (osal_event_hdr_t *)pMsg ); // 处理函数
VO