Marcus Lee / SerialBuffer

Dependencies:   LinearArray

serialBuffer.cpp

Committer:
UHSLMarcus
Date:
2016-10-03
Revision:
0:dd6d74f39384
Child:
1:985b6f1c7347

File content as of revision 0:dd6d74f39384:

#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;   
}