Browse Source

Merge pull request #6202 from thinkyhead/rc_fix_broken_ubl

Patching up UBL, vetting recent commits
Scott Lahteine 7 years ago
parent
commit
34b23ff312

+ 18
- 19
Marlin/G26_Mesh_Validation_Tool.cpp View File

@@ -128,7 +128,7 @@
128 128
   extern bool code_value_bool();
129 129
   extern bool code_has_value();
130 130
   extern void lcd_init();
131
-  extern void lcd_setstatuspgm(const char* const message, uint8_t level);
131
+  extern void lcd_setstatuspgm(const char* const message, const uint8_t level);
132 132
   #define PLANNER_XY_FEEDRATE() (min(planner.max_feedrate_mm_s[X_AXIS], planner.max_feedrate_mm_s[Y_AXIS])) //bob
133 133
   bool prepare_move_to_destination_cartesian();
134 134
   void line_to_destination();
@@ -156,7 +156,7 @@
156 156
                               // won't leave us in a bad state.
157 157
 
158 158
   float valid_trig_angle(float);
159
-  mesh_index_pair find_closest_circle_to_print(float, float);
159
+  mesh_index_pair find_closest_circle_to_print(const float&, const float&);
160 160
 
161 161
   static float extrusion_multiplier = EXTRUSION_MULTIPLIER,
162 162
                retraction_multiplier = RETRACTION_MULTIPLIER,
@@ -183,8 +183,8 @@
183 183
     int   i, xi, yi;
184 184
     mesh_index_pair location;
185 185
 
186
-    // Don't allow Mesh Validation without homing first
187
-    // If the paramter parsing did not go OK, we abort the command
186
+    // Don't allow Mesh Validation without homing first,
187
+    // or if the parameter parsing did not go OK, abort
188 188
     if (axis_unhomed_error(true, true, true) || parse_G26_parameters()) return;
189 189
 
190 190
     if (current_position[Z_AXIS] < Z_CLEARANCE_BETWEEN_PROBES) {
@@ -391,8 +391,8 @@
391 391
     return d;
392 392
   }
393 393
 
