Added Sate output

This commit is contained in:
IMback
2017-11-05 17:37:21 +01:00
parent b4d1844b68
commit 21b636aa98
48 changed files with 1363 additions and 3742 deletions

View File

@ -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()
@ -24,7 +38,7 @@ void Serial::putChar(const char c)
UDR0 = c;
}
void Serial::putString(const char* in, const unsigned int length)
void Serial::write(const char* in, const unsigned int length)
{
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
{
@ -32,7 +46,7 @@ void Serial::putString(const char* in, const unsigned int length)
}
}
void Serial::putString(const char in[])
void Serial::write(const char in[])
{
for(unsigned int i = 0; i < strlen(in); i++)
{
@ -40,6 +54,35 @@ void Serial::putString(const char in[])
}
}
void Serial::write(int32_t in)
{
if(in == 0)
{
putChar('0');
}
else
{
bool flag = false;
char str[64] = { 0 };
int16_t i = 62;
if (in < 0)
{
flag = true;
in = abs(in);
}
while (in != 0 && i > 0)
{
str[i--] = (in % 10) + '0';
in /= 10;
}
if (flag) str[i--] = '-';
write(str + i + 1, 64-(i+1));
}
}
bool Serial::dataIsWaiting()
{
return (interruptIndex > _rxIndex);
@ -47,10 +90,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 +106,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 +119,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;