MTS_SPI_Slave unfinished

Fork of MTS-Serial by MultiTech

MTSSerial.cpp

Committer:
Mike Fiore
Date:
2014-05-20
Revision:
4:d348d001283b
Parent:
1:d34b566d6f47
Child:
12:e12b79a4ab4f

File content as of revision 4:d348d001283b:

#include "mbed.h"
#include "MTSSerial.h"
#include "MTSLog.h"

using namespace mts;

MTSSerial::MTSSerial(PinName TXD, PinName RXD, int txBufferSize, int rxBufferSize)
    : MTSBufferedIO(txBufferSize, rxBufferSize)
    , serial(TXD,RXD)
{
    serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
}

MTSSerial::~MTSSerial()
{
}

void MTSSerial::baud(int baudrate)
{
    serial.baud(baudrate);
}

void MTSSerial::format(int bits, SerialBase::Parity parity, int stop_bits)
{
    serial.format(bits, parity, stop_bits);
}

void MTSSerial::handleRead()
{
    char byte = serial.getc();
    if(rxBuffer.write(byte) != 1) {
        logError("Serial Rx Byte Dropped [%c][0x%02X]", byte, byte);
    }
}

void MTSSerial::handleWrite()
{
    while(txBuffer.size() != 0) {
        if (serial.writeable()) {
            char byte;
            if(txBuffer.read(byte) == 1) {
                serial.attach(NULL, Serial::RxIrq);
                serial.putc(byte);
                serial.attach(this, &MTSSerial::handleRead, Serial::RxIrq);
            }
        } else {
            return;
        }
    }
}