Browse Source

Optimize number-to-string functions, no-fan display

Scott Lahteine 7 years ago
parent
commit
87d240042a
2 changed files with 87 additions and 100 deletions
  1. 7
    7
      Marlin/ultralcd_impl_DOGM.h
  2. 80
    93
      Marlin/utility.cpp

+ 7
- 7
Marlin/ultralcd_impl_DOGM.h View File

@@ -422,17 +422,17 @@ static void lcd_implementation_status_screen() {
422 422
       _draw_heater_status(81, -1);
423 423
     #endif
424 424
 
425
-    if (PAGE_CONTAINS(20, 27)) {
426
-      // Fan
427
-      u8g.setPrintPos(104, 27);
428
-      #if HAS_FAN0
429
-        int per = ((fanSpeeds[0] + 1) * 100) / 256;
425
+    #if HAS_FAN0
426
+      if (PAGE_CONTAINS(20, 27)) {
427
+        // Fan
428
+        const int per = ((fanSpeeds[0] + 1) * 100) / 256;
430 429
         if (per) {
430
+          u8g.setPrintPos(104, 27);
431 431
           lcd_print(itostr3(per));
432 432
           u8g.print('%');
433 433
         }
434
-      #endif
435
-    }
434
+      }
435
+    #endif
436 436
   }
437 437
 
438 438
   #if ENABLED(SDSUPPORT)

+ 80
- 93
Marlin/utility.cpp View File

