#include "OSAL.h" #include "ZGlobals.h" #include "AF.h" #include "aps_groups.h" #include "ZDApp.h" #include "stdio.h" #include "SampleApp.h" #include "SampleAppHw.h" #include "OnBoard.h" //#include "hal_lcd.h" #include "hal_led.h" #include "hal_key.h" #include "hal_uart.h" #include "hal_adc.h" #include "MT_UART.h" #include "MT_APP.h" #include "MT.h" #include "hal_uart.h" #include "string.h" #define buzz P0_7 #define bg_led P0_6 uint8 sendblueFlag=0;//发送标志位 uint8 rfRxflag=0; // 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 ); void SD_App_SendBroadcastMessage( void ); void SD_App_SendPointToPointMessage(void); void App_KeyCheck(void); /* 串口基本定义 */ #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); //回调函数声明,定义在最后面; void App_userTask(void); void sys_delay_us(unsigned int n); /* 配置串口 */ halUARTCfg_t uartConfig; //定义串口配置结构体变量; void Uart_Config(void); //函数声明; void Uart_Config(void) //函数定义; { uartConfig.configured = TRUE; //允许配置; uartConfig.baudRate = HAL_UART_BR_9600;//波特率; 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向af注册端点描述。 afRegister( &SD_App_epDesc ); // 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); P1DIR|=0x02; P1_1=0;//灯关闭 P0DIR|=0x80; buzz=0; APCFG |= 0x40;//p06开启adc HalAdcInit(); //初始化adc转换 HalAdcSetReference(HAL_ADC_REF_AVDD);//设置参考电压为vdd } //32MHZ us延时函数; #pragma optimize=none void sys_delay_us(unsigned int n) { n>>=1; while(n--) { asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); } } //用户应用任务的事件处理函数 //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 key is pressed case KEY_CHANGE://按键事件 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) { // );//'C'协调器 // HalUARTWrite(MY_DEFINE_UART_PORT ,"Coordinator_OK!!\r\n" , 18); } if(SD_App_NwkState == DEV_ROUTER) { // ;//‘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); } if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT )//发送消息-此事件由计时器生成 { // Send the periodic message 处理周期性事件, //利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器 //开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了, //可以做为传感器定时采集、上传任务 App_userTask(); 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; } char Buff[10]={0}; uint8 rt_flag=0; uint8 bj_flag=0; long adc_val=0; void App_userTask(void) { if(rt_flag) { P1_1=1;//开灯 sys_delay_us(30000); sys_delay_us(30000); adc_val = HalAdcRead(HAL_ADC_CHANNEL_6,HAL_ADC_RESOLUTION_12);//ad读取adc adc_val = (adc_val*3300)/4096;// if(adc_val<10||adc_val>40) //灯 短路或者开路了 { buzz=1;//报警 }else { buzz=0; } }else { P1_1=0;buzz=0; } } //接收数据,参数为接收到的数据 void SD_App_MessageMSGCB( afIncomingMSGPacket_t *pkt ) { // uint8 temp[6]={0}; //收到数据 switch (pkt->clusterId)//判断簇ID { case SD_APP_POINT_TO_POINT_CLUSTERID: //簇1 break; case SD_APP_BROADCAST_CLUSTERID: //簇2 if(pkt->cmd.Data[0]==0xAA) { rt_flag = pkt->cmd.Data[1]; rfRxflag=1; } 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); if(strstr((char*)RX_BUFFER,"get")!=NULL) { sendblueFlag=1; } else if(strstr((char*)RX_BUFFER,"close")!=NULL) { sendblueFlag=0; } //发送回给电脑,使用 hal_uart.h 的接口函数: //HalUARTWrite(MY_DEFINE_UART_PORT , RX_BUFFER , RX_Length); } }