XBee API mode library

Revision:
16:cdfcb63b2c4b
Parent:
14:af6e497bbf52
Child:
17:2f728fd13bc0
--- a/XBee.cpp	Thu Mar 14 09:45:41 2013 +0000
+++ b/XBee.cpp	Wed Mar 20 00:28:27 2013 +0000
@@ -22,67 +22,37 @@
 
 #include "XBee.h"
 
-#ifndef XBEE_RTOS
-XBee::XBee(Serial& ser, int apiMode, bool debug)
-    : Serial(ser), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
-      state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) {
-    memset(buf, 0, BUFSIZE);
-    attach(this, &XBee::rxInterruptHandler, 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), sendConfirmation(0), debug(debug) {
-    memset(buf, 0, BUFSIZE);
-    attach(this, &XBee::rxInterruptHandler, RxIrq);
-}
+#define LOCK()          NVIC_DisableIRQ(UARTx_IRQn[_serial.index])
+#define UNLOCK()        NVIC_EnableIRQ(UARTx_IRQn[_serial.index])
+const IRQn_Type UARTx_IRQn[] = {UART0_IRQn, UART1_IRQn, UART2_IRQn, UART3_IRQn};
 
-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), sendConfirmation(0), debug(debug) {
+XBee::XBee(Serial& ser, int apiMode)
+    : Serial(ser), mon(USBTX, USBRX), apiMode(apiMode),
+      in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
-    attach(this, &XBee::rxInterruptHandler, 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), sendConfirmation(0), debug(debug) {
-    memset(buf, 0, BUFSIZE);
-    attach(this, &XBee::rxInterruptHandler, RxIrq);
-}
-#else
-XBee::XBee(Serial& ser, int apiMode, bool debug)
-    : Serial(ser), AbstractThread(osPriorityAboveNormal), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
-      state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) {
-    memset(buf, 0, BUFSIZE);
-    start();
     attach(this, &XBee::rxISR, RxIrq);
 }
 
-XBee::XBee(PinName tx, PinName rx, int apiMode, bool debug)
-    : Serial(tx, rx), AbstractThread(osPriorityAboveNormal), mon(USBTX, USBRX), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
-      state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) {
+XBee::XBee(PinName tx, PinName rx, int apiMode)
+    : Serial(tx, rx), mon(USBTX, USBRX), apiMode(apiMode),
+      in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
-    start();
     attach(this, &XBee::rxISR, RxIrq);
 }
 
-XBee::XBee(Serial& ser, Serial& mon, int apiMode, bool debug)
-    : Serial(ser), AbstractThread(osPriorityAboveNormal), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
-      state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) {
+XBee::XBee(Serial& ser, Serial& mon, int apiMode)
+    : Serial(ser), mon(mon), apiMode(apiMode),
+      in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
-    start();
     attach(this, &XBee::rxISR, RxIrq);
 }
 
-XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode, bool debug)
-    : Serial(tx, rx), AbstractThread(osPriorityAboveNormal), mon(mon), leds(LED1, LED2, LED3, LED4), apiMode(apiMode),
-      state(UNKNOWN), in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0), debug(debug) {
+XBee::XBee(PinName tx, PinName rx, Serial& mon, int apiMode)
+    : Serial(tx, rx), mon(mon), apiMode(apiMode),
+      in(0), out(0), received(-1), free(BUFSIZE), frame_id(1), sendConfirmation(0) {
     memset(buf, 0, BUFSIZE);
-    start();
     attach(this, &XBee::rxISR, RxIrq);
 }
-#endif
 
 bool XBee::init(float timeout) {
     while (readable()) getc();
@@ -202,7 +172,14 @@
                 }
             }
         }
-        buf[INDEX(index + 2)] = 0x00;
+        buf[INDEX(index + 2)] = None;
+        if (index == out) {
+            LOCK();
+            int size = SIZE(buf, out);
+            out = INDEX(out + 2 + size);
+            free += (2 + size);
+            UNLOCK();
+        }
     }
 
     return succeeded ? data : 0;