Browse Source

Optimize, reduce size of MarlinSerial

Scott Lahteine 7 years ago
parent
commit
14395a1a96
2 changed files with 75 additions and 86 deletions
  1. 40
    51
      Marlin/MarlinSerial.cpp
  2. 35
    35
      Marlin/MarlinSerial.h

+ 40
- 51
Marlin/MarlinSerial.cpp View File

@@ -68,8 +68,8 @@ FORCE_INLINE void store_char(unsigned char c) {
68 68
 }
69 69
 
70 70
 #if TX_BUFFER_SIZE > 0
71
-  FORCE_INLINE void _tx_udr_empty_irq(void)
72
-  {
71
+
72
+  FORCE_INLINE void _tx_udr_empty_irq(void) {
73 73
     // If interrupts are enabled, there must be more data in the output
74 74
     // buffer. Send the next byte
75 75
     uint8_t t = tx_buffer.tail;
@@ -95,7 +95,7 @@ FORCE_INLINE void store_char(unsigned char c) {
95 95
     }
96 96
   #endif
97 97
 
98
-#endif
98
+#endif // TX_BUFFER_SIZE
99 99
 
100 100
 #if defined(M_USARTx_RX_vect)
101 101
   ISR(M_USARTx_RX_vect) {
@@ -160,15 +160,8 @@ void MarlinSerial::checkRx(void) {
160 160
 }
161 161
 
162 162
 int MarlinSerial::peek(void) {
163
-  int v;
164 163
   CRITICAL_SECTION_START;
165
-  uint8_t t = rx_buffer.tail;
166
-  if (rx_buffer.head == t) {
167
-    v = -1;
168
-  }
169
-  else {
170
-    v = rx_buffer.buffer[t];
171
-  }
164
+    int v = rx_buffer.head == rx_buffer.tail ? -1 : rx_buffer.buffer[rx_buffer.tail];
172 165
   CRITICAL_SECTION_END;
173 166
   return v;
174 167
 }
@@ -176,22 +169,22 @@ int MarlinSerial::peek(void) {
176 169
 int MarlinSerial::read(void) {
177 170
   int v;
178 171
   CRITICAL_SECTION_START;
179
-  uint8_t t = rx_buffer.tail;
180
-  if (rx_buffer.head == t) {
181
-    v = -1;
182
-  }
183
-  else {
184
-    v = rx_buffer.buffer[t];
185
-    rx_buffer.tail = (uint8_t)(t + 1) & (RX_BUFFER_SIZE - 1);
186
-  }
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);
179
+    }
187 180
   CRITICAL_SECTION_END;
188 181
   return v;
189 182
 }
190 183
 
191 184
 uint8_t MarlinSerial::available(void) {
192 185
   CRITICAL_SECTION_START;
193
-    uint8_t h = rx_buffer.head;
194
-    uint8_t t = rx_buffer.tail;
186
+    uint8_t h = rx_buffer.head,
187
+            t = rx_buffer.tail;
195 188
   CRITICAL_SECTION_END;
196 189
   return (uint8_t)(RX_BUFFER_SIZE + h - t) & (RX_BUFFER_SIZE - 1);
197 190
 }
@@ -251,11 +244,10 @@ void MarlinSerial::flush(void) {
251 244
     }
252 245
 
253 246
     tx_buffer.buffer[tx_buffer.head] = c;
254
-    { CRITICAL_SECTION_START;
255
-        tx_buffer.head = i;
256
-        SBI(M_UCSRxB, M_UDRIEx);
257
-      CRITICAL_SECTION_END;
258
-    }
247
+    CRITICAL_SECTION_START;
248
+      tx_buffer.head = i;
249
+      SBI(M_UCSRxB, M_UDRIEx);
250
+    CRITICAL_SECTION_END;
259 251
     return;
260 252
   }
261 253
 
@@ -386,23 +378,18 @@ void MarlinSerial::println(double n, int digits) {
386 378
 // Private Methods /////////////////////////////////////////////////////////////
387 379
 
388 380
 void MarlinSerial::printNumber(unsigned long n, uint8_t base) {
389
-  unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
390
-  unsigned long i = 0;
391
-
392
-  if (n == 0) {
393
-    print('0');
394
-    return;
395
-  }
396
-
397
-  while (n > 0) {
398
-    buf[i++] = n % base;
399
-    n /= base;
381
+  if (n) {
382
+    unsigned char buf[8 * sizeof(long)]; // Enough space for base 2
383
+    int8_t i = 0;
384
+    while (n) {
385
+      buf[i++] = n % base;
386
+      n /= base;
387
+    }
388
+    while (i--)
389
+      print((char)(buf[i] + (buf[i] < 10 ? '0' : 'A' - 10)));
400 390
   }
401
-
402
-  for (; i > 0; i--)
403
-    print((char)(buf[i - 1] < 10 ?
404
-                 '0' + buf[i - 1] :
405
-                 'A' + buf[i - 1] - 10));
391
+  else
392
+    print('0');
406 393
 }
407 394
 
408 395
 void MarlinSerial::printFloat(double number, uint8_t digits) {
@@ -415,7 +402,7 @@ void MarlinSerial::printFloat(double number, uint8_t digits) {
415 402
   // Round correctly so that print(1.999, 2) prints as "2.00"
416 403
   double rounding = 0.5;
417 404
   for (uint8_t i = 0; i < digits; ++i)
418
-    rounding /= 10.0;
405
+    rounding *= 0.1;
419 406
 
420 407
   number += rounding;
421 408
 
@@ -425,14 +412,15 @@ void MarlinSerial::printFloat(double number, uint8_t digits) {
425 412
   print(int_part);
426 413
 
427 414
   // Print the decimal point, but only if there are digits beyond
428
-  if (digits > 0) print('.');
429
-
430
-  // Extract digits from the remainder one at a time
431
-  while (digits-- > 0) {
432
-    remainder *= 10.0;
433
-    int toPrint = int(remainder);
434
-    print(toPrint);
435
-    remainder -= toPrint;
415
+  if (digits) {
416
+    print('.');
417
+    // Extract digits from the remainder one at a time
418
+    while (digits--) {
419
+      remainder *= 10.0;
420
+      int toPrint = int(remainder);
421
+      print(toPrint);
422
+      remainder -= toPrint;
423
+    }
436 424
   }
437 425
 }
438 426
 // Preinstantiate Objects //////////////////////////////////////////////////////
@@ -534,4 +522,5 @@ MarlinSerial customizedSerial;
534 522
         }
535 523
     }
536 524
   }
525
+
537 526
 #endif

+ 35
- 35
Marlin/MarlinSerial.h View File

@@ -127,47 +127,47 @@ class MarlinSerial { //: public Stream
127 127
 
128 128
   public:
129 129
     MarlinSerial();
130
-    void begin(long);
131
-    void end();
132
-    int peek(void);
133
-    int read(void);
134
-    void flush(void);
135
-    uint8_t available(void);
136
-    void checkRx(void);
137
-    void write(uint8_t c);
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);
138 138
     #if TX_BUFFER_SIZE > 0
139
-      uint8_t availableForWrite(void);
140
-      void flushTX(void);
139
+      static uint8_t availableForWrite(void);
140
+      static void flushTX(void);
141 141
     #endif
142 142
 
143 143
   private:
144
-    void printNumber(unsigned long, uint8_t);
145
-    void printFloat(double, uint8_t);
144
+    static void printNumber(unsigned long, uint8_t);
145
+    static void printFloat(double, uint8_t);
146 146
 
147 147
   public:
148
-    FORCE_INLINE void write(const char* str) { while (*str) write(*str++); }
149
-    FORCE_INLINE void write(const uint8_t* buffer, size_t size) { while (size--) write(*buffer++); }
150
-    FORCE_INLINE void print(const String& s) { for (int i = 0; i < (int)s.length(); i++) write(s[i]); }
151
-    FORCE_INLINE void print(const char* str) { write(str); }
152
-
153
-    void print(char, int = BYTE);
154
-    void print(unsigned char, int = BYTE);
155
-    void print(int, int = DEC);
156
-    void print(unsigned int, int = DEC);
157
-    void print(long, int = DEC);
158
-    void print(unsigned long, int = DEC);
159
-    void print(double, int = 2);
160
-
161
-    void println(const String& s);
162
-    void println(const char[]);
163
-    void println(char, int = BYTE);
164
-    void println(unsigned char, int = BYTE);
165
-    void println(int, int = DEC);
166
-    void println(unsigned int, int = DEC);
167
-    void println(long, int = DEC);
168
-    void println(unsigned long, int = DEC);
169
-    void println(double, int = 2);
170
-    void println(void);
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 171
 };
172 172
 
173 173
 extern MarlinSerial customizedSerial;

Loading…
Cancel
Save