123

Dependencies:   mbed XBeeLib_Fixed

Committer:
spastor
Date:
Fri May 08 11:52:54 2015 +0200
Revision:
2:994e979d9df8
Parent:
0:0d348bfc487e
Automatic upload

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hbujanda 0:0d348bfc487e 1 /**
hbujanda 0:0d348bfc487e 2 * Copyright (c) 2015 Digi International Inc.,
hbujanda 0:0d348bfc487e 3 * All rights not expressly granted are reserved.
hbujanda 0:0d348bfc487e 4 *
hbujanda 0:0d348bfc487e 5 * This Source Code Form is subject to the terms of the Mozilla Public
hbujanda 0:0d348bfc487e 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
hbujanda 0:0d348bfc487e 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
hbujanda 0:0d348bfc487e 8 *
hbujanda 0:0d348bfc487e 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
hbujanda 0:0d348bfc487e 10 * =======================================================================
hbujanda 0:0d348bfc487e 11 */
hbujanda 0:0d348bfc487e 12
hbujanda 0:0d348bfc487e 13 #include "mbed.h"
hbujanda 0:0d348bfc487e 14 #include "XBeeLib.h"
hbujanda 0:0d348bfc487e 15 #if defined(ENABLE_LOGGING)
hbujanda 0:0d348bfc487e 16 #include "DigiLoggerMbedSerial.h"
hbujanda 0:0d348bfc487e 17 using namespace DigiLog;
hbujanda 0:0d348bfc487e 18 #endif
hbujanda 0:0d348bfc487e 19
hbujanda 0:0d348bfc487e 20 using namespace XBeeLib;
hbujanda 0:0d348bfc487e 21
hbujanda 0:0d348bfc487e 22 Serial *log_serial;
hbujanda 0:0d348bfc487e 23
hbujanda 0:0d348bfc487e 24 /** Callback function, invoked at packet reception */
hbujanda 0:0d348bfc487e 25 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
hbujanda 0:0d348bfc487e 26 {
spastor 2:994e979d9df8 27 const uint64_t remote_addr64 = remote.get_addr64();
hbujanda 0:0d348bfc487e 28
spastor 2:994e979d9df8 29 log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
hbujanda 0:0d348bfc487e 30
hbujanda 0:0d348bfc487e 31 for (int i = 0; i < len; i++)
hbujanda 0:0d348bfc487e 32 log_serial->printf("%02x ", data[i]);
hbujanda 0:0d348bfc487e 33
hbujanda 0:0d348bfc487e 34 log_serial->printf("\r\n");
hbujanda 0:0d348bfc487e 35 }
hbujanda 0:0d348bfc487e 36
hbujanda 0:0d348bfc487e 37 int main()
hbujanda 0:0d348bfc487e 38 {
hbujanda 0:0d348bfc487e 39 log_serial = new Serial(DEBUG_TX, DEBUG_RX);
hbujanda 0:0d348bfc487e 40 log_serial->baud(9600);
hbujanda 0:0d348bfc487e 41 log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
hbujanda 0:0d348bfc487e 42 log_serial->printf(XB_LIB_BANNER);
hbujanda 0:0d348bfc487e 43
hbujanda 0:0d348bfc487e 44 #if defined(ENABLE_LOGGING)
hbujanda 0:0d348bfc487e 45 new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
hbujanda 0:0d348bfc487e 46 #endif
hbujanda 0:0d348bfc487e 47
hbujanda 0:0d348bfc487e 48 XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
hbujanda 0:0d348bfc487e 49
hbujanda 0:0d348bfc487e 50 /* Register callbacks */
hbujanda 0:0d348bfc487e 51 xbee.register_receive_cb(&receive_cb);
hbujanda 0:0d348bfc487e 52
hbujanda 0:0d348bfc487e 53 RadioStatus const radioStatus = xbee.init();
hbujanda 0:0d348bfc487e 54 MBED_ASSERT(radioStatus == Success);
hbujanda 0:0d348bfc487e 55
hbujanda 0:0d348bfc487e 56 /* Wait until the device has joined the network */
hbujanda 0:0d348bfc487e 57 log_serial->printf("Waiting for device to join the network: ");
hbujanda 0:0d348bfc487e 58 while (!xbee.is_joined()) {
hbujanda 0:0d348bfc487e 59 wait_ms(1000);
hbujanda 0:0d348bfc487e 60 log_serial->printf(".");
hbujanda 0:0d348bfc487e 61 }
hbujanda 0:0d348bfc487e 62 log_serial->printf("OK\r\n");
hbujanda 0:0d348bfc487e 63
hbujanda 0:0d348bfc487e 64 while (true) {
hbujanda 0:0d348bfc487e 65 xbee.process_rx_frames();
hbujanda 0:0d348bfc487e 66 wait_ms(100);
hbujanda 0:0d348bfc487e 67 log_serial->printf(".");
hbujanda 0:0d348bfc487e 68 }
hbujanda 0:0d348bfc487e 69
hbujanda 0:0d348bfc487e 70 delete(log_serial);
hbujanda 0:0d348bfc487e 71 }