XBee API mode library
Diff: Receive.cpp
- Revision:
- 14:af6e497bbf52
- Parent:
- 10:3da24a020e67
- Child:
- 16:cdfcb63b2c4b
diff -r 5f102be205f8 -r af6e497bbf52 Receive.cpp --- a/Receive.cpp Thu Mar 14 08:02:24 2013 +0000 +++ b/Receive.cpp Thu Mar 14 09:38:54 2013 +0000 @@ -31,33 +31,27 @@ #define UNLOCK() mutex.unlock(); #endif -XBee::FrameType XBee::receive(float timeout) -{ +XBee::FrameType XBee::receive(float timeout) { flush(); while (true) { LOCK(); - //::printf(">receive()\n"); if (out != in || free == 0) { FrameType type = getFrameType(buf[INDEX(out + 2)]); if (type != None) { received = out; - //::printf("<receive()1\n"); UNLOCK(); return type; } int size = SIZE(buf, out); out = INDEX(out + 2 + size); free += (2 + size); - //::printf("<receive()2\n"); UNLOCK(); continue; } else if (timeout <= 0) { - //::printf("<receive()3\n"); UNLOCK(); return None; } - //::printf("<receive()4\n"); UNLOCK(); #ifndef XBEE_RTOS @@ -100,8 +94,7 @@ return -1; } -XBee::FrameType XBee::getFrameType(char c) -{ +XBee::FrameType XBee::getFrameType(char c) { switch (c) { case 0x00: return None; @@ -128,8 +121,7 @@ } } -void XBee::flush() -{ +void XBee::flush() { LOCK(); if (received == out) { do { @@ -143,9 +135,8 @@ } #ifndef XBEE_RTOS -void XBee::rxInterruptHandler() -{ - static bool escaped = false; +void XBee::rxInterruptHandler() { + static bool escaped = false; if (debug) leds = leds ^ 4; //**LEDS=x@xx @@ -208,8 +199,7 @@ } } #else -void XBee::rxISR() -{ +void XBee::rxISR() { int c = -1; switch (_serial.index) { case 0: { @@ -240,8 +230,7 @@ } } -void XBee::run() -{ +void XBee::run() { bool escaped = false; while (true) { @@ -251,7 +240,6 @@ while (rxBuf.readable()) { char c = rxBuf.getc() & 255; - //::printf("getc = %02X\n", c); if (apiMode == 2) { if (escaped) { c ^= 0x20; @@ -269,7 +257,6 @@ state = LENGTH2; break; case LENGTH2: - //::printf(">length2 in = %04X, out = %04X, free = %d\n", in, out, free); if ((buf[cur] << 8 | c) + 2 < BUFSIZE) { state = DATA; while (free < (buf[cur] << 8 | c) + 2) { @@ -284,7 +271,6 @@ state = UNKNOWN; if (debug) leds = leds & 12; //** update leds } - //::printf("<length2 in = %04X, out = %04X, free = %d\n", in, out, free); break; case DATA: buf[cur] = c; @@ -311,99 +297,4 @@ mutex.unlock(); } } -#endif -#if 0 -void XBee::rxHandler(void const *args) -{ - bool escaped = false; - - XBee& self = *((XBee *) args); - BusOut& leds = self.leds; - int& apiMode = self.apiMode; - volatile int& cur = self.cur; - volatile int& in = self.in; - volatile int& out = self.out; - volatile int& free = self.free; - bool& debug = self.debug; - Mutex& mutex = self.mutex; - - //int prevFree = free; - - while (true) { - mutex.lock(); - //if (prevFree != free) { - //::printf("rx in = %04X, out = %04X, free = %d -> %d\n", in, out, prevFree, free); - //prevFree = free; - //} - if (debug && self.readable()) leds = leds ^ 4; // x@xx - - while (self.readable()) { - //if (prevFree != free) { - //::printf("length2 in = %04X, out = %04X, free = %d -> %d\n", in, out, prevFree, free); - //prevFree = free; - //} - char c = self.getc() & 255; - - if (apiMode == 2) { - if (escaped) { - c ^= 0x20; - escaped = false; - } else if (c == ESCAPE) { - escaped = true; - continue; - } - } - - switch (self.state) { - case LENGTH1: - cur = in; - self.buf[cur] = c; - self.state = LENGTH2; - break; - case LENGTH2: - //::printf(">length2 in = %04X, out = %04X, free = %d\n", in, out, free); - if ((self.buf[cur] << 8 | c) + 2 < BUFSIZE) { - self.state = DATA; - while (free < (self.buf[cur] << 8 | c) + 2) { - int size = SIZE(self.buf, out); - out = INDEX(out + size + 2); - free += (size + 2); - } - self.buf[INDEX(cur + 1)] = c; - cur = INDEX(cur + 2); - if (debug) leds = (leds & 12) | 2; // xx10 - //::printf("<length2 in = %04X, out = %04X, free = %d\n", in, out, free); - } else { - self.state = UNKNOWN; - leds = leds & 12; //** update leds - } - break; - case DATA: - self.buf[cur] = c; - cur = INDEX(cur + 1); - if (debug) leds = (leds & 12) | (cur & 3); //**LEDS=xx@@ - if (cur == INDEX(in + 2 + SIZE(self.buf, in))) self.state = SUMCHECK; - break; - case SUMCHECK: - //::printf(">sumcheck in = %04X, out = %04X, free = %d\n", in, out, free); - for (int i = INDEX(in + 2); i != cur; i = INDEX(i + 1)) { - c += self.buf[i]; - } - if (c == 255) { - if (debug) leds = (leds & 12) | 3; //**LEDS=xx11 - in = cur; - free = in <= out ? out - in : BUFSIZE + out - in; // maybe in == out then free == 0, but != BUFSIZE - } - self.state = UNKNOWN; - //::printf("<sumcheck in = %04X, out = %04X, free = %d\n", in, out, free); - break; - default: - if (c == PREAMBLE) self.state = LENGTH1; - if (debug) leds = (leds & 12) | 1; //**LEDS=xx01 - } - } - mutex.unlock(); - Thread::wait(1); - } -} #endif \ No newline at end of file