#include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" #include #include "MT_UART.h" #include "MT_APP.h" #include "MT.h" #include "hal_uart.h" #include "hal_adc.h" #include #include "string.h" #include #include "public.h" void App_GatherTask(void); // This list should be filled with Application specific Cluster IDs. const cId_t SD_App_ClusterList[SD_APP_MAX_CLUSTERS] = { SD_APP_BROADCAST_CLUSTERID, SD_APP_POINT_TO_POINT_CLUSTERID };//这就是每一个端点里面的簇有2个并且簇的ID分别是1,2 const SimpleDescriptionFormat_t SD_App_SimpleDesc = { SD_APP_ENDPOINT, // int Endpoint; SD_APP_PROFID, // uint16 AppProfId[2]; SD_APP_DEVICEID, // uint16 AppDeviceId[2]; SD_APP_DEVICE_VERSION, // int AppDevVer:4; SD_APP_FLAGS, // int AppFlags:4; SD_APP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SD_App_ClusterList, // uint8 *pAppInClusterList; SD_APP_MAX_CLUSTERS, // uint8 AppNumInClusters; (cId_t *)SD_App_ClusterList // uint8 *pAppInClusterList; }; // This is the Endpoint/Interface description. It is defined here, but // filled-in in SD_App_Init(). Another way to go would be to fill // in the structure here and make it a "const" (in code space). The // way it's defined in this sample app it is define in RAM. endPointDesc_t SD_App_epDesc;//定义的端点描述符 /********************************************************************* * LOCAL VARIABLES */ uint8 SD_App_TaskID; // Task ID for internal task/event processing // This variable will be received when // SD_App_Init() is called. devStates_t SD_App_NwkState; uint8 SD_App_TransID; // This is the unique message ID (counter) afAddrType_t SD_App_Broadcast_DstAddr; afAddrType_t SD_App_Point_To_Point_DstAddr; /********************************************************************* * LOCAL FUNCTIONS */ void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pckt ); /* 串口基本定义 */ #define MY_DEFINE_UART_PORT 0 //自定义串口号(0,1); #define RX_MAX_LENGTH 20 //接收缓冲区最大值: 20个字节; uint8 RX_BUFFER[RX_MAX_LENGTH]; //接收缓冲区; void UartCallBackFunction(uint8 port , uint8 event); //回调函数声明,定义在最后面; /* 配置串口 */ halUARTCfg_t uartConfig; //定义串口配置结构体变量; void Uart_Config(void); //函数声明; void Uart_Config(void) //函数定义; { uartConfig.configured = TRUE; //允许配置; uartConfig.baudRate = HAL_UART_BR_115200;//波特率; uartConfig.flowControl = FALSE; uartConfig.flowControlThreshold = 64; //don't care - see uart driver. uartConfig.rx.maxBufSize = 128; //串口接收缓冲区大小 uartConfig.tx.maxBufSize = 128; //串口发送缓冲区大小 uartConfig.idleTimeout = 6; //don't care - see uart driver. uartConfig.intEnable = TRUE; //使能中断 uartConfig.callBackFunc = UartCallBackFunction; //指定回调函数名; } /********************************************************************* * @fn SD_App_Init * * @brief Initialization function for the Generic App Task. * This is called during initialization and should contain * any application specific initialization (ie. hardware * initialization/setup, table initialization, power up * notificaiton ... ). * * @param task_id - the ID assigned by OSAL. This ID should be * used to send messages and set timers. * * @return none */ void SD_App_Init( uint8 task_id ) { SD_App_TaskID = task_id; SD_App_NwkState = DEV_INIT; SD_App_TransID = 0; #if defined ( BUILD_ALL_DEVICES ) // The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START // We are looking at a jumper (defined in SD_AppHw.c) to be jumpered // together - if they are - we will start up a coordinator. Otherwise, // the device will start as a router. if ( readCoordinatorJumper() ) zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR; else zgDeviceLogicalType = ZG_DEVICETYPE_ROUTER; #endif // BUILD_ALL_DEVICES #if defined ( HOLD_AUTO_START ) // HOLD_AUTO_START is a compile option that will surpress ZDApp // from starting the device and wait for the application to // start the device. ZDOInitDevice(0); #endif /* 串口操作 */ Uart_Config(); //配置串口 HalUARTOpen(MY_DEFINE_UART_PORT , &uartConfig); //打开串口 HalUARTWrite(MY_DEFINE_UART_PORT ,"HAL_UART_OK\r\n" , 15); // Setup for the periodic message's destination address // Broadcast to everyone SD_App_Broadcast_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播模式 SD_App_Broadcast_DstAddr.endPoint = SD_APP_ENDPOINT; SD_App_Broadcast_DstAddr.addr.shortAddr = 0xFFFF; SD_App_Point_To_Point_DstAddr.addrMode = (afAddrMode_t)Addr16Bit; SD_App_Point_To_Point_DstAddr.endPoint = SD_APP_ENDPOINT; SD_App_Point_To_Point_DstAddr.addr.shortAddr = 0x0000; // // Fill out the endpoint description. SD_App_epDesc.endPoint = SD_APP_ENDPOINT; SD_App_epDesc.task_id = &SD_App_TaskID; SD_App_epDesc.simpleDesc = (SimpleDescriptionFormat_t *)&SD_App_SimpleDesc; SD_App_epDesc.latencyReq = noLatencyReqs; // // // Register the endpoint description with the AF afRegister( &SD_App_epDesc ); // Register for all key events - This app will handle all key events // RegisterForKeys( SD_App_TaskID );//注册所有按键事件-此应用程序将处理所有关键事件。 // P1DIR|=0x02;//p11输出 // Start sending the periodic message in a regular interval. //这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始 //触发第一个周期信息的发送,然后周而复始下去 osal_start_timerEx(SD_App_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT); P0DIR&=~0x20;//p05设置为输入 P0DIR&=~0x80;//p06设置为输入 InitTimer3(); InitTimer4(); // P0DIR|=0x20; } #define RT_Input P0_7 uint8 SendData[10]={0};//发送数据缓存区 int IfgTimer=0; char RT_InFlag=0;//感应标志 uint16 ifg_Rt =0;//计时 char Send_Flag=0; static uint16 GetTime=0; void App_GatherTask(void) { SendData[0]=0xAA; if(++GetTime>80)//上电40S不检测 等到人体感应传感器稳定后开始检测 { GetTime=80; if(RT_Input==1) { SendData[1]=1; //有人 ifg_Rt=0; } else { ifg_Rt++; if(ifg_Rt>36)//计时 20s { SendData[1]=0;//没人 ifg_Rt=0; } } } AF_DataRequest(&SD_App_Broadcast_DstAddr, //发送目的地址+端点地址和传送模式 &SD_App_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EP SD_APP_BROADCAST_CLUSTERID, //被Profile指定的有效的集群号 2, // 发送数据长度 SendData,// 发送数据缓冲区 &SD_App_TransID, // 任务ID号 AF_DISCV_ROUTE, // 有效位掩码的发送选项 AF_DEFAULT_RADIUS );//传送跳数,通常设置为AF_DEFAULT_RADIUS } //用户应用任务的事件处理函数 //task_id 任务id号,events 事件id号 uint16 SD_App_ProcessEvent( uint8 task_id, uint16 events ) { afIncomingMSGPacket_t *MSGpkt; (void)task_id; // Intentionally unreferenced parameter if ( events & SYS_EVENT_MSG )//接收系统消息事件 再进行判断 { //接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记 MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID ); //根据不同任务id号接收消息 在强制转换 while ( MSGpkt ) { switch ( MSGpkt->hdr.event ) { case CMD_SERIAL_MSG: break; // Received when a messages is received (OTA) for this endpoint case AF_INCOMING_MSG_CMD: //接收数据事件,调用函数AF_DataRequest()接收数据 //注册完了之后我们就能收到外面从这个端点进来的数据信息了 SD_App_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理 break; case ZDO_STATE_CHANGE: //只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。 //同时完成对协调器,路由器,终端的设置 SD_App_NwkState = (devStates_t)(MSGpkt->hdr.status); if ((SD_App_NwkState == DEV_ZB_COORD)//判断是否为协调器 || (SD_App_NwkState == DEV_ROUTER) || (SD_App_NwkState == DEV_END_DEVICE)) { if(SD_App_NwkState == DEV_ZB_COORD) { // LS164_BYTE(11);//'C'协调器 HalUARTWrite(MY_DEFINE_UART_PORT ,"Coordinator_OK!!\r\n" , 18); } if(SD_App_NwkState == DEV_ROUTER) { // LS164_BYTE(12);//‘R’路由器 HalUARTWrite(MY_DEFINE_UART_PORT ,"Router_OK!!\r\n" , 13); } if(SD_App_NwkState == DEV_END_DEVICE) { // LS164_BYTE(13);//‘E’终端 HalUARTWrite(MY_DEFINE_UART_PORT ,"EndDevice_OK!!\r\n" ,16); } } else { // Device is no longer in the network } break; default: break; } // Release the memory 释放内存 osal_msg_deallocate( (uint8 *)MSGpkt ); // Next - if one is available 指针指向下一个放在缓冲区的待处理的事件, //返回while ( MSGpkt )判断重新处理事件,直到缓冲区没有等待处理事件为止 MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SD_App_TaskID ); } // return unprocessed events返回未处理的事件 return (events ^ SYS_EVENT_MSG); } // Send a message out - This event is generated by a timer // (setup in SampleApp_Init()). if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )//发送消息-此事件由计时器生成 { // Send the periodic message 处理周期性事件, //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器 //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了, //可以做为传感器定时采集、上传任务 App_GatherTask(); // Setup to send message again in normal period (+ a little jitter) osal_start_timerEx( SD_App_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT, (SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );//再次启动 // 返回未处理的事件 return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT); } return 0; } //接收数据,参数为接收到的数据 void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { //收到广播数据 switch ( pkt->clusterId )//判断簇ID { case SD_APP_POINT_TO_POINT_CLUSTERID: //簇1 break; case SD_APP_BROADCAST_CLUSTERID: //簇2 break; } } /********************************************************************* * * 函数名: UartCallBackFunction * 函数功能:串口回调函数,接收到数据时会调用到该函数; * 传入参数:port:串口号 event:事件 * 返回参数:无 * *********************************************************************/ static void UartCallBackFunction(uint8 port , uint8 event) { uint8 RX_Length = 0; //接收到字符串大小; RX_Length = Hal_UART_RxBufLen(MY_DEFINE_UART_PORT); //读取接收字符串大小; if(RX_Length != 0) //有数据存在; { //读取串口数据; HalUARTRead(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length); //发送回给电脑,使用 hal_uart.h 的接口函数: HalUARTWrite(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length); } }