Changed static .data strings to PSTR(), removed oom situation
This commit is contained in:
46
serial.cpp
46
serial.cpp
@ -1,26 +1,12 @@
|
||||
#include "serial.h"
|
||||
|
||||
char rxBuffer[BUFFER_SIZE];
|
||||
volatile uint32_t interruptIndex = 0;
|
||||
volatile uint32_t _rxIndex = 0;
|
||||
|
||||
bool stopped = false;
|
||||
volatile uint16_t interruptIndex = 0;
|
||||
|
||||
ISR (USART_RX_vect) //I have seen worse interrupt sintax
|
||||
{
|
||||
rxBuffer[interruptIndex % BUFFER_SIZE] = UDR0;
|
||||
if (interruptIndex - BUFFER_SIZE > 0 && _rxIndex - BUFFER_SIZE > 0)
|
||||
{
|
||||
interruptIndex -= BUFFER_SIZE;
|
||||
_rxIndex -= BUFFER_SIZE;
|
||||
}
|
||||
if(serialFlowControl && !stopped && interruptIndex - _rxIndex > BUFFER_SIZE - 64)
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x13;
|
||||
stopped = true;
|
||||
}
|
||||
if(interruptIndex - _rxIndex < BUFFER_SIZE )interruptIndex++;
|
||||
interruptIndex++;
|
||||
}
|
||||
|
||||
Serial::Serial()
|
||||
@ -54,6 +40,14 @@ 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)
|
||||
{
|
||||
@ -83,6 +77,7 @@ void Serial::write(int32_t in)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool Serial::dataIsWaiting()
|
||||
{
|
||||
return (interruptIndex > _rxIndex);
|
||||
@ -90,15 +85,10 @@ bool Serial::dataIsWaiting()
|
||||
|
||||
char Serial::getChar()
|
||||
{
|
||||
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush(); //may explode only occasionaly
|
||||
if(dataIsWaiting())
|
||||
{
|
||||
_rxIndex++;
|
||||
if(serialFlowControl && stopped && interruptIndex - _rxIndex < 64)
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x11;
|
||||
stopped = false;
|
||||
}
|
||||
return rxBuffer[(_rxIndex -1) % BUFFER_SIZE];
|
||||
}
|
||||
else return '\0';
|
||||
@ -106,12 +96,12 @@ char Serial::getChar()
|
||||
|
||||
unsigned int Serial::getString(char* buffer, const int bufferLength)
|
||||
{
|
||||
unsigned int i = 0;
|
||||
int i = 0;
|
||||
for(; i <= (interruptIndex-_rxIndex) && i <= BUFFER_SIZE && rxBuffer[(_rxIndex+i) % BUFFER_SIZE] != _terminator; i++);
|
||||
|
||||
if( i < (interruptIndex-_rxIndex) && i > 0)
|
||||
{
|
||||
unsigned int j = 0;
|
||||
int j = 0;
|
||||
for(; j < i && j < bufferLength-1 ; j++)
|
||||
{
|
||||
buffer[j] = getChar();
|
||||
@ -119,8 +109,12 @@ unsigned int Serial::getString(char* buffer, const int bufferLength)
|
||||
buffer[j+1]='\0';
|
||||
_rxIndex++;
|
||||
}
|
||||
else i = 0;
|
||||
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush();
|
||||
}
|
||||
|
||||
if (rxBuffer[(_rxIndex+i) % BUFFER_SIZE] == _terminator) _rxIndex++;
|
||||
|
||||
return i;
|
||||
|
Reference in New Issue
Block a user