Selaa lähdekoodia

Move number-to-string functions to libs

Scott Lahteine 5 vuotta sitten
vanhempi
commit
2a96d4e23a

+ 2
- 269
Marlin/src/core/utility.cpp Näytä tiedosto

@@ -48,280 +48,13 @@ void safe_delay(millis_t ms) {
48 48
 
49 49
 #endif // EEPROM_SETTINGS || SD_FIRMWARE_UPDATE
50 50
 
51
-#if ANY(ULTRA_LCD, DEBUG_LEVELING_FEATURE, EXTENSIBLE_UI)
52
-
53
-  char conv[8] = { 0 };
54
-
55
-  #define DIGIT(n) ('0' + (n))
56
-  #define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
57
-  #define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
58
-  #define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
59
-
60
-  // Convert a full-range unsigned 8bit int to a percentage
61
-  char* ui8tostr4pct(const uint8_t i) {
62
-    const uint8_t n = ui8_to_percent(i);
63
-    conv[3] = RJDIGIT(n, 100);
64
-    conv[4] = RJDIGIT(n, 10);
65
-    conv[5] = DIGIMOD(n, 1);
66
-    conv[6] = '%';
67
-    return &conv[3];
68
-  }
69
-
70
-  // Convert unsigned 8bit int to string 123 format
71
-  char* ui8tostr3(const uint8_t i) {
72
-    conv[4] = RJDIGIT(i, 100);
73
-    conv[5] = RJDIGIT(i, 10);
74
-    conv[6] = DIGIMOD(i, 1);
75
-    return &conv[4];
76
-  }
77
-
78
-  // Convert signed 8bit int to rj string with 123 or -12 format
79
-  char* i8tostr3(const int8_t x) {
80
-    int xx = x;
81
-    conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
82
-    conv[5] = RJDIGIT(xx, 10);
83
-    conv[6] = DIGIMOD(xx, 1);
84
-    return &conv[4];
85
-  }
86
-
87
-  // Convert unsigned 16bit int to string 123 format
88
-  char* ui16tostr3(const uint16_t xx) {
89
-    conv[4] = RJDIGIT(xx, 100);
90
-    conv[5] = RJDIGIT(xx, 10);
91
-    conv[6] = DIGIMOD(xx, 1);
92
-    return &conv[4];
93
-  }
94
-
95
-  // Convert unsigned 16bit int to string 1234 format
96
-  char* ui16tostr4(const uint16_t xx) {
97
-    conv[3] = RJDIGIT(xx, 1000);
98
-    conv[4] = RJDIGIT(xx, 100);
99
-    conv[5] = RJDIGIT(xx, 10);
100
-    conv[6] = DIGIMOD(xx, 1);
101
-    return &conv[3];
102
-  }
103
-
104
-  // Convert signed 16bit int to rj string with 123 or -12 format
105
-  char* i16tostr3(const int16_t x) {
106
-    int xx = x;
107
-    conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
108
-    conv[5] = RJDIGIT(xx, 10);
109
-    conv[6] = DIGIMOD(xx, 1);
110
-    return &conv[4];
111
-  }
112
-
113
-  // Convert unsigned 16bit int to lj string with 123 format
114
-  char* i16tostr3left(const int16_t i) {
115
-    char *str = &conv[6];
116
-    *str = DIGIMOD(i, 1);
117
-    if (i >= 10) {
118
-      *(--str) = DIGIMOD(i, 10);
119
-      if (i >= 100)
120
-        *(--str) = DIGIMOD(i, 100);
121
-    }
122
-    return str;
123
-  }
124
-
125
-  // Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
126
-  char* i16tostr4sign(const int16_t i) {
127
-    const bool neg = i < 0;
128
-    const int ii = neg ? -i : i;
129
-    if (i >= 1000) {
130
-      conv[3] = DIGIMOD(ii, 1000);
131
-      conv[4] = DIGIMOD(ii, 100);
132
-      conv[5] = DIGIMOD(ii, 10);
133
-    }
134
-    else if (ii >= 100) {
135
-      conv[3] = neg ? '-' : ' ';
136
-      conv[4] = DIGIMOD(ii, 100);
137
-      conv[5] = DIGIMOD(ii, 10);
138
-    }
139
-    else {
140
-      conv[3] = ' ';
141
-      conv[4] = ' ';
142
-      if (ii >= 10) {
143
-        conv[4] = neg ? '-' : ' ';
144
-        conv[5] = DIGIMOD(ii, 10);
145
-      }
146
-      else {
147
-        conv[5] = neg ? '-' : ' ';
148
-      }
149
-    }
150
-    conv[6] = DIGIMOD(ii, 1);
151
-    return &conv[3];
152
-  }
153
-
154
-  // Convert unsigned float to string with 1.23 format
155
-  char* ftostr12ns(const float &f) {
156
-    const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
157
-    conv[3] = DIGIMOD(i, 100);
158
-    conv[4] = '.';
159
-    conv[5] = DIGIMOD(i, 10);
160
-    conv[6] = DIGIMOD(i, 1);
161
-    return &conv[3];
162
-  }
163
-
164
-  // Convert signed float to fixed-length string with 12.34 / -2.34 format or 123.45 / -23.45 format
165
-  char* ftostr42_52(const float &f) {
166
-    if (f <= -10 || f >= 100) return ftostr52(f); // need more digits
167
-    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
168
-    conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
169
-    conv[3] = DIGIMOD(i, 100);
170
-    conv[4] = '.';
171
-    conv[5] = DIGIMOD(i, 10);
172
-    conv[6] = DIGIMOD(i, 1);
173
-    return &conv[2];
174
-  }
175
-
176
-  // Convert signed float to fixed-length string with 023.45 / -23.45 format
177
-  char* ftostr52(const float &f) {
178
-    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
179
-    conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
180
-    conv[2] = DIGIMOD(i, 1000);
181
-    conv[3] = DIGIMOD(i, 100);
182
-    conv[4] = '.';
183
-    conv[5] = DIGIMOD(i, 10);
184
-    conv[6] = DIGIMOD(i, 1);
185
-    return &conv[1];
186
-  }
187
-
188
-  #if ENABLED(LCD_DECIMAL_SMALL_XY)
189
-
190
-    // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
191
-    char* ftostr4sign(const float &f) {
192
-      const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
193
-      if (!WITHIN(i, -99, 999)) return i16tostr4sign((int)f);
194
-      const bool neg = i < 0;
195
-      const int ii = neg ? -i : i;
196
-      conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
197
-      conv[4] = DIGIMOD(ii, 10);
198
-      conv[5] = '.';
199
-      conv[6] = DIGIMOD(ii, 1);
200
-      return &conv[3];
201
-    }
202
-
203
-  #endif // LCD_DECIMAL_SMALL_XY
204
-
205
-  // Convert float to fixed-length string with +123.4 / -123.4 format
206
-  char* ftostr41sign(const float &f) {
207
-    int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
208
-    conv[1] = MINUSOR(i, '+');
209
-    conv[2] = DIGIMOD(i, 1000);
210
-    conv[3] = DIGIMOD(i, 100);
211
-    conv[4] = DIGIMOD(i, 10);
212
-    conv[5] = '.';
213
-    conv[6] = DIGIMOD(i, 1);
214
-    return &conv[1];
215
-  }
216
-
217
-  // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
218
-  char* ftostr43sign(const float &f, char plus/*=' '*/) {
219
-    long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
220
-    conv[1] = i ? MINUSOR(i, plus) : ' ';
221
-    conv[2] = DIGIMOD(i, 1000);
222
-    conv[3] = '.';
223
-    conv[4] = DIGIMOD(i, 100);
224
-    conv[5] = DIGIMOD(i, 10);
225
-    conv[6] = DIGIMOD(i, 1);
226
-    return &conv[1];
227
-  }
228
-
229
-  // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
230
-  char* ftostr54sign(const float &f, char plus/*=' '*/) {
231
-    long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
232
-    conv[0] = i ? MINUSOR(i, plus) : ' ';
233
-    conv[1] = DIGIMOD(i, 10000);
234
-    conv[2] = '.';
235
-    conv[3] = DIGIMOD(i, 1000);
236
-    conv[4] = DIGIMOD(i, 100);
237
-    conv[5] = DIGIMOD(i, 10);
238
-    conv[6] = DIGIMOD(i, 1);
239
-    return &conv[0];
240
-  }
241
-
242
-  // Convert unsigned float to rj string with 12345 format
243
-  char* ftostr5rj(const float &f) {
244
-    const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
245
-    conv[2] = RJDIGIT(i, 10000);
246
-    conv[3] = RJDIGIT(i, 1000);
247
-    conv[4] = RJDIGIT(i, 100);
248
-    conv[5] = RJDIGIT(i, 10);
249
-    conv[6] = DIGIMOD(i, 1);
250
-    return &conv[2];
251
-  }
252
-
253
-  // Convert signed float to string with +1234.5 format
254
-  char* ftostr51sign(const float &f) {
255
-    long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
256
-    conv[0] = MINUSOR(i, '+');
257
-    conv[1] = DIGIMOD(i, 10000);
258
-    conv[2] = DIGIMOD(i, 1000);
259
-    conv[3] = DIGIMOD(i, 100);
260
-    conv[4] = DIGIMOD(i, 10);
261
-    conv[5] = '.';
262
-    conv[6] = DIGIMOD(i, 1);
263
-    return conv;
264
-  }
265
-
266
-  // Convert signed float to string with +123.45 format
267
-  char* ftostr52sign(const float &f) {
268
-    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
269
-    conv[0] = MINUSOR(i, '+');
270
-    conv[1] = DIGIMOD(i, 10000);
271
-    conv[2] = DIGIMOD(i, 1000);
272
-    conv[3] = DIGIMOD(i, 100);
273
-    conv[4] = '.';
274
-    conv[5] = DIGIMOD(i, 10);
275
-    conv[6] = DIGIMOD(i, 1);
276
-    return conv;
277
-  }
278
-
279
-  // Convert unsigned float to string with 1234.5 format omitting trailing zeros
280
-  char* ftostr51rj(const float &f) {
281
-    const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
282
-    conv[0] = ' ';
283
-    conv[1] = RJDIGIT(i, 10000);
284
-    conv[2] = RJDIGIT(i, 1000);
285
-    conv[3] = RJDIGIT(i, 100);
286
-    conv[4] = DIGIMOD(i, 10);
287
-    conv[5] = '.';
288
-    conv[6] = DIGIMOD(i, 1);
289
-    return conv;
290
-  }
291
-
292
-  // Convert signed float to space-padded string with -_23.4_ format
293
-  char* ftostr52sp(const float &f) {
294
-    long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
295
-    uint8_t dig;
296
-    conv[0] = MINUSOR(i, ' ');
297
-    conv[1] = RJDIGIT(i, 10000);
298
-    conv[2] = RJDIGIT(i, 1000);
299
-    conv[3] = DIGIMOD(i, 100);
300
-
301
-    if ((dig = i % 10)) {          // second digit after decimal point?
302
-      conv[4] = '.';
303
-      conv[5] = DIGIMOD(i, 10);
304
-      conv[6] = DIGIT(dig);
305
-    }
306
-    else {
307
-      if ((dig = (i / 10) % 10)) { // first digit after decimal point?
308
-        conv[4] = '.';
309
-        conv[5] = DIGIT(dig);
310
-      }
311
-      else                          // nothing after decimal point
312
-        conv[4] = conv[5] = ' ';
313
-      conv[6] = ' ';
314
-    }
315
-    return conv;
316
-  }
317
-
318
-#endif // ULTRA_LCD
319
-
320 51
 #if ENABLED(DEBUG_LEVELING_FEATURE)
321 52
 
322 53
   #include "../module/probe.h"
323 54
   #include "../module/motion.h"
324 55
   #include "../module/stepper.h"
56
+  #include "../module/stepper.h"
57
+  #include "../libs/numtostr.h"
325 58
   #include "../feature/bedlevel/bedlevel.h"
326 59
 
327 60
   void log_machine_info() {

+ 0
- 72
Marlin/src/core/utility.h Näytä tiedosto

@@ -53,78 +53,6 @@ inline void serial_delay(const millis_t ms) {
53 53
   FORCE_INLINE bool is_bitmap_set(uint16_t bits[16], const uint8_t x, const uint8_t y) { return TEST(bits[y], x); }
54 54
 #endif
55 55
 
56
-#if ANY(ULTRA_LCD, DEBUG_LEVELING_FEATURE, EXTENSIBLE_UI)
57
-
58
-  // Convert a full-range unsigned 8bit int to a percentage
59
-  char* ui8tostr4pct(const uint8_t i);
60
-
61
-  // Convert uint8_t to string with 123 format
62
-  char* ui8tostr3(const uint8_t x);
63
-
64
-  // Convert int8_t to string with 123 format
65
-  char* i8tostr3(const int8_t x);
66
-
67
-  // Convert uint16_t to string with 123 format
68
-  char* ui16tostr3(const uint16_t x);
69
-
70
-  // Convert uint16_t to string with 1234 format
71
-  char* ui16tostr4(const uint16_t x);
72
-
73
-  // Convert int16_t to string with 123 format
74
-  char* i16tostr3(const int16_t x);
75
-
76
-  // Convert unsigned int to lj string with 123 format
77
-  char* i16tostr3left(const int16_t xx);
78
-
79
-  // Convert signed int to rj string with _123, -123, _-12, or __-1 format
80
-  char* i16tostr4sign(const int16_t x);
81
-
82
-  // Convert unsigned float to string with 1.23 format
83
-  char* ftostr12ns(const float &x);
84
-
85
-  // Convert signed float to fixed-length string with 12.34 / -2.34 or 023.45 / -23.45 format
86
-  char* ftostr42_52(const float &x);
87
-
88
-  // Convert signed float to fixed-length string with 023.45 / -23.45 format
89
-  char* ftostr52(const float &x);
90
-
91
-  // Convert float to fixed-length string with +123.4 / -123.4 format
92
-  char* ftostr41sign(const float &x);
93
-
94
-  // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
95
-  char* ftostr43sign(const float &x, char plus=' ');
96
-
97
-  // Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
98
-  char* ftostr54sign(const float &x, char plus=' ');
99
-
100
-  // Convert unsigned float to rj string with 12345 format
101
-  char* ftostr5rj(const float &x);
102
-
103
-  // Convert signed float to string with +1234.5 format
104
-  char* ftostr51sign(const float &x);
105
-
106
-  // Convert signed float to space-padded string with -_23.4_ format
107
-  char* ftostr52sp(const float &x);
108
-
109
-  // Convert signed float to string with +123.45 format
110
-  char* ftostr52sign(const float &x);
111
-
112
-  // Convert unsigned float to string with 1234.5 format omitting trailing zeros
113
-  char* ftostr51rj(const float &x);
114
-
115
-  // Convert float to rj string with 123 or -12 format
116
-  FORCE_INLINE char* ftostr3(const float &x) { return i16tostr3(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
117
-
118
-  #if ENABLED(LCD_DECIMAL_SMALL_XY)
119
-    // Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format
120
-    char* ftostr4sign(const float &fx);
121
-  #else
122
-    // Convert float to rj string with 1234, _123, -123, __12, _-12, ___1, or __-1 format
123
-    FORCE_INLINE char* ftostr4sign(const float &x) { return i16tostr4sign(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
124
-  #endif
125
-
126
-#endif // ULTRA_LCD
127
-
128 56
 #if ENABLED(DEBUG_LEVELING_FEATURE)
129 57
   void log_machine_info();
130 58
 #else

+ 1
- 0
Marlin/src/lcd/HD44780/ultralcd_HD44780.cpp Näytä tiedosto

@@ -33,6 +33,7 @@
33 33
 
34 34
 #include "ultralcd_HD44780.h"
35 35
 #include "../ultralcd.h"
36
+#include "../../libs/numtostr.h"
36 37
 
37 38
 #include "../../sd/cardreader.h"
38 39
 #include "../../module/temperature.h"

+ 1
- 0
Marlin/src/lcd/dogm/status_screen_DOGM.cpp Näytä tiedosto

@@ -33,6 +33,7 @@
33 33
 #include "ultralcd_DOGM.h"
34 34
 #include "../ultralcd.h"
35 35
 #include "../lcdprint.h"
36
+#include "../../libs/numtostr.h"
36 37
 
37 38
 #include "../../module/motion.h"
38 39
 #include "../../module/temperature.h"

+ 1
- 0
Marlin/src/lcd/dogm/ultralcd_DOGM.cpp Näytä tiedosto

@@ -48,6 +48,7 @@
48 48
 
49 49
 #include "../lcdprint.h"
50 50
 #include "../fontutils.h"
51
+#include "../../libs/numtostr.h"
51 52
 #include "../ultralcd.h"
52 53
 
53 54
 #include "../../sd/cardreader.h"

+ 1
- 0
Marlin/src/lcd/extensible_ui/ui_api.cpp Näytä tiedosto

@@ -57,6 +57,7 @@
57 57
 
58 58
 #if ENABLED(PRINTCOUNTER)
59 59
   #include "../../core/utility.h"
60
+  #include "../../libs/numtostr.h"
60 61
 #endif
61 62
 
62 63
 #if DO_SWITCH_EXTRUDER || EITHER(SWITCHING_NOZZLE, PARKING_EXTRUDER)

+ 1
- 0
Marlin/src/lcd/menu/menu.h Näytä tiedosto

@@ -22,6 +22,7 @@
22 22
 #pragma once
23 23
 
24 24
 #include "../ultralcd.h"
25
+#include "../../libs/numtostr.h"
25 26
 #include "../../inc/MarlinConfig.h"
26 27
 
27 28
 #include "limits.h"

+ 289
- 0
Marlin/src/libs/numtostr.cpp Näytä tiedosto

@@ -0,0 +1,289 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2019 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 "numtostr.h"
24
+#include "../core/utility.h"
25
+
26
+char conv[8] = { 0 };
27
+
28
+#define DIGIT(n) ('0' + (n))
29
+#define DIGIMOD(n, f) DIGIT((n)/(f) % 10)
30
+#define RJDIGIT(n, f) ((n) >= (f) ? DIGIMOD(n, f) : ' ')
31
+#define MINUSOR(n, alt) (n >= 0 ? (alt) : (n = -n, '-'))
32
+
33
+// Convert a full-range unsigned 8bit int to a percentage
34
+char* ui8tostr4pct(const uint8_t i) {
35
+  const uint8_t n = ui8_to_percent(i);
36
+  conv[3] = RJDIGIT(n, 100);
37
+  conv[4] = RJDIGIT(n, 10);
38
+  conv[5] = DIGIMOD(n, 1);
39
+  conv[6] = '%';
40
+  return &conv[3];
41
+}
42
+
43
+// Convert unsigned 8bit int to string 123 format
44
+char* ui8tostr3(const uint8_t i) {
45
+  conv[4] = RJDIGIT(i, 100);
46
+  conv[5] = RJDIGIT(i, 10);
47
+  conv[6] = DIGIMOD(i, 1);
48
+  return &conv[4];
49
+}
50
+
51
+// Convert signed 8bit int to rj string with 123 or -12 format
52
+char* i8tostr3(const int8_t x) {
53
+  int xx = x;
54
+  conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
55
+  conv[5] = RJDIGIT(xx, 10);
56
+  conv[6] = DIGIMOD(xx, 1);
57
+  return &conv[4];
58
+}
59
+
60
+// Convert unsigned 16bit int to string 123 format
61
+char* ui16tostr3(const uint16_t xx) {
62
+  conv[4] = RJDIGIT(xx, 100);
63
+  conv[5] = RJDIGIT(xx, 10);
64
+  conv[6] = DIGIMOD(xx, 1);
65
+  return &conv[4];
66
+}
67
+
68
+// Convert unsigned 16bit int to string 1234 format
69
+char* ui16tostr4(const uint16_t xx) {
70
+  conv[3] = RJDIGIT(xx, 1000);
71
+  conv[4] = RJDIGIT(xx, 100);
72
+  conv[5] = RJDIGIT(xx, 10);
73
+  conv[6] = DIGIMOD(xx, 1);
74
+  return &conv[3];
75
+}
76
+
77
+// Convert signed 16bit int to rj string with 123 or -12 format
78
+char* i16tostr3(const int16_t x) {
79
+  int xx = x;
80
+  conv[4] = MINUSOR(xx, RJDIGIT(xx, 100));
81
+  conv[5] = RJDIGIT(xx, 10);
82
+  conv[6] = DIGIMOD(xx, 1);
83
+  return &conv[4];
84
+}
85
+
86
+// Convert unsigned 16bit int to lj string with 123 format
87
+char* i16tostr3left(const int16_t i) {
88
+  char *str = &conv[6];
89
+  *str = DIGIMOD(i, 1);
90
+  if (i >= 10) {
91
+    *(--str) = DIGIMOD(i, 10);
92
+    if (i >= 100)
93
+      *(--str) = DIGIMOD(i, 100);
94
+  }
95
+  return str;
96
+}
97
+
98
+// Convert signed 16bit int to rj string with 1234, _123, -123, _-12, or __-1 format
99
+char* i16tostr4sign(const int16_t i) {
100
+  const bool neg = i < 0;
101
+  const int ii = neg ? -i : i;
102
+  if (i >= 1000) {
103
+    conv[3] = DIGIMOD(ii, 1000);
104
+    conv[4] = DIGIMOD(ii, 100);
105
+    conv[5] = DIGIMOD(ii, 10);
106
+  }
107
+  else if (ii >= 100) {
108
+    conv[3] = neg ? '-' : ' ';
109
+    conv[4] = DIGIMOD(ii, 100);
110
+    conv[5] = DIGIMOD(ii, 10);
111
+  }
112
+  else {
113
+    conv[3] = ' ';
114
+    conv[4] = ' ';
115
+    if (ii >= 10) {
116
+      conv[4] = neg ? '-' : ' ';
117
+      conv[5] = DIGIMOD(ii, 10);
118
+    }
119
+    else {
120
+      conv[5] = neg ? '-' : ' ';
121
+    }
122
+  }
123
+  conv[6] = DIGIMOD(ii, 1);
124
+  return &conv[3];
125
+}
126
+
127
+// Convert unsigned float to string with 1.23 format
128
+char* ftostr12ns(const float &f) {
129
+  const long i = ((f < 0 ? -f : f) * 1000 + 5) / 10;
130
+  conv[3] = DIGIMOD(i, 100);
131
+  conv[4] = '.';
132
+  conv[5] = DIGIMOD(i, 10);
133
+  conv[6] = DIGIMOD(i, 1);
134
+  return &conv[3];
135
+}
136
+
137
+// Convert signed float to fixed-length string with 12.34 / -2.34 format or 123.45 / -23.45 format
138
+char* ftostr42_52(const float &f) {
139
+  if (f <= -10 || f >= 100) return ftostr52(f); // need more digits
140
+  long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
141
+  conv[2] = (f >= 0 && f < 10) ? ' ' : MINUSOR(i, DIGIMOD(i, 1000));
142
+  conv[3] = DIGIMOD(i, 100);
143
+  conv[4] = '.';
144
+  conv[5] = DIGIMOD(i, 10);
145
+  conv[6] = DIGIMOD(i, 1);
146
+  return &conv[2];
147
+}
148
+
149
+// Convert signed float to fixed-length string with 023.45 / -23.45 format
150
+char* ftostr52(const float &f) {
151
+  long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
152
+  conv[1] = MINUSOR(i, DIGIMOD(i, 10000));
153
+  conv[2] = DIGIMOD(i, 1000);
154
+  conv[3] = DIGIMOD(i, 100);
155
+  conv[4] = '.';
156
+  conv[5] = DIGIMOD(i, 10);
157
+  conv[6] = DIGIMOD(i, 1);
158
+  return &conv[1];
159
+}
160
+
161
+#if ENABLED(LCD_DECIMAL_SMALL_XY)
162
+
163
+  // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
164
+  char* ftostr4sign(const float &f) {
165
+    const int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
166
+    if (!WITHIN(i, -99, 999)) return i16tostr4sign((int)f);
167
+    const bool neg = i < 0;
168
+    const int ii = neg ? -i : i;
169
+    conv[3] = neg ? '-' : (ii >= 100 ? DIGIMOD(ii, 100) : ' ');
170
+    conv[4] = DIGIMOD(ii, 10);
171
+    conv[5] = '.';
172
+    conv[6] = DIGIMOD(ii, 1);
173
+    return &conv[3];
174
+  }
175
+
176
+#endif // LCD_DECIMAL_SMALL_XY
177
+
178
+// Convert float to fixed-length string with +123.4 / -123.4 format
179
+char* ftostr41sign(const float &f) {
180
+  int i = (f * 100 + (f < 0 ? -5: 5)) / 10;
181
+  conv[1] = MINUSOR(i, '+');
182
+  conv[2] = DIGIMOD(i, 1000);
183
+  conv[3] = DIGIMOD(i, 100);
184
+  conv[4] = DIGIMOD(i, 10);
185
+  conv[5] = '.';
186
+  conv[6] = DIGIMOD(i, 1);
187
+  return &conv[1];
188
+}
189
+
190
+// Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
191
+char* ftostr43sign(const float &f, char plus/*=' '*/) {
192
+  long i = (f * 10000 + (f < 0 ? -5: 5)) / 10;
193
+  conv[1] = i ? MINUSOR(i, plus) : ' ';
194
+  conv[2] = DIGIMOD(i, 1000);
195
+  conv[3] = '.';
196
+  conv[4] = DIGIMOD(i, 100);
197
+  conv[5] = DIGIMOD(i, 10);
198
+  conv[6] = DIGIMOD(i, 1);
199
+  return &conv[1];
200
+}
201
+
202
+// Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
203
+char* ftostr54sign(const float &f, char plus/*=' '*/) {
204
+  long i = (f * 100000 + (f < 0 ? -5: 5)) / 10;
205
+  conv[0] = i ? MINUSOR(i, plus) : ' ';
206
+  conv[1] = DIGIMOD(i, 10000);
207
+  conv[2] = '.';
208
+  conv[3] = DIGIMOD(i, 1000);
209
+  conv[4] = DIGIMOD(i, 100);
210
+  conv[5] = DIGIMOD(i, 10);
211
+  conv[6] = DIGIMOD(i, 1);
212
+  return &conv[0];
213
+}
214
+
215
+// Convert unsigned float to rj string with 12345 format
216
+char* ftostr5rj(const float &f) {
217
+  const long i = ((f < 0 ? -f : f) * 10 + 5) / 10;
218
+  conv[2] = RJDIGIT(i, 10000);
219
+  conv[3] = RJDIGIT(i, 1000);
220
+  conv[4] = RJDIGIT(i, 100);
221
+  conv[5] = RJDIGIT(i, 10);
222
+  conv[6] = DIGIMOD(i, 1);
223
+  return &conv[2];
224
+}
225
+
226
+// Convert signed float to string with +1234.5 format
227
+char* ftostr51sign(const float &f) {
228
+  long i = (f * 100 + (f < 0 ? -5: 5)) / 10;
229
+  conv[0] = MINUSOR(i, '+');
230
+  conv[1] = DIGIMOD(i, 10000);
231
+  conv[2] = DIGIMOD(i, 1000);
232
+  conv[3] = DIGIMOD(i, 100);
233
+  conv[4] = DIGIMOD(i, 10);
234
+  conv[5] = '.';
235
+  conv[6] = DIGIMOD(i, 1);
236
+  return conv;
237
+}
238
+
239
+// Convert signed float to string with +123.45 format
240
+char* ftostr52sign(const float &f) {
241
+  long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
242
+  conv[0] = MINUSOR(i, '+');
243
+  conv[1] = DIGIMOD(i, 10000);
244
+  conv[2] = DIGIMOD(i, 1000);
245
+  conv[3] = DIGIMOD(i, 100);
246
+  conv[4] = '.';
247
+  conv[5] = DIGIMOD(i, 10);
248
+  conv[6] = DIGIMOD(i, 1);
249
+  return conv;
250
+}
251
+
252
+// Convert unsigned float to string with 1234.5 format omitting trailing zeros
253
+char* ftostr51rj(const float &f) {
254
+  const long i = ((f < 0 ? -f : f) * 100 + 5) / 10;
255
+  conv[0] = ' ';
256
+  conv[1] = RJDIGIT(i, 10000);
257
+  conv[2] = RJDIGIT(i, 1000);
258
+  conv[3] = RJDIGIT(i, 100);
259
+  conv[4] = DIGIMOD(i, 10);
260
+  conv[5] = '.';
261
+  conv[6] = DIGIMOD(i, 1);
262
+  return conv;
263
+}
264
+
265
+// Convert signed float to space-padded string with -_23.4_ format
266
+char* ftostr52sp(const float &f) {
267
+  long i = (f * 1000 + (f < 0 ? -5: 5)) / 10;
268
+  uint8_t dig;
269
+  conv[0] = MINUSOR(i, ' ');
270
+  conv[1] = RJDIGIT(i, 10000);
271
+  conv[2] = RJDIGIT(i, 1000);
272
+  conv[3] = DIGIMOD(i, 100);
273
+
274
+  if ((dig = i % 10)) {          // second digit after decimal point?
275
+    conv[4] = '.';
276
+    conv[5] = DIGIMOD(i, 10);
277
+    conv[6] = DIGIT(dig);
278
+  }
279
+  else {
280
+    if ((dig = (i / 10) % 10)) { // first digit after decimal point?
281
+      conv[4] = '.';
282
+      conv[5] = DIGIT(dig);
283
+    }
284
+    else                          // nothing after decimal point
285
+      conv[4] = conv[5] = ' ';
286
+    conv[6] = ' ';
287
+  }
288
+  return conv;
289
+}

+ 92
- 0
Marlin/src/libs/numtostr.h Näytä tiedosto

@@ -0,0 +1,92 @@
1
+/**
2
+ * Marlin 3D Printer Firmware
3
+ * Copyright (C) 2019 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
+#pragma once
23
+
24
+#include "../inc/MarlinConfigPre.h"
25
+
26
+// Convert a full-range unsigned 8bit int to a percentage
27
+char* ui8tostr4pct(const uint8_t i);
28
+
29
+// Convert uint8_t to string with 123 format
30
+char* ui8tostr3(const uint8_t x);
31
+
32
+// Convert int8_t to string with 123 format
33
+char* i8tostr3(const int8_t x);
34
+
35
+// Convert uint16_t to string with 123 format
36
+char* ui16tostr3(const uint16_t x);
37
+
38
+// Convert uint16_t to string with 1234 format
39
+char* ui16tostr4(const uint16_t x);
40
+
41
+// Convert int16_t to string with 123 format
42
+char* i16tostr3(const int16_t x);
43
+
44
+// Convert unsigned int to lj string with 123 format
45
+char* i16tostr3left(const int16_t xx);
46
+
47
+// Convert signed int to rj string with _123, -123, _-12, or __-1 format
48
+char* i16tostr4sign(const int16_t x);
49
+
50
+// Convert unsigned float to string with 1.23 format
51
+char* ftostr12ns(const float &x);
52
+
53
+// Convert signed float to fixed-length string with 12.34 / -2.34 or 023.45 / -23.45 format
54
+char* ftostr42_52(const float &x);
55
+
56
+// Convert signed float to fixed-length string with 023.45 / -23.45 format
57
+char* ftostr52(const float &x);
58
+
59
+// Convert float to fixed-length string with +123.4 / -123.4 format
60
+char* ftostr41sign(const float &x);
61
+
62
+// Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
63
+char* ftostr43sign(const float &x, char plus=' ');
64
+
65
+// Convert signed float to string (5 digit) with -1.2345 / _0.0000 / +1.2345 format
66
+char* ftostr54sign(const float &x, char plus=' ');
67
+
68
+// Convert unsigned float to rj string with 12345 format
69
+char* ftostr5rj(const float &x);
70
+
71
+// Convert signed float to string with +1234.5 format
72
+char* ftostr51sign(const float &x);
73
+
74
+// Convert signed float to space-padded string with -_23.4_ format
75
+char* ftostr52sp(const float &x);
76
+
77
+// Convert signed float to string with +123.45 format
78
+char* ftostr52sign(const float &x);
79
+
80
+// Convert unsigned float to string with 1234.5 format omitting trailing zeros
81
+char* ftostr51rj(const float &x);
82
+
83
+// Convert float to rj string with 123 or -12 format
84
+FORCE_INLINE char* ftostr3(const float &x) { return i16tostr3(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
85
+
86
+#if ENABLED(LCD_DECIMAL_SMALL_XY)
87
+  // Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format
88
+  char* ftostr4sign(const float &fx);
89
+#else
90
+  // Convert float to rj string with 1234, _123, -123, __12, _-12, ___1, or __-1 format
91
+  FORCE_INLINE char* ftostr4sign(const float &x) { return i16tostr4sign(int16_t(x + (x < 0 ? -0.5f : 0.5f))); }
92
+#endif

Loading…
Peruuta
Tallenna