From f390b3f31454a0cb9506957d15cc49ba3e5a0b0f Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 6 Dec 2014 20:59:06 -0800 Subject: [PATCH 01/10] Patch to make Z look more like X and Y on UltraLCD --- Marlin/ultralcd.cpp | 14 ++++++++++++++ Marlin/ultralcd.h | 1 + Marlin/ultralcd_implementation_hitachi_HD44780.h | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index fe12761f1..fbfa859c5 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1443,6 +1443,20 @@ char *ftostr12ns(const float &x) return conv; } +// convert float to space-padded string with -_23.4_ format +char *ftostr32np(const float &x) { + char *c = ftostr32(x); + if (c[0] == '0' || c[0] == '-') { + if (c[0] == '0') c[0] = ' '; + if (c[1] == '0') c[1] = ' '; + } + if (c[5] == '0') { + c[5] = ' '; + if (c[4] == '0') c[4] = c[3] = ' '; + } + return c; +} + char *itostr31(const int &xx) { conv[0]=(xx>=0)?'+':'-'; diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h index 339955e59..44f176a80 100644 --- a/Marlin/ultralcd.h +++ b/Marlin/ultralcd.h @@ -119,6 +119,7 @@ char *ftostr31ns(const float &x); // float to string without sign character char *ftostr31(const float &x); char *ftostr32(const float &x); char *ftostr12ns(const float &x); +char *ftostr32np(const float &x); // remove zero-padding from ftostr32 char *ftostr5(const float &x); char *ftostr51(const float &x); char *ftostr52(const float &x); diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index 923e249c8..b7ab9c6c4 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -475,7 +475,7 @@ static void lcd_implementation_status_screen() # endif//LCD_WIDTH > 19 lcd.setCursor(LCD_WIDTH - 8, 1); lcd.print('Z'); - lcd.print(ftostr32(current_position[Z_AXIS] + 0.00001)); + lcd.print(ftostr32np(current_position[Z_AXIS] + 0.00001)); #endif//LCD_HEIGHT > 2 #if LCD_HEIGHT > 3 From f80a602783338ebd573b3c5dec793ff560a33708 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 8 Dec 2014 01:43:44 -0800 Subject: [PATCH 02/10] 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. --- Marlin/ultralcd.cpp | 49 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index fbfa859c5..9bd0d548d 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1445,16 +1445,49 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { - char *c = ftostr32(x); - if (c[0] == '0' || c[0] == '-') { - if (c[0] == '0') c[0] = ' '; - if (c[1] == '0') c[1] = ' '; + long xx = abs(x * 100); + uint8_t dig; + + 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') { - c[5] = ' '; - if (c[4] == '0') c[4] = c[3] = ' '; + else { // 1 or 0 decimal place + dig = (xx / 10) % 10; + 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) From e83ecec809e888a7bdf2d471e417a7b55400b130 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 22 Dec 2014 17:04:22 -0800 Subject: [PATCH 03/10] As it should be --- Marlin/ultralcd.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 9bd0d548d..c148641a3 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1445,6 +1445,7 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { +<<<<<<< HEAD long xx = abs(x * 100); uint8_t dig; @@ -1488,6 +1489,18 @@ char *ftostr32np(const float &x) { } conv[6] = '\0'; return conv; +======= + char *c = ftostr32(x); + if (c[0] == '0' || c[0] == '-') { + if (c[0] == '0') c[0] = ' '; + if (c[1] == '0') c[1] = ' '; + } + if (c[5] == '0') { + c[5] = ' '; + if (c[4] == '0') c[4] = c[3] = ' '; + } + return c; +>>>>>>> Patch to make Z look more like X and Y on UltraLCD } char *itostr31(const int &xx) From bdf2c94bffbbfdd9432186ce32efed81f21e744a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 8 Dec 2014 01:43:44 -0800 Subject: [PATCH 04/10] 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. --- Marlin/ultralcd.cpp | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index c148641a3..84eaca59e 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1445,7 +1445,6 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { -<<<<<<< HEAD long xx = abs(x * 100); uint8_t dig; @@ -1472,8 +1471,7 @@ char *ftostr32np(const float &x) { dig = xx % 10; if (dig) { // 2 decimal places conv[5] = '0' + dig; - dig = (xx / 10) % 10; - conv[4] = '0' + dig; + conv[4] = '0' + (xx / 10) % 10; conv[3] = '.'; } else { // 1 or 0 decimal place @@ -1489,18 +1487,6 @@ char *ftostr32np(const float &x) { } conv[6] = '\0'; return conv; -======= - char *c = ftostr32(x); - if (c[0] == '0' || c[0] == '-') { - if (c[0] == '0') c[0] = ' '; - if (c[1] == '0') c[1] = ' '; - } - if (c[5] == '0') { - c[5] = ' '; - if (c[4] == '0') c[4] = c[3] = ' '; - } - return c; ->>>>>>> Patch to make Z look more like X and Y on UltraLCD } char *itostr31(const int &xx) From d080027ff4bcb3b9645399c736b9947a91c230e5 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 22 Dec 2014 17:11:35 -0800 Subject: [PATCH 05/10] ftostr32np > ftostr32sp sp=space-padded, similar to: ns=no-sign --- Marlin/ultralcd.cpp | 2 +- Marlin/ultralcd.h | 2 +- Marlin/ultralcd_implementation_hitachi_HD44780.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 84eaca59e..e2a9e2717 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1444,7 +1444,7 @@ char *ftostr12ns(const float &x) } // convert float to space-padded string with -_23.4_ format -char *ftostr32np(const float &x) { +char *ftostr32sp(const float &x) { long xx = abs(x * 100); uint8_t dig; diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h index 44f176a80..351a2654b 100644 --- a/Marlin/ultralcd.h +++ b/Marlin/ultralcd.h @@ -119,7 +119,7 @@ char *ftostr31ns(const float &x); // float to string without sign character char *ftostr31(const float &x); char *ftostr32(const float &x); char *ftostr12ns(const float &x); -char *ftostr32np(const float &x); // remove zero-padding from ftostr32 +char *ftostr32sp(const float &x); // remove zero-padding from ftostr32 char *ftostr5(const float &x); char *ftostr51(const float &x); char *ftostr52(const float &x); diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index b7ab9c6c4..e6cab02bc 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -475,7 +475,7 @@ static void lcd_implementation_status_screen() # endif//LCD_WIDTH > 19 lcd.setCursor(LCD_WIDTH - 8, 1); lcd.print('Z'); - lcd.print(ftostr32np(current_position[Z_AXIS] + 0.00001)); + lcd.print(ftostr32sp(current_position[Z_AXIS] + 0.00001)); #endif//LCD_HEIGHT > 2 #if LCD_HEIGHT > 3 From a2109cb492e92e969031aa424533937d6c3d8f38 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 6 Dec 2014 20:59:06 -0800 Subject: [PATCH 06/10] Patch to make Z look more like X and Y on UltraLCD --- Marlin/ultralcd.cpp | 15 ++++++++++++++- Marlin/ultralcd.h | 1 + Marlin/ultralcd_implementation_hitachi_HD44780.h | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index d3060dc50..837cc3f72 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1496,7 +1496,20 @@ char *ftostr12ns(const float &x) return conv; } -// Convert int to lj string with +123.0 format +// convert float to space-padded string with -_23.4_ format +char *ftostr32np(const float &x) { + char *c = ftostr32(x); + if (c[0] == '0' || c[0] == '-') { + if (c[0] == '0') c[0] = ' '; + if (c[1] == '0') c[1] = ' '; + } + if (c[5] == '0') { + c[5] = ' '; + if (c[4] == '0') c[4] = c[3] = ' '; + } + return c; +} + char *itostr31(const int &xx) { conv[0]=(xx>=0)?'+':'-'; diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h index 339955e59..44f176a80 100644 --- a/Marlin/ultralcd.h +++ b/Marlin/ultralcd.h @@ -119,6 +119,7 @@ char *ftostr31ns(const float &x); // float to string without sign character char *ftostr31(const float &x); char *ftostr32(const float &x); char *ftostr12ns(const float &x); +char *ftostr32np(const float &x); // remove zero-padding from ftostr32 char *ftostr5(const float &x); char *ftostr51(const float &x); char *ftostr52(const float &x); diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index 29deabb4e..5d9711632 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -548,7 +548,7 @@ static void lcd_implementation_status_screen() # endif//LCD_WIDTH > 19 lcd.setCursor(LCD_WIDTH - 8, 1); lcd.print('Z'); - lcd.print(ftostr32(current_position[Z_AXIS] + 0.00001)); + lcd.print(ftostr32np(current_position[Z_AXIS] + 0.00001)); #endif//LCD_HEIGHT > 2 #if LCD_HEIGHT > 3 From 449dad71f2e72b95b552e0768952712649be49b0 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 8 Dec 2014 01:43:44 -0800 Subject: [PATCH 07/10] 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. --- Marlin/ultralcd.cpp | 49 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 837cc3f72..0b761be6a 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1498,16 +1498,49 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { - char *c = ftostr32(x); - if (c[0] == '0' || c[0] == '-') { - if (c[0] == '0') c[0] = ' '; - if (c[1] == '0') c[1] = ' '; + long xx = abs(x * 100); + uint8_t dig; + + 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') { - c[5] = ' '; - if (c[4] == '0') c[4] = c[3] = ' '; + else { // 1 or 0 decimal place + dig = (xx / 10) % 10; + 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) From d1f21d11890042306ab4d51ff8154127c1382711 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 22 Dec 2014 17:04:22 -0800 Subject: [PATCH 08/10] As it should be --- Marlin/ultralcd.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index 0b761be6a..fcb064b03 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1498,6 +1498,7 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { +<<<<<<< HEAD long xx = abs(x * 100); uint8_t dig; @@ -1541,6 +1542,18 @@ char *ftostr32np(const float &x) { } conv[6] = '\0'; return conv; +======= + char *c = ftostr32(x); + if (c[0] == '0' || c[0] == '-') { + if (c[0] == '0') c[0] = ' '; + if (c[1] == '0') c[1] = ' '; + } + if (c[5] == '0') { + c[5] = ' '; + if (c[4] == '0') c[4] = c[3] = ' '; + } + return c; +>>>>>>> Patch to make Z look more like X and Y on UltraLCD } char *itostr31(const int &xx) From 10e1b6ef8bca16e1640d839327d4cbfd01ff9df2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 8 Dec 2014 01:43:44 -0800 Subject: [PATCH 09/10] 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. --- Marlin/ultralcd.cpp | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index fcb064b03..cc387fdf5 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1498,7 +1498,6 @@ char *ftostr12ns(const float &x) // convert float to space-padded string with -_23.4_ format char *ftostr32np(const float &x) { -<<<<<<< HEAD long xx = abs(x * 100); uint8_t dig; @@ -1525,8 +1524,7 @@ char *ftostr32np(const float &x) { dig = xx % 10; if (dig) { // 2 decimal places conv[5] = '0' + dig; - dig = (xx / 10) % 10; - conv[4] = '0' + dig; + conv[4] = '0' + (xx / 10) % 10; conv[3] = '.'; } else { // 1 or 0 decimal place @@ -1542,18 +1540,6 @@ char *ftostr32np(const float &x) { } conv[6] = '\0'; return conv; -======= - char *c = ftostr32(x); - if (c[0] == '0' || c[0] == '-') { - if (c[0] == '0') c[0] = ' '; - if (c[1] == '0') c[1] = ' '; - } - if (c[5] == '0') { - c[5] = ' '; - if (c[4] == '0') c[4] = c[3] = ' '; - } - return c; ->>>>>>> Patch to make Z look more like X and Y on UltraLCD } char *itostr31(const int &xx) From e7db8ee9e50f80d4e57102aa0548d9672306c6e4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Mon, 22 Dec 2014 17:11:35 -0800 Subject: [PATCH 10/10] ftostr32np > ftostr32sp sp=space-padded, similar to: ns=no-sign --- Marlin/ultralcd.cpp | 2 +- Marlin/ultralcd.h | 2 +- Marlin/ultralcd_implementation_hitachi_HD44780.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/ultralcd.cpp b/Marlin/ultralcd.cpp index cc387fdf5..83895e2fd 100644 --- a/Marlin/ultralcd.cpp +++ b/Marlin/ultralcd.cpp @@ -1497,7 +1497,7 @@ char *ftostr12ns(const float &x) } // convert float to space-padded string with -_23.4_ format -char *ftostr32np(const float &x) { +char *ftostr32sp(const float &x) { long xx = abs(x * 100); uint8_t dig; diff --git a/Marlin/ultralcd.h b/Marlin/ultralcd.h index 44f176a80..351a2654b 100644 --- a/Marlin/ultralcd.h +++ b/Marlin/ultralcd.h @@ -119,7 +119,7 @@ char *ftostr31ns(const float &x); // float to string without sign character char *ftostr31(const float &x); char *ftostr32(const float &x); char *ftostr12ns(const float &x); -char *ftostr32np(const float &x); // remove zero-padding from ftostr32 +char *ftostr32sp(const float &x); // remove zero-padding from ftostr32 char *ftostr5(const float &x); char *ftostr51(const float &x); char *ftostr52(const float &x); diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index 5d9711632..1a93fe3e6 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -548,7 +548,7 @@ static void lcd_implementation_status_screen() # endif//LCD_WIDTH > 19 lcd.setCursor(LCD_WIDTH - 8, 1); lcd.print('Z'); - lcd.print(ftostr32np(current_position[Z_AXIS] + 0.00001)); + lcd.print(ftostr32sp(current_position[Z_AXIS] + 0.00001)); #endif//LCD_HEIGHT > 2 #if LCD_HEIGHT > 3