Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of XBeeZB_power_mngmnt_cyclic_sleep by
main.cpp@4:378daca584ef, 2015-06-01 (annotated)
- Committer:
- spastor
- Date:
- Mon Jun 01 19:02:01 2015 +0200
- Revision:
- 4:378daca584ef
- Parent:
- 2:cd92b9bcb7f1
- Child:
- 7:0b34ce30516a
Automatic upload
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| hbujanda | 2:cd92b9bcb7f1 | 1 | /** |
| hbujanda | 2:cd92b9bcb7f1 | 2 | * Copyright (c) 2015 Digi International Inc., |
| hbujanda | 2:cd92b9bcb7f1 | 3 | * All rights not expressly granted are reserved. |
| hbujanda | 2:cd92b9bcb7f1 | 4 | * |
| hbujanda | 2:cd92b9bcb7f1 | 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| hbujanda | 2:cd92b9bcb7f1 | 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| hbujanda | 2:cd92b9bcb7f1 | 7 | * You can obtain one at http://mozilla.org/MPL/2.0/. |
| hbujanda | 2:cd92b9bcb7f1 | 8 | * |
| hbujanda | 2:cd92b9bcb7f1 | 9 | * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343 |
| hbujanda | 2:cd92b9bcb7f1 | 10 | * ======================================================================= |
| hbujanda | 2:cd92b9bcb7f1 | 11 | */ |
| hbujanda | 2:cd92b9bcb7f1 | 12 | |
| hbujanda | 2:cd92b9bcb7f1 | 13 | #include "mbed.h" |
| hbujanda | 2:cd92b9bcb7f1 | 14 | #include "XBeeLib.h" |
| hbujanda | 2:cd92b9bcb7f1 | 15 | #if defined(ENABLE_LOGGING) |
| hbujanda | 2:cd92b9bcb7f1 | 16 | #include "DigiLoggerMbedSerial.h" |
| hbujanda | 2:cd92b9bcb7f1 | 17 | using namespace DigiLog; |
| hbujanda | 2:cd92b9bcb7f1 | 18 | #endif |
| hbujanda | 2:cd92b9bcb7f1 | 19 | |
| hbujanda | 2:cd92b9bcb7f1 | 20 | using namespace XBeeLib; |
| hbujanda | 2:cd92b9bcb7f1 | 21 | |
| hbujanda | 2:cd92b9bcb7f1 | 22 | /* Configure Sleep Options */ |
| hbujanda | 2:cd92b9bcb7f1 | 23 | #define SLEEP_OPTIONS 0 /* Short Sleep */ |
| hbujanda | 2:cd92b9bcb7f1 | 24 | |
| hbujanda | 2:cd92b9bcb7f1 | 25 | /* Configure Sleep Period in mS */ |
| hbujanda | 2:cd92b9bcb7f1 | 26 | #define SLEEP_PERIOD_MS (5000 / 10) /* 5000 mS */ |
| hbujanda | 2:cd92b9bcb7f1 | 27 | |
| hbujanda | 2:cd92b9bcb7f1 | 28 | /* Configure Time before sleep */ |
| hbujanda | 2:cd92b9bcb7f1 | 29 | #define TIME_BEFORE_SLEEP_MS 500 /* 500 mS */ |
| hbujanda | 2:cd92b9bcb7f1 | 30 | |
| hbujanda | 2:cd92b9bcb7f1 | 31 | /* Configure Number of sleep periods */ |
| hbujanda | 2:cd92b9bcb7f1 | 32 | #define SLEEP_PERIOD_NUMBER 65535 /* High value so radio doesn't awake unnecessarily */ |
| hbujanda | 2:cd92b9bcb7f1 | 33 | |
| hbujanda | 2:cd92b9bcb7f1 | 34 | |
| hbujanda | 2:cd92b9bcb7f1 | 35 | Serial *log_serial; |
| hbujanda | 2:cd92b9bcb7f1 | 36 | |
| hbujanda | 2:cd92b9bcb7f1 | 37 | /** Callback function, invoked at packet reception */ |
| hbujanda | 2:cd92b9bcb7f1 | 38 | static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len) |
| hbujanda | 2:cd92b9bcb7f1 | 39 | { |
| hbujanda | 2:cd92b9bcb7f1 | 40 | const uint64_t remote_addr64 = remote.get_addr64(); |
| hbujanda | 2:cd92b9bcb7f1 | 41 | |
| spastor | 4:378daca584ef | 42 | log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", |
| hbujanda | 2:cd92b9bcb7f1 | 43 | UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len); |
| hbujanda | 2:cd92b9bcb7f1 | 44 | |
| hbujanda | 2:cd92b9bcb7f1 | 45 | log_serial->printf("%.*s\r\n\r\n", len, data); |
| hbujanda | 2:cd92b9bcb7f1 | 46 | } |
| hbujanda | 2:cd92b9bcb7f1 | 47 | |
| spastor | 4:378daca584ef | 48 | int main() |
| hbujanda | 2:cd92b9bcb7f1 | 49 | { |
| hbujanda | 2:cd92b9bcb7f1 | 50 | log_serial = new Serial(DEBUG_TX, DEBUG_RX); |
| hbujanda | 2:cd92b9bcb7f1 | 51 | log_serial->baud(9600); |
| hbujanda | 2:cd92b9bcb7f1 | 52 | log_serial->printf("Sample application to demo cyclic sleep power management with the XBeeZB\r\n\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 53 | log_serial->printf(XB_LIB_BANNER); |
| hbujanda | 2:cd92b9bcb7f1 | 54 | |
| hbujanda | 2:cd92b9bcb7f1 | 55 | #if defined(ENABLE_LOGGING) |
| hbujanda | 2:cd92b9bcb7f1 | 56 | new DigiLoggerMbedSerial(log_serial, LogLevelInfo); |
| hbujanda | 2:cd92b9bcb7f1 | 57 | #endif |
| hbujanda | 2:cd92b9bcb7f1 | 58 | |
| hbujanda | 2:cd92b9bcb7f1 | 59 | XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); |
| hbujanda | 2:cd92b9bcb7f1 | 60 | |
| hbujanda | 2:cd92b9bcb7f1 | 61 | /* Register callbacks */ |
| hbujanda | 2:cd92b9bcb7f1 | 62 | xbee.register_receive_cb(&receive_cb); |
| hbujanda | 2:cd92b9bcb7f1 | 63 | |
| hbujanda | 2:cd92b9bcb7f1 | 64 | RadioStatus const radioStatus = xbee.init(); |
| hbujanda | 2:cd92b9bcb7f1 | 65 | MBED_ASSERT(radioStatus == Success); |
| hbujanda | 2:cd92b9bcb7f1 | 66 | |
| hbujanda | 2:cd92b9bcb7f1 | 67 | /* Configure sleep parameters */ |
| hbujanda | 2:cd92b9bcb7f1 | 68 | AtCmdFrame::AtCmdResp cmdresp; |
| hbujanda | 2:cd92b9bcb7f1 | 69 | |
| hbujanda | 2:cd92b9bcb7f1 | 70 | cmdresp = xbee.set_param("SO", (uint32_t)SLEEP_OPTIONS); |
| hbujanda | 2:cd92b9bcb7f1 | 71 | if (cmdresp != AtCmdFrame::AtCmdRespOk) { |
| hbujanda | 2:cd92b9bcb7f1 | 72 | log_serial->printf("SO Failed!!\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 73 | } |
| hbujanda | 2:cd92b9bcb7f1 | 74 | |
| hbujanda | 2:cd92b9bcb7f1 | 75 | cmdresp = xbee.set_param("SP", SLEEP_PERIOD_MS); |
| hbujanda | 2:cd92b9bcb7f1 | 76 | if (cmdresp != AtCmdFrame::AtCmdRespOk) { |
| hbujanda | 2:cd92b9bcb7f1 | 77 | log_serial->printf("SP Failed!!\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 78 | } |
| hbujanda | 2:cd92b9bcb7f1 | 79 | |
| hbujanda | 2:cd92b9bcb7f1 | 80 | cmdresp = xbee.set_param("ST", TIME_BEFORE_SLEEP_MS); |
| hbujanda | 2:cd92b9bcb7f1 | 81 | if (cmdresp != AtCmdFrame::AtCmdRespOk) { |
| hbujanda | 2:cd92b9bcb7f1 | 82 | log_serial->printf("ST Failed!!\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 83 | } |
| hbujanda | 2:cd92b9bcb7f1 | 84 | |
| hbujanda | 2:cd92b9bcb7f1 | 85 | cmdresp = xbee.set_param("SN", SLEEP_PERIOD_NUMBER); |
| hbujanda | 2:cd92b9bcb7f1 | 86 | if (cmdresp != AtCmdFrame::AtCmdRespOk) { |
| hbujanda | 2:cd92b9bcb7f1 | 87 | log_serial->printf("SN Failed!!\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 88 | } |
| hbujanda | 2:cd92b9bcb7f1 | 89 | |
| hbujanda | 2:cd92b9bcb7f1 | 90 | /* Configure Sleep mode */ |
| hbujanda | 2:cd92b9bcb7f1 | 91 | cmdresp = xbee.set_param("SM", 4); /* Cyclic Sleep */ |
| hbujanda | 2:cd92b9bcb7f1 | 92 | if (cmdresp != AtCmdFrame::AtCmdRespOk) { |
| hbujanda | 2:cd92b9bcb7f1 | 93 | log_serial->printf("SM Failed!!\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 94 | } |
| hbujanda | 2:cd92b9bcb7f1 | 95 | |
| hbujanda | 2:cd92b9bcb7f1 | 96 | /* Wait until the device has joined the network */ |
| hbujanda | 2:cd92b9bcb7f1 | 97 | log_serial->printf("Waiting for device to join the network: "); |
| hbujanda | 2:cd92b9bcb7f1 | 98 | while (!xbee.is_joined()) { |
| hbujanda | 2:cd92b9bcb7f1 | 99 | wait_ms(1000); |
| hbujanda | 2:cd92b9bcb7f1 | 100 | log_serial->printf("."); |
| hbujanda | 2:cd92b9bcb7f1 | 101 | } |
| hbujanda | 2:cd92b9bcb7f1 | 102 | log_serial->printf("OK\r\n"); |
| hbujanda | 2:cd92b9bcb7f1 | 103 | |
| hbujanda | 2:cd92b9bcb7f1 | 104 | /* Start processing frames */ |
| hbujanda | 2:cd92b9bcb7f1 | 105 | while (true) { |
| hbujanda | 2:cd92b9bcb7f1 | 106 | xbee.process_rx_frames(); |
| hbujanda | 2:cd92b9bcb7f1 | 107 | wait_ms(100); |
| hbujanda | 2:cd92b9bcb7f1 | 108 | log_serial->printf("."); |
| hbujanda | 2:cd92b9bcb7f1 | 109 | } |
| hbujanda | 2:cd92b9bcb7f1 | 110 | |
| hbujanda | 2:cd92b9bcb7f1 | 111 | delete(log_serial); |
| hbujanda | 2:cd92b9bcb7f1 | 112 | } |
