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.
pull/1/head
Scott Lahteine 10 years ago
parent
commit
449dad71f2
  1. 49
      Marlin/ultralcd.cpp

49
Marlin/ultralcd.cpp

@ -1498,16 +1498,49 @@ char *ftostr12ns(const float &x)
// convert float to space-padded string with -_23.4_ format // convert float to space-padded string with -_23.4_ format
char *ftostr32np(const float &x) { char *ftostr32np(const float &x) {
char *c = ftostr32(x); long xx = abs(x * 100);
if (c[0] == '0' || c[0] == '-') { uint8_t dig;
if (c[0] == '0') c[0] = ' ';
if (c[1] == '0') c[1] = ' '; if (x < 0) { // negative val = -_0
conv[0] = '-';
dig = (xx / 1000) % 10;
conv[1] = dig ? '0' + dig : ' ';
}
else { // positive val = __0
dig = (xx / 10000) % 10;
if (dig) {
conv[0] = '0' + dig;
conv[1] = '0' + (xx / 1000) % 10;
}
else {
conv[0] = ' ';
dig = (xx / 1000) % 10;
conv[1] = dig ? '0' + dig : ' ';
}
}
conv[2] = '0' + (xx / 100) % 10; // lsd always
dig = xx % 10;
if (dig) { // 2 decimal places
conv[5] = '0' + dig;
dig = (xx / 10) % 10;
conv[4] = '0' + dig;
conv[3] = '.';
} }
if (c[5] == '0') { else { // 1 or 0 decimal place
c[5] = ' '; dig = (xx / 10) % 10;
if (c[4] == '0') c[4] = c[3] = ' '; if (dig) {
conv[4] = '0' + dig;
conv[3] = '.';
}
else {
conv[3] = conv[4] = ' ';
}
conv[5] = ' ';
} }
return c; conv[6] = '\0';
return conv;
} }
char *itostr31(const int &xx) char *itostr31(const int &xx)

Loading…
Cancel
Save