瀏覽代碼

Indent MarlinSerial code

Scott Lahteine 7 年之前
父節點
當前提交
eaa66f3c46
共有 2 個檔案被更改,包括 508 行新增520 行删除
  1. 413
    418
      Marlin/MarlinSerial.cpp
  2. 95
    102
      Marlin/MarlinSerial.h

+ 413
- 418
Marlin/MarlinSerial.cpp 查看文件

@@ -33,495 +33,490 @@
33 33
 #include "stepper.h"
34 34
 #include "Marlin.h"
35 35
 
36
-#ifndef USBCON
37
-// this next line disables the entire HardwareSerial.cpp,
38
-// this is so I can support Attiny series and any other chip without a UART
39
-#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
36
+// Disable HardwareSerial.cpp to support chips without a UART (Attiny, etc.)
40 37
 
41
-#if UART_PRESENT(SERIAL_PORT)
42
-  ring_buffer_r rx_buffer  =  { { 0 }, 0, 0 };
43
-  #if TX_BUFFER_SIZE > 0
44
-    ring_buffer_t tx_buffer  =  { { 0 }, 0, 0 };
45
-    static bool _written;
38
+#if !defined(USBCON) && (defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H))
39
+
40
+  #if UART_PRESENT(SERIAL_PORT)
41
+    ring_buffer_r rx_buffer  =  { { 0 }, 0, 0 };
42
+    #if TX_BUFFER_SIZE > 0
43
+      ring_buffer_t tx_buffer  =  { { 0 }, 0, 0 };
44
+      static bool _written;
45
+    #endif
46 46
   #endif
47
-#endif
48 47
 
