Example of using the XBeeApi library to receive a message via a callback method. This example shows how to use the library when using mbed-rtos. See the comment at the top of main.cpp
Dependencies: XBeeApi mbed-rtos mbed
main.cpp@0:3714a5cebfdb, 2014-07-08 (annotated)
- Committer:
- johnb
- Date:
- Tue Jul 08 20:20:41 2014 +0000
- Revision:
- 0:3714a5cebfdb
Example of using the XBeeApi library to receive a message via a callback method. This example shows how to use the library when using mbed-rtos. See the comment at the top of main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
johnb | 0:3714a5cebfdb | 1 | /** |
johnb | 0:3714a5cebfdb | 2 | @file |
johnb | 0:3714a5cebfdb | 3 | @brief Example of using the XBeeApi library to receive a message |
johnb | 0:3714a5cebfdb | 4 | via a callback method |
johnb | 0:3714a5cebfdb | 5 | This example has a minimum of error checking in order |
johnb | 0:3714a5cebfdb | 6 | to keep the code compact |
johnb | 0:3714a5cebfdb | 7 | |
johnb | 0:3714a5cebfdb | 8 | This example shows how to use the library when using mbed-rtos. |
johnb | 0:3714a5cebfdb | 9 | Before compiling you must open "XbeeApi\Config\XBeeApiCfg.hpp" |
johnb | 0:3714a5cebfdb | 10 | and change the '#if 0' to '#if 1' on the line above the comment |
johnb | 0:3714a5cebfdb | 11 | reading "Use RTOS features to make XBeeApi threadsafe" |
johnb | 0:3714a5cebfdb | 12 | |
johnb | 0:3714a5cebfdb | 13 | @author John Bailey |
johnb | 0:3714a5cebfdb | 14 | |
johnb | 0:3714a5cebfdb | 15 | @copyright Copyright 2014 John Bailey |
johnb | 0:3714a5cebfdb | 16 | |
johnb | 0:3714a5cebfdb | 17 | @section LICENSE |
johnb | 0:3714a5cebfdb | 18 | |
johnb | 0:3714a5cebfdb | 19 | Licensed under the Apache License, Version 2.0 (the "License"); |
johnb | 0:3714a5cebfdb | 20 | you may not use this file except in compliance with the License. |
johnb | 0:3714a5cebfdb | 21 | You may obtain a copy of the License at |
johnb | 0:3714a5cebfdb | 22 | |
johnb | 0:3714a5cebfdb | 23 | http://www.apache.org/licenses/LICENSE-2.0 |
johnb | 0:3714a5cebfdb | 24 | |
johnb | 0:3714a5cebfdb | 25 | Unless required by applicable law or agreed to in writing, software |
johnb | 0:3714a5cebfdb | 26 | distributed under the License is distributed on an "AS IS" BASIS, |
johnb | 0:3714a5cebfdb | 27 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
johnb | 0:3714a5cebfdb | 28 | See the License for the specific language governing permissions and |
johnb | 0:3714a5cebfdb | 29 | limitations under the License. |
johnb | 0:3714a5cebfdb | 30 | |
johnb | 0:3714a5cebfdb | 31 | */ |
johnb | 0:3714a5cebfdb | 32 | |
johnb | 0:3714a5cebfdb | 33 | #include "mbed.h" |
johnb | 0:3714a5cebfdb | 34 | #include "xbeeapi.hpp" |
johnb | 0:3714a5cebfdb | 35 | |
johnb | 0:3714a5cebfdb | 36 | Serial pc(USBTX, USBRX); // tx, rx |
johnb | 0:3714a5cebfdb | 37 | |
johnb | 0:3714a5cebfdb | 38 | /* TODO: You may need to change these based on the device/connections that you're using */ |
johnb | 0:3714a5cebfdb | 39 | #define XBEE_TX_PIN PTA2 |
johnb | 0:3714a5cebfdb | 40 | #define XBEE_RX_PIN PTA1 |
johnb | 0:3714a5cebfdb | 41 | |
johnb | 0:3714a5cebfdb | 42 | /* Network address for our XBee */ |
johnb | 0:3714a5cebfdb | 43 | const uint16_t myNetworkAddress = 0x1234; |
johnb | 0:3714a5cebfdb | 44 | |
johnb | 0:3714a5cebfdb | 45 | /* ID for the Personal Area Network we're going to join */ |
johnb | 0:3714a5cebfdb | 46 | const XBeeApiCmdAt::panId_t myPANId = 1000; |
johnb | 0:3714a5cebfdb | 47 | |
johnb | 0:3714a5cebfdb | 48 | /* Network channel to use */ |
johnb | 0:3714a5cebfdb | 49 | const XBeeApiCmdAt::channel_t myChannelId = 14; |
johnb | 0:3714a5cebfdb | 50 | |
johnb | 0:3714a5cebfdb | 51 | XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC ); |
johnb | 0:3714a5cebfdb | 52 | |
johnb | 0:3714a5cebfdb | 53 | class FrameDumper : public XBeeApiRxFrameDecoder |
johnb | 0:3714a5cebfdb | 54 | { |
johnb | 0:3714a5cebfdb | 55 | public: |
johnb | 0:3714a5cebfdb | 56 | FrameDumper( XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device ) |
johnb | 0:3714a5cebfdb | 57 | { |
johnb | 0:3714a5cebfdb | 58 | } |
johnb | 0:3714a5cebfdb | 59 | |
johnb | 0:3714a5cebfdb | 60 | virtual void frameRxCallback( XBeeApiRxFrame* const p_frame ) |
johnb | 0:3714a5cebfdb | 61 | { |
johnb | 0:3714a5cebfdb | 62 | const uint8_t* outData; |
johnb | 0:3714a5cebfdb | 63 | uint16_t outLoop; |
johnb | 0:3714a5cebfdb | 64 | pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() ); |
johnb | 0:3714a5cebfdb | 65 | p_frame->getDataPtr( 0, &outData, &outLoop ); |
johnb | 0:3714a5cebfdb | 66 | pc.printf("Data [%d]: ",outLoop); |
johnb | 0:3714a5cebfdb | 67 | for( ; |
johnb | 0:3714a5cebfdb | 68 | outLoop > 0; |
johnb | 0:3714a5cebfdb | 69 | outLoop--,outData++ ) |
johnb | 0:3714a5cebfdb | 70 | { |
johnb | 0:3714a5cebfdb | 71 | pc.printf("0x%02X ",*outData); |
johnb | 0:3714a5cebfdb | 72 | } |
johnb | 0:3714a5cebfdb | 73 | pc.printf("\r\n"); |
johnb | 0:3714a5cebfdb | 74 | } |
johnb | 0:3714a5cebfdb | 75 | }; |
johnb | 0:3714a5cebfdb | 76 | |
johnb | 0:3714a5cebfdb | 77 | int main() { |
johnb | 0:3714a5cebfdb | 78 | /* This example will use the blocking API for simplicity */ |
johnb | 0:3714a5cebfdb | 79 | XBeeApiCmdAtBlocking atIf( &xbeeDevice ); |
johnb | 0:3714a5cebfdb | 80 | |
johnb | 0:3714a5cebfdb | 81 | XBeeDevice::XBeeDeviceReturn_t status; |
johnb | 0:3714a5cebfdb | 82 | |
johnb | 0:3714a5cebfdb | 83 | /* This is the object that will be receiving the RX call-backs */ |
johnb | 0:3714a5cebfdb | 84 | FrameDumper decoder( &xbeeDevice ); |
johnb | 0:3714a5cebfdb | 85 | |
johnb | 0:3714a5cebfdb | 86 | if( !xbeeDevice.setupDispatchTask() ) |
johnb | 0:3714a5cebfdb | 87 | { |
johnb | 0:3714a5cebfdb | 88 | pc.printf("setupDispatchTask() failed - XBeeDevice class won't receive any data\r\n"); |
johnb | 0:3714a5cebfdb | 89 | } |
johnb | 0:3714a5cebfdb | 90 | |
johnb | 0:3714a5cebfdb | 91 | /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions. |
johnb | 0:3714a5cebfdb | 92 | This step may not be needed in the case that the XBee has already been configured |
johnb | 0:3714a5cebfdb | 93 | to use Mode 2 and the setting has been stored in NV */ |
johnb | 0:3714a5cebfdb | 94 | status = xbeeDevice.setUpApi(); |
johnb | 0:3714a5cebfdb | 95 | |
johnb | 0:3714a5cebfdb | 96 | if( status == XBeeDevice::XBEEDEVICE_OK ) |
johnb | 0:3714a5cebfdb | 97 | { |
johnb | 0:3714a5cebfdb | 98 | /* Set the 16-bit source address of this XBee */ |
johnb | 0:3714a5cebfdb | 99 | atIf.setSourceAddress( myNetworkAddress ); |
johnb | 0:3714a5cebfdb | 100 | |
johnb | 0:3714a5cebfdb | 101 | /* Set up a peer-to-peer network using the specified PAN and channel */ |
johnb | 0:3714a5cebfdb | 102 | if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) ) |
johnb | 0:3714a5cebfdb | 103 | { |
johnb | 0:3714a5cebfdb | 104 | while( 1 ) |
johnb | 0:3714a5cebfdb | 105 | { |
johnb | 0:3714a5cebfdb | 106 | /* Wait while frames are received */ |
johnb | 0:3714a5cebfdb | 107 | } |
johnb | 0:3714a5cebfdb | 108 | } |
johnb | 0:3714a5cebfdb | 109 | else |
johnb | 0:3714a5cebfdb | 110 | { |
johnb | 0:3714a5cebfdb | 111 | pc.printf("xbeeSetNetworkTypeP2P failed\r\n"); |
johnb | 0:3714a5cebfdb | 112 | } |
johnb | 0:3714a5cebfdb | 113 | } |
johnb | 0:3714a5cebfdb | 114 | else |
johnb | 0:3714a5cebfdb | 115 | { |
johnb | 0:3714a5cebfdb | 116 | pc.printf("setUpApi failed with status %d\r\n",status); |
johnb | 0:3714a5cebfdb | 117 | } |
johnb | 0:3714a5cebfdb | 118 | } |