Browse Source

Round all floats in string conversion functions (#10566)

Scott Lahteine 7 years ago
parent
commit
9e0d99c0c6
No account linked to committer's email address

+ 12
- 12
Marlin/src/core/utility.cpp View File

@@ -87,7 +87,7 @@ void safe_delay(millis_t ms) {
87 87
   }
88 88
 
89 89
   // Convert signed int to rj string with 1234, _123, -123, _-12, or __-1 format
90
-  char *itostr4sign(const int x) {
90
+  char* itostr4sign(const int x) {
91 91
     const bool neg = x < 0;
92 92
     const int xx = neg ? -x : x;
93 93
     if (x >= 1000) {
@@ -119,7 +119,7 @@ void safe_delay(millis_t ms) {
119 119
 
120 120
   // Convert unsigned float to string with 1.23 format
121 121
   char* ftostr12ns(const float &x) {
122
-    const long xx = (x < 0 ? -x : x) * 100;
122
+    const long xx = ((x < 0 ? -x : x) + 0.001) * 100;
123 123
     conv[3] = DIGIMOD(xx, 100);
124 124
     conv[4] = '.';
125 125
     conv[5] = DIGIMOD(xx, 10);
@@ -128,8 +128,8 @@ void safe_delay(millis_t ms) {
128 128
   }
129 129
 
130 130
   // Convert signed float to fixed-length string with 023.45 / -23.45 format
131
-  char *ftostr32(const float &x) {
132
-    long xx = x * 100;
131
+  char* ftostr32(const float &x) {
132
+    long xx = FIXFLOAT(x) * 100;
133 133
     conv[1] = MINUSOR(xx, DIGIMOD(xx, 10000));
134 134
     conv[2] = DIGIMOD(xx, 1000);
135 135
     conv[3] = DIGIMOD(xx, 100);
@@ -142,8 +142,8 @@ void safe_delay(millis_t ms) {
142 142
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
143 143
 
144 144
     // Convert float to rj string with 1234, _123, -123, _-12, 12.3, _1.2, or -1.2 format
145
-    char *ftostr4sign(const float &fx) {
146
-      const int x = fx * 10;
145
+    char* ftostr4sign(const float &fx) {
146
+      const int x = FIXFLOAT(fx) * 10;
147 147
       if (!WITHIN(x, -99, 999)) return itostr4sign((int)fx);
148 148
       const bool neg = x < 0;
149 149
       const int xx = neg ? -x : x;
@@ -158,7 +158,7 @@ void safe_delay(millis_t ms) {
158 158
 
159 159
   // Convert float to fixed-length string with +123.4 / -123.4 format
160 160
   char* ftostr41sign(const float &x) {
161
-    int xx = x * 10;
161
+    int xx = FIXFLOAT(x) * 10;
162 162
     conv[1] = MINUSOR(xx, '+');
163 163
     conv[2] = DIGIMOD(xx, 1000);
164 164
     conv[3] = DIGIMOD(xx, 100);
@@ -170,7 +170,7 @@ void safe_delay(millis_t ms) {
170 170
 
171 171
   // Convert signed float to string (6 digit) with -1.234 / _0.000 / +1.234 format
172 172
   char* ftostr43sign(const float &x, char plus/*=' '*/) {
173
-    long xx = x * 1000;
173
+    long xx = FIXFLOAT(x) * 1000;
174 174
     conv[1] = xx ? MINUSOR(xx, plus) : ' ';
175 175
     conv[2] = DIGIMOD(xx, 1000);
176 176
     conv[3] = '.';
@@ -193,7 +193,7 @@ void safe_delay(millis_t ms) {
193 193
 
194 194
   // Convert signed float to string with +1234.5 format
195 195
   char* ftostr51sign(const float &x) {
196
-    long xx = x * 10;
196
+    long xx = FIXFLOAT(x) * 10;
197 197
     conv[0] = MINUSOR(xx, '+');
198 198
     conv[1] = DIGIMOD(xx, 10000);
199 199
     conv[2] = DIGIMOD(xx, 1000);
@@ -206,7 +206,7 @@ void safe_delay(millis_t ms) {
206 206
 
207 207
   // Convert signed float to string with +123.45 format
208 208
   char* ftostr52sign(const float &x) {
209
-    long xx = x * 100;
209
+    long xx = FIXFLOAT(x) * 100;
210 210
     conv[0] = MINUSOR(xx, '+');
211 211
     conv[1] = DIGIMOD(xx, 10000);
212 212
     conv[2] = DIGIMOD(xx, 1000);
@@ -219,7 +219,7 @@ void safe_delay(millis_t ms) {
219 219
 
220 220
   // Convert unsigned float to string with 1234.56 format omitting trailing zeros
221 221
   char* ftostr62rj(const float &x) {
222
-    const long xx = (x < 0 ? -x : x) * 100;
222
+    const long xx = ((x < 0 ? -x : x) + 0.001) * 100;
223 223
     conv[0] = RJDIGIT(xx, 100000);
224 224
     conv[1] = RJDIGIT(xx, 10000);
225 225
     conv[2] = RJDIGIT(xx, 1000);
@@ -232,7 +232,7 @@ void safe_delay(millis_t ms) {
232 232
 
233 233
   // Convert signed float to space-padded string with -_23.4_ format
234 234
   char* ftostr52sp(const float &x) {
235
-    long xx = x * 100;
235
+    long xx = FIXFLOAT(x) * 100;
236 236
     uint8_t dig;
237 237
     conv[1] = MINUSOR(xx, RJDIGIT(xx, 10000));
238 238
     conv[2] = RJDIGIT(xx, 1000);

+ 4
- 4
Marlin/src/core/utility.h View File

@@ -57,7 +57,7 @@ void safe_delay(millis_t ms);
57 57
   char* itostr3left(const int xx);
58 58
 
59 59
   // Convert signed int to rj string with _123, -123, _-12, or __-1 format
60
-  char *itostr4sign(const int x);
60
+  char* itostr4sign(const int x);
61 61
 
62 62
   // Convert unsigned float to string with 1.23 format
63 63
   char* ftostr12ns(const float &x);
@@ -87,14 +87,14 @@ void safe_delay(millis_t ms);
87 87
   char* ftostr62rj(const float &x);
88 88
 
89 89
   // Convert float to rj string with 123 or -12 format
90
-  FORCE_INLINE char *ftostr3(const float &x) { return itostr3((int)x); }
90
+  FORCE_INLINE char* ftostr3(const float &x) { return itostr3(int(FIXFLOAT(x))); }
91 91
 
92 92
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
93 93
     // Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format
94
-    char *ftostr4sign(const float &fx);
94
+    char* ftostr4sign(const float &fx);
95 95
   #else
96 96
     // Convert float to rj string with 1234, _123, -123, __12, _-12, ___1, or __-1 format
97
-    FORCE_INLINE char *ftostr4sign(const float &x) { return itostr4sign((int)x); }
97
+    FORCE_INLINE char* ftostr4sign(const float &x) { return itostr4sign(int(FIXFLOAT(x))); }
98 98
   #endif
99 99
 
100 100
 #endif // ULTRA_LCD

+ 3
- 3
Marlin/src/gcode/probe/G30.cpp View File

@@ -55,9 +55,9 @@ void GcodeSuite::G30() {
55 55
   const float measured_z = probe_pt(xpos, ypos, raise_after, 1);
56 56
 
57 57
   if (!isnan(measured_z)) {
58
-    SERIAL_PROTOCOLPAIR("Bed X: ", FIXFLOAT(xpos));
59
-    SERIAL_PROTOCOLPAIR(" Y: ", FIXFLOAT(ypos));
60
-    SERIAL_PROTOCOLLNPAIR(" Z: ", FIXFLOAT(measured_z));
58
+    SERIAL_PROTOCOLPAIR_F("Bed X: ", xpos);
59
+    SERIAL_PROTOCOLPAIR_F(" Y: ", ypos);
60
+    SERIAL_PROTOCOLLNPAIR_F(" Z: ", measured_z);
61 61
   }
62 62
 
63 63
   clean_up_after_endstop_or_probe_move();

+ 1
- 1
Marlin/src/lcd/dogm/status_screen_DOGM.h View File

@@ -343,7 +343,7 @@ static void lcd_implementation_status_screen() {
343 343
   if (page.page == 0) {
344 344
     strcpy(xstring, ftostr4sign(LOGICAL_X_POSITION(current_position[X_AXIS])));
345 345
     strcpy(ystring, ftostr4sign(LOGICAL_Y_POSITION(current_position[Y_AXIS])));
346
-    strcpy(zstring, ftostr52sp(FIXFLOAT(LOGICAL_Z_POSITION(current_position[Z_AXIS]))));
346
+    strcpy(zstring, ftostr52sp(LOGICAL_Z_POSITION(current_position[Z_AXIS])));
347 347
     #if ENABLED(FILAMENT_LCD_DISPLAY)
348 348
       strcpy(wstring, ftostr12ns(filament_width_meas));
349 349
       strcpy(mstring, itostr3(100.0 * (

+ 1
- 1
Marlin/src/lcd/ultralcd.cpp View File

@@ -4829,7 +4829,6 @@ void kill_screen(const char* lcd_msg) {
4829 4829
     } \
4830 4830
     typedef void _name
4831 4831
 
4832
-  DEFINE_MENU_EDIT_TYPE(uint32_t, long5, ftostr5rj, 0.01);
4833 4832
   DEFINE_MENU_EDIT_TYPE(int16_t, int3, itostr3, 1);
4834 4833
   DEFINE_MENU_EDIT_TYPE(uint8_t, int8, i8tostr3, 1);
4835 4834
   DEFINE_MENU_EDIT_TYPE(float, float3, ftostr3, 1.0);
@@ -4839,6 +4838,7 @@ void kill_screen(const char* lcd_msg) {
4839 4838
   DEFINE_MENU_EDIT_TYPE(float, float51, ftostr51sign, 10.0);
4840 4839
   DEFINE_MENU_EDIT_TYPE(float, float52, ftostr52sign, 100.0);
4841 4840
   DEFINE_MENU_EDIT_TYPE(float, float62, ftostr62rj, 100.0);
4841
+  DEFINE_MENU_EDIT_TYPE(uint32_t, long5, ftostr5rj, 0.01);
4842 4842
 
4843 4843
   /**
4844 4844
    *

+ 1
- 1
Marlin/src/lcd/ultralcd_impl_HD44780.h View File

@@ -676,7 +676,7 @@ static void lcd_implementation_status_screen() {
676 676
 
677 677
     lcd_moveto(LCD_WIDTH - 8, 1);
678 678
     _draw_axis_label(Z_AXIS, PSTR(MSG_Z), blink);
679
-    lcd_put_u8str(ftostr52sp(FIXFLOAT(LOGICAL_Z_POSITION(current_position[Z_AXIS]))));
679
+    lcd_put_u8str(ftostr52sp(LOGICAL_Z_POSITION(current_position[Z_AXIS])));
680 680
 
681 681
     #if HAS_LEVELING && !TEMP_SENSOR_BED
682 682
       lcd_put_wchar(planner.leveling_active || blink ? '_' : ' ');

Loading…
Cancel
Save