394
-  mesh_index_pair find_closest_circle_to_print( float X, float Y) {
395
-    float f, mx, my, dx, dy, closest = 99999.99;
394
+  mesh_index_pair find_closest_circle_to_print(const float &X, const float &Y) {
395
+    float closest = 99999.99;
396 396
     mesh_index_pair return_val;
397 397
 
398 398
     return_val.x_index = return_val.y_index = -1;
@@ -400,28 +400,27 @@
400 400
     for (uint8_t i = 0; i < UBL_MESH_NUM_X_POINTS; i++) {
401 401
       for (uint8_t j = 0; j < UBL_MESH_NUM_Y_POINTS; j++) {
402 402
         if (!is_bit_set(circle_flags, i, j)) {
403
-          mx = ubl.mesh_index_to_xpos[i];  // We found a circle that needs to be printed
404
-          my = ubl.mesh_index_to_ypos[j];
403
+          const float mx = ubl.mesh_index_to_xpos[i],  // We found a circle that needs to be printed
404
+                      my = ubl.mesh_index_to_ypos[j];
405 405
 
406
-          dx = X - mx;        // Get the distance to this intersection
407
-          dy = Y - my;
408
-          f = HYPOT(dx, dy);
406
+          // Get the distance to this intersection
407
+          float f = HYPOT(X - mx, Y - my);
409 408
 
410
-          dx = x_pos - mx;                  // It is possible that we are being called with the values
411
-          dy = y_pos - my;                  // to let us find the closest circle to the start position.
412
-          f += HYPOT(dx, dy) / 15.0;        // But if this is not the case,
413
-                                            // we are going to add in a small
414
-                                            // weighting to the distance calculation to help it choose
415
-                                            // a better place to continue.
409
+          // It is possible that we are being called with the values
410
+          // to let us find the closest circle to the start position.
411
+          // But if this is not the case, add a small weighting to the
412
+          // distance calculation to help it choose a better place to continue.
413
+          f += HYPOT(x_pos - mx, y_pos - my) / 15.0;
416 414
 
415
+          // Add in the specified amount of Random Noise to our search
417 416
           if (random_deviation > 1.0)
418
-            f += random(0.0, random_deviation); // Add in the specified amount of Random Noise to our search
417
+            f += random(0.0, random_deviation);
419 418
 
420 419
           if (f < closest) {
421 420
             closest = f;              // We found a closer location that is still
422 421
             return_val.x_index = i;   // un-printed  --- save the data for it
423 422
             return_val.y_index = j;
424
-            return_val.distance= closest;
423
+            return_val.distance = closest;
425 424
           }
426 425
         }
427 426
       }

+ 1
- 61
Marlin/Marlin.h View File

@@ -39,19 +39,7 @@
39 39
 #include "types.h"
40 40
 #include "fastio.h"
41 41
 #include "utility.h"
42
-
43
-#ifdef USBCON
44
-  #include "HardwareSerial.h"
45
-  #if ENABLED(BLUETOOTH)
46
-    #define MYSERIAL bluetoothSerial
47
-  #else
48
-    #define MYSERIAL Serial
49
-  #endif // BLUETOOTH
50
-#else
51
-  #include "MarlinSerial.h"
52
-  #define MYSERIAL customizedSerial
53
-#endif
54
-
42
+#include "serial.h"
55 43
 #include "WString.h"
56 44
 
57 45
 #if ENABLED(PRINTCOUNTER)
@@ -60,54 +48,6 @@
60 48
   #include "stopwatch.h"
61 49
 #endif
62 50
 
63
-extern const char echomagic[] PROGMEM;
64
-extern const char errormagic[] PROGMEM;
65
-
66
-#define SERIAL_CHAR(x) (MYSERIAL.write(x))
67
-#define SERIAL_EOL SERIAL_CHAR('\n')
68
-
69
-#define SERIAL_PROTOCOLCHAR(x)              SERIAL_CHAR(x)
70
-#define SERIAL_PROTOCOL(x)                  (MYSERIAL.print(x))
71
-#define SERIAL_PROTOCOL_F(x,y)              (MYSERIAL.print(x,y))
72
-#define SERIAL_PROTOCOLPGM(x)               (serialprintPGM(PSTR(x)))
73
-#define SERIAL_PROTOCOLLN(x)                do{ MYSERIAL.print(x); SERIAL_EOL; }while(0)
74
-#define SERIAL_PROTOCOLLNPGM(x)             (serialprintPGM(PSTR(x "\n")))
75
-#define SERIAL_PROTOCOLPAIR(name, value)    (serial_echopair_P(PSTR(name),(value)))
76
-#define SERIAL_PROTOCOLLNPAIR(name, value)  do{ SERIAL_PROTOCOLPAIR(name, value); SERIAL_EOL; }while(0)
77
-
78
-#define SERIAL_ECHO_START             (serialprintPGM(echomagic))
79
-#define SERIAL_ECHO(x)                 SERIAL_PROTOCOL(x)
80
-#define SERIAL_ECHOPGM(x)              SERIAL_PROTOCOLPGM(x)
81
-#define SERIAL_ECHOLN(x)               SERIAL_PROTOCOLLN(x)
82
-#define SERIAL_ECHOLNPGM(x)            SERIAL_PROTOCOLLNPGM(x)
83
-#define SERIAL_ECHOPAIR(name,value)    SERIAL_PROTOCOLPAIR(name, value)
84
-#define SERIAL_ECHOLNPAIR(name, value) SERIAL_PROTOCOLLNPAIR(name, value)
85
-#define SERIAL_ECHO_F(x,y)             SERIAL_PROTOCOL_F(x,y)
86
-
87
-#define SERIAL_ERROR_START            (serialprintPGM(errormagic))
88
-#define SERIAL_ERROR(x)                SERIAL_PROTOCOL(x)
89
-#define SERIAL_ERRORPGM(x)             SERIAL_PROTOCOLPGM(x)
90
-#define SERIAL_ERRORLN(x)              SERIAL_PROTOCOLLN(x)
91
-#define SERIAL_ERRORLNPGM(x)           SERIAL_PROTOCOLLNPGM(x)
92
-
93
-void serial_echopair_P(const char* s_P, const char *v);
94
-void serial_echopair_P(const char* s_P, char v);
95
-void serial_echopair_P(const char* s_P, int v);
96
-void serial_echopair_P(const char* s_P, long v);
97
-void serial_echopair_P(const char* s_P, float v);
98
-void serial_echopair_P(const char* s_P, double v);
99
-void serial_echopair_P(const char* s_P, unsigned int v);
100
-void serial_echopair_P(const char* s_P, unsigned long v);
101
-FORCE_INLINE void serial_echopair_P(const char* s_P, uint8_t v) { serial_echopair_P(s_P, (int)v); }
102
-FORCE_INLINE void serial_echopair_P(const char* s_P, uint16_t v) { serial_echopair_P(s_P, (int)v); }
103
-FORCE_INLINE void serial_echopair_P(const char* s_P, bool v) { serial_echopair_P(s_P, (int)v); }
104
-FORCE_INLINE void serial_echopair_P(const char* s_P, void *v) { serial_echopair_P(s_P, (unsigned long)v); }
105
-
106
-// Things to write to serial from Program memory. Saves 400 to 2k of RAM.
107
-FORCE_INLINE void serialprintPGM(const char* str) {
108
-  while (char ch = pgm_read_byte(str++)) MYSERIAL.write(ch);
109
-}
110
-
111 51
 void idle(
112 52
   #if ENABLED(FILAMENT_CHANGE_FEATURE)
113 53
     bool no_stepper_sleep = false  // pass true to keep steppers from disabling on timeout

+ 413
- 418
Marlin/MarlinSerial.cpp View File

@@ -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 View File

@@ -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

+ 7
- 23
Marlin/Marlin_main.cpp View File

@@ -447,8 +447,6 @@ volatile bool wait_for_heatup = true;
447 447
   volatile bool wait_for_user = false;
448 448
 #endif
449 449
 
450
-const char errormagic[] PROGMEM = "Error:";
451
-const char echomagic[] PROGMEM = "echo:";
452 450
 const char axis_codes[XYZE] = {'X', 'Y', 'Z', 'E'};
453 451
 
454 452
 // Number of characters read in the current line of serial input
@@ -696,14 +694,6 @@ void set_current_from_steppers_for_axis(const AxisEnum axis);
696 694
   void plan_cubic_move(const float offset[4]);
697 695
 #endif
698 696
 
699
-void serial_echopair_P(const char* s_P, const char *v)   { serialprintPGM(s_P); SERIAL_ECHO(v); }
700
-void serial_echopair_P(const char* s_P, char v)          { serialprintPGM(s_P); SERIAL_CHAR(v); }
701
-void serial_echopair_P(const char* s_P, int v)           { serialprintPGM(s_P); SERIAL_ECHO(v); }
702
-void serial_echopair_P(const char* s_P, long v)          { serialprintPGM(s_P); SERIAL_ECHO(v); }
703
-void serial_echopair_P(const char* s_P, float v)         { serialprintPGM(s_P); SERIAL_ECHO(v); }
704
-void serial_echopair_P(const char* s_P, double v)        { serialprintPGM(s_P); SERIAL_ECHO(v); }
705
-void serial_echopair_P(const char* s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); }
706
-
707 697
 void tool_change(const uint8_t tmp_extruder, const float fr_mm_s=0.0, bool no_move=false);
708 698
 static void report_current_position();
709 699
 
@@ -1789,15 +1779,10 @@ static void clean_up_after_endstop_or_probe_move() {
1789 1779
       SERIAL_ECHOLNPGM(" " MSG_FIRST);
1790 1780
 
1791 1781
       #if ENABLED(ULTRA_LCD)
1792
-        char message[3 * (LCD_WIDTH) + 1] = ""; // worst case is kana.utf with up to 3*LCD_WIDTH+1
1793
-        strcat_P(message, PSTR(MSG_HOME " "));
1794
-        if (xx) strcat_P(message, PSTR(MSG_X));
1795
-        if (yy) strcat_P(message, PSTR(MSG_Y));
1796
-        if (zz) strcat_P(message, PSTR(MSG_Z));
1797
-        strcat_P(message, PSTR(" " MSG_FIRST));
1798
-        lcd_setstatus(message);
1782
+        lcd_status_printf_P(0, PSTR(MSG_HOME " %s%s%s " MSG_FIRST), xx ? MSG_X : "", yy ? MSG_Y : "", zz ? MSG_Z : "");
1799 1783
       #endif
1800 1784
       return true;
1785
+
1801 1786
     }
1802 1787
     return false;
1803 1788
   }
@@ -5153,7 +5138,6 @@ inline void gcode_M31() {
5153 5138
   char buffer[21];
5154 5139
   duration_t elapsed = print_job_timer.duration();
5155 5140
   elapsed.toString(buffer);
5156
-
5157 5141
   lcd_setstatus(buffer);
5158 5142
 
5159 5143
   SERIAL_ECHO_START;
@@ -5700,7 +5684,7 @@ inline void gcode_M104() {
5700 5684
       }
5701 5685
     #endif
5702 5686
 
5703
-    if (code_value_temp_abs() > thermalManager.degHotend(target_extruder)) status_printf(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING);
5687
+    if (code_value_temp_abs() > thermalManager.degHotend(target_extruder)) lcd_status_printf_P(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING);
5704 5688
   }
5705 5689
 
5706 5690
   #if ENABLED(AUTOTEMP)
@@ -5898,7 +5882,7 @@ inline void gcode_M109() {
5898 5882
       else print_job_timer.start();
5899 5883
     #endif
5900 5884
 
5901
-    if (thermalManager.isHeatingHotend(target_extruder)) status_printf(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING);
5885
+    if (thermalManager.isHeatingHotend(target_extruder)) lcd_status_printf_P(0, PSTR("E%i %s"), target_extruder + 1, MSG_HEATING);
5902 5886
   }
5903 5887
 
5904 5888
   #if ENABLED(AUTOTEMP)
@@ -8903,7 +8887,7 @@ void process_next_command() {
8903 8887
         gcode_G28();
8904 8888
         break;
8905 8889
 
8906
-      #if PLANNER_LEVELING && !ENABLED(AUTO_BED_LEVELING_UBL) || ENABLED(AUTO_BED_LEVELING_UBL) && ENABLED(UBL_G26_MESH_EDITING)
8890
+      #if PLANNER_LEVELING || ENABLED(AUTO_BED_LEVELING_UBL)
8907 8891
         case 29: // G29 Detailed Z probe, probes the bed at 3 or more points,
8908 8892
                  // or provides access to the UBL System if enabled.
8909 8893
           gcode_G29();
@@ -10175,6 +10159,8 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) {
10175 10159
   /**
10176 10160
    * Prepare a linear move in a Cartesian setup.
10177 10161
    * If Mesh Bed Leveling is enabled, perform a mesh move.
10162
+   *
10163
+   * Returns true if the caller didn't update current_position.
10178 10164
    */
10179 10165
   inline bool prepare_move_to_destination_cartesian() {
10180 10166
     // Do not use feedrate_percentage for E or Z only moves
@@ -10190,9 +10176,7 @@ void set_current_from_steppers_for_axis(const AxisEnum axis) {
10190 10176
         else
10191 10177
       #elif ENABLED(AUTO_BED_LEVELING_UBL)
10192 10178
         if (ubl.state.active) {
10193
-
10194 10179
           ubl_line_to_destination(MMS_SCALED(feedrate_mm_s), active_extruder);
10195
-
10196 10180
           return false;
10197 10181
         }
10198 10182
         else

+ 12
- 18
Marlin/UBL.h View File

@@ -29,6 +29,7 @@
29 29
 
30 30
   #if ENABLED(AUTO_BED_LEVELING_UBL)
31 31
 
32
+    #define UBL_VERSION "1.00"
32 33
     #define UBL_OK false
33 34
     #define UBL_ERR true
34 35
 
@@ -98,9 +99,6 @@
98 99
         float g29_correction_fade_height = 10.0,
99 100
               g29_fade_height_multiplier = 1.0 / 10.0; // It's cheaper to do a floating point multiply than divide,
100 101
                                                        // so keep this value and its reciprocal.
101
-      #else
102
-        const float g29_correction_fade_height = 10.0,
103
-                    g29_fade_height_multiplier = 1.0 / 10.0;
104 102
       #endif
105 103
 
106 104
       // If you change this struct, adjust TOTAL_STRUCT_SIZE
@@ -118,8 +116,7 @@
118 116
     class unified_bed_leveling {
119 117
       private:
120 118
 
121
-        static float last_specified_z,
122
-                     fade_scaling_factor_for_current_height;
119
+        static float last_specified_z;
123 120
 
124 121
       public:
125 122
 
@@ -307,32 +304,29 @@
307 304
         }
308 305
 
309 306
         /**
310
-         * This routine is used to scale the Z correction depending upon the current nozzle height. It is
311
-         * optimized for speed. It avoids floating point operations by checking if the requested scaling
312
-         * factor is going to be the same as the last time the function calculated a value. If so, it just
313
-         * returns it.
307
+         * This function sets the Z leveling fade factor based on the given Z height,
308
+         * only re-calculating when necessary.
314 309
          *
315
-         * It returns a scaling factor of 1.0 if UBL is inactive.
316
-         * It returns a scaling factor of 0.0 if Z is past the specified 'Fade Height'
310
+         *  Returns 1.0 if g29_correction_fade_height is 0.0.
311
+         *  Returns 0.0 if Z is past the specified 'Fade Height'.
317 312
          */
318 313
         #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
319 314
 
320 315
           static FORCE_INLINE float fade_scaling_factor_for_z(const float &lz) {
316
+            if (state.g29_correction_fade_height == 0.0) return 1.0;
317
+
318
+            static float fade_scaling_factor = 1.0;
321 319
             const float rz = RAW_Z_POSITION(lz);
322 320
             if (last_specified_z != rz) {
323 321
               last_specified_z = rz;
324
-              fade_scaling_factor_for_current_height =
325
-                state.active && rz < state.g29_correction_fade_height
322
+              fade_scaling_factor =
323
+                rz < state.g29_correction_fade_height
326 324
                   ? 1.0 - (rz * state.g29_fade_height_multiplier)
327 325
                   : 0.0;
328 326
             }
329
-            return fade_scaling_factor_for_current_height;
327
+            return fade_scaling_factor;
330 328
           }
331 329
 
332
-        #else
333
-
334
-          static constexpr float fade_scaling_factor_for_z(const float &lz) { UNUSED(lz); return 1.0; }
335
-
336 330
         #endif
337 331
 
338 332
     }; // class unified_bed_leveling

+ 3
- 4
Marlin/UBL_Bed_Leveling.cpp View File

@@ -61,7 +61,6 @@
61 61
 
62 62
   float unified_bed_leveling::z_values[UBL_MESH_NUM_X_POINTS][UBL_MESH_NUM_Y_POINTS],
63 63
         unified_bed_leveling::last_specified_z,
64
-        unified_bed_leveling::fade_scaling_factor_for_current_height,
65 64
         unified_bed_leveling::mesh_index_to_xpos[UBL_MESH_NUM_X_POINTS + 1], // +1 safety margin for now, until determinism prevails
66 65
         unified_bed_leveling::mesh_index_to_ypos[UBL_MESH_NUM_Y_POINTS + 1];
67 66
 
@@ -102,8 +101,9 @@
102 101
        * updated, but until then, we try to ease the transition
103 102
        * for our Beta testers.
104 103
        */
105
-      if (ubl.state.g29_fade_height_multiplier != 1.0 / ubl.state.g29_correction_fade_height) {
106
-        ubl.state.g29_fade_height_multiplier = 1.0 / ubl.state.g29_correction_fade_height;
104
+      const float recip = ubl.state.g29_correction_fade_height ? 1.0 / ubl.state.g29_correction_fade_height : 1.0;
105
+      if (ubl.state.g29_fade_height_multiplier != recip) {
106
+        ubl.state.g29_fade_height_multiplier = recip;
107 107
         store_state();
108 108
       }
109 109
     #endif
@@ -160,7 +160,6 @@
160 160
     ZERO(z_values);
161 161
 
162 162
     last_specified_z = -999.9;
163
-    fade_scaling_factor_for_current_height = 0.0;
164 163
   }
165 164
 
166 165
   void unified_bed_leveling::invalidate() {

+ 14
- 13
Marlin/UBL_G29.cpp View File

@@ -307,7 +307,8 @@
307 307
   static float x_pos, y_pos, measured_z, card_thickness = 0.0, ubl_constant = 0.0;
308 308
 
309 309
   #if ENABLED(ULTRA_LCD)
310
-    void lcd_setstatus(const char* message, bool persist);
310
+    extern void lcd_setstatus(const char* message, const bool persist);
311
+    extern void lcd_setstatuspgm(const char* message, const uint8_t level);
311 312
   #endif
312 313
 
313 314
   void gcode_G29() {
@@ -655,7 +656,7 @@
655 656
           if (ELAPSED(millis(), nxt)) {
656 657
             SERIAL_PROTOCOLLNPGM("\nZ-Offset Adjustment Stopped.");
657 658
             do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
658
-            lcd_setstatus("Z-Offset Stopped", true);
659
+            lcd_setstatuspgm("Z-Offset Stopped");
659 660
             restore_ubl_active_state_and_leave();
660 661
             goto LEAVE;
661 662
           }
@@ -673,7 +674,8 @@
673 674
     LEAVE:
674 675
 
675 676
     #if ENABLED(ULTRA_LCD)
676
-      lcd_setstatus("                         ", true);
677
+      lcd_reset_alert_level();
678
+      lcd_setstatuspgm("");
677 679
       lcd_quick_feedback();
678 680
     #endif
679 681
 
@@ -977,7 +979,7 @@
977 979
 
978 980
   bool g29_parameter_parsing() {
979 981
     #if ENABLED(ULTRA_LCD)
980
-      lcd_setstatus("Doing G29 UBL !", true);
982
+      lcd_setstatuspgm("Doing G29 UBL!");
981 983
       lcd_quick_feedback();
982 984
     #endif
983 985
 
@@ -1088,7 +1090,7 @@
1088 1090
     ubl_state_recursion_chk++;
1089 1091
     if (ubl_state_recursion_chk != 1) {
1090 1092
       SERIAL_ECHOLNPGM("save_ubl_active_state_and_disabled() called multiple times in a row.");
1091
-      lcd_setstatus("save_UBL_active() error", true);
1093
+      lcd_setstatuspgm("save_UBL_active() error");
1092 1094
       lcd_quick_feedback();
1093 1095
       return;
1094 1096
     }
@@ -1099,7 +1101,7 @@
1099 1101
   void restore_ubl_active_state_and_leave() {
1100 1102
     if (--ubl_state_recursion_chk) {
1101 1103
       SERIAL_ECHOLNPGM("restore_ubl_active_state_and_leave() called too many times.");
1102
-      lcd_setstatus("restore_UBL_active() error", true);
1104
+      lcd_setstatuspgm("restore_UBL_active() error");
1103 1105
       lcd_quick_feedback();
1104 1106
       return;
1105 1107
     }
@@ -1114,7 +1116,7 @@
1114 1116
   void g29_what_command() {
1115 1117
     const uint16_t k = E2END - ubl.eeprom_start;
1116 1118
 
1117
-    SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version 1.00 ");
1119
+    SERIAL_PROTOCOLPGM("Unified Bed Leveling System Version " UBL_VERSION " ");
1118 1120
     if (ubl.state.active)
1119 1121
       SERIAL_PROTOCOLCHAR('A');
1120 1122
     else
@@ -1132,8 +1134,7 @@
1132 1134
     safe_delay(50);
1133 1135
 
1134 1136
     #if ENABLED(ENABLE_LEVELING_FADE_HEIGHT)
1135
-      SERIAL_PROTOCOLPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
1136
-      SERIAL_EOL;
1137
+      SERIAL_PROTOCOLLNPAIR("g29_correction_fade_height : ", ubl.state.g29_correction_fade_height);
1137 1138
     #endif
1138 1139
 
1139 1140
     SERIAL_PROTOCOLPGM("z_offset: ");
@@ -1340,7 +1341,7 @@
1340 1341
     memset(not_done, 0xFF, sizeof(not_done));
1341 1342
 
1342 1343
     #if ENABLED(ULTRA_LCD)
1343
-      lcd_setstatus("Fine Tuning Mesh.", true);
1344
+      lcd_setstatuspgm("Fine Tuning Mesh");
1344 1345
     #endif
1345 1346
 
1346 1347
     do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
@@ -1399,7 +1400,7 @@
1399 1400
           lcd_return_to_status();
1400 1401
           //SERIAL_PROTOCOLLNPGM("\nFine Tuning of Mesh Stopped.");
1401 1402
           do_blocking_move_to_z(Z_CLEARANCE_DEPLOY_PROBE);
1402
-          lcd_setstatus("Mesh Editing Stopped", true);
1403
+          lcd_setstatuspgm("Mesh Editing Stopped");
1403 1404
 
1404 1405
           while (ubl_lcd_clicked()) idle();
1405 1406
 
@@ -1427,9 +1428,9 @@
1427 1428
     do_blocking_move_to_xy(lx, ly);
1428 1429
 
1429 1430
     #if ENABLED(ULTRA_LCD)
1430
-      lcd_setstatus("Done Editing Mesh", true);
1431
+      lcd_setstatuspgm("Done Editing Mesh");
1431 1432
     #endif
1432
-    SERIAL_ECHOLNPGM("Done Editing Mesh.");
1433
+    SERIAL_ECHOLNPGM("Done Editing Mesh");
1433 1434
   }
1434 1435
 
1435 1436
 #endif // AUTO_BED_LEVELING_UBL

+ 1
- 3
Marlin/endstops.cpp View File

@@ -170,9 +170,7 @@ void Endstops::report_state() {
170 170
     SERIAL_EOL;
171 171
 
172 172
     #if ENABLED(ULTRA_LCD)
173
-      char msg[3 * strlen(MSG_LCD_ENDSTOPS) + 8 + 1]; // Room for a UTF 8 string
174
-      sprintf_P(msg, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
175
-      lcd_setstatus(msg);
173
+      lcd_status_printf_P(0, PSTR(MSG_LCD_ENDSTOPS " %c %c %c %c"), chrX, chrY, chrZ, chrP);
176 174
     #endif
177 175
 
178 176
     hit_on_purpose();

+ 34
- 0
Marlin/serial.cpp View File

@@ -0,0 +1,34 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#include "serial.h"
24
+
25
+const char errormagic[] PROGMEM = "Error:";
26
+const char echomagic[] PROGMEM = "echo:";
27
+
28
+void serial_echopair_P(const char* s_P, const char *v)   { serialprintPGM(s_P); SERIAL_ECHO(v); }
29
+void serial_echopair_P(const char* s_P, char v)          { serialprintPGM(s_P); SERIAL_CHAR(v); }
30
+void serial_echopair_P(const char* s_P, int v)           { serialprintPGM(s_P); SERIAL_ECHO(v); }
31
+void serial_echopair_P(const char* s_P, long v)          { serialprintPGM(s_P); SERIAL_ECHO(v); }
32
+void serial_echopair_P(const char* s_P, float v)         { serialprintPGM(s_P); SERIAL_ECHO(v); }
33
+void serial_echopair_P(const char* s_P, double v)        { serialprintPGM(s_P); SERIAL_ECHO(v); }
34
+void serial_echopair_P(const char* s_P, unsigned long v) { serialprintPGM(s_P); SERIAL_ECHO(v); }

+ 88
- 0
Marlin/serial.h View File

@@ -0,0 +1,88 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4
+ *
5
+ * Based on Sprinter and grbl.
6
+ * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
7
+ *
8
+ * This program is free software: you can redistribute it and/or modify
9
+ * it under the terms of the GNU General Public License as published by
10
+ * the Free Software Foundation, either version 3 of the License, or
11
+ * (at your option) any later version.
12
+ *
13
+ * This program is distributed in the hope that it will be useful,
14
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+ * GNU General Public License for more details.
17
+ *
18
+ * You should have received a copy of the GNU General Public License
19
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+ *
21
+ */
22
+
23
+#ifndef __SERIAL_H__
24
+#define __SERIAL_H__
25
+
26
+#ifdef USBCON
27
+  #include "HardwareSerial.h"
28
+  #if ENABLED(BLUETOOTH)
29
+    #define MYSERIAL bluetoothSerial
30
+  #else
31
+    #define MYSERIAL Serial
32
+  #endif // BLUETOOTH
33
+#else
34
+  #include "MarlinSerial.h"
35
+  #define MYSERIAL customizedSerial
36
+#endif
37
+
38
+extern const char echomagic[] PROGMEM;
39
+extern const char errormagic[] PROGMEM;
40
+
41
+#define SERIAL_CHAR(x) (MYSERIAL.write(x))
42
+#define SERIAL_EOL SERIAL_CHAR('\n')
43
+
44
+#define SERIAL_PROTOCOLCHAR(x)              SERIAL_CHAR(x)
45
+#define SERIAL_PROTOCOL(x)                  (MYSERIAL.print(x))
46
+#define SERIAL_PROTOCOL_F(x,y)              (MYSERIAL.print(x,y))
47
+#define SERIAL_PROTOCOLPGM(x)               (serialprintPGM(PSTR(x)))
48
+#define SERIAL_PROTOCOLLN(x)                do{ MYSERIAL.print(x); SERIAL_EOL; }while(0)
49
+#define SERIAL_PROTOCOLLNPGM(x)             (serialprintPGM(PSTR(x "\n")))
50
+#define SERIAL_PROTOCOLPAIR(name, value)    (serial_echopair_P(PSTR(name),(value)))
51
+#define SERIAL_PROTOCOLLNPAIR(name, value)  do{ SERIAL_PROTOCOLPAIR(name, value); SERIAL_EOL; }while(0)
52
+
53
+#define SERIAL_ECHO_START             (serialprintPGM(echomagic))
54
+#define SERIAL_ECHO(x)                 SERIAL_PROTOCOL(x)
55
+#define SERIAL_ECHOPGM(x)              SERIAL_PROTOCOLPGM(x)
56
+#define SERIAL_ECHOLN(x)               SERIAL_PROTOCOLLN(x)
57
+#define SERIAL_ECHOLNPGM(x)            SERIAL_PROTOCOLLNPGM(x)
58
+#define SERIAL_ECHOPAIR(name,value)    SERIAL_PROTOCOLPAIR(name, value)
59
+#define SERIAL_ECHOLNPAIR(name, value) SERIAL_PROTOCOLLNPAIR(name, value)
60
+#define SERIAL_ECHO_F(x,y)             SERIAL_PROTOCOL_F(x,y)
61
+
62
+#define SERIAL_ERROR_START            (serialprintPGM(errormagic))
63
+#define SERIAL_ERROR(x)                SERIAL_PROTOCOL(x)
64
+#define SERIAL_ERRORPGM(x)             SERIAL_PROTOCOLPGM(x)
65
+#define SERIAL_ERRORLN(x)              SERIAL_PROTOCOLLN(x)
66
+#define SERIAL_ERRORLNPGM(x)           SERIAL_PROTOCOLLNPGM(x)
67
+
68
+void serial_echopair_P(const char* s_P, const char *v);
69
+void serial_echopair_P(const char* s_P, char v);
70
+void serial_echopair_P(const char* s_P, int v);
71
+void serial_echopair_P(const char* s_P, long v);
72
+void serial_echopair_P(const char* s_P, float v);
73
+void serial_echopair_P(const char* s_P, double v);
74
+void serial_echopair_P(const char* s_P, unsigned int v);
75
+void serial_echopair_P(const char* s_P, unsigned long v);
76
+FORCE_INLINE void serial_echopair_P(const char* s_P, uint8_t v) { serial_echopair_P(s_P, (int)v); }
77
+FORCE_INLINE void serial_echopair_P(const char* s_P, uint16_t v) { serial_echopair_P(s_P, (int)v); }
78
+FORCE_INLINE void serial_echopair_P(const char* s_P, bool v) { serial_echopair_P(s_P, (int)v); }
79
+FORCE_INLINE void serial_echopair_P(const char* s_P, void *v) { serial_echopair_P(s_P, (unsigned long)v); }
80
+
81
+//
82
+// Functions for serial printing from PROGMEM. (Saves loads of SRAM.)
83
+//
84
+FORCE_INLINE void serialprintPGM(const char* str) {
85
+  while (char ch = pgm_read_byte(str++)) MYSERIAL.write(ch);
86
+}
87
+
88
+#endif // __SERIAL_H__

+ 7
- 7
Marlin/ultralcd.cpp View File

@@ -677,7 +677,7 @@ void kill_screen(const char* lcd_msg) {
677 677
         thermalManager.autotempShutdown();
678 678
       #endif
679 679
       wait_for_heatup = false;
680
-      lcd_setstatus(MSG_PRINT_ABORTED, true);
680
+      lcd_setstatuspgm(PSTR(MSG_PRINT_ABORTED), true);
681 681
     }
682 682
 
683 683
   #endif // SDSUPPORT
@@ -3552,30 +3552,30 @@ void lcd_finishstatus(bool persist=false) {
3552 3552
 
3553 3553
 bool lcd_hasstatus() { return (lcd_status_message[0] != '\0'); }
3554 3554
 
3555
-void lcd_setstatus(const char* const message, bool persist) {
3555
+void lcd_setstatus(const char * const message, const bool persist) {
3556 3556
   if (lcd_status_message_level > 0) return;
3557 3557
   strncpy(lcd_status_message, message, 3 * (LCD_WIDTH));
3558 3558
   lcd_finishstatus(persist);
3559 3559
 }
3560 3560
 
3561
-void lcd_setstatuspgm(const char* const message, uint8_t level) {
3561
+void lcd_setstatuspgm(const char * const message, const uint8_t level) {
3562 3562
   if (level < lcd_status_message_level) return;
3563 3563
   lcd_status_message_level = level;
3564 3564
   strncpy_P(lcd_status_message, message, 3 * (LCD_WIDTH));
3565 3565
   lcd_finishstatus(level > 0);
3566 3566
 }
3567 3567
 
3568
-void status_printf(uint8_t level, const char *status, ...) {
3568
+void lcd_status_printf_P(const uint8_t level, const char * const fmt, ...) {
3569 3569
   if (level < lcd_status_message_level) return;
3570 3570
   lcd_status_message_level = level;
3571 3571
   va_list args;
3572
-  va_start(args, status);
3573
-  vsnprintf_P(lcd_status_message, 3 * (LCD_WIDTH), status, args);
3572
+  va_start(args, fmt);
3573
+  vsnprintf_P(lcd_status_message, 3 * (LCD_WIDTH), fmt, args);
3574 3574
   va_end(args);
3575 3575
   lcd_finishstatus(level > 0);
3576 3576
 }
3577 3577
 
3578
-void lcd_setalertstatuspgm(const char* const message) {
3578
+void lcd_setalertstatuspgm(const char * const message) {
3579 3579
   lcd_setstatuspgm(message, 1);
3580 3580
   #if ENABLED(ULTIPANEL)
3581 3581
     lcd_return_to_status();

+ 2
- 2
Marlin/ultralcd.h View File

@@ -39,7 +39,7 @@
39 39
   bool lcd_hasstatus();
40 40
   void lcd_setstatus(const char* message, const bool persist=false);
41 41
   void lcd_setstatuspgm(const char* message, const uint8_t level=0);
42
-  void status_printf(uint8_t level, const char *Status, ...);
42
+  void lcd_status_printf_P(const uint8_t level, const char * const fmt, ...);
43 43
   void lcd_setalertstatuspgm(const char* message);
44 44
   void lcd_reset_alert_level();
45 45
   void lcd_kill_screen();
@@ -154,7 +154,7 @@
154 154
   inline bool lcd_hasstatus() { return false; }
155 155
   inline void lcd_setstatus(const char* const message, const bool persist=false) { UNUSED(message); UNUSED(persist); }
156 156
   inline void lcd_setstatuspgm(const char* const message, const uint8_t level=0) { UNUSED(message); UNUSED(level); }
157
-  inline void status_printf(uint8_t level, const char *status, ...) { UNUSED(level); UNUSED(status); }
157
+  inline void lcd_status_printf_P(const uint8_t level, const char * const fmt, ...) { UNUSED(level); UNUSED(fmt); }
158 158
   inline void lcd_buttons_update() {}
159 159
   inline void lcd_reset_alert_level() {}
160 160
   inline bool lcd_detected() { return true; }

Loading…
Cancel
Save