improved serial handeling added aux state info
This commit is contained in:
79
serial.cpp
79
serial.cpp
@ -1,12 +1,19 @@
|
||||
#include "serial.h"
|
||||
#include "ringbuffer.h"
|
||||
|
||||
char rxBuffer[BUFFER_SIZE];
|
||||
volatile uint16_t interruptIndex = 0;
|
||||
RingBuffer<SERIAL_BUFFER_SIZE> rxBuffer;
|
||||
|
||||
bool stopped = false;
|
||||
|
||||
ISR (USART_RX_vect) //I have seen worse interrupt sintax
|
||||
{
|
||||
rxBuffer[interruptIndex % BUFFER_SIZE] = UDR0;
|
||||
interruptIndex++;
|
||||
rxBuffer.write(UDR0);
|
||||
if(serialFlowControl && !stopped && rxBuffer.isOverun(32))
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x13;
|
||||
stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
Serial::Serial()
|
||||
@ -32,6 +39,17 @@ void Serial::write(const char* in, const unsigned int length)
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write_p(const char in[])
|
||||
{
|
||||
char ch = pgm_read_byte(in);
|
||||
while (ch != '\0')
|
||||
{
|
||||
putChar(ch);
|
||||
in++;
|
||||
ch = pgm_read_byte(in);
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write(const char in[])
|
||||
{
|
||||
for(unsigned int i = 0; i < strlen(in); i++)
|
||||
@ -40,14 +58,6 @@ void Serial::write(const char in[])
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write_p(const char *in)
|
||||
{
|
||||
while (pgm_read_byte(in) != '\0')
|
||||
{
|
||||
|
||||
putChar(pgm_read_byte(in++));
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write(int32_t in)
|
||||
{
|
||||
@ -77,54 +87,39 @@ void Serial::write(int32_t in)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Serial::dataIsWaiting()
|
||||
{
|
||||
return (interruptIndex > _rxIndex);
|
||||
return !rxBuffer.isEmpty();
|
||||
}
|
||||
|
||||
char Serial::getChar()
|
||||
{
|
||||
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush(); //may explode only occasionaly
|
||||
if(dataIsWaiting())
|
||||
if(!rxBuffer.isEmpty())
|
||||
{
|
||||
_rxIndex++;
|
||||
return rxBuffer[(_rxIndex -1) % BUFFER_SIZE];
|
||||
if(serialFlowControl && stopped && !rxBuffer.isOverun(32))
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x11;
|
||||
stopped = false;
|
||||
}
|
||||
return rxBuffer.read();
|
||||
}
|
||||
else return '\0';
|
||||
}
|
||||
|
||||
unsigned int Serial::getString(char* buffer, const int bufferLength)
|
||||
{
|
||||
int i = 0;
|
||||
for(; i <= (interruptIndex-_rxIndex) && i <= BUFFER_SIZE && rxBuffer[(_rxIndex+i) % BUFFER_SIZE] != _terminator; i++);
|
||||
|
||||
if( i < (interruptIndex-_rxIndex) && i > 0)
|
||||
{
|
||||
int j = 0;
|
||||
for(; j < i && j < bufferLength-1 ; j++)
|
||||
{
|
||||
buffer[j] = getChar();
|
||||
}
|
||||
buffer[j+1]='\0';
|
||||
_rxIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush();
|
||||
}
|
||||
return rxBuffer.getString(_terminator, buffer, bufferLength);
|
||||
}
|
||||
|
||||
if (rxBuffer[(_rxIndex+i) % BUFFER_SIZE] == _terminator) _rxIndex++;
|
||||
|
||||
return i;
|
||||
unsigned int Serial::read(char* buffer, const unsigned int length )
|
||||
{
|
||||
return rxBuffer.read((uint8_t*)buffer, length);
|
||||
}
|
||||
|
||||
void Serial::flush()
|
||||
{
|
||||
_rxIndex = 0;
|
||||
interruptIndex = 0;
|
||||
for(int i = 0; i < BUFFER_SIZE; i++) rxBuffer[i] = ' ';
|
||||
rxBuffer.flush();
|
||||
}
|
||||
|
||||
void Serial::setTerminator(char terminator){_terminator = terminator;}
|
||||
|
Reference in New Issue
Block a user