XBee API mode library 1.0

Revision:
3:48f7780963e2
Parent:
0:ea8459db49ef
--- a/XBee.cpp	Fri Nov 25 05:11:51 2011 +0000
+++ b/XBee.cpp	Thu Apr 12 10:20:17 2012 +0000
@@ -21,7 +21,6 @@
 */
 
 #include "XBee.h"
-#include "macros.h"
 
 XBee::XBee(Serial& ser, int apiMode, bool debug)
         : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
@@ -30,6 +29,13 @@
     attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
 }
 
+XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug)
+        : Serial(tx, rx), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
+        state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
+    memset(buf, 0, BUFSIZE);
+    attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
+}
+
 XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug)
         : Serial(ser), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
         state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
@@ -37,6 +43,13 @@
     attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
 }
 
+XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug)
+        : Serial(tx, rx), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
+        state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), debug(debug) {
+    memset(buf, 0, BUFSIZE);
+    attach(this, apiMode == 1 ? &XBee::rxInterruptHandler : &XBee::rxInterruptHandler2, Serial::RxIrq);
+}
+
 bool XBee::init(float timeout) {
     while (readable()) getc();
     while (timeout > 0 && getFirmwareVersion() == -1) {
@@ -59,22 +72,19 @@
     }
 }
 
-void XBee::setDestination(unsigned long long address64, unsigned short address16) {
-    char addr64[] = {BYTE(address64, 7), BYTE(address64, 6), BYTE(address64, 5), BYTE(address64, 4),
-                     BYTE(address64, 3), BYTE(address64, 2), BYTE(address64, 1), BYTE(address64, 0)
-                    };
-    char addr16[] = {BYTE(address16, 1), BYTE(address16, 0)};
-    setDestination(addr64, addr16);
+void XBee::setDestination(XBeeAddress64 address64, XBeeAddress16 address16) {
+    setDestination(address64.raw_address(), address16.raw_address());
 }
 
-void XBee::setDestination(char address64[]) {
-    char address16[] = {0xFF, 0xFE};
-    setDestination(address64, address16);
+void XBee::setDestination(uint64_t address64, uint16_t address16) {
+    for (int i = 0; i < 8; i++, address64 >>= 8)
+        destination64[7 - i] = address64 & 255;
+    destination16[0] = (address16 >> 8) & 255;
+    destination16[1] = address16 & 255;
 }
 
 void XBee::setDestination(char address64[], char address16[]) {
-    for (int i = 0; i < sizeof(destination64); i++)
-        destination64[i] = address64[i];
+    memcpy(destination64, address64, 8);
     destination16[0] = address16[0];
     destination16[1] = address16[1];
 }
@@ -87,7 +97,53 @@
     return frame_id;
 }
 
-void *XBee::executeCommand(char *command, char* param, int param_length) {
+void *XBee::executeCommand(const char *command, int8_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, int16_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, int32_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, int64_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, uint8_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, uint16_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, uint32_t param) {
+    return executeCommand(command, (uint64_t) param);
+}
+
+void *XBee::executeCommand(const char *command, uint64_t param) {
+    uint8_t param_buf[8] = {
+        (uint8_t) (param >> 56), (uint8_t) (param >> 48),
+        (uint8_t) (param >> 40), (uint8_t) (param >> 32),
+        (uint8_t) (param >> 24), (uint8_t) (param >> 16),
+        (uint8_t) (param >> 8), (uint8_t) param
+    };
+
+    int n = 0;
+    while (n < 7 && param_buf[n] == 0) n++;
+
+    return executeCommand(command, param_buf + n, 8 - n);
+}
+
+void *XBee::executeCommand(const char *command, const char *param) {
+    return executeCommand(command, (uint8_t *) param, strlen(param));
+}
+
+void *XBee::executeCommand(const char *command, const uint8_t *param, int param_length) {
     static char data[21];
     bool succeeded = false;
     char id = getFrameID();
@@ -99,7 +155,7 @@
         if (scan(index, FrameID, &id2) && id == id2 && scan(index, Status, &status) && status == 0) {
             memset(data, 0, sizeof(data));
             if ((command[0] == 'N' && command[1] == 'I') ||
-                (command[0] == 'I' && command[1] == 'S')) {
+                    (command[0] == 'I' && command[1] == 'S')) {
                 if (scan(index, CommandData, data, sizeof(data)))
                     succeeded = true;
             } else {