XBee API mode library

Revision:
17:2f728fd13bc0
Parent:
16:cdfcb63b2c4b
--- a/XBee.cpp	Wed Mar 20 00:28:27 2013 +0000
+++ b/XBee.cpp	Thu Mar 21 06:51:31 2013 +0000
@@ -22,44 +22,55 @@
 
 #include "XBee.h"
 
-#define LOCK()          NVIC_DisableIRQ(UARTx_IRQn[_serial.index])
-#define UNLOCK()        NVIC_EnableIRQ(UARTx_IRQn[_serial.index])
+#define LOCK()      NVIC_DisableIRQ(UARTx_IRQn[_serial.index])
+#define UNLOCK()    NVIC_EnableIRQ(UARTx_IRQn[_serial.index])
+#define INDEX(n)    ((n) % BUFSIZE)
+#define SIZE(b, i)  (b[i] << 8 | b[INDEX(i + 1)])
+#define min(x, y)   ((x) <= (y) ? (x) : (y))
+
 const IRQn_Type UARTx_IRQn[] = {UART0_IRQn, UART1_IRQn, UART2_IRQn, UART3_IRQn};
 
-XBee::XBee(Serial& ser, int apiMode)
-    : Serial(ser), mon(USBTX, USBRX), apiMode(apiMode),
+XBee::XBee(Serial& ser, int api)
+    : Serial(ser), mon(USBTX, USBRX), sem(0), api(api),
       in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
     attach(this, &XBee::rxISR, RxIrq);
+    timer.start();
 }
 
-XBee::XBee(PinName tx, PinName rx, int apiMode)
-    : Serial(tx, rx), mon(USBTX, USBRX), apiMode(apiMode),
+XBee::XBee(PinName tx, PinName rx, int api)
+    : Serial(tx, rx), mon(USBTX, USBRX), sem(0), api(api),
       in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
     attach(this, &XBee::rxISR, RxIrq);
+    timer.start();
 }
 
-XBee::XBee(Serial& ser, Serial& mon, int apiMode)
-    : Serial(ser), mon(mon), apiMode(apiMode),
+XBee::XBee(Serial& ser, Serial& mon, int api)
+    : Serial(ser), mon(mon), sem(0), api(api),
       in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
     attach(this, &XBee::rxISR, RxIrq);
+    timer.start();
 }
 
-XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode)
-    : Serial(tx, rx), mon(mon), apiMode(apiMode),
+XBee::XBee(PinName tx, PinName rx, Serial& mon, int api)
+    : Serial(tx, rx), mon(mon), sem(0), api(api),
       in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
     attach(this, &XBee::rxISR, RxIrq);
+    timer.start();
 }
 
 bool XBee::init(float timeout) {
-    while (readable()) getc();
-    while (timeout > 0 && getFirmwareVersion() == -1) {
-        timeout -= 1;
+    Timer timer2;
+    timer2.start();
+    
+    while (readable())
+        getc();
+        
+    while (timer2.read() < timeout && getFirmwareVersion() == -1)
         wait(0.8);
-    }
 
     switch (getFirmwareVersion() & 0xFF00) {
         case 0x2100:
@@ -200,8 +211,4 @@
     memcpy(toBuf, &buf[fromIndex], length1);
     if (length1 < length)
         memcpy(toBuf + length1, &buf[0], length - length1);
-}
-
-void XBee::setDebug(bool debug) {
-    this->debug = debug;
 }
\ No newline at end of file