文件选择用zstack实现自己的sample--点灯

本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~

    1,复制samples上面的GenericApp,并粘贴到以后文件夹下,重命名为LED;

    2,删除GenericApp.c和GenericApp.h

    3,  新建 文件Coordinator.h,Coordinator.c  Enddevice.c并且将文件添加到工程 。

    三个文件里面的代码如下:

    Coordinator.h:

#ifndef COORDINATOR_H
#define COORDINATOR_H
#include "ZComDef.h"

#define GENERICAPP_ENDPOINT                10

#define GENERICAPP_PROFID                  0x0F04 
#define GENERICAPP_DEVICEID                0x0001
#define GENERICAPP_DEVICE_VERSION          0   
#define GENERICAPP_FLAGS                   0
#define GENERICAPP_MAX_CLUSTERS            1  
#define GENERICAPP_CLUSTERID               1

extern void GenericApp_Init(byte task_id);
extern UINT16 GenericApp_ProcessEvent(byte task_id, UINT16 events);
#endif

    Coordinator.c:

#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"

#include "Coordinator.h"
#include "DebugTrace.h"


#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include <string.h>


// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};

endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;   // Task ID for internal task/event processing
                          // This variable will be received when
                          // GenericApp_Init() is called.

byte GenericApp_TransID;  // This is the unique message ID (counter)

