Переглянути джерело

Merge pull request #3306 from thinkyhead/rc_fix_num2str

Clean up num2str functions, extend ftostr43
Scott Lahteine 8 роки тому
джерело
коміт
9c43369ebb
2 змінених файлів з 59 додано та 47 видалено
  1. 55
    43
      Marlin/ultralcd.cpp
  2. 4
    4
      Marlin/ultralcd.h

+ 55
- 43
Marlin/ultralcd.cpp Переглянути файл

@@ -2166,7 +2166,7 @@ char *ftostr3(const float& x) { return itostr3((int)x); }
2166 2166
 // Convert float to rj string with _123, -123, _-12, or __-1 format
2167 2167
 char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
2168 2168
 
2169
-// Convert int to string with 12 format
2169
+// Convert unsigned int to string with 12 format
2170 2170
 char* itostr2(const uint8_t& x) {
2171 2171
   //sprintf(conv,"%5.1f",x);
2172 2172
   int xx = x;
@@ -2176,7 +2176,7 @@ char* itostr2(const uint8_t& x) {
2176 2176
   return conv;
2177 2177
 }
2178 2178
 
2179
-// Convert float to string with +123.4 format
2179
+// Convert float to string with +123.4 / -123.4 format
2180 2180
 char* ftostr31(const float& x) {
2181 2181
   int xx = abs(x * 10);
2182 2182
   conv[0] = (x >= 0) ? '+' : '-';
@@ -2189,7 +2189,7 @@ char* ftostr31(const float& x) {
2189 2189
   return conv;
2190 2190
 }
2191 2191
 
2192
-// Convert float to string with 123.4 format, dropping sign
2192
+// Convert unsigned float to string with 123.4 format, dropping sign
2193 2193
 char* ftostr31ns(const float& x) {
2194 2194
   int xx = abs(x * 10);
2195 2195
   conv[0] = (xx / 1000) % 10 + '0';
@@ -2201,7 +2201,7 @@ char* ftostr31ns(const float& x) {
2201 2201
   return conv;
2202 2202
 }
2203 2203
 
2204
-// Convert float to string with 123.45 format
2204
+// Convert signed float to string with 023.45 / -23.45 format
2205 2205
 char *ftostr32(const float& x) {
2206 2206
   long xx = abs(x * 100);
2207 2207
   conv[0] = x >= 0 ? (xx / 10000) % 10 + '0' : '-';
@@ -2214,23 +2214,27 @@ char *ftostr32(const float& x) {
2214 2214
   return conv;
2215 2215
 }
2216 2216
 
2217
-// Convert float to string with 1.234 format
2217
+// Convert signed float to string (len 5 or 6) with 1.234 / -1.234 format
2218 2218
 char* ftostr43(const float& x) {
2219 2219
   long xx = x * 1000;
2220
-  if (xx >= 0)
2221
-    conv[0] = (xx / 1000) % 10 + '0';
2222
-  else
2220
+  char *conv_ptr = conv;
2221
+  if (xx >= 0) {
2222
+    *conv_ptr++ = ' ';
2223
+  }
2224
+  else {
2223 2225
     conv[0] = '-';
2224
-  xx = abs(xx);
2225
-  conv[1] = '.';
2226
-  conv[2] = (xx / 100) % 10 + '0';
2227
-  conv[3] = (xx / 10) % 10 + '0';
2228
-  conv[4] = (xx) % 10 + '0';
2229
-  conv[5] = 0;
2230
-  return conv;
2226
+    xx = -xx;
2227
+  }
2228
+  conv[1] = (xx / 1000) % 10 + '0';
2229
+  conv[2] = '.';
2230
+  conv[3] = (xx / 100) % 10 + '0';
2231
+  conv[4] = (xx / 10) % 10 + '0';
2232
+  conv[5] = (xx) % 10 + '0';
2233
+  conv[6] = 0;
2234
+  return conv_ptr;
2231 2235
 }
2232 2236
 
2233
-// Convert float to string with 1.23 format
2237
+// Convert unsigned float to string with 1.23 format
2234 2238
 char* ftostr12ns(const float& x) {
2235 2239
   long xx = x * 100;
2236 2240
   xx = abs(xx);
@@ -2242,11 +2246,12 @@ char* ftostr12ns(const float& x) {
2242 2246
   return conv;
2243 2247
 }
2244 2248
 
2245
-// Convert float to space-padded string with -_23.4_ format
2249
+// Convert signed float to space-padded string with -_23.4_ format
2246 2250
 char* ftostr32sp(const float& x) {
2247
-  long xx = abs(x * 100);
2251
+  long xx = x * 100;
2248 2252
   uint8_t dig;
2249
-  if (x < 0) { // negative val = -_0
2253
+  if (xx < 0) { // negative val = -_0
2254
+    xx = -xx;
2250 2255
     conv[0] = '-';
2251 2256
     dig = (xx / 1000) % 10;
2252 2257
     conv[1] = dig ? '0' + dig : ' ';
@@ -2287,10 +2292,17 @@ char* ftostr32sp(const float& x) {
2287 2292
   return conv;
2288 2293
 }
2289 2294
 
2290
-// Convert int to lj string with +123.0 format
2295
+// Convert signed int to lj string with +012.0 / -012.0 format
2291 2296
 char* itostr31(const int& x) {
2292
-  conv[0] = x >= 0 ? '+' : '-';
2293
-  int xx = abs(x);
2297
+  int xx;
2298
+  if (x >= 0) {
2299
+    conv[0] = '+';
2300
+    xx = x;
2301
+  }
2302
+  else {
2303
+    conv[0] = '-';
2304
+    xx = -x;
2305
+  }
2294 2306
   conv[1] = (xx / 100) % 10 + '0';
2295 2307
   conv[2] = (xx / 10) % 10 + '0';
2296 2308
   conv[3] = xx % 10 + '0';
@@ -2300,7 +2312,7 @@ char* itostr31(const int& x) {
2300 2312
   return conv;
2301 2313
 }
2302 2314
 
2303
-// Convert int to rj string with 123 or -12 format
2315
+// Convert signed int to rj string with 123 or -12 format
2304 2316
 char* itostr3(const int& x) {
2305 2317
   int xx = x;
2306 2318
   if (xx < 0) {
@@ -2316,37 +2328,37 @@ char* itostr3(const int& x) {
2316 2328
   return conv;
2317 2329
 }
2318 2330
 
2319
-// Convert int to lj string with 123 format
2320
-char* itostr3left(const int& xx) {
2321
-  if (xx >= 100) {
2322
-    conv[0] = (xx / 100) % 10 + '0';
2323
-    conv[1] = (xx / 10) % 10 + '0';
2324
-    conv[2] = xx % 10 + '0';
2331
+// Convert unsigned int to lj string with 123 format
2332
+char* itostr3left(const int& x) {
2333
+  if (x >= 100) {
2334
+    conv[0] = (x / 100) % 10 + '0';
2335
+    conv[1] = (x / 10) % 10 + '0';
2336
+    conv[2] = x % 10 + '0';
2325 2337
     conv[3] = 0;
2326 2338
   }
2327
-  else if (xx >= 10) {
2328
-    conv[0] = (xx / 10) % 10 + '0';
2329
-    conv[1] = xx % 10 + '0';
2339
+  else if (x >= 10) {
2340
+    conv[0] = (x / 10) % 10 + '0';
2341
+    conv[1] = x % 10 + '0';
2330 2342
     conv[2] = 0;
2331 2343
   }
2332 2344
   else {
2333
-    conv[0] = xx % 10 + '0';
2345
+    conv[0] = x % 10 + '0';
2334 2346
     conv[1] = 0;
2335 2347
   }
2336 2348
   return conv;
2337 2349
 }
2338 2350
 
2339
-// Convert int to rj string with 1234 format
2340
-char* itostr4(const int& xx) {
2341
-  conv[0] = xx >= 1000 ? (xx / 1000) % 10 + '0' : ' ';
2342
-  conv[1] = xx >= 100 ? (xx / 100) % 10 + '0' : ' ';
2343
-  conv[2] = xx >= 10 ? (xx / 10) % 10 + '0' : ' ';
2344
-  conv[3] = xx % 10 + '0';
2351
+// Convert unsigned int to rj string with 1234 format
2352
+char* itostr4(const int& x) {
2353
+  conv[0] = x >= 1000 ? (x / 1000) % 10 + '0' : ' ';
2354
+  conv[1] = x >= 100 ? (x / 100) % 10 + '0' : ' ';
2355
+  conv[2] = x >= 10 ? (x / 10) % 10 + '0' : ' ';
2356
+  conv[3] = x % 10 + '0';
2345 2357
   conv[4] = 0;
2346 2358
   return conv;
2347 2359
 }
2348 2360
 
2349
-// Convert int to rj string with _123, -123, _-12, or __-1 format
2361
+// Convert signed int to rj string with _123, -123, _-12, or __-1 format
2350 2362
 char *itostr4sign(const int& x) {
2351 2363
   int xx = abs(x);
2352 2364
   int sign = 0;
@@ -2370,7 +2382,7 @@ char *itostr4sign(const int& x) {
2370 2382
   return conv;
2371 2383
 }
2372 2384
 
2373
-// Convert float to rj string with 12345 format
2385
+// Convert unsigned float to rj string with 12345 format
2374 2386
 char* ftostr5(const float& x) {
2375 2387
   long xx = abs(x);
2376 2388
   conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
@@ -2382,7 +2394,7 @@ char* ftostr5(const float& x) {
2382 2394
   return conv;
2383 2395
 }
2384 2396
 
2385
-// Convert float to string with +1234.5 format
2397
+// Convert signed float to string with +1234.5 format
2386 2398
 char* ftostr51(const float& x) {
2387 2399
   long xx = abs(x * 10);
2388 2400
   conv[0] = (x >= 0) ? '+' : '-';
@@ -2396,7 +2408,7 @@ char* ftostr51(const float& x) {
2396 2408
   return conv;
2397 2409
 }
2398 2410
 
2399
-// Convert float to string with +123.45 format
2411
+// Convert signed float to string with +123.45 format
2400 2412
 char* ftostr52(const float& x) {
2401 2413
   conv[0] = (x >= 0) ? '+' : '-';
2402 2414
   long xx = abs(x * 100);

+ 4
- 4
Marlin/ultralcd.h Переглянути файл

@@ -145,10 +145,10 @@
145 145
 #endif //ULTRA_LCD
146 146
 
147 147
 char* itostr2(const uint8_t& x);
148
-char* itostr31(const int& xx);
149
-char* itostr3(const int& xx);
150
-char* itostr3left(const int& xx);
151
-char* itostr4(const int& xx);
148
+char* itostr31(const int& x);
149
+char* itostr3(const int& x);
150
+char* itostr3left(const int& x);
151
+char* itostr4(const int& x);
152 152
 char* itostr4sign(const int& x);
153 153
 
154 154
 char* ftostr3(const float& x);

Завантаження…
Відмінити
Зберегти