MarlinSerial.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. HardwareSerial.h - Hardware serial library for Wiring
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 28 September 2010 by Mark Sproul
  16. */
  17. #ifndef MarlinSerial_h
  18. #define MarlinSerial_h
  19. #include "Marlin.h"
  20. #define DEC 10
  21. #define HEX 16
  22. #define OCT 8
  23. #define BIN 2
  24. #define BYTE 0
  25. #if MOTHERBOARD != 8 // ! teensylu
  26. // Define constants and variables for buffering incoming serial data. We're
  27. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  28. // location to which to write the next incoming character and rx_buffer_tail
  29. // is the index of the location from which to read.
  30. #define RX_BUFFER_SIZE 128
  31. struct ring_buffer
  32. {
  33. unsigned char buffer[RX_BUFFER_SIZE];
  34. int head;
  35. int tail;
  36. };
  37. #if defined(UBRRH) || defined(UBRR0H)
  38. extern ring_buffer rx_buffer;
  39. #endif
  40. class MarlinSerial //: public Stream
  41. {
  42. public:
  43. MarlinSerial();
  44. void begin(long);
  45. void end();
  46. int peek(void);
  47. int read(void);
  48. void flush(void);
  49. FORCE_INLINE int available(void)
  50. {
  51. return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
  52. }
  53. FORCE_INLINE void write(uint8_t c)
  54. {
  55. while (!((UCSR0A) & (1 << UDRE0)))
  56. ;
  57. UDR0 = c;
  58. }
  59. FORCE_INLINE void checkRx(void)
  60. {
  61. if((UCSR0A & (1<<RXC0)) != 0) {
  62. unsigned char c = UDR0;
  63. int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
  64. // if we should be storing the received character into the location
  65. // just before the tail (meaning that the head would advance to the
  66. // current location of the tail), we're about to overflow the buffer
  67. // and so we don't write the character or advance the head.
  68. if (i != rx_buffer.tail) {
  69. rx_buffer.buffer[rx_buffer.head] = c;
  70. rx_buffer.head = i;
  71. }
  72. }
  73. }
  74. private:
  75. void printNumber(unsigned long, uint8_t);
  76. void printFloat(double, uint8_t);
  77. public:
  78. FORCE_INLINE void write(const char *str)
  79. {
  80. while (*str)
  81. write(*str++);
  82. }
  83. FORCE_INLINE void write(const uint8_t *buffer, size_t size)
  84. {
  85. while (size--)
  86. write(*buffer++);
  87. }
  88. FORCE_INLINE void print(const String &s)
  89. {
  90. for (int i = 0; i < (int)s.length(); i++) {
  91. write(s[i]);
  92. }
  93. }
  94. FORCE_INLINE void print(const char *str)
  95. {
  96. write(str);
  97. }
  98. void print(char, int = BYTE);
  99. void print(unsigned char, int = BYTE);
  100. void print(int, int = DEC);
  101. void print(unsigned int, int = DEC);
  102. void print(long, int = DEC);
  103. void print(unsigned long, int = DEC);
  104. void print(double, int = 2);
  105. void println(const String &s);
  106. void println(const char[]);
  107. void println(char, int = BYTE);
  108. void println(unsigned char, int = BYTE);
  109. void println(int, int = DEC);
  110. void println(unsigned int, int = DEC);
  111. void println(long, int = DEC);
  112. void println(unsigned long, int = DEC);
  113. void println(double, int = 2);
  114. void println(void);
  115. };
  116. extern MarlinSerial MSerial;
  117. #endif // ! teensylu
  118. #endif