void GenericApp_MessageMSGCB ( afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage( void);

void GenericApp_Init(byte task_id)
{
    GenericApp_TaskID                 = task_id;
    GenericApp_TransID                = 0;
    GenericApp_epDesc.endPoint        = GENERICAPP_ENDPOINT;
    GenericApp_epDesc.task_id         = &GenericApp_TaskID;
    GenericApp_epDesc.simpleDesc      =
                    (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
    GenericApp_epDesc.latencyReq      = noLatencyReqs;
    afRegister(&GenericApp_epDesc);
}

UINT16 GenericApp_ProcessEvent( byte task_id,  UINT16 events )
{
    afIncomingMSGPacket_t *MSGpkt;
    if ( events & SYS_EVENT_MSG )
    {
        MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID);
        while( MSGpkt )
        {
            switch ( MSGpkt->hdr.event )
            {
            case AF_INCOMING_MSG_CMD:
              {
                  GenericApp_MessageMSGCB( MSGpkt );
                   break;
              }
            default:
              break;
            }
            osal_msg_deallocate( (uint8 *)MSGpkt );
            MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive
              (GenericApp_TaskID);
        }
        return  (events  ^ SYS_EVENT_MSG);
    }
    return  0;
}

void GenericApp_MessageMSGCB(afIncomingMSGPacket_t *pkt)
{
  unsigned char  buffer[4] = "    ";
  switch ( pkt->clusterId)
  {
  case GENERICAPP_CLUSTERID:
    {
        osal_memcpy(buffer,pkt->cmd.Data,3);
        if ( (buffer[0] == 'L') ||
             (buffer[1] == 'E') ||
             (buffer[2] == 'D'))
        {
            HalLedBlink(HAL_LED_2,0,50,500);
        }
        else
        {
            HalLedSet(HAL_LED_2,HAL_LED_MODE_ON);
        }
        break;
          
    }
  }
}
    每日一道理
生命,是一场漫长的棋局。这盘棋没有猎猎西风,没有四起狼烟,只有在取舍和进退中抉择。只有像棋中的小卒那样,勇往直前,毫不退缩沿着沟沟坎坎的人生之路,艰难而执着的求索,前进,才会谱写人生最壮丽的强者之歌。

    Enddevice.c:

#include "OSAL.h"
#include "AF.h"
#include "ZDApp.h"
#include "ZDObject.h"
#include "ZDProfile.h"

#include "Coordinator.h"
#include "DebugTrace.h"


#if !defined( WIN32 )
  #include "OnBoard.h"
#endif

/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "hal_uart.h"
#include <string.h>
// This list should be filled with Application specific Cluster IDs.
const cId_t GenericApp_ClusterList[GENERICAPP_MAX_CLUSTERS] =
{
  GENERICAPP_CLUSTERID
};

const SimpleDescriptionFormat_t GenericApp_SimpleDesc =
{
  GENERICAPP_ENDPOINT,              //  int Endpoint;
  GENERICAPP_PROFID,                //  uint16 AppProfId[2];
  GENERICAPP_DEVICEID,              //  uint16 AppDeviceId[2];
  GENERICAPP_DEVICE_VERSION,        //  int   AppDevVer:4;
  GENERICAPP_FLAGS,                 //  int   AppFlags:4;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList,  //  byte *pAppInClusterList;
  GENERICAPP_MAX_CLUSTERS,          //  byte  AppNumInClusters;
  (cId_t *)GenericApp_ClusterList   //  byte *pAppInClusterList;
};

endPointDesc_t GenericApp_epDesc;
byte GenericApp_TaskID;   // Task ID for internal task/event processing
                          // This variable will be received when
                          // GenericApp_Init() is called.

byte GenericApp_TransID;  // This is the unique message ID (counter)
devStates_t GenericApp_NwkState;

void GenericApp_MessageMSGCB ( afIncomingMSGPacket_t *pckt);
void GenericApp_SendTheMessage( void);

void GenericApp_Init(byte task_id)
{
    GenericApp_TaskID                 = task_id;
    GenericApp_NwkState               = DEV_INIT;
    GenericApp_TransID                = 0;
    GenericApp_epDesc.endPoint        = GENERICAPP_ENDPOINT;
    GenericApp_epDesc.task_id         = &GenericApp_TaskID;
    GenericApp_epDesc.simpleDesc      =
                    (SimpleDescriptionFormat_t *)&GenericApp_SimpleDesc;
    GenericApp_epDesc.latencyReq      = noLatencyReqs;
    afRegister(&GenericApp_epDesc);
}

UINT16 GenericApp_ProcessEvent( byte task_id,  UINT16 events )
{
    afIncomingMSGPacket_t *MSGpkt;
    if ( events & SYS_EVENT_MSG )
    {
        MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive(GenericApp_TaskID);
        while( MSGpkt )
        {
            switch ( MSGpkt->hdr.event )
            {
            case ZDO_STATE_CHANGE:
              {
                   GenericApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
                   if(GenericApp_NwkState == DEV_END_DEVICE)
                   {
                      GenericApp_SendTheMessage();
                   }
                   break;
              }
            default:
              break;
            }
            osal_msg_deallocate( (uint8 *)MSGpkt );
            MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive
              (GenericApp_TaskID);
        }
        return  (events  ^ SYS_EVENT_MSG);
    }
    return  0;
}

void GenericApp_SendTheMessage( void )
{
    unsigned char theMessageDate[4] = "LED";
    afAddrType_t my_DstAddr;
    my_DstAddr.addrMode = (afAddrMode_t)Addr16Bit;
    my_DstAddr.endPoint = GENERICAPP_ENDPOINT;
    my_DstAddr.addr.shortAddr = 0x0000;
    AF_DataRequest( &my_DstAddr,
                    &GenericApp_epDesc,
                    GENERICAPP_CLUSTERID,
                    3,
                    theMessageDate,
                    &GenericApp_TransID,
                    AF_DISCV_ROUTE,
                    AF_DEFAULT_RADIUS);
    HalLedBlink(HAL_LED_2,0,50,500);
}

    选择CoodinatorEB,在Endevice.c上右键选择options,把Exclude from build  这个选项勾选 上;

    同样选择Endevice,让Coordinator.c不编译 。

    分别将两个程序下载到 两个不同的板子 上。先开启协调器的电源,这时候LED2的灯是 没有 闪烁的,再开启终端设备的电源 ,过几秒后后协调器 和终端设备的LED2会一同闪烁。

文章结束给大家分享下程序员的一些笑话语录: 问答
Q:你是怎么区分一个内向的程序员和一个外向的程序员的? A:外向的程序员会看着你的鞋和你说话时。
Q:为什么程序员不能区分万圣节和圣诞节? A:这是因为 Oct 31 == Dec 25!(八进制的 31==十进制的 25)

你可能感兴趣的:(sample)