inital version
This commit is contained in:
128
serial.cpp
Normal file
128
serial.cpp
Normal file
@ -0,0 +1,128 @@
|
||||
#include "serial.h"
|
||||
#include "ringbuffer.h"
|
||||
|
||||
volatile RingBuffer<SERIAL_BUFFER_SIZE, volatile uint8_t> rxBuffer;
|
||||
|
||||
bool stopped = false;
|
||||
|
||||
ISR(USART_RX_vect) //I have seen worse interrupt sintax
|
||||
{
|
||||
rxBuffer.write(UDR0);
|
||||
if(serialFlowControl && !stopped && rxBuffer.remainingCapacity() < 32)
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = 0x13;
|
||||
stopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
Serial::Serial()
|
||||
{
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); //Enable RX and TX
|
||||
UCSR0B |= (1 << RXCIE0); //Enable Rx interuppt
|
||||
sei();
|
||||
}
|
||||
|
||||
void Serial::putChar(const char c)
|
||||
{
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
}
|
||||
|
||||
void Serial::write(const char* in, const unsigned int length)
|
||||
{
|
||||
for(unsigned int i = 0; i < length && in[i] != '\0'; i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void Serial::write_p(const char in[])
|
||||
{
|
||||
cli();
|
||||
char ch = pgm_read_byte(in);
|
||||
while (ch != '\0')
|
||||
{
|
||||
putChar(ch);
|
||||
in++;
|
||||
ch = pgm_read_byte(in);
|
||||
}
|
||||
sei();
|
||||
}
|
||||
|
||||
void Serial::write(const char in[])
|
||||
{
|
||||
for(unsigned int i = 0; i < strlen(in); i++)
|
||||
{
|
||||
putChar(in[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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 !rxBuffer.isEmpty();
|
||||
}
|
||||
|
||||
char Serial::getChar()
|
||||
{
|
||||
if(!rxBuffer.isEmpty())
|
||||
{
|
||||
if(serialFlowControl && stopped && rxBuffer.remainingCapacity() > 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)
|
||||
{
|
||||
return rxBuffer.getString(_terminator, (uint8_t*)buffer, bufferLength);
|
||||
}
|
||||
|
||||
unsigned int Serial::read(char* buffer, const unsigned int length )
|
||||
{
|
||||
return rxBuffer.read((uint8_t*)buffer, length);
|
||||
}
|
||||
|
||||
void Serial::flush()
|
||||
{
|
||||
rxBuffer.flush();
|
||||
}
|
||||
|
||||
void Serial::setTerminator(char terminator){_terminator = terminator;}
|
Reference in New Issue
Block a user