Marcus Lee / SerialBuffer

Dependencies:   LinearArray

Revision:
0:dd6d74f39384
Child:
1:985b6f1c7347
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/serialBuffer.cpp	Mon Oct 03 12:34:17 2016 +0000
@@ -0,0 +1,35 @@
+#include "serialBuffer.h"
+
+SerialBuffer::SerialBuffer(PinName tx, PinName rx) : RawSerial(tx, rx) {
+    RawSerial::attach(this, &SerialBuffer::rx_interrupt, Serial::RxIrq);
+    return;
+}
+
+SerialBuffer::~SerialBuffer() {
+    RawSerial::attach(NULL, RawSerial::RxIrq);
+    return;
+}
+
+int SerialBuffer::readable() {
+    return rx_buffer.readable();
+}
+char SerialBuffer::getc() {
+    char * output;
+    RawSerial::attach(NULL, RawSerial::RxIrq);
+    
+    CircularBuffer<char>::nextItem nextChar = rx_buffer.getNext();
+    if (nextChar.success)
+        output = (char*)nextChar.item;        
+
+    RawSerial::attach(this, &SerialBuffer::rx_interrupt, Serial::RxIrq);
+    
+    return *output;
+}
+
+void rx_interrupt(void) {
+    if(serial_readable(&_serial) && rx_buffer.writeable()) {
+        rx_buffer.add(serial_getc(&_serial));
+    }
+
+    return;   
+}
\ No newline at end of file