1
0
mirror of https://github.com/MarlinFirmware/Marlin.git synced 2024-11-30 23:30:16 +00:00
MarlinFirmware/Marlin/MarlinSerial.h

151 lines
3.8 KiB
C
Raw Normal View History

/*
HardwareSerial.h - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 28 September 2010 by Mark Sproul
*/
#ifndef MarlinSerial_h
#define MarlinSerial_h
#include "Marlin.h"
2011-11-28 20:51:44 +00:00
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
#define BYTE 0
2012-02-09 18:27:45 +00:00
#if MOTHERBOARD != 8 // ! teensylu
// Define constants and variables for buffering incoming serial data. We're
// using a ring buffer (I think), in which rx_buffer_head is the index of the
// location to which to write the next incoming character and rx_buffer_tail
// is the index of the location from which to read.
#define RX_BUFFER_SIZE 128
struct ring_buffer
{
unsigned char buffer[RX_BUFFER_SIZE];
int head;
int tail;
};
#if defined(UBRRH) || defined(UBRR0H)
extern ring_buffer rx_buffer;
#endif
class MarlinSerial //: public Stream
{
public:
MarlinSerial();
void begin(long);
void end();
2011-11-28 18:34:42 +00:00
int peek(void);
int read(void);
void flush(void);
2011-11-28 20:51:44 +00:00
FORCE_INLINE int available(void)
{
return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
}
2011-11-28 18:34:42 +00:00
2011-11-28 20:51:44 +00:00
FORCE_INLINE void write(uint8_t c)
{
while (!((UCSR0A) & (1 << UDRE0)))
;
UDR0 = c;
}
2011-11-28 20:51:44 +00:00
FORCE_INLINE void checkRx(void)
{
if((UCSR0A & (1<<RXC0)) != 0) {
unsigned char c = UDR0;
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != rx_buffer.tail) {
rx_buffer.buffer[rx_buffer.head] = c;
rx_buffer.head = i;
}
}
}
private:
void printNumber(unsigned long, uint8_t);
void printFloat(double, uint8_t);
public:
2011-11-28 20:51:44 +00:00
FORCE_INLINE void write(const char *str)
2011-11-28 18:34:42 +00:00
{
while (*str)
write(*str++);
}
2011-11-28 20:51:44 +00:00
FORCE_INLINE void write(const uint8_t *buffer, size_t size)
2011-11-28 18:34:42 +00:00
{
while (size--)
write(*buffer++);
}
2011-11-28 20:51:44 +00:00
FORCE_INLINE void print(const String &s)
2011-11-28 18:34:42 +00:00
{
for (int i = 0; i < (int)s.length(); i++) {
2011-11-28 18:34:42 +00:00
write(s[i]);
}
}
2011-11-28 20:51:44 +00:00
FORCE_INLINE void print(const char *str)
2011-11-28 18:34:42 +00:00
{
write(str);
}
void print(char, int = BYTE);
void print(unsigned char, int = BYTE);
void print(int, int = DEC);
void print(unsigned int, int = DEC);
void print(long, int = DEC);
void print(unsigned long, int = DEC);
void print(double, int = 2);
void println(const String &s);
void println(const char[]);
void println(char, int = BYTE);
void println(unsigned char, int = BYTE);
void println(int, int = DEC);
void println(unsigned int, int = DEC);
void println(long, int = DEC);
void println(unsigned long, int = DEC);
void println(double, int = 2);
void println(void);
};
2012-02-09 18:27:45 +00:00
extern MarlinSerial MSerial;
#endif // ! teensylu
#endif