|
@@ -32,8 +32,7 @@
|
32
|
32
|
ring_buffer rx_buffer = { { 0 }, 0, 0 };
|
33
|
33
|
#endif
|
34
|
34
|
|
35
|
|
-FORCE_INLINE void store_char(unsigned char c)
|
36
|
|
-{
|
|
35
|
+FORCE_INLINE void store_char(unsigned char c) {
|
37
|
36
|
int i = (unsigned int)(rx_buffer.head + 1) % RX_BUFFER_SIZE;
|
38
|
37
|
|
39
|
38
|
// if we should be storing the received character into the location
|
|
@@ -51,8 +50,7 @@ FORCE_INLINE void store_char(unsigned char c)
|
51
|
50
|
#if defined(M_USARTx_RX_vect)
|
52
|
51
|
// fixed by Mark Sproul this is on the 644/644p
|
53
|
52
|
//SIGNAL(SIG_USART_RECV)
|
54
|
|
- SIGNAL(M_USARTx_RX_vect)
|
55
|
|
- {
|
|
53
|
+ SIGNAL(M_USARTx_RX_vect) {
|
56
|
54
|
unsigned char c = M_UDRx;
|
57
|
55
|
store_char(c);
|
58
|
56
|
}
|
|
@@ -60,26 +58,22 @@ FORCE_INLINE void store_char(unsigned char c)
|
60
|
58
|
|
61
|
59
|
// Constructors ////////////////////////////////////////////////////////////////
|
62
|
60
|
|
63
|
|
-MarlinSerial::MarlinSerial()
|
64
|
|
-{
|
65
|
|
-
|
66
|
|
-}
|
|
61
|
+MarlinSerial::MarlinSerial() { }
|
67
|
62
|
|
68
|
63
|
// Public Methods //////////////////////////////////////////////////////////////
|
69
|
64
|
|
70
|
|
-void MarlinSerial::begin(long baud)
|
71
|
|
-{
|
|
65
|
+void MarlinSerial::begin(long baud) {
|
72
|
66
|
uint16_t baud_setting;
|
73
|
67
|
bool useU2X = true;
|
74
|
68
|
|
75
|
|
-#if F_CPU == 16000000UL && SERIAL_PORT == 0
|
76
|
|
- // hard-coded exception for compatibility with the bootloader shipped
|
77
|
|
- // with the Duemilanove and previous boards and the firmware on the 8U2
|
78
|
|
- // on the Uno and Mega 2560.
|
79
|
|
- if (baud == 57600) {
|
80
|
|
- useU2X = false;
|
81
|
|
- }
|
82
|
|
-#endif
|
|
69
|
+ #if F_CPU == 16000000UL && SERIAL_PORT == 0
|
|
70
|
+ // hard-coded exception for compatibility with the bootloader shipped
|
|
71
|
+ // with the Duemilanove and previous boards and the firmware on the 8U2
|
|
72
|
+ // on the Uno and Mega 2560.
|
|
73
|
+ if (baud == 57600) {
|
|
74
|
+ useU2X = false;
|
|
75
|
+ }
|
|
76
|
+ #endif
|
83
|
77
|
|
84
|
78
|
if (useU2X) {
|
85
|
79
|
M_UCSRxA = 1 << M_U2Xx;
|
|
@@ -98,17 +92,14 @@ void MarlinSerial::begin(long baud)
|
98
|
92
|
sbi(M_UCSRxB, M_RXCIEx);
|
99
|
93
|
}
|
100
|
94
|
|
101
|
|
-void MarlinSerial::end()
|
102
|
|
-{
|
|
95
|
+void MarlinSerial::end() {
|
103
|
96
|
cbi(M_UCSRxB, M_RXENx);
|
104
|
97
|
cbi(M_UCSRxB, M_TXENx);
|
105
|
98
|
cbi(M_UCSRxB, M_RXCIEx);
|
106
|
99
|
}
|
107
|
100
|
|
108
|
101
|
|
109
|
|
-
|
110
|
|
-int MarlinSerial::peek(void)
|
111
|
|
-{
|
|
102
|
+int MarlinSerial::peek(void) {
|
112
|
103
|
if (rx_buffer.head == rx_buffer.tail) {
|
113
|
104
|
return -1;
|
114
|
105
|
} else {
|
|
@@ -116,20 +107,19 @@ int MarlinSerial::peek(void)
|
116
|
107
|
}
|
117
|
108
|
}
|
118
|
109
|
|
119
|
|
-int MarlinSerial::read(void)
|
120
|
|
-{
|
|
110
|
+int MarlinSerial::read(void) {
|
121
|
111
|
// if the head isn't ahead of the tail, we don't have any characters
|
122
|
112
|
if (rx_buffer.head == rx_buffer.tail) {
|
123
|
113
|
return -1;
|
124
|
|
- } else {
|
|
114
|
+ }
|
|
115
|
+ else {
|
125
|
116
|
unsigned char c = rx_buffer.buffer[rx_buffer.tail];
|
126
|
117
|
rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE;
|
127
|
118
|
return c;
|
128
|
119
|
}
|
129
|
120
|
}
|
130
|
121
|
|
131
|
|
-void MarlinSerial::flush()
|
132
|
|
-{
|
|
122
|
+void MarlinSerial::flush() {
|
133
|
123
|
// don't reverse this or there may be problems if the RX interrupt
|
134
|
124
|
// occurs after reading the value of rx_buffer_head but before writing
|
135
|
125
|
// the value to rx_buffer_tail; the previous value of rx_buffer_head
|
|
@@ -143,38 +133,30 @@ void MarlinSerial::flush()
|
143
|
133
|
}
|
144
|
134
|
|
145
|
135
|
|
146
|
|
-
|
147
|
|
-
|
148
|
136
|
/// imports from print.h
|
149
|
137
|
|
150
|
138
|
|
151
|
|
-
|
152
|
|
-
|
153
|
|
-void MarlinSerial::print(char c, int base)
|
154
|
|
-{
|
|
139
|
+void MarlinSerial::print(char c, int base) {
|
155
|
140
|
print((long) c, base);
|
156
|
141
|
}
|
157
|
142
|
|
158
|
|
-void MarlinSerial::print(unsigned char b, int base)
|
159
|
|
-{
|
|
143
|
+void MarlinSerial::print(unsigned char b, int base) {
|
160
|
144
|
print((unsigned long) b, base);
|
161
|
145
|
}
|
162
|
146
|
|
163
|
|
-void MarlinSerial::print(int n, int base)
|
164
|
|
-{
|
|
147
|
+void MarlinSerial::print(int n, int base) {
|
165
|
148
|
print((long) n, base);
|
166
|
149
|
}
|
167
|
150
|
|
168
|
|
-void MarlinSerial::print(unsigned int n, int base)
|
169
|
|
-{
|
|
151
|
+void MarlinSerial::print(unsigned int n, int base) {
|
170
|
152
|
print((unsigned long) n, base);
|
171
|
153
|
}
|
172
|
154
|
|
173
|
|
-void MarlinSerial::print(long n, int base)
|
174
|
|
-{
|
|
155
|
+void MarlinSerial::print(long n, int base) {
|
175
|
156
|
if (base == 0) {
|
176
|
157
|
write(n);
|
177
|
|
- } else if (base == 10) {
|
|
158
|
+ }
|
|
159
|
+ else if (base == 10) {
|
178
|
160
|
if (n < 0) {
|
179
|
161
|
print('-');
|
180
|
162
|
n = -n;
|
|
@@ -185,81 +167,68 @@ void MarlinSerial::print(long n, int base)
|
185
|
167
|
}
|
186
|
168
|
}
|
187
|
169
|
|
188
|
|
-void MarlinSerial::print(unsigned long n, int base)
|
189
|
|
-{
|
|
170
|
+void MarlinSerial::print(unsigned long n, int base) {
|
190
|
171
|
if (base == 0) write(n);
|
191
|
172
|
else printNumber(n, base);
|
192
|
173
|
}
|
193
|
174
|
|
194
|
|
-void MarlinSerial::print(double n, int digits)
|
195
|
|
-{
|
|
175
|
+void MarlinSerial::print(double n, int digits) {
|
196
|
176
|
printFloat(n, digits);
|
197
|
177
|
}
|
198
|
178
|
|
199
|
|
-void MarlinSerial::println(void)
|
200
|
|
-{
|
|
179
|
+void MarlinSerial::println(void) {
|
201
|
180
|
print('\r');
|
202
|
181
|
print('\n');
|
203
|
182
|
}
|
204
|
183
|
|
205
|
|
-void MarlinSerial::println(const String &s)
|
206
|
|
-{
|
|
184
|
+void MarlinSerial::println(const String &s) {
|
207
|
185
|
print(s);
|
208
|
186
|
println();
|
209
|
187
|
}
|
210
|
188
|
|
211
|
|
-void MarlinSerial::println(const char c[])
|
212
|
|
-{
|
|
189
|
+void MarlinSerial::println(const char c[]) {
|
213
|
190
|
print(c);
|
214
|
191
|
println();
|
215
|
192
|
}
|
216
|
193
|
|
217
|
|
-void MarlinSerial::println(char c, int base)
|
218
|
|
-{
|
|
194
|
+void MarlinSerial::println(char c, int base) {
|
219
|
195
|
print(c, base);
|
220
|
196
|
println();
|
221
|
197
|
}
|
222
|
198
|
|
223
|
|
-void MarlinSerial::println(unsigned char b, int base)
|
224
|
|
-{
|
|
199
|
+void MarlinSerial::println(unsigned char b, int base) {
|
225
|
200
|
print(b, base);
|
226
|
201
|
println();
|
227
|
202
|
}
|
228
|
203
|
|
229
|
|
-void MarlinSerial::println(int n, int base)
|
230
|
|
-{
|
|
204
|
+void MarlinSerial::println(int n, int base) {
|
231
|
205
|
print(n, base);
|
232
|
206
|
println();
|
233
|
207
|
}
|
234
|
208
|
|
235
|
|
-void MarlinSerial::println(unsigned int n, int base)
|
236
|
|
-{
|
|
209
|
+void MarlinSerial::println(unsigned int n, int base) {
|
237
|
210
|
print(n, base);
|
238
|
211
|
println();
|
239
|
212
|
}
|
240
|
213
|
|
241
|
|
-void MarlinSerial::println(long n, int base)
|
242
|
|
-{
|
|
214
|
+void MarlinSerial::println(long n, int base) {
|
243
|
215
|
print(n, base);
|
244
|
216
|
println();
|
245
|
217
|
}
|
246
|
218
|
|
247
|
|
-void MarlinSerial::println(unsigned long n, int base)
|
248
|
|
-{
|
|
219
|
+void MarlinSerial::println(unsigned long n, int base) {
|
249
|
220
|
print(n, base);
|
250
|
221
|
println();
|
251
|
222
|
}
|
252
|
223
|
|
253
|
|
-void MarlinSerial::println(double n, int digits)
|
254
|
|
-{
|
|
224
|
+void MarlinSerial::println(double n, int digits) {
|
255
|
225
|
print(n, digits);
|
256
|
226
|
println();
|
257
|
227
|
}
|
258
|
228
|
|
259
|
229
|
// Private Methods /////////////////////////////////////////////////////////////
|
260
|
230
|
|
261
|
|
-void MarlinSerial::printNumber(unsigned long n, uint8_t base)
|
262
|
|
-{
|
|
231
|
+void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
|
263
|
232
|
unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
|
264
|
233
|
unsigned long i = 0;
|
265
|
234
|
|
|
@@ -279,18 +248,16 @@ void MarlinSerial::printNumber(unsigned long n, uint8_t base)
|
279
|
248
|
'A' + buf[i - 1] - 10));
|
280
|
249
|
}
|
281
|
250
|
|
282
|
|
-void MarlinSerial::printFloat(double number, uint8_t digits)
|
283
|
|
-{
|
|
251
|
+void MarlinSerial::printFloat(double number, uint8_t digits) {
|
284
|
252
|
// Handle negative numbers
|
285
|
|
- if (number < 0.0)
|
286
|
|
- {
|
|
253
|
+ if (number < 0.0) {
|
287
|
254
|
print('-');
|
288
|
255
|
number = -number;
|
289
|
256
|
}
|
290
|
257
|
|
291
|
258
|
// Round correctly so that print(1.999, 2) prints as "2.00"
|
292
|
259
|
double rounding = 0.5;
|
293
|
|
- for (uint8_t i=0; i<digits; ++i)
|
|
260
|
+ for (uint8_t i = 0; i < digits; ++i)
|
294
|
261
|
rounding /= 10.0;
|
295
|
262
|
|
296
|
263
|
number += rounding;
|
|
@@ -305,8 +272,7 @@ void MarlinSerial::printFloat(double number, uint8_t digits)
|
305
|
272
|
print(".");
|
306
|
273
|
|
307
|
274
|
// Extract digits from the remainder one at a time
|
308
|
|
- while (digits-- > 0)
|
309
|
|
- {
|
|
275
|
+ while (digits-- > 0) {
|
310
|
276
|
remainder *= 10.0;
|
311
|
277
|
int toPrint = int(remainder);
|
312
|
278
|
print(toPrint);
|