@@ -35,7 +35,7 @@ void safe_delay(millis_t ms) {
35 35
 
36 36
 #if ENABLED(ULTRA_LCD)
37 37
 
38
-  char conv[9];
38
+  char conv[8] = { 0 };
39 39
 
40 40
   #define DIGIT(n) ('0' + (n))
41 41
   #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
@@ -43,29 +43,25 @@ void safe_delay(millis_t ms) {
43 43
   #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
44 44
 
45 45
   // Convert unsigned int to string with 12 format
46
-  char* itostr2(const uint8_t& x) {
47
-    int xx = x;
48
-    conv[0] = DIGIMOD(xx, 10);
49
-    conv[1] = DIGIMOD(xx, 1);
50
-    conv[2] = '\0';
51
-    return conv;
46
+  char* itostr2(const uint8_t& xx) {
47
+    conv[5] = DIGIMOD(xx, 10);
48
+    conv[6] = DIGIMOD(xx, 1);
49
+    return &conv[5];
52 50
   }
53 51
 
54 52
   // Convert signed int to rj string with 123 or -12 format
55 53
   char* itostr3(const int& x) {
56 54
     int xx = x;
57
-    conv[0] = MINUSOR(xx, RJDIGIT(xx, 100));
58
-    conv[1] = RJDIGIT(xx, 10);
59
-    conv[2] = DIGIMOD(xx, 1);
60
-    conv[3] = '\0';
61
-    return conv;
55
+    conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
56
+    conv[5] = RJDIGIT(xx, 10);
57
+    conv[6] = DIGIMOD(xx, 1);
58
+    return &conv[4];
62 59
   }
63 60
 
64 61
   // Convert unsigned int to lj string with 123 format
65 62
   char* itostr3left(const int& xx) {
66
-    char *str = &conv[3];
67
-    *str = '\0';
68
-    *(--str) = DIGIMOD(xx, 1);
63
+    char *str = &conv[6];
64
+    *str = DIGIMOD(xx, 1);
69 65
     if (xx >= 10) {
70 66
       *(--str) = DIGIMOD(xx, 10);
71 67
       if (xx >= 100)
@@ -76,72 +72,70 @@ void safe_delay(millis_t ms) {
76 72
 
77 73
   // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
78 74
   char *itostr4sign(const int& x) {
79
-    int xx = abs(x);
75
+    const bool neg = x < 0;
76
+    const int xx = neg ? -x : x;
80 77
     if (x >= 1000) {
81
-      conv[0] = DIGIMOD(xx, 1000);
82
-      conv[1] = DIGIMOD(xx, 100);
83
-      conv[2] = DIGIMOD(xx, 10);
78
+      conv[3] = DIGIMOD(xx, 1000);
79
+      conv[4] = DIGIMOD(xx, 100);
80
+      conv[5] = DIGIMOD(xx, 10);
84 81
     }
85 82
     else {
86 83
       if (xx >= 100) {
87
-        conv[0] = x < 0 ? '-' : ' ';
88
-        conv[1] = DIGIMOD(xx, 100);
89
-        conv[2] = DIGIMOD(xx, 10);
84
+        conv[3] = neg ? '-' : ' ';
85
+        conv[4] = DIGIMOD(xx, 100);
86
+        conv[5] = DIGIMOD(xx, 10);
90 87
       }
91 88
       else {
92
-        conv[0] = ' ';
89
+        conv[4] = ' ';
93 90
         if (xx >= 10) {
94
-          conv[1] = x < 0 ? '-' : ' ';
95
-          conv[2] = DIGIMOD(xx, 10);
91
+          conv[4] = neg ? '-' : ' ';
92
+          conv[5] = DIGIMOD(xx, 10);
96 93
         }
97 94
         else {
98
-          conv[1] = ' ';
99
-          conv[2] = x < 0 ? '-' : ' ';
95
+          conv[4] = ' ';
96
+          conv[5] = neg ? '-' : ' ';
100 97
         }
101 98
       }
102 99
     }
103
-    conv[3] = DIGIMOD(xx, 1);
104
-    conv[4] = '\0';
105
-    return conv;
100
+    conv[6] = DIGIMOD(xx, 1);
101
+    return &conv[3];
106 102
   }
107 103
 
108 104
   // Convert unsigned float to string with 1.23 format
109 105
   char* ftostr12ns(const float& x) {
110
-    long xx = abs(x * 100);
111
-    conv[0] = DIGIMOD(xx, 100);
112
-    conv[1] = '.';
113
-    conv[2] = DIGIMOD(xx, 10);
114
-    conv[3] = DIGIMOD(xx, 1);
115
-    conv[4] = '\0';
116
-    return conv;
106
+    const long xx = (x < 0 ? -x : x) * 100;
107
+    conv[3] = DIGIMOD(xx, 100);
108
+    conv[4] = '.';
109
+    conv[5] = DIGIMOD(xx, 10);
110
+    conv[6] = DIGIMOD(xx, 1);
111
+    return &conv[3];
117 112
   }
118 113
 
119 114
   // Convert signed float to fixed-length string with 023.45 / -23.45 format
120 115
   char *ftostr32(const float& x) {
121 116
     long xx = x * 100;
122
-    conv[0] = MINUSOR(xx, DIGIMOD(xx, 10000));
123
-    conv[1] = DIGIMOD(xx, 1000);
124
-    conv[2] = DIGIMOD(xx, 100);
125
-    conv[3] = '.';
126
-    conv[4] = DIGIMOD(xx, 10);
127
-    conv[5] = DIGIMOD(xx, 1);
128
-    conv[6] = '\0';
129
-    return conv;
117
+    conv[1] = MINUSOR(xx, DIGIMOD(xx, 10000));
118
+    conv[2] = DIGIMOD(xx, 1000);
119
+    conv[3] = DIGIMOD(xx, 100);
120
+    conv[4] = '.';
121
+    conv[5] = DIGIMOD(xx, 10);
122
+    conv[6] = DIGIMOD(xx, 1);
123
+    return &conv[1];
130 124
   }
131 125
 
132 126
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
133 127
 
134 128
     // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
135 129
     char *ftostr4sign(const float& fx) {
136
-      int x = fx * 10;
130
+      const int x = fx * 10;
137 131
       if (!WITHIN(x, -99, 999)) return itostr4sign((int)fx);
138
-      int xx = abs(x);
139
-      conv[0] = x < 0 ? '-' : (xx >= 100 ? DIGIMOD(xx, 100) : ' ');
140
-      conv[1] = DIGIMOD(xx, 10);
141
-      conv[2] = '.';
142
-      conv[3] = DIGIMOD(xx, 1);
143
-      conv[4] = '\0';
144
-      return conv;
132
+      const bool neg = x < 0;
133
+      const int xx = neg ? -x : x;
134
+      conv[3] = neg ? '-' : (xx >= 100 ? DIGIMOD(xx, 100) : ' ');
135
+      conv[4] = DIGIMOD(xx, 10);
136
+      conv[5] = '.';
137
+      conv[6] = DIGIMOD(xx, 1);
138
+      return &conv[3];
145 139
     }
146 140
 
147 141
   #endif // LCD_DECIMAL_SMALL_XY
@@ -149,39 +143,36 @@ void safe_delay(millis_t ms) {
149 143
   // Convert float to fixed-length string with +123.4 / -123.4 format
150 144
   char* ftostr41sign(const float& x) {
151 145
     int xx = x * 10;
152
-    conv[0] = MINUSOR(xx, '+');
153
-    conv[1] = DIGIMOD(xx, 1000);
154
-    conv[2] = DIGIMOD(xx, 100);
155
-    conv[3] = DIGIMOD(xx, 10);
156
-    conv[4] = '.';
157
-    conv[5] = DIGIMOD(xx, 1);
158
-    conv[6] = '\0';
159
-    return conv;
146
+    conv[1] = MINUSOR(xx, '+');
147
+    conv[2] = DIGIMOD(xx, 1000);
148
+    conv[3] = DIGIMOD(xx, 100);
149
+    conv[4] = DIGIMOD(xx, 10);
150
+    conv[5] = '.';
151
+    conv[6] = DIGIMOD(xx, 1);
152
+    return &conv[1];
160 153
   }
161 154
 
162 155
   // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
163 156
   char* ftostr43sign(const float& x, char plus/*=' '*/) {
164 157
     long xx = x * 1000;
165
-    conv[0] = xx ? MINUSOR(xx, plus) : ' ';
166
-    conv[1] = DIGIMOD(xx, 1000);
167
-    conv[2] = '.';
168
-    conv[3] = DIGIMOD(xx, 100);
169
-    conv[4] = DIGIMOD(xx, 10);
170
-    conv[5] = DIGIMOD(xx, 1);
171
-    conv[6] = '\0';
172
-    return conv;
158
+    conv[1] = xx ? MINUSOR(xx, plus) : ' ';
159
+    conv[2] = DIGIMOD(xx, 1000);
160
+    conv[3] = '.';
161
+    conv[4] = DIGIMOD(xx, 100);
162
+    conv[5] = DIGIMOD(xx, 10);
163
+    conv[6] = DIGIMOD(xx, 1);
164
+    return &conv[1];
173 165
   }
174 166
 
175 167
   // Convert unsigned float to rj string with 12345 format
176 168
   char* ftostr5rj(const float& x) {
177
-    long xx = abs(x);
178
-    conv[0] = RJDIGIT(xx, 10000);
179
-    conv[1] = RJDIGIT(xx, 1000);
180
-    conv[2] = RJDIGIT(xx, 100);
181
-    conv[3] = RJDIGIT(xx, 10);
182
-    conv[4] = DIGIMOD(xx, 1);
183
-    conv[5] = '\0';
184
-    return conv;
169
+    const long xx = x < 0 ? -x : x;
170
+    conv[2] = RJDIGIT(xx, 10000);
171
+    conv[3] = RJDIGIT(xx, 1000);
172
+    conv[4] = RJDIGIT(xx, 100);
173
+    conv[5] = RJDIGIT(xx, 10);
174
+    conv[6] = DIGIMOD(xx, 1);
175
+    return &conv[2];
185 176
   }
186 177
 
187 178
   // Convert signed float to string with +1234.5 format
@@ -194,7 +185,6 @@ void safe_delay(millis_t ms) {
194 185
     conv[4] = DIGIMOD(xx, 10);
195 186
     conv[5] = '.';
196 187
     conv[6] = DIGIMOD(xx, 1);
197
-    conv[7] = '\0';
198 188
     return conv;
199 189
   }
200 190
 
@@ -208,13 +198,12 @@ void safe_delay(millis_t ms) {
208 198
     conv[4] = '.';
209 199
     conv[5] = DIGIMOD(xx, 10);
210 200
     conv[6] = DIGIMOD(xx, 1);
211
-    conv[7] = '\0';
212 201
     return conv;
213 202
   }
214 203
 
215 204
   // Convert unsigned float to string with 1234.56 format omitting trailing zeros
216 205
   char* ftostr62rj(const float& x) {
217
-    long xx = abs(x * 100);
206
+    const long xx = (x < 0 ? -x : x) * 100;
218 207
     conv[0] = RJDIGIT(xx, 100000);
219 208
     conv[1] = RJDIGIT(xx, 10000);
220 209
     conv[2] = RJDIGIT(xx, 1000);
@@ -222,7 +211,6 @@ void safe_delay(millis_t ms) {
222 211
     conv[4] = '.';
223 212
     conv[5] = DIGIMOD(xx, 10);
224 213
     conv[6] = DIGIMOD(xx, 1);
225
-    conv[7] = '\0';
226 214
     return conv;
227 215
   }
228 216
 
@@ -230,26 +218,25 @@ void safe_delay(millis_t ms) {
230 218
   char* ftostr52sp(const float& x) {
231 219
     long xx = x * 100;
232 220
     uint8_t dig;
233
-    conv[0] = MINUSOR(xx, RJDIGIT(xx, 10000));
234
-    conv[1] = RJDIGIT(xx, 1000);
235
-    conv[2] = DIGIMOD(xx, 100);
221
+    conv[1] = MINUSOR(xx, RJDIGIT(xx, 10000));
222
+    conv[2] = RJDIGIT(xx, 1000);
223
+    conv[3] = DIGIMOD(xx, 100);
236 224
 
237 225
     if ((dig = xx % 10)) {          // second digit after decimal point?
238
-      conv[3] = '.';
239
-      conv[4] = DIGIMOD(xx, 10);
240
-      conv[5] = DIGIT(dig);
226
+      conv[4] = '.';
227
+      conv[5] = DIGIMOD(xx, 10);
228
+      conv[6] = DIGIT(dig);
241 229
     }
242 230
     else {
243 231
       if ((dig = (xx / 10) % 10)) { // first digit after decimal point?
244
-        conv[3] = '.';
245
-        conv[4] = DIGIT(dig);
232
+        conv[4] = '.';
233
+        conv[5] = DIGIT(dig);
246 234
       }
247 235
       else                          // nothing after decimal point
248
-        conv[3] = conv[4] = ' ';
249
-      conv[5] = ' ';
236
+        conv[4] = conv[5] = ' ';
237
+      conv[6] = ' ';
250 238
     }
251
-    conv[6] = '\0';
252
-    return conv;
239
+    return &conv[1];
253 240
   }
254 241
 
255 242
 #endif // ULTRA_LCD

Loading…
Cancel
Save