Works with simple/no kinematics
This commit is contained in:
parent
dd6f37a960
commit
471d018bfe
7 changed files with 221 additions and 245 deletions
37
serial.cpp
37
serial.cpp
|
|
@ -1,12 +1,26 @@
|
|||
#include "serial.h"
|
||||
|
||||
char rxBuffer[BUFFER_SIZE];
|
||||
volatile uint16_t interruptIndex = 0;
|
||||
volatile uint32_t interruptIndex = 0;
|
||||
volatile uint32_t _rxIndex = 0;
|
||||
|
||||
bool stopped = false;
|
||||
|
||||
ISR (USART_RX_vect) //I have seen worse interrupt sintax
|
||||
{
|
||||
rxBuffer[interruptIndex % BUFFER_SIZE] = UDR0;
|
||||
interruptIndex++;
|
||||
/*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++;
|
||||
}
|
||||
|
||||
Serial::Serial()
|
||||
|
|
@ -47,10 +61,15 @@ 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';
|
||||
|
|
@ -58,12 +77,12 @@ char Serial::getChar()
|
|||
|
||||
unsigned int Serial::getString(char* buffer, const int bufferLength)
|
||||
{
|
||||
int i = 0;
|
||||
unsigned 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;
|
||||
unsigned int j = 0;
|
||||
for(; j < i && j < bufferLength-1 ; j++)
|
||||
{
|
||||
buffer[j] = getChar();
|
||||
|
|
@ -71,12 +90,8 @@ unsigned int Serial::getString(char* buffer, const int bufferLength)
|
|||
buffer[j+1]='\0';
|
||||
_rxIndex++;
|
||||
}
|
||||
else
|
||||
{
|
||||
i = 0;
|
||||
if( _rxIndex >= (32768) - 2*BUFFER_SIZE ) flush();
|
||||
}
|
||||
|
||||
else i = 0;
|
||||
|
||||
if (rxBuffer[(_rxIndex+i) % BUFFER_SIZE] == _terminator) _rxIndex++;
|
||||
|
||||
return i;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue