Example of using the XBeeApi library to receive a message via a callback method

Dependencies:   XBeeApi mbed

Revision:
0:2c02b2e821f2
Child:
1:d8492e31f655
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Jul 06 21:47:58 2014 +0000
@@ -0,0 +1,102 @@
+/**
+   @file
+   @brief Example of using the XBeeApi library to receive a message
+          via a callback method
+          This example has a minimum of error checking in order
+          to keep the code compact
+
+   @author John Bailey 
+
+   @copyright Copyright 2014 John Bailey
+
+   @section LICENSE
+   
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+
+*/
+
+#include "mbed.h"   
+#include "xbeeapi.hpp"
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+/* TODO: You may need to change these based on the device/connections that you're using */ 
+#define XBEE_TX_PIN PTA2
+#define XBEE_RX_PIN PTA1
+
+/* ID for the Personal Area Network we're going to join */
+const XBeeApiCmdAt::panId_t myPANId = 1000;
+
+/* Network channel to use */
+const XBeeApiCmdAt::channel_t myChannelId = 14;
+
+XBeeDevice xbeeDevice( XBEE_TX_PIN, XBEE_RX_PIN, NC, NC );
+
+class FrameDumper : public XBeeApiRxFrameDecoder
+{
+    public:
+    FrameDumper( XBeeDevice* p_device ) : XBeeApiRxFrameDecoder( p_device )
+    {
+    }
+
+    virtual void frameRxCallback( XBeeApiRxFrame* const p_frame )
+    {
+        const uint8_t* outData;
+        uint16_t outLoop;
+        pc.printf(" API ID: 0x%02X\r\n",p_frame->getApiId() );
+        p_frame->getDataPtr( 0, &outData, &outLoop );
+        pc.printf("Data [%d]: ",outLoop);
+        for( ;
+            outLoop > 0;
+            outLoop--,outData++ )
+        {
+            pc.printf("0x%02X ",*outData);
+        }
+        pc.printf("\r\n");
+    }
+};
+
+int main() {
+    /* This example will use the blocking API for simplicity */   
+    XBeeApiCmdAtBlocking atIf( &xbeeDevice );
+
+    XBeeDevice::XBeeDeviceReturn_t status;
+
+    /* This is the object that will be receiving the RX call-backs */
+    FrameDumper decoder( &xbeeDevice );
+
+    /* Get API mode 2 set up - this is a pre-requisit to using other XBeeApi functions.
+       This step may not be needed in the case that the XBee has already been configured
+       to use Mode 2 and the setting has been stored in NV */
+    status = xbeeDevice.setUpApi();
+
+    if( status == XBeeDevice::XBEEDEVICE_OK )
+    {
+        /* Set up a peer-to-peer network using the specified PAN and channel */
+        if( xbeeSetNetworkTypeP2P( &atIf, myPANId, myChannelId ) )
+        {
+            while( 1 )
+            {
+                /* Wait while frames are received */
+            }
+        }
+        else
+        {
+            pc.printf("xbeeSetNetworkTypeP2P failed\r\n");
+        }
+    }
+    else
+    {
+        pc.printf("setUpApi failed with status %d\r\n",status);
+    }
+}
\ No newline at end of file