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

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

57
   char* itostr3left(const int xx);
57
   char* itostr3left(const int xx);
58
 
58
 
59
   // Convert signed int to rj string with _123, -123, _-12, or __-1 format
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
   // Convert unsigned float to string with 1.23 format
62
   // Convert unsigned float to string with 1.23 format
63
   char* ftostr12ns(const float &x);
63
   char* ftostr12ns(const float &x);
87
   char* ftostr62rj(const float &x);
87
   char* ftostr62rj(const float &x);
88
 
88
 
89
   // Convert float to rj string with 123 or -12 format
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
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
92
   #if ENABLED(LCD_DECIMAL_SMALL_XY)
93
     // Convert float to rj string with 1234, _123, 12.3, _1.2, -123, _-12, or -1.2 format
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
   #else
95
   #else
96
     // Convert float to rj string with 1234, _123, -123, __12, _-12, ___1, or __-1 format
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
   #endif
98
   #endif
99
 
99
 
100
 #endif // ULTRA_LCD
100
 #endif // ULTRA_LCD

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

55
   const float measured_z = probe_pt(xpos, ypos, raise_after, 1);
55
   const float measured_z = probe_pt(xpos, ypos, raise_after, 1);
56
 
56
 
57
   if (!isnan(measured_z)) {
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
   clean_up_after_endstop_or_probe_move();
63
   clean_up_after_endstop_or_probe_move();

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

343
   if (page.page == 0) {
343
   if (page.page == 0) {
344
     strcpy(xstring, ftostr4sign(LOGICAL_X_POSITION(current_position[X_AXIS])));
344
     strcpy(xstring, ftostr4sign(LOGICAL_X_POSITION(current_position[X_AXIS])));
345
     strcpy(ystring, ftostr4sign(LOGICAL_Y_POSITION(current_position[Y_AXIS])));
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
     #if ENABLED(FILAMENT_LCD_DISPLAY)
347
     #if ENABLED(FILAMENT_LCD_DISPLAY)
348
       strcpy(wstring, ftostr12ns(filament_width_meas));
348
       strcpy(wstring, ftostr12ns(filament_width_meas));
349
       strcpy(mstring, itostr3(100.0 * (
349
       strcpy(mstring, itostr3(100.0 * (

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

4829
     } \
4829
     } \
4830
     typedef void _name
4830
     typedef void _name
4831
 
4831
 
4832
-  DEFINE_MENU_EDIT_TYPE(uint32_t, long5, ftostr5rj, 0.01);
4833
   DEFINE_MENU_EDIT_TYPE(int16_t, int3, itostr3, 1);
4832
   DEFINE_MENU_EDIT_TYPE(int16_t, int3, itostr3, 1);
4834
   DEFINE_MENU_EDIT_TYPE(uint8_t, int8, i8tostr3, 1);
4833
   DEFINE_MENU_EDIT_TYPE(uint8_t, int8, i8tostr3, 1);
4835
   DEFINE_MENU_EDIT_TYPE(float, float3, ftostr3, 1.0);
4834
   DEFINE_MENU_EDIT_TYPE(float, float3, ftostr3, 1.0);
4839
   DEFINE_MENU_EDIT_TYPE(float, float51, ftostr51sign, 10.0);
4838
   DEFINE_MENU_EDIT_TYPE(float, float51, ftostr51sign, 10.0);
4840
   DEFINE_MENU_EDIT_TYPE(float, float52, ftostr52sign, 100.0);
4839
   DEFINE_MENU_EDIT_TYPE(float, float52, ftostr52sign, 100.0);
4841
   DEFINE_MENU_EDIT_TYPE(float, float62, ftostr62rj, 100.0);
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
 
676
 
677
     lcd_moveto(LCD_WIDTH - 8, 1);
677
     lcd_moveto(LCD_WIDTH - 8, 1);
678
     _draw_axis_label(Z_AXIS, PSTR(MSG_Z), blink);
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
     #if HAS_LEVELING && !TEMP_SENSOR_BED
681
     #if HAS_LEVELING && !TEMP_SENSOR_BED
682
       lcd_put_wchar(planner.leveling_active || blink ? '_' : ' ');
682
       lcd_put_wchar(planner.leveling_active || blink ? '_' : ' ');

Loading…
Cancel
Save