John Bailey / Mbed 2 deprecated XBeeApiReceiveCallbackExample

Dependencies:   XBeeApi mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002    @file
00003    @brief Example of using the XBeeApi library to receive a message
00004           via a callback method
00005           This example has a minimum of error checking in order
00006           to keep the code compact
00007 
00008    @author John Bailey 
00009 
00010    @copyright Copyright 2014 John Bailey
00011 
00012    @section LICENSE
00013    
00014 Licensed under the Apache License, Version 2.0 (the "License");
00015 you may not use this file except in compliance with the License.
00016 You may obtain a copy of the License at
00017 
00018     http://www.apache.org/licenses/LICENSE-2.0
00019 
00020 Unless required by applicable law or agreed to in writing, software
00021 distributed under the License is distributed on an "AS IS" BASIS,
00022 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00023 See the License for the specific language governing permissions and
00024 limitations under the License.
00025 
00026 */
00027 
00028 #include "mbed.h"   
00029 #include "xbeeapi.hpp"
00030 
00031 Serial pc(USBTX, USBRX); // tx, rx
00032 
00033 /* TODO: You may need to change these based on the device/connections that you're using */ 
00034 #define XBEE_TX_PIN PTA2
00035 #define XBEE_RX_PIN PTA1
00036 
00037 /* Network address for our XBee */
00038 const uint16_t myNetworkAddress = 0x1234;
00039 
00040 /* ID for the Personal Area Network we're going to join */
00041 const XBeeApiCmdAt::panId_t myPANId = 1000;
00042 
00043 /* Network channel to use */
00044 const XBeeApiCmdAt::channel_t myChannelId = 14;
00045 
00046 XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
00047 
00048 class FrameDumper : public XBeeApiRxFrameDecoder
00049 {
00050     public:
00051     FrameDumper( XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device )
00052     {
00053     }
00054 
00055     virtual void frameRxCallback( XBeeApiRxFrame* const p_frame )
00056     {
00057         const uint8_t* outData;
00058         uint16_t outLoop;
00059         pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() );
00060         p_frame->getDataPtr( 0, &outData, &outLoop );
00061         pc.printf("Data [%d]: ",outLoop);
00062         for( ;
00063             outLoop > 0;
00064             outLoop--,outData++ )
00065         {
00066             pc.printf("0x%02X ",*outData);
00067         }
00068         pc.printf("\r\n");
00069     }
00070 };
00071 
00072 int main() {
00073     /* This example will use the blocking API for simplicity */   
00074     XBeeApiCmdAtBlocking atIf( &xbeeDevice );
00075 
00076     XBeeDevice::XBeeDeviceReturn_t status;
00077 
00078     /* This is the object that will be receiving the RX call-backs */
00079     FrameDumper decoder( &xbeeDevice );
00080 
00081     /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
00082        This step may not be needed in the case that the XBee has already been configured
00083        to use Mode 2 and the setting has been stored in NV */
00084     status = xbeeDevice.setUpApi();
00085 
00086     if( status == XBeeDevice::XBEEDEVICE_OK )
00087     {
00088         /* Set the 16-bit source address of this XBee */
00089         atIf.setSourceAddress( myNetworkAddress );
00090 
00091         /* Set up a peer-to-peer network using the specified PAN and channel */
00092         if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
00093         {
00094             while( 1 )
00095             {
00096                 /* Wait while frames are received */
00097             }
00098         }
00099         else
00100         {
00101             pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
00102         }
00103     }
00104     else
00105     {
00106         pc.printf("setUpApi failed with status %d\r\n",status);
00107     }
00108 }