WSNDEMO里面包含有很多值得看的东西
特别是那个文档AVR2130_LWMesh_Developer_Guide_v1.2.1.pdf
把DEMO讲得比较详细
依靠这个,完全不懂ZigBee的楼主,用按键远程控制了一个LED
特别是第五章 Application Programming,建议能够简单看看
汉化小组依然还是很有必要存在呀
慢慢来看
1、数据结构配置
两块板之间数据的发送和接收都通过AppMessage_t这个结构体进行传递
- typedef struct AppMessage_t {
- uint8_t commandId;
- uint8_t nodeType;
- uint64_t extAddr;
- uint16_t shortAddr;
- uint32_t softVersion;
- uint32_t channelMask;
- uint16_t panId;
- uint8_t workingChannel;
- uint16_t parentShortAddr;
- uint8_t lqi;
- int8_t rssi;
- uint8_t led_flag;
- struct {
- uint8_t type;
- uint8_t size;
- int32_t battery;
- int32_t temperature;
- int32_t light;
- } sensors;
- struct {
- uint8_t type;
- uint8_t size;
- char text[APP_CAPTION_SIZE];
- } caption;
- } AppMessage_t;
复制代码撸主新增了一个成员uint8_t led_flag
用于记录LED的状态,其实这个状态是由Router或者End Device的按键SW0进行控制
2、COORDINATOR侧
COORDINATOR用于接收Router或者End Device的数据
所有接收的动作都在appDataInd这个函数中
- static bool appDataInd(NWK_DataInd_t *ind)
- {
- AppMessage_t *msg = (AppMessage_t *)ind->data;
- #if (LED_COUNT > 0)
- //LED_Toggle(LED_DATA);
- #endif
- msg->lqi = ind->lqi;
- msg->rssi = ind->rssi;
- #if APP_COORDINATOR
- appUartSendMessage(ind->data, ind->size);
- if (APP_CommandsPending(ind->srcAddr)) {
- NWK_SetAckControl(APP_COMMAND_PENDING);
- }
- if (msg->led_flag)
- {
- LED_On(LED_DATA);
- }
- else
- {
- LED_Off(LED_DATA);
- }
-
- #endif
- return true;
- }
复制代码楼主新增了一段根据led_flag来判断LED靓妹的代码,就是那段if else
3、Router|End Device侧
所有发送的数据都在appSendData这个函数中
- static void appSendData(void)
- {
- #ifdef NWK_ENABLE_ROUTING
- appMsg.parentShortAddr = NWK_RouteNextHop(0, 0);
- #else
- appMsg.parentShortAddr = 0;
- #endif
- appMsg.sensors.battery = rand() & 0xffff;
- appMsg.sensors.temperature = rand() & 0x7f;
- appMsg.sensors.light = rand() & 0xff;
- if (!Key_Detect)
- {
- appMsg.led_flag = 1;
- usart_write_buffer_wait(&usart_instance, "LED ON!!\r\n", sizeof("LED ON!!\r\n"));
- }
- else
- {
- appMsg.led_flag = 0;
- usart_write_buffer_wait(&usart_instance, "LED Off!!\r\n", sizeof("LED Off!!\r\n"));
- }
-
- #if APP_COORDINATOR
- appUartSendMessage((uint8_t *)&appMsg, sizeof(appMsg));
- SYS_TimerStart(&appDataSendingTimer);
- appState = APP_STATE_WAIT_SEND_TIMER;
- #else
- appNwkDataReq.dstAddr = 0;
- appNwkDataReq.dstEndpoint = APP_ENDPOINT;
- appNwkDataReq.srcEndpoint = APP_ENDPOINT;
- appNwkDataReq.options = NWK_OPT_ACK_REQUEST | NWK_OPT_ENABLE_SECURITY;
- appNwkDataReq.data = (uint8_t *)&appMsg;
- appNwkDataReq.size = sizeof(appMsg);
- appNwkDataReq.confirm = appDataConf;
- #if (LED_COUNT > 0)
- LED_On(LED_DATA);
- #endif
- NWK_DataReq(&appNwkDataReq);
- appState = APP_STATE_WAIT_CONF;
- #endif
- }
复制代码同样新增一段if else代码,来根据sw的按下与否,置位led_flag
4、主函数
demo主要运行的代码在wsndemo_main里面
这里对按键进行了初始化
- int wsndemo_main(void)
- {
- struct port_config Port_con;
- port_get_config_defaults(&Port_con);
- Port_con.direction = SYSTEM_PINMUX_PIN_DIR_INPUT;
- Port_con.input_pull = SYSTEM_PINMUX_PIN_PULL_UP;
- port_pin_set_config(PIN_PA28,&Port_con);
- configure_usart();
- SYS_Init();
-
- #if APP_ENDDEVICE
- sm_init();
- #endif
- #if APP_COORDINATOR
- sio2host_init();
- #endif
- cpu_irq_enable();
- while (1) {
- SYS_TaskHandler();
- APP_TaskHandler();
- }
- }
复制代码并定义 #define Key_Detect port_pin_get_input_level(PIN_PA28)
LED其实在这个demo中已经初始化过
只需调用对应LED_On或者LED_Off函数即可
封装好的API就是好用啊
5、显示结果
抛开除电源以外所有的线缆,直接用锂电池供电
直接上GIF了
嗯,先到这
再慢慢试试其他demo