My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MarlinSerial.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <inttypes.h>
  20. #include <Stream.h>
  21. #include <string.h>
  22. #define FORCE_INLINE __attribute__((always_inline)) inline
  23. #include "WString.h"
  24. #define DEC 10
  25. #define HEX 16
  26. #define OCT 8
  27. #define BIN 2
  28. #define BYTE 0
  29. // Define constants and variables for buffering incoming serial data. We're
  30. // using a ring buffer (I think), in which rx_buffer_head is the index of the
  31. // location to which to write the next incoming character and rx_buffer_tail
  32. // is the index of the location from which to read.
  33. #define RX_BUFFER_SIZE 128
  34. struct ring_buffer
  35. {
  36. unsigned char buffer[RX_BUFFER_SIZE];
  37. int head;
  38. int tail;
  39. };
  40. #if defined(UBRRH) || defined(UBRR0H)
  41. extern ring_buffer rx_buffer;
  42. #endif
  43. class MarlinSerial //: public Stream
  44. {
  45. public:
  46. MarlinSerial();
  47. void begin(long);
  48. void end();
  49. int peek(void);
  50. int read(void);
  51. void flush(void);
  52. FORCE_INLINE int available(void)
  53. {
  54. return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE;
  55. }
  56. FORCE_INLINE void write(uint8_t c)
  57. {
  58. while (!((UCSR0A) & (1 << UDRE0)))
  59. ;
  60. UDR0 = c;
  61. }
  62. FORCE_INLINE void checkRx(void)
  63. {
  64. if((UCSR0A & (1<<RXC0)) != 0) {
  65. unsigned char c = UDR0;
  66. int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
  67. // if we should be storing the received character into the location
  68. // just before the tail (meaning that the head would advance to the
  69. // current location of the tail), we're about to overflow the buffer
  70. // and so we don't write the character or advance the head.
  71. if (i != rx_buffer.tail) {
  72. rx_buffer.buffer[rx_buffer.head] = c;
  73. rx_buffer.head = i;
  74. }
  75. }
  76. }
  77. private:
  78. void printNumber(unsigned long, uint8_t);
  79. void printFloat(double, uint8_t);
  80. public:
  81. FORCE_INLINE void write(const char *str)
  82. {
  83. while (*str)
  84. write(*str++);
  85. }
  86. FORCE_INLINE void write(const uint8_t *buffer, size_t size)
  87. {
  88. while (size--)
  89. write(*buffer++);
  90. }
  91. FORCE_INLINE void print(const String &s)
  92. {
  93. for (int i = 0; i < s.length(); i++) {
  94. write(s[i]);
  95. }
  96. }
  97. FORCE_INLINE void print(const char *str)
  98. {
  99. write(str);
  100. }
  101. void print(char, int = BYTE);
  102. void print(unsigned char, int = BYTE);
  103. void print(int, int = DEC);
  104. void print(unsigned int, int = DEC);
  105. void print(long, int = DEC);
  106. void print(unsigned long, int = DEC);
  107. void print(double, int = 2);
  108. void println(const String &s);
  109. void println(const char[]);
  110. void println(char, int = BYTE);
  111. void println(unsigned char, int = BYTE);
  112. void println(int, int = DEC);
  113. void println(unsigned int, int = DEC);
  114. void println(long, int = DEC);
  115. void println(unsigned long, int = DEC);
  116. void println(double, int = 2);
  117. void println(void);
  118. };
  119. #if defined(UBRRH) || defined(UBRR0H)
  120. extern MarlinSerial MSerial;
  121. #endif
  122. #endif