My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MarlinSerial.h 3.8KB

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