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