Browse Source

Least stack-usage self-contained ftostr32np()

This is the optimal code for a self-contained formatter, although the
original code is crafty in being smaller and simpler, and can be
evaluated as using the original output as a scratch pad for state,
making the final formatter more straightforward. While this code is
longer, all code-paths are minimal.
Scott Lahteine 10 years ago
parent
commit
f80a602783
1 changed files with 41 additions and 8 deletions
  1. 41
    8
      Marlin/ultralcd.cpp

+ 41
- 8
Marlin/ultralcd.cpp View File

1445
 
1445
 
1446
 //  convert float to space-padded string with -_23.4_ format
1446
 //  convert float to space-padded string with -_23.4_ format
1447
 char *ftostr32np(const float &x) {
1447
 char *ftostr32np(const float &x) {
1448
-  char *c = ftostr32(x);
1449
-  if (c[0] == '0' || c[0] == '-') {
1450
-    if (c[0] == '0') c[0] = ' ';
1451
-    if (c[1] == '0') c[1] = ' ';
1448
+  long xx = abs(x * 100);
1449
+  uint8_t dig;
1450
+
1451
+  if (x < 0) { // negative val = -_0
1452
+    conv[0] = '-';
1453
+    dig = (xx / 1000) % 10;
1454
+    conv[1] = dig ? '0' + dig : ' ';
1455
+  }
1456
+  else { // positive val = __0
1457
+    dig = (xx / 10000) % 10;
1458
+    if (dig) {
1459
+      conv[0] = '0' + dig;
1460
+      conv[1] = '0' + (xx / 1000) % 10;
1461
+    }
1462
+    else {
1463
+      conv[0] = ' ';
1464
+      dig = (xx / 1000) % 10;
1465
+      conv[1] = dig ? '0' + dig : ' ';
1466
+    }
1467
+  }
1468
+
1469
+  conv[2] = '0' + (xx / 100) % 10; // lsd always
1470
+
1471
+  dig = xx % 10;
1472
+  if (dig) { // 2 decimal places
1473
+    conv[5] = '0' + dig;
1474
+    dig = (xx / 10) % 10;
1475
+    conv[4] = '0' + dig;
1476
+    conv[3] = '.';
1452
   }
1477
   }
1453
-  if (c[5] == '0') {
1454
-    c[5] = ' ';
1455
-    if (c[4] == '0') c[4] = c[3] = ' ';
1478
+  else { // 1 or 0 decimal place
1479
+    dig = (xx / 10) % 10;
1480
+    if (dig) {
1481
+      conv[4] = '0' + dig;
1482
+      conv[3] = '.';
1483
+    }
1484
+    else {
1485
+      conv[3] = conv[4] = ' ';
1486
+    }
1487
+    conv[5] = ' ';
1456
   }
1488
   }
1457
-  return c;
1489
+  conv[6] = '\0';
1490
+  return conv;
1458
 }
1491
 }
1459
 
1492
 
1460
 char *itostr31(const int &xx)
1493
 char *itostr31(const int &xx)

Loading…
Cancel
Save