48
+  #if ENABLED(EMERGENCY_PARSER)
49
+
50
+    #include "language.h"
51
+
52
+    // Currently looking for: M108, M112, M410
53
+    // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
54
+
55
+    FORCE_INLINE void emergency_parser(const unsigned char c) {
56
+
57
+      static e_parser_state state = state_RESET;
49 58
 
50
-FORCE_INLINE void store_char(unsigned char c) {
51
-  CRITICAL_SECTION_START;
52
-    uint8_t h = rx_buffer.head;
53
-    uint8_t i = (uint8_t)(h + 1)  & (RX_BUFFER_SIZE - 1);
59
+      switch (state) {
60
+        case state_RESET:
61
+          switch (c) {
62
+            case ' ': break;
63
+            case 'N': state = state_N;      break;
64
+            case 'M': state = state_M;      break;
65
+            default: state = state_IGNORE;
66
+          }
67
+          break;
68
+
69
+        case state_N:
70
+          switch (c) {
71
+            case '0': case '1': case '2':
72
+            case '3': case '4': case '5':
73
+            case '6': case '7': case '8':
74
+            case '9': case '-': case ' ':   break;
75
+            case 'M': state = state_M;      break;
76
+            default:  state = state_IGNORE;
77
+          }
78
+          break;
79
+
80
+        case state_M:
81
+          switch (c) {
82
+            case ' ': break;
83
+            case '1': state = state_M1;     break;
84
+            case '4': state = state_M4;     break;
85
+            default: state = state_IGNORE;
86
+          }
87
+          break;
54 88
 
55
-    // if we should be storing the received character into the location
56
-    // just before the tail (meaning that the head would advance to the
57
-    // current location of the tail), we're about to overflow the buffer
58
-    // and so we don't write the character or advance the head.
59
-    if (i != rx_buffer.tail) {
60
-      rx_buffer.buffer[h] = c;
61
-      rx_buffer.head = i;
89
+        case state_M1:
90
+          switch (c) {
91
+            case '0': state = state_M10;    break;
92
+            case '1': state = state_M11;    break;
93
+            default: state = state_IGNORE;
94
+          }
95
+          break;
96
+
97
+        case state_M10:
98
+          state = (c == '8') ? state_M108 : state_IGNORE;
99
+          break;
100
+
101
+        case state_M11:
102
+          state = (c == '2') ? state_M112 : state_IGNORE;
103
+          break;
104
+
105
+        case state_M4:
106
+          state = (c == '1') ? state_M41 : state_IGNORE;
107
+          break;
108
+
109
+        case state_M41:
110
+          state = (c == '0') ? state_M410 : state_IGNORE;
111
+          break;
112
+
113
+        case state_IGNORE:
114
+          if (c == '\n') state = state_RESET;
115
+          break;
116
+
117
+        default:
118
+          if (c == '\n') {
119
+            switch (state) {
120
+              case state_M108:
121
+                wait_for_user = wait_for_heatup = false;
122
+                break;
123
+              case state_M112:
124
+                kill(PSTR(MSG_KILLED));
125
+                break;
126
+              case state_M410:
127
+                quickstop_stepper();
128
+                break;
129
+              default:
130
+                break;
131
+            }
132
+            state = state_RESET;
133
+          }
134
+      }
62 135
     }
63
-  CRITICAL_SECTION_END;
64 136
 
65
-  #if ENABLED(EMERGENCY_PARSER)
66
-    emergency_parser(c);
67 137
   #endif
68
-}
69 138
 
70
-#if TX_BUFFER_SIZE > 0
71 139
 
72
-  FORCE_INLINE void _tx_udr_empty_irq(void) {
73
-    // If interrupts are enabled, there must be more data in the output
74
-    // buffer. Send the next byte
75
-    uint8_t t = tx_buffer.tail;
76
-    uint8_t c = tx_buffer.buffer[t];
77
-    tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
140
+  FORCE_INLINE void store_char(unsigned char c) {
141
+    CRITICAL_SECTION_START;
142
+      uint8_t h = rx_buffer.head;
143
+      uint8_t i = (uint8_t)(h + 1)  & (RX_BUFFER_SIZE - 1);
144
+
145
+      // if we should be storing the received character into the location
146
+      // just before the tail (meaning that the head would advance to the
147
+      // current location of the tail), we're about to overflow the buffer
148
+      // and so we don't write the character or advance the head.
149
+      if (i != rx_buffer.tail) {
150
+        rx_buffer.buffer[h] = c;
151
+        rx_buffer.head = i;
152
+      }
153
+    CRITICAL_SECTION_END;
78 154
 
79
-    M_UDRx = c;
155
+    #if ENABLED(EMERGENCY_PARSER)
156
+      emergency_parser(c);
157
+    #endif
158
+  }
80 159
 
81
-    // clear the TXC bit -- "can be cleared by writing a one to its bit
82
-    // location". This makes sure flush() won't return until the bytes
83
-    // actually got written
84
-    SBI(M_UCSRxA, M_TXCx);
160
+  #if TX_BUFFER_SIZE > 0
85 161
 
86
-    if (tx_buffer.head == tx_buffer.tail) {
87
-      // Buffer empty, so disable interrupts
88
-      CBI(M_UCSRxB, M_UDRIEx);
89
-    }
90
-  }
162
+    FORCE_INLINE void _tx_udr_empty_irq(void) {
163
+      // If interrupts are enabled, there must be more data in the output
164
+      // buffer. Send the next byte
165
+      uint8_t t = tx_buffer.tail;
166
+      uint8_t c = tx_buffer.buffer[t];
167
+      tx_buffer.tail = (t + 1) & (TX_BUFFER_SIZE - 1);
91 168
 
92
-  #ifdef M_USARTx_UDRE_vect
93
-    ISR(M_USARTx_UDRE_vect) {
94
-      _tx_udr_empty_irq();
169
+      M_UDRx = c;
170
+
171
+      // clear the TXC bit -- "can be cleared by writing a one to its bit
172
+      // location". This makes sure flush() won't return until the bytes
173
+      // actually got written
174
+      SBI(M_UCSRxA, M_TXCx);
175
+
176
+      if (tx_buffer.head == tx_buffer.tail) {
177
+        // Buffer empty, so disable interrupts
178
+        CBI(M_UCSRxB, M_UDRIEx);
179
+      }
95 180
     }
96
-  #endif
97 181
 
98
-#endif // TX_BUFFER_SIZE
182
+    #ifdef M_USARTx_UDRE_vect
183
+      ISR(M_USARTx_UDRE_vect) {
184
+        _tx_udr_empty_irq();
185
+      }
186
+    #endif
99 187
 
100
-#ifdef M_USARTx_RX_vect
101
-  ISR(M_USARTx_RX_vect) {
102
-    unsigned char c  =  M_UDRx;
103
-    store_char(c);
104
-  }
105
-#endif
188
+  #endif // TX_BUFFER_SIZE
106 189
 
107
-// Constructors ////////////////////////////////////////////////////////////////
190
+  #ifdef M_USARTx_RX_vect
191
+    ISR(M_USARTx_RX_vect) {
192
+      unsigned char c  =  M_UDRx;
193
+      store_char(c);
194
+    }
195
+  #endif
108 196
 
109
-MarlinSerial::MarlinSerial() { }
197
+  // Public Methods
110 198
 
111
-// Public Methods //////////////////////////////////////////////////////////////
199
+  void MarlinSerial::begin(long baud) {
200
+    uint16_t baud_setting;
201
+    bool useU2X = true;
112 202
 
113
-void MarlinSerial::begin(long baud) {
114
-  uint16_t baud_setting;
115
-  bool useU2X = true;
203
+    #if F_CPU == 16000000UL && SERIAL_PORT == 0
204
+      // hard-coded exception for compatibility with the bootloader shipped
205
+      // with the Duemilanove and previous boards and the firmware on the 8U2
206
+      // on the Uno and Mega 2560.
207
+      if (baud == 57600) {
208
+        useU2X = false;
209
+      }
210
+    #endif
116 211
 
117
-  #if F_CPU == 16000000UL && SERIAL_PORT == 0
118
-    // hard-coded exception for compatibility with the bootloader shipped
119
-    // with the Duemilanove and previous boards and the firmware on the 8U2
120
-    // on the Uno and Mega 2560.
121
-    if (baud == 57600) {
122
-      useU2X = false;
212
+    if (useU2X) {
213
+      M_UCSRxA = _BV(M_U2Xx);
214
+      baud_setting = (F_CPU / 4 / baud - 1) / 2;
215
+    }
216
+    else {
217
+      M_UCSRxA = 0;
218
+      baud_setting = (F_CPU / 8 / baud - 1) / 2;
123 219
     }
124
-  #endif
125 220
 
126
-  if (useU2X) {
127
-    M_UCSRxA = _BV(M_U2Xx);
128
-    baud_setting = (F_CPU / 4 / baud - 1) / 2;
129
-  }
130
-  else {
131
-    M_UCSRxA = 0;
132
-    baud_setting = (F_CPU / 8 / baud - 1) / 2;
133
-  }
221
+    // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
222
+    M_UBRRxH = baud_setting >> 8;
223
+    M_UBRRxL = baud_setting;
134 224
 
135
-  // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
136
-  M_UBRRxH = baud_setting >> 8;
137
-  M_UBRRxL = baud_setting;
225
+    SBI(M_UCSRxB, M_RXENx);
226
+    SBI(M_UCSRxB, M_TXENx);
227
+    SBI(M_UCSRxB, M_RXCIEx);
228
+    #if TX_BUFFER_SIZE > 0
229
+      CBI(M_UCSRxB, M_UDRIEx);
230
+      _written = false;
231
+    #endif
232
+  }
138 233
 
139
-  SBI(M_UCSRxB, M_RXENx);
140
-  SBI(M_UCSRxB, M_TXENx);
141
-  SBI(M_UCSRxB, M_RXCIEx);
142
-  #if TX_BUFFER_SIZE > 0
234
+  void MarlinSerial::end() {
235
+    CBI(M_UCSRxB, M_RXENx);
236
+    CBI(M_UCSRxB, M_TXENx);
237
+    CBI(M_UCSRxB, M_RXCIEx);
143 238
     CBI(M_UCSRxB, M_UDRIEx);
144
-    _written = false;
145
-  #endif
146
-}
147
-
148
-void MarlinSerial::end() {
149
-  CBI(M_UCSRxB, M_RXENx);
150
-  CBI(M_UCSRxB, M_TXENx);
151
-  CBI(M_UCSRxB, M_RXCIEx);
152
-  CBI(M_UCSRxB, M_UDRIEx);
153
-}
154
-
155
-void MarlinSerial::checkRx(void) {
156
-  if (TEST(M_UCSRxA, M_RXCx)) {
157
-    uint8_t c  =  M_UDRx;
158
-    store_char(c);
159 239
   }
160
-}
161
-
162
-int MarlinSerial::peek(void) {
163
-  CRITICAL_SECTION_START;
164
-    int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
165
-  CRITICAL_SECTION_END;
166
-  return v;
167
-}
168
-
169
-int MarlinSerial::read(void) {
170
-  int v;
171
-  CRITICAL_SECTION_START;
172
-    uint8_t t = rx_buffer.tail;
173
-    if (rx_buffer.head == t) {
174
-      v = -1;
175
-    }
176
-    else {
177
-      v = rx_buffer.buffer[t];
178
-      rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
240
+
241
+  void MarlinSerial::checkRx(void) {
242
+    if (TEST(M_UCSRxA, M_RXCx)) {
243
+      uint8_t c  =  M_UDRx;
244
+      store_char(c);
179 245
     }
180
-  CRITICAL_SECTION_END;
181
-  return v;
182
-}
183
-
184
-uint8_t MarlinSerial::available(void) {
185
-  CRITICAL_SECTION_START;
186
-    uint8_t h = rx_buffer.head,
187
-            t = rx_buffer.tail;
188
-  CRITICAL_SECTION_END;
189
-  return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
190
-}
191
-
192
-void MarlinSerial::flush(void) {
193
-  // RX
194
-  // don't reverse this or there may be problems if the RX interrupt
195
-  // occurs after reading the value of rx_buffer_head but before writing
196
-  // the value to rx_buffer_tail; the previous value of rx_buffer_head
197
-  // may be written to rx_buffer_tail, making it appear as if the buffer
198
-  // were full, not empty.
199
-  CRITICAL_SECTION_START;
200
-    rx_buffer.head = rx_buffer.tail;
201
-  CRITICAL_SECTION_END;
202
-}
203
-
204
-#if TX_BUFFER_SIZE > 0
205
-  uint8_t MarlinSerial::availableForWrite(void) {
246
+  }
247
+
248
+  int MarlinSerial::peek(void) {
206 249
     CRITICAL_SECTION_START;
207
-      uint8_t h = tx_buffer.head;
208
-      uint8_t t = tx_buffer.tail;
250
+      int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
251
+    CRITICAL_SECTION_END;
252
+    return v;
253
+  }
254
+
255
+  int MarlinSerial::read(void) {
256
+    int v;
257
+    CRITICAL_SECTION_START;
258
+      uint8_t t = rx_buffer.tail;
259
+      if (rx_buffer.head == t) {
260
+        v = -1;
261
+      }
262
+      else {
263
+        v = rx_buffer.buffer[t];
264
+        rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
265
+      }
209 266
     CRITICAL_SECTION_END;
210
-    return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
267
+    return v;
211 268
   }
212 269
 
213
-  void MarlinSerial::write(uint8_t c) {
214
-    _written = true;
270
+  uint8_t MarlinSerial::available(void) {
215 271
     CRITICAL_SECTION_START;
216
-      bool emty = (tx_buffer.head == tx_buffer.tail);
272
+      uint8_t h = rx_buffer.head,
273
+              t = rx_buffer.tail;
217 274
     CRITICAL_SECTION_END;
218
-    // If the buffer and the data register is empty, just write the byte
219
-    // to the data register and be done. This shortcut helps
220
-    // significantly improve the effective datarate at high (>
221
-    // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
222
-    if (emty && TEST(M_UCSRxA, M_UDREx)) {
275
+    return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
276
+  }
277
+
278
+  void MarlinSerial::flush(void) {
279
+    // RX
280
+    // don't reverse this or there may be problems if the RX interrupt
281
+    // occurs after reading the value of rx_buffer_head but before writing
282
+    // the value to rx_buffer_tail; the previous value of rx_buffer_head
283
+    // may be written to rx_buffer_tail, making it appear as if the buffer
284
+    // were full, not empty.
285
+    CRITICAL_SECTION_START;
286
+      rx_buffer.head = rx_buffer.tail;
287
+    CRITICAL_SECTION_END;
288
+  }
289
+
290
+  #if TX_BUFFER_SIZE > 0
291
+    uint8_t MarlinSerial::availableForWrite(void) {
223 292
       CRITICAL_SECTION_START;
224
-        M_UDRx = c;
225
-        SBI(M_UCSRxA, M_TXCx);
293
+        uint8_t h = tx_buffer.head;
294
+        uint8_t t = tx_buffer.tail;
226 295
       CRITICAL_SECTION_END;
227
-      return;
228
-    }
229
-    uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
230
-
231
-    // If the output buffer is full, there's nothing for it other than to
232
-    // wait for the interrupt handler to empty it a bit
233
-    while (i == tx_buffer.tail) {
234
-      if (!TEST(SREG, SREG_I)) {
235
-        // Interrupts are disabled, so we'll have to poll the data
236
-        // register empty flag ourselves. If it is set, pretend an
237
-        // interrupt has happened and call the handler to free up
238
-        // space for us.
239
-        if (TEST(M_UCSRxA, M_UDREx))
240
-          _tx_udr_empty_irq();
241
-      } else {
242
-        // nop, the interrupt handler will free up space for us
243
-      }
296
+      return (uint8_t)(TX_BUFFER_SIZE + h - t) & (TX_BUFFER_SIZE - 1);
244 297
     }
245 298
 
246
-    tx_buffer.buffer[tx_buffer.head] = c;
247
-    { CRITICAL_SECTION_START;
248
-        tx_buffer.head = i;
249
-        SBI(M_UCSRxB, M_UDRIEx);
299
+    void MarlinSerial::write(uint8_t c) {
300
+      _written = true;
301
+      CRITICAL_SECTION_START;
302
+        bool emty = (tx_buffer.head == tx_buffer.tail);
250 303
       CRITICAL_SECTION_END;
251
-    }
252
-    return;
253
-  }
304
+      // If the buffer and the data register is empty, just write the byte
305
+      // to the data register and be done. This shortcut helps
306
+      // significantly improve the effective datarate at high (>
307
+      // 500kbit/s) bitrates, where interrupt overhead becomes a slowdown.
308
+      if (emty && TEST(M_UCSRxA, M_UDREx)) {
309
+        CRITICAL_SECTION_START;
310
+          M_UDRx = c;
311
+          SBI(M_UCSRxA, M_TXCx);
312
+        CRITICAL_SECTION_END;
313
+        return;
314
+      }
315
+      uint8_t i = (tx_buffer.head + 1) & (TX_BUFFER_SIZE - 1);
316
+
317
+      // If the output buffer is full, there's nothing for it other than to
318
+      // wait for the interrupt handler to empty it a bit
319
+      while (i == tx_buffer.tail) {
320
+        if (!TEST(SREG, SREG_I)) {
321
+          // Interrupts are disabled, so we'll have to poll the data
322
+          // register empty flag ourselves. If it is set, pretend an
323
+          // interrupt has happened and call the handler to free up
324
+          // space for us.
325
+          if (TEST(M_UCSRxA, M_UDREx))
326
+            _tx_udr_empty_irq();
327
+        } else {
328
+          // nop, the interrupt handler will free up space for us
329
+        }
330
+      }
254 331
 
255
-  void MarlinSerial::flushTX(void) {
256
-    // TX
257
-    // If we have never written a byte, no need to flush. This special
258
-    // case is needed since there is no way to force the TXC (transmit
259
-    // complete) bit to 1 during initialization
260
-    if (!_written)
332
+      tx_buffer.buffer[tx_buffer.head] = c;
333
+      { CRITICAL_SECTION_START;
334
+          tx_buffer.head = i;
335
+          SBI(M_UCSRxB, M_UDRIEx);
336
+        CRITICAL_SECTION_END;
337
+      }
261 338
       return;
262
-
263
-    while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
264
-      if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
265
-        // Interrupts are globally disabled, but the DR empty
266
-        // interrupt should be enabled, so poll the DR empty flag to
267
-        // prevent deadlock
268
-        if (TEST(M_UCSRxA, M_UDREx))
269
-          _tx_udr_empty_irq();
270 339
     }
271
-    // If we get here, nothing is queued anymore (DRIE is disabled) and
272
-    // the hardware finished tranmission (TXC is set).
273
-}
274
-
275
-#else
276
-  void MarlinSerial::write(uint8_t c) {
277
-    while (!TEST(M_UCSRxA, M_UDREx))
278
-      ;
279
-    M_UDRx = c;
340
+
341
+    void MarlinSerial::flushTX(void) {
342
+      // TX
343
+      // If we have never written a byte, no need to flush. This special
344
+      // case is needed since there is no way to force the TXC (transmit
345
+      // complete) bit to 1 during initialization
346
+      if (!_written)
347
+        return;
348
+
349
+      while (TEST(M_UCSRxB, M_UDRIEx) || !TEST(M_UCSRxA, M_TXCx)) {
350
+        if (!TEST(SREG, SREG_I) && TEST(M_UCSRxB, M_UDRIEx))
351
+          // Interrupts are globally disabled, but the DR empty
352
+          // interrupt should be enabled, so poll the DR empty flag to
353
+          // prevent deadlock
354
+          if (TEST(M_UCSRxA, M_UDREx))
355
+            _tx_udr_empty_irq();
356
+      }
357
+      // If we get here, nothing is queued anymore (DRIE is disabled) and
358
+      // the hardware finished tranmission (TXC is set).
280 359
   }
281
-#endif
282 360
 
283
-// end NEW
361
+  #else
362
+    void MarlinSerial::write(uint8_t c) {
363
+      while (!TEST(M_UCSRxA, M_UDREx))
364
+        ;
365
+      M_UDRx = c;
366
+    }
367
+  #endif
284 368
 
285
-/// imports from print.h
369
+  // end NEW
286 370
 
371
+  /// imports from print.h
287 372
 
288
-void MarlinSerial::print(char c, int base) {
289
-  print((long) c, base);
290
-}
291 373
 
292
-void MarlinSerial::print(unsigned char b, int base) {
293
-  print((unsigned long) b, base);
294
-}
374
+  void MarlinSerial::print(char c, int base) {
375
+    print((long) c, base);
376
+  }
295 377
 
296
-void MarlinSerial::print(int n, int base) {
297
-  print((long) n, base);
298
-}
378
+  void MarlinSerial::print(unsigned char b, int base) {
379
+    print((unsigned long) b, base);
380
+  }
299 381
 
300
-void MarlinSerial::print(unsigned int n, int base) {
301
-  print((unsigned long) n, base);
302
-}
382
+  void MarlinSerial::print(int n, int base) {
383
+    print((long) n, base);
384
+  }
303 385
 
304
-void MarlinSerial::print(long n, int base) {
305
-  if (base == 0) {
306
-    write(n);
386
+  void MarlinSerial::print(unsigned int n, int base) {
387
+    print((unsigned long) n, base);
307 388
   }
308
-  else if (base == 10) {
309
-    if (n < 0) {
310
-      print('-');
311
-      n = -n;
389
+
390
+  void MarlinSerial::print(long n, int base) {
391
+    if (base == 0) {
392
+      write(n);
393
+    }
394
+    else if (base == 10) {
395
+      if (n < 0) {
396
+        print('-');
397
+        n = -n;
398
+      }
399
+      printNumber(n, 10);
400
+    }
401
+    else {
402
+      printNumber(n, base);
312 403
     }
313
-    printNumber(n, 10);
314 404
   }
315
-  else {
316
-    printNumber(n, base);
405
+
406
+  void MarlinSerial::print(unsigned long n, int base) {
407
+    if (base == 0) write(n);
408
+    else printNumber(n, base);
317 409
   }
318
-}
319
-
320
-void MarlinSerial::print(unsigned long n, int base) {
321
-  if (base == 0) write(n);
322
-  else printNumber(n, base);
323
-}
324
-
325
-void MarlinSerial::print(double n, int digits) {
326
-  printFloat(n, digits);
327
-}
328
-
329
-void MarlinSerial::println(void) {
330
-  print('\r');
331
-  print('\n');
332
-}
333
-
334
-void MarlinSerial::println(const String& s) {
335
-  print(s);
336
-  println();
337
-}
338
-
339
-void MarlinSerial::println(const char c[]) {
340
-  print(c);
341
-  println();
342
-}
343
-
344
-void MarlinSerial::println(char c, int base) {
345
-  print(c, base);
346
-  println();
347
-}
348
-
349
-void MarlinSerial::println(unsigned char b, int base) {
350
-  print(b, base);
351
-  println();
352
-}
353
-
354
-void MarlinSerial::println(int n, int base) {
355
-  print(n, base);
356
-  println();
357
-}
358
-
359
-void MarlinSerial::println(unsigned int n, int base) {
360
-  print(n, base);
361
-  println();
362
-}
363
-
364
-void MarlinSerial::println(long n, int base) {
365
-  print(n, base);
366
-  println();
367
-}
368
-
369
-void MarlinSerial::println(unsigned long n, int base) {
370
-  print(n, base);
371
-  println();
372
-}
373
-
374
-void MarlinSerial::println(double n, int digits) {
375
-  print(n, digits);
376
-  println();
377
-}
378
-
379
-// Private Methods /////////////////////////////////////////////////////////////
380
-
381
-void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
382
-  if (n) {
383
-    unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
384
-    int8_t i = 0;
385
-    while (n) {
386
-      buf[i++] = n % base;
387
-      n /= base;
388
-    }
389
-    while (i--)
390
-      print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
410
+
411
+  void MarlinSerial::print(double n, int digits) {
412
+    printFloat(n, digits);
391 413
   }
392
-  else
393
-    print('0');
394
-}
395
-
396
-void MarlinSerial::printFloat(double number, uint8_t digits) {
397
-  // Handle negative numbers
398
-  if (number < 0.0) {
399
-    print('-');
400
-    number = -number;
414
+
415
+  void MarlinSerial::println(void) {
416
+    print('\r');
417
+    print('\n');
401 418
   }
402 419
 
403
-  // Round correctly so that print(1.999, 2) prints as "2.00"
404
-  double rounding = 0.5;
405
-  for (uint8_t i = 0; i < digits; ++i)
406
-    rounding *= 0.1;
407
-
408
-  number += rounding;
409
-
410
-  // Extract the integer part of the number and print it
411
-  unsigned long int_part = (unsigned long)number;
412
-  double remainder = number - (double)int_part;
413
-  print(int_part);
414
-
415
-  // Print the decimal point, but only if there are digits beyond
416
-  if (digits) {
417
-    print('.');
418
-    // Extract digits from the remainder one at a time
419
-    while (digits--) {
420
-      remainder *= 10.0;
421
-      int toPrint = int(remainder);
422
-      print(toPrint);
423
-      remainder -= toPrint;
424
-    }
420
+  void MarlinSerial::println(const String& s) {
421
+    print(s);
422
+    println();
425 423
   }
426
-}
427
-// Preinstantiate Objects //////////////////////////////////////////////////////
428 424
 
425
+  void MarlinSerial::println(const char c[]) {
426
+    print(c);
427
+    println();
428
+  }
429 429
 
430
-MarlinSerial customizedSerial;
430
+  void MarlinSerial::println(char c, int base) {
431
+    print(c, base);
432
+    println();
433
+  }
431 434
 
432
-#endif // whole file
433
-#endif // !USBCON
435
+  void MarlinSerial::println(unsigned char b, int base) {
436
+    print(b, base);
437
+    println();
438
+  }
434 439
 
435
-// For AT90USB targets use the UART for BT interfacing
436
-#if defined(USBCON) && ENABLED(BLUETOOTH)
437
-  HardwareSerial bluetoothSerial;
438
-#endif
440
+  void MarlinSerial::println(int n, int base) {
441
+    print(n, base);
442
+    println();
443
+  }
439 444
 
440
-#if ENABLED(EMERGENCY_PARSER)
445
+  void MarlinSerial::println(unsigned int n, int base) {
446
+    print(n, base);
447
+    println();
448
+  }
441 449
 
442
-  // Currently looking for: M108, M112, M410
443
-  // If you alter the parser please don't forget to update the capabilities in Conditionals_post.h
450
+  void MarlinSerial::println(long n, int base) {
451
+    print(n, base);
452
+    println();
453
+  }
444 454
 
445
-  FORCE_INLINE void emergency_parser(unsigned char c) {
455
+  void MarlinSerial::println(unsigned long n, int base) {
456
+    print(n, base);
457
+    println();
458
+  }
446 459
 
447
-    static e_parser_state state = state_RESET;
460
+  void MarlinSerial::println(double n, int digits) {
461
+    print(n, digits);
462
+    println();
463
+  }
448 464
 
449
-    switch (state) {
450
-      case state_RESET:
451
-        switch (c) {
452
-          case ' ': break;
453
-          case 'N': state = state_N;      break;
454
-          case 'M': state = state_M;      break;
455
-          default: state = state_IGNORE;
456
-        }
457
-        break;
458
-
459
-      case state_N:
460
-        switch (c) {
461
-          case '0': case '1': case '2':
462
-          case '3': case '4': case '5':
463
-          case '6': case '7': case '8':
464
-          case '9': case '-': case ' ':   break;
465
-          case 'M': state = state_M;      break;
466
-          default:  state = state_IGNORE;
467
-        }
468
-        break;
469
-
470
-      case state_M:
471
-        switch (c) {
472
-          case ' ': break;
473
-          case '1': state = state_M1;     break;
474
-          case '4': state = state_M4;     break;
475
-          default: state = state_IGNORE;
476
-        }
477
-        break;
465
+  // Private Methods
478 466
 
479
-      case state_M1:
480
-        switch (c) {
481
-          case '0': state = state_M10;    break;
482
-          case '1': state = state_M11;    break;
483
-          default: state = state_IGNORE;
484
-        }
485
-        break;
486
-
487
-      case state_M10:
488
-        state = (c == '8') ? state_M108 : state_IGNORE;
489
-        break;
490
-
491
-      case state_M11:
492
-        state = (c == '2') ? state_M112 : state_IGNORE;
493
-        break;
494
-
495
-      case state_M4:
496
-        state = (c == '1') ? state_M41 : state_IGNORE;
497
-        break;
498
-
499
-      case state_M41:
500
-        state = (c == '0') ? state_M410 : state_IGNORE;
501
-        break;
502
-
503
-      case state_IGNORE:
504
-        if (c == '\n') state = state_RESET;
505
-        break;
506
-
507
-      default:
508
-        if (c == '\n') {
509
-          switch (state) {
510
-            case state_M108:
511
-              wait_for_user = wait_for_heatup = false;
512
-              break;
513
-            case state_M112:
514
-              kill(PSTR(MSG_KILLED));
515
-              break;
516
-            case state_M410:
517
-              quickstop_stepper();
518
-              break;
519
-            default:
520
-              break;
521
-          }
522
-          state = state_RESET;
523
-        }
467
+  void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
468
+    if (n) {
469
+      unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
470
+      int8_t i = 0;
471
+      while (n) {
472
+        buf[i++] = n % base;
473
+        n /= base;
474
+      }
475
+      while (i--)
476
+        print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
524 477
     }
478
+    else
479
+      print('0');
525 480
   }
526 481
 
482
+  void MarlinSerial::printFloat(double number, uint8_t digits) {
483
+    // Handle negative numbers
484
+    if (number < 0.0) {
485
+      print('-');
486
+      number = -number;
487
+    }
488
+
489
+    // Round correctly so that print(1.999, 2) prints as "2.00"
490
+    double rounding = 0.5;
491
+    for (uint8_t i = 0; i < digits; ++i)
492
+      rounding *= 0.1;
493
+
494
+    number += rounding;
495
+
496
+    // Extract the integer part of the number and print it
497
+    unsigned long int_part = (unsigned long)number;
498
+    double remainder = number - (double)int_part;
499
+    print(int_part);
500
+
501
+    // Print the decimal point, but only if there are digits beyond
502
+    if (digits) {
503
+      print('.');
504
+      // Extract digits from the remainder one at a time
505
+      while (digits--) {
506
+        remainder *= 10.0;
507
+        int toPrint = int(remainder);
508
+        print(toPrint);
509
+        remainder -= toPrint;
510
+      }
511
+    }
512
+  }
513
+
514
+  // Preinstantiate
515
+  MarlinSerial customizedSerial;
516
+
517
+#endif // !USBCON && (UBRRH || UBRR0H || UBRR1H || UBRR2H || UBRR3H)
518
+
519
+// For AT90USB targets use the UART for BT interfacing
520
+#if defined(USBCON) && ENABLED(BLUETOOTH)
521
+  HardwareSerial bluetoothSerial;
527 522
 #endif

+ 95
- 102
Marlin/MarlinSerial.h 查看文件

@@ -29,8 +29,8 @@
29 29
 
30 30
 */
31 31
 
32
-#ifndef MarlinSerial_h
33
-#define MarlinSerial_h
32
+#ifndef MARLINSERIAL_H
33
+#define MARLINSERIAL_H
34 34
 
35 35
 #include "MarlinConfig.h"
36 36
 
@@ -52,125 +52,118 @@
52 52
   #define SERIAL_REGNAME_INTERNAL(registerbase,number,suffix) registerbase##number##suffix
53 53
 #endif
54 54
 
55
-// Registers used by MarlinSerial class (these are expanded
56
-// depending on selected serial port
57
-#define M_UCSRxA SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
58
-#define M_UCSRxB SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
59
-#define M_RXENx SERIAL_REGNAME(RXEN,SERIAL_PORT,)
60
-#define M_TXENx SERIAL_REGNAME(TXEN,SERIAL_PORT,)
61
-#define M_TXCx SERIAL_REGNAME(TXC,SERIAL_PORT,)
62
-#define M_RXCIEx SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
63
-#define M_UDREx SERIAL_REGNAME(UDRE,SERIAL_PORT,)
64
-#define M_UDRIEx SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
65
-#define M_UDRx SERIAL_REGNAME(UDR,SERIAL_PORT,)
66
-#define M_UBRRxH SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
67
-#define M_UBRRxL SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
68
-#define M_RXCx SERIAL_REGNAME(RXC,SERIAL_PORT,)
69
-#define M_USARTx_RX_vect SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
70
-#define M_U2Xx SERIAL_REGNAME(U2X,SERIAL_PORT,)
55
+// Registers used by MarlinSerial class (expanded depending on selected serial port)
56
+#define M_UCSRxA           SERIAL_REGNAME(UCSR,SERIAL_PORT,A) // defines M_UCSRxA to be UCSRnA where n is the serial port number
57
+#define M_UCSRxB           SERIAL_REGNAME(UCSR,SERIAL_PORT,B)
58
+#define M_RXENx            SERIAL_REGNAME(RXEN,SERIAL_PORT,)
59
+#define M_TXENx            SERIAL_REGNAME(TXEN,SERIAL_PORT,)
60
+#define M_TXCx             SERIAL_REGNAME(TXC,SERIAL_PORT,)
61
+#define M_RXCIEx           SERIAL_REGNAME(RXCIE,SERIAL_PORT,)
62
+#define M_UDREx            SERIAL_REGNAME(UDRE,SERIAL_PORT,)
63
+#define M_UDRIEx           SERIAL_REGNAME(UDRIE,SERIAL_PORT,)
64
+#define M_UDRx             SERIAL_REGNAME(UDR,SERIAL_PORT,)
65
+#define M_UBRRxH           SERIAL_REGNAME(UBRR,SERIAL_PORT,H)
66
+#define M_UBRRxL           SERIAL_REGNAME(UBRR,SERIAL_PORT,L)
67
+#define M_RXCx             SERIAL_REGNAME(RXC,SERIAL_PORT,)
68
+#define M_USARTx_RX_vect   SERIAL_REGNAME(USART,SERIAL_PORT,_RX_vect)
69
+#define M_U2Xx             SERIAL_REGNAME(U2X,SERIAL_PORT,)
71 70
 #define M_USARTx_UDRE_vect SERIAL_REGNAME(USART,SERIAL_PORT,_UDRE_vect)
72 71
 
73
-
74 72
 #define DEC 10
75 73
 #define HEX 16
76 74
 #define OCT 8
77 75
 #define BIN 2
78 76
 #define BYTE 0
79 77
 
80
-
81 78
 #ifndef USBCON
82
-// Define constants and variables for buffering incoming serial data.  We're
83
-// using a ring buffer (I think), in which rx_buffer_head is the index of the
84
-// location to which to write the next incoming character and rx_buffer_tail
85
-// is the index of the location from which to read.
86
-// 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
87
-#ifndef RX_BUFFER_SIZE
88
-  #define RX_BUFFER_SIZE 128
89
-#endif
90
-#ifndef TX_BUFFER_SIZE
91
-  #define TX_BUFFER_SIZE 32
92
-#endif
93
-#if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2))
94
-  #error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
95
-#endif
96
-#if !((TX_BUFFER_SIZE == 256) ||(TX_BUFFER_SIZE == 128) ||(TX_BUFFER_SIZE == 64) ||(TX_BUFFER_SIZE == 32) ||(TX_BUFFER_SIZE == 16) ||(TX_BUFFER_SIZE == 8) ||(TX_BUFFER_SIZE == 4) ||(TX_BUFFER_SIZE == 2) ||(TX_BUFFER_SIZE == 0))
97
-  #error TX_BUFFER_SIZE has to be a power of 2 or 0
98
-#endif
99
-
100
-struct ring_buffer_r {
101
-  unsigned char buffer[RX_BUFFER_SIZE];
102
-  volatile uint8_t head;
103
-  volatile uint8_t tail;
104
-};
79
+  // Define constants and variables for buffering incoming serial data.  We're
80
+  // using a ring buffer (I think), in which rx_buffer_head is the index of the
81
+  // location to which to write the next incoming character and rx_buffer_tail
82
+  // is the index of the location from which to read.
83
+  // 256 is the max limit due to uint8_t head and tail. Use only powers of 2. (...,16,32,64,128,256)
84
+  #ifndef RX_BUFFER_SIZE
85
+    #define RX_BUFFER_SIZE 128
86
+  #endif
87
+  #ifndef TX_BUFFER_SIZE
88
+    #define TX_BUFFER_SIZE 32
89
+  #endif
90
+  #if !((RX_BUFFER_SIZE == 256) ||(RX_BUFFER_SIZE == 128) ||(RX_BUFFER_SIZE == 64) ||(RX_BUFFER_SIZE == 32) ||(RX_BUFFER_SIZE == 16) ||(RX_BUFFER_SIZE == 8) ||(RX_BUFFER_SIZE == 4) ||(RX_BUFFER_SIZE == 2))
91
+    #error "RX_BUFFER_SIZE has to be a power of 2 and >= 2"
92
+  #endif
93
+  #if !((TX_BUFFER_SIZE == 256) ||(TX_BUFFER_SIZE == 128) ||(TX_BUFFER_SIZE == 64) ||(TX_BUFFER_SIZE == 32) ||(TX_BUFFER_SIZE == 16) ||(TX_BUFFER_SIZE == 8) ||(TX_BUFFER_SIZE == 4) ||(TX_BUFFER_SIZE == 2) ||(TX_BUFFER_SIZE == 0))
94
+    #error TX_BUFFER_SIZE has to be a power of 2 or 0
95
+  #endif
105 96
 
106
-#if TX_BUFFER_SIZE > 0
107
-  struct ring_buffer_t {
108
-    unsigned char buffer[TX_BUFFER_SIZE];
97
+  struct ring_buffer_r {
98
+    unsigned char buffer[RX_BUFFER_SIZE];
109 99
     volatile uint8_t head;
110 100
     volatile uint8_t tail;
111 101
   };
112
-#endif
113 102
 
114
-#if UART_PRESENT(SERIAL_PORT)
115
-  extern ring_buffer_r rx_buffer;
116 103
   #if TX_BUFFER_SIZE > 0
117
-    extern ring_buffer_t tx_buffer;
104
+    struct ring_buffer_t {
105
+      unsigned char buffer[TX_BUFFER_SIZE];
106
+      volatile uint8_t head;
107
+      volatile uint8_t tail;
108
+    };
118 109
   #endif
119
-#endif
120
-
121
-#if ENABLED(EMERGENCY_PARSER)
122
-  #include "language.h"
123
-  void emergency_parser(unsigned char c);
124
-#endif
125 110
 
126
-class MarlinSerial { //: public Stream
127
-
128
-  public:
129
-    MarlinSerial();
130
-    static void begin(long);
131
-    static void end();
132
-    static int peek(void);
133
-    static int read(void);
134
-    static void flush(void);
135
-    static uint8_t available(void);
136
-    static void checkRx(void);
137
-    static void write(uint8_t c);
111
+  #if UART_PRESENT(SERIAL_PORT)
112
+    extern ring_buffer_r rx_buffer;
138 113
     #if TX_BUFFER_SIZE > 0
139
-      static uint8_t availableForWrite(void);
140
-      static void flushTX(void);
114
+      extern ring_buffer_t tx_buffer;
141 115
     #endif
116
+  #endif
117
+
118
+  class MarlinSerial { //: public Stream
119
+
120
+    public:
121
+      MarlinSerial() {};
122
+      static void begin(long);
123
+      static void end();
124
+      static int peek(void);
125
+      static int read(void);
126
+      static void flush(void);
127
+      static uint8_t available(void);
128
+      static void checkRx(void);
129
+      static void write(uint8_t c);
130
+      #if TX_BUFFER_SIZE > 0
131
+        static uint8_t availableForWrite(void);
132
+        static void flushTX(void);
133
+      #endif
134
+
135
+    private:
136
+      static void printNumber(unsigned long, uint8_t);
137
+      static void printFloat(double, uint8_t);
138
+
139
+    public:
140
+      static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
141
+      static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
142
+      static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
143
+      static FORCE_INLINE void print(const char* str) { write(str); }
144
+
145
+      static void print(char, int = BYTE);
146
+      static void print(unsigned char, int = BYTE);
147
+      static void print(int, int = DEC);
148
+      static void print(unsigned int, int = DEC);
149
+      static void print(long, int = DEC);
150
+      static void print(unsigned long, int = DEC);
151
+      static void print(double, int = 2);
152
+
153
+      static void println(const String& s);
154
+      static void println(const char[]);
155
+      static void println(char, int = BYTE);
156
+      static void println(unsigned char, int = BYTE);
157
+      static void println(int, int = DEC);
158
+      static void println(unsigned int, int = DEC);
159
+      static void println(long, int = DEC);
160
+      static void println(unsigned long, int = DEC);
161
+      static void println(double, int = 2);
162
+      static void println(void);
163
+  };
164
+
165
+  extern MarlinSerial customizedSerial;
142 166
 
143
-  private:
144
-    static void printNumber(unsigned long, uint8_t);
145
-    static void printFloat(double, uint8_t);
146
-
147
-  public:
148
-    static FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
149
-    static FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
150
-    static FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
151
-    static FORCE_INLINE void print(const char* str) { write(str); }
152
-
153
-    static void print(char, int = BYTE);
154
-    static void print(unsigned char, int = BYTE);
155
-    static void print(int, int = DEC);
156
-    static void print(unsigned int, int = DEC);
157
-    static void print(long, int = DEC);
158
-    static void print(unsigned long, int = DEC);
159
-    static void print(double, int = 2);
160
-
161
-    static void println(const String& s);
162
-    static void println(const char[]);
163
-    static void println(char, int = BYTE);
164
-    static void println(unsigned char, int = BYTE);
165
-    static void println(int, int = DEC);
166
-    static void println(unsigned int, int = DEC);
167
-    static void println(long, int = DEC);
168
-    static void println(unsigned long, int = DEC);
169
-    static void println(double, int = 2);
170
-    static void println(void);
171
-};
172
-
173
-extern MarlinSerial customizedSerial;
174 167
 #endif // !USBCON
175 168
 
176 169
 // Use the UART for Bluetooth in AT90USB configurations
@@ -178,4 +171,4 @@ extern MarlinSerial customizedSerial;
178 171
   extern HardwareSerial bluetoothSerial;
179 172
 #endif
180 173
 
181
-#endif
174
+#endif // MARLINSERIAL_H

Loading…
取消
儲存