Browse Source

Merge pull request #3306 from thinkyhead/rc_fix_num2str

Clean up num2str functions, extend ftostr43
Scott Lahteine 8 years ago
parent
commit
9c43369ebb
2 changed files with 59 additions and 47 deletions
  1. 55
    43
      Marlin/ultralcd.cpp
  2. 4
    4
      Marlin/ultralcd.h

+ 55
- 43
Marlin/ultralcd.cpp View File

2166
 // Convert float to rj string with _123, -123, _-12, or __-1 format
2166
 // Convert float to rj string with _123, -123, _-12, or __-1 format
2167
 char *ftostr4sign(const float& x) { return itostr4sign((int)x); }
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
 char* itostr2(const uint8_t& x) {
2170
 char* itostr2(const uint8_t& x) {
2171
   //sprintf(conv,"%5.1f",x);
2171
   //sprintf(conv,"%5.1f",x);
2172
   int xx = x;
2172
   int xx = x;
2176
   return conv;
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
 char* ftostr31(const float& x) {
2180
 char* ftostr31(const float& x) {
2181
   int xx = abs(x * 10);
2181
   int xx = abs(x * 10);
2182
   conv[0] = (x >= 0) ? '+' : '-';
2182
   conv[0] = (x >= 0) ? '+' : '-';
2189
   return conv;
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
 char* ftostr31ns(const float& x) {
2193
 char* ftostr31ns(const float& x) {
2194
   int xx = abs(x * 10);
2194
   int xx = abs(x * 10);
2195
   conv[0] = (xx / 1000) % 10 + '0';
2195
   conv[0] = (xx / 1000) % 10 + '0';
2201
   return conv;
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
 char *ftostr32(const float& x) {
2205
 char *ftostr32(const float& x) {
2206
   long xx = abs(x * 100);
2206
   long xx = abs(x * 100);
2207
   conv[0] = x >= 0 ? (xx / 10000) % 10 + '0' : '-';
2207
   conv[0] = x >= 0 ? (xx / 10000) % 10 + '0' : '-';
2214
   return conv;
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
 char* ftostr43(const float& x) {
2218
 char* ftostr43(const float& x) {
2219
   long xx = x * 1000;
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
     conv[0] = '-';
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
 char* ftostr12ns(const float& x) {
2238
 char* ftostr12ns(const float& x) {
2235
   long xx = x * 100;
2239
   long xx = x * 100;
2236
   xx = abs(xx);
2240
   xx = abs(xx);
2242
   return conv;
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
 char* ftostr32sp(const float& x) {
2250
 char* ftostr32sp(const float& x) {
2247
-  long xx = abs(x * 100);
2251
+  long xx = x * 100;
2248
   uint8_t dig;
2252
   uint8_t dig;
2249
-  if (x < 0) { // negative val = -_0
2253
+  if (xx < 0) { // negative val = -_0
2254
+    xx = -xx;
2250
     conv[0] = '-';
2255
     conv[0] = '-';
2251
     dig = (xx / 1000) % 10;
2256
     dig = (xx / 1000) % 10;
2252
     conv[1] = dig ? '0' + dig : ' ';
2257
     conv[1] = dig ? '0' + dig : ' ';
2287
   return conv;
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
 char* itostr31(const int& x) {
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
   conv[1] = (xx / 100) % 10 + '0';
2306
   conv[1] = (xx / 100) % 10 + '0';
2295
   conv[2] = (xx / 10) % 10 + '0';
2307
   conv[2] = (xx / 10) % 10 + '0';
2296
   conv[3] = xx % 10 + '0';
2308
   conv[3] = xx % 10 + '0';
2300
   return conv;
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
 char* itostr3(const int& x) {
2316
 char* itostr3(const int& x) {
2305
   int xx = x;
2317
   int xx = x;
2306
   if (xx < 0) {
2318
   if (xx < 0) {
2316
   return conv;
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
     conv[3] = 0;
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
     conv[2] = 0;
2342
     conv[2] = 0;
2331
   }
2343
   }
2332
   else {
2344
   else {
2333
-    conv[0] = xx % 10 + '0';
2345
+    conv[0] = x % 10 + '0';
2334
     conv[1] = 0;
2346
     conv[1] = 0;
2335
   }
2347
   }
2336
   return conv;
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
   conv[4] = 0;
2357
   conv[4] = 0;
2346
   return conv;
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
 char *itostr4sign(const int& x) {
2362
 char *itostr4sign(const int& x) {
2351
   int xx = abs(x);
2363
   int xx = abs(x);
2352
   int sign = 0;
2364
   int sign = 0;
2370
   return conv;
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
 char* ftostr5(const float& x) {
2386
 char* ftostr5(const float& x) {
2375
   long xx = abs(x);
2387
   long xx = abs(x);
2376
   conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
2388
   conv[0] = xx >= 10000 ? (xx / 10000) % 10 + '0' : ' ';
2382
   return conv;
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
 char* ftostr51(const float& x) {
2398
 char* ftostr51(const float& x) {
2387
   long xx = abs(x * 10);
2399
   long xx = abs(x * 10);
2388
   conv[0] = (x >= 0) ? '+' : '-';
2400
   conv[0] = (x >= 0) ? '+' : '-';
2396
   return conv;
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
 char* ftostr52(const float& x) {
2412
 char* ftostr52(const float& x) {
2401
   conv[0] = (x >= 0) ? '+' : '-';
2413
   conv[0] = (x >= 0) ? '+' : '-';
2402
   long xx = abs(x * 100);
2414
   long xx = abs(x * 100);

+ 4
- 4
Marlin/ultralcd.h View File

145
 #endif //ULTRA_LCD
145
 #endif //ULTRA_LCD
146
 
146
 
147
 char* itostr2(const uint8_t& x);
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
 char* itostr4sign(const int& x);
152
 char* itostr4sign(const int& x);
153
 
153
 
154
 char* ftostr3(const float& x);
154
 char* ftostr3(const float& x);

Loading…
Cancel
Save