Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

io/MTSSerialFlowControl.cpp

Committer:
sgodinez
Date:
2013-12-30
Revision:
96:27bdf4aa3a31
Parent:
86:186bbf974c7c
Child:
101:27bb34e23304

File content as of revision 96:27bdf4aa3a31:

#include "MTSSerialFlowControl.h"

using namespace mts;

MTSSerialFlowControl::MTSSerialFlowControl(PinName TXD, PinName RXD, PinName RTS, PinName CTS, int txBufSize, int rxBufSize) 
: MTSSerial(TXD, RXD, txBufSize, rxBufSize)
, rts(RTS)
, cts(CTS)
{
    notifyStartSending();

    highThreshold = MAX(rxBufSize - 10, rxBufSize * 0.85);
    lowThreshold = rxBufSize * 0.3;

    rxBuffer.attach(this, &MTSSerialFlowControl::notifyStartSending, lowThreshold, Vars::LESS);
}

MTSSerialFlowControl::~MTSSerialFlowControl()
{

}

void MTSSerialFlowControl::notifyStartSending()
{
    rts.write(0);
    //printf("RTS: START SENDING US BYTES - RX[%d]\r\n", rxBuffer.size());
}

void MTSSerialFlowControl::notifyStopSending()
{
    rts.write(1);
    //printf("RTS: STOP SENDING US BYTES - RX[%d]\r\n", rxBuffer.size());
}

void MTSSerialFlowControl::handleRead()
{
    while (serial.readable()) {
        char byte = serial.getc();
        if(rxBuffer.write(byte) != 1) {
            printf("[ERROR] Serial Rx Byte Dropped [%c][%02X]\r\n", byte, byte);
            notifyStopSending();
        }
        if (rxBuffer.size() > highThreshold) {
            notifyStopSending();
        }
    }
}

void MTSSerialFlowControl::handleWrite()
{
    while(txBuffer.size() != 0) {
        if (serial.writeable() && cts.read() == 0) {
            char byte;
            if(txBuffer.read(byte) == 1) {
                serial.putc(byte);
            }
        } else {
            return;
        }
    }
}