设为首页 加入收藏

TOP

[ZigBee] 16、Zigbee协议栈应用(二)——基于OSAL的无线控制LED闪烁分析(下)(三)
2017-10-11 16:39:42 】 浏览:2713
Tags:ZigBee Zigbee 协议 应用 基于 OSAL 无线 控制 LED 闪烁 分析
//设定组名 58 aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把该组登记添加到APS中 59 60 #if defined ( LCD_SUPPORTED ) 61 HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息 62 #endif 63 } SampleApp_Init( uint8 task_id )

 

上篇讲过OS启动后进入大循环,扫描当前优先级最高的任务执行!

其中若osal_run_task执行了本工程定制化任务的消息,通过调用tasksArr[idx](上面 OSAL_SampleApp.c中讲的任务数组就相当于调用了SampleApp_ProcessEvent函数,将消息传送给任务处理函数:

 1 //用户应用任务的事件处理函数
 2 uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
 3 {
 4     afIncomingMSGPacket_t *MSGpkt;
 5     (void)task_id;  // Intentionally unreferenced parameter
 6 
 7     if ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断
 8     {
 9         //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记
10         MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
11         while ( MSGpkt )
12         {
13             switch ( MSGpkt->hdr.event )
14             {
15             // Received when a key is pressed
16 case KEY_CHANGE://按键事件
17                 SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );
18                 break;
19 
20             // Received when a messages is received (OTA) for this endpoint
21 case AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据
22 SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理(1、数据发送函数)
23                 break;
24 
25             // Received whenever the device changes state in the network
26 case ZDO_STATE_CHANGE: 27                 //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。
28                 //同时完成对协调器,路由器,终端的设置
29                 SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
30                 //if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件
31                 if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) )
32                 {
33                     // Start sending the periodic message in a regular interval.
34                     //这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始
35                     //触发第一个周期信息的发送,然后周而复始下去
36                     osal_start_timerEx( SampleApp_TaskID,
37                                         SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
38                                         SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
39                 }
40                 else
41                 {
42                     // Device is no longer in the network
43                 }
44                 break;
45 
46             default:
47                 break;
48             }
49 
50             // Release the memory 事件处理完了,释放消息占用的内存
51             osal_msg_deallocate( (uint8 *)MSGpkt );
52 
53             // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,
54             //返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止
55             MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );
56         }
57 
58         // return unprocessed events 返回未处理的事件
59         return (events ^ SYS_EVENT_MSG);
60     }
61 
62     // Send a message out - This event is generated by a timer
63     //  (setup in SampleApp_Init()).
64     if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )
65     {
66 // Send the periodic message 处理周期性事件, 67 //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器 68 //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了, 69 //可以做为传感器定时采集、上传任务 70  SampleApp_SendPeriodicMessage(); 71 
72         // Setup to send message again in normal period (+ a little jitter)
73         osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
74                             (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );
75 
76         // return unprocessed events 返回未处理的事件
77         return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);
78     }
79 
80     // Discard unknown events
81     return 0;
82 }

 

接收函数:

 1 //接收数据,参数为接收到的数据
 2 void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
 3 {
 4     uint16 flashTime;
 5     byte buf[3];
 6 
 7     switch ( pkt->clusterId ) //判断簇ID
首页 上一页 1 2 3 4 下一页 尾页 3/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇curses.h: No such file or direc.. 下一篇Arduino舵机控制

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目