My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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