浏览代码

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

Scott Lahteine 8 年前
父节点
当前提交
87d240042a
共有 2 个文件被更改,包括 87 次插入100 次删除
  1. 7
    7
      Marlin/ultralcd_impl_DOGM.h
  2. 80
    93
      Marlin/utility.cpp

+ 7
- 7
Marlin/ultralcd_impl_DOGM.h 查看文件

422
       _draw_heater_status(81, -1);
422
       _draw_heater_status(81, -1);
423
     #endif
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
         if (per) {
429
         if (per) {
430
+          u8g.setPrintPos(104, 27);
431
           lcd_print(itostr3(per));
431
           lcd_print(itostr3(per));
432
           u8g.print('%');
432
           u8g.print('%');
433
         }
433
         }
434
-      #endif
435
-    }
434
+      }
435
+    #endif
436
   }
436
   }
437
 
437
 
438
   #if ENABLED(SDSUPPORT)
438
   #if ENABLED(SDSUPPORT)

+ 80
- 93
Marlin/utility.cpp 查看文件

35
 
35
 
36
 #if ENABLED(ULTRA_LCD)
36
 #if ENABLED(ULTRA_LCD)
37
 
37
 
38
-  char conv[9];
38
+  char conv[8] = { 0 };
39
 
39
 
40
   #define DIGIT(n) ('0' + (n))
40
   #define DIGIT(n) ('0' + (n))
41
   #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
41
   #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
43
   #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
43
   #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
44
 
44
 
45
   // Convert unsigned int to string with 12 format
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
   // Convert signed int to rj string with 123 or -12 format
52
   // Convert signed int to rj string with 123 or -12 format
55
   char* itostr3(const int& x) {
53
   char* itostr3(const int& x) {
56
     int xx = x;
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
   // Convert unsigned int to lj string with 123 format
61
   // Convert unsigned int to lj string with 123 format
65
   char* itostr3left(const int& xx) {
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
     if (xx >= 10) {
65
     if (xx >= 10) {
70
       *(--str) = DIGIMOD(xx, 10);
66
       *(--str) = DIGIMOD(xx, 10);
71
       if (xx >= 100)
67
       if (xx >= 100)
76
 
72
 
77
   // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
73
   // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
78
   char *itostr4sign(const int& x) {
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
     if (x >= 1000) {
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
     else {
82
     else {
86
       if (xx >= 100) {
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
       else {
88
       else {
92
-        conv[0] = ' ';
89
+        conv[4] = ' ';
93
         if (xx >= 10) {
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
         else {
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
   // Convert unsigned float to string with 1.23 format
104
   // Convert unsigned float to string with 1.23 format
109
   char* ftostr12ns(const float& x) {
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
   // Convert signed float to fixed-length string with 023.45 / -23.45 format
114
   // Convert signed float to fixed-length string with 023.45 / -23.45 format
120
   char *ftostr32(const float& x) {
115
   char *ftostr32(const float& x) {
121
     long xx = x * 100;
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
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
126
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
133
 
127
 
134
     // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
128
     // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
135
     char *ftostr4sign(const float& fx) {
129
     char *ftostr4sign(const float& fx) {
136
-      int x = fx * 10;
130
+      const int x = fx * 10;
137
       if (!WITHIN(x, -99, 999)) return itostr4sign((int)fx);
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
   #endif // LCD_DECIMAL_SMALL_XY
141
   #endif // LCD_DECIMAL_SMALL_XY
149
   // Convert float to fixed-length string with +123.4 / -123.4 format
143
   // Convert float to fixed-length string with +123.4 / -123.4 format
150
   char* ftostr41sign(const float& x) {
144
   char* ftostr41sign(const float& x) {
151
     int xx = x * 10;
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
   // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
155
   // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
163
   char* ftostr43sign(const float& x, char plus/*=' '*/) {
156
   char* ftostr43sign(const float& x, char plus/*=' '*/) {
164
     long xx = x * 1000;
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
   // Convert unsigned float to rj string with 12345 format
167
   // Convert unsigned float to rj string with 12345 format
176
   char* ftostr5rj(const float& x) {
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
   // Convert signed float to string with +1234.5 format
178
   // Convert signed float to string with +1234.5 format
194
     conv[4] = DIGIMOD(xx, 10);
185
     conv[4] = DIGIMOD(xx, 10);
195
     conv[5] = '.';
186
     conv[5] = '.';
196
     conv[6] = DIGIMOD(xx, 1);
187
     conv[6] = DIGIMOD(xx, 1);
197
-    conv[7] = '\0';
198
     return conv;
188
     return conv;
199
   }
189
   }
200
 
190
 
208
     conv[4] = '.';
198
     conv[4] = '.';
209
     conv[5] = DIGIMOD(xx, 10);
199
     conv[5] = DIGIMOD(xx, 10);
210
     conv[6] = DIGIMOD(xx, 1);
200
     conv[6] = DIGIMOD(xx, 1);
211
-    conv[7] = '\0';
212
     return conv;
201
     return conv;
213
   }
202
   }
214
 
203
 
215
   // Convert unsigned float to string with 1234.56 format omitting trailing zeros
204
   // Convert unsigned float to string with 1234.56 format omitting trailing zeros
216
   char* ftostr62rj(const float& x) {
205
   char* ftostr62rj(const float& x) {
217
-    long xx = abs(x * 100);
206
+    const long xx = (x < 0 ? -x : x) * 100;
218
     conv[0] = RJDIGIT(xx, 100000);
207
     conv[0] = RJDIGIT(xx, 100000);
219
     conv[1] = RJDIGIT(xx, 10000);
208
     conv[1] = RJDIGIT(xx, 10000);
220
     conv[2] = RJDIGIT(xx, 1000);
209
     conv[2] = RJDIGIT(xx, 1000);
222
     conv[4] = '.';
211
     conv[4] = '.';
223
     conv[5] = DIGIMOD(xx, 10);
212
     conv[5] = DIGIMOD(xx, 10);
224
     conv[6] = DIGIMOD(xx, 1);
213
     conv[6] = DIGIMOD(xx, 1);
225
-    conv[7] = '\0';
226
     return conv;
214
     return conv;
227
   }
215
   }
228
 
216
 
230
   char* ftostr52sp(const float& x) {
218
   char* ftostr52sp(const float& x) {
231
     long xx = x * 100;
219
     long xx = x * 100;
232
     uint8_t dig;
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
     if ((dig = xx % 10)) {          // second digit after decimal point?
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
     else {
230
     else {
243
       if ((dig = (xx / 10) % 10)) { // first digit after decimal point?
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
       else                          // nothing after decimal point
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
 #endif // ULTRA_LCD
242
 #endif // ULTRA_LCD

正在加载...
取消
保存