Browse Source

Fix code attempting to sprintf %f (#14869)

Arduino doesn't (always) support `float` formatting in strings. So either cast to `int` or use `dtostrf()` to fix these usages.
Scott Lahteine 6 years ago
parent
commit
c8e30b6639
No account linked to committer's email address

+ 6
- 7
Marlin/src/feature/power_loss_recovery.cpp View File

@@ -332,8 +332,7 @@ void PrintJobRecovery::resume() {
332 332
     // Restore leveling state before 'G92 Z' to ensure
333 333
     // the Z stepper count corresponds to the native Z.
334 334
     if (info.fade || info.leveling) {
335
-      dtostrf(info.fade, 1, 1, str_1);
336
-      sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), str_1);
335
+      sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), dtostrf(info.fade, 1, 1, str_1));
337 336
       gcode.process_subcommands_now(cmd);
338 337
     }
339 338
   #endif
@@ -355,9 +354,10 @@ void PrintJobRecovery::resume() {
355 354
   #endif
356 355
 
357 356
   // Move back to the saved XY
358
-  dtostrf(info.current_position[X_AXIS], 1, 3, str_1);
359
-  dtostrf(info.current_position[Y_AXIS], 1, 3, str_2);
360
-  sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), str_1, str_2);
357
+  sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"),
358
+    dtostrf(info.current_position[X_AXIS], 1, 3, str_1),
359
+    dtostrf(info.current_position[Y_AXIS], 1, 3, str_2)
360
+  );
361 361
   gcode.process_subcommands_now(cmd);
362 362
 
363 363
   // Move back to the saved Z
@@ -382,8 +382,7 @@ void PrintJobRecovery::resume() {
382 382
   gcode.process_subcommands_now(cmd);
383 383
 
384 384
   // Restore E position with G92.9
385
-  dtostrf(info.current_position[E_AXIS], 1, 3, str_1);
386
-  sprintf_P(cmd, PSTR("G92.9 E%s"), str_1);
385
+  sprintf_P(cmd, PSTR("G92.9 E%s"), dtostrf(info.current_position[E_AXIS], 1, 3, str_1));
387 386
   gcode.process_subcommands_now(cmd);
388 387
 
389 388
   // Relative mode

+ 1
- 2
Marlin/src/gcode/calibrate/M48.cpp View File

@@ -252,8 +252,7 @@ void GcodeSuite::M48() {
252 252
     #if HAS_SPI_LCD
253 253
       // Display M48 results in the status bar
254 254
       char sigma_str[8];
255
-      dtostrf(sigma, 2, 6, sigma_str);
256
-      ui.status_printf_P(0, PSTR(MSG_M48_DEVIATION ": %s"), sigma_str);
255
+      ui.status_printf_P(0, PSTR(MSG_M48_DEVIATION ": %s"), dtostrf(sigma, 2, 6, sigma_str));
257 256
     #endif
258 257
   }
259 258
 

+ 31
- 9
Marlin/src/gcode/feature/L6470/M906.cpp View File

@@ -101,8 +101,12 @@ void L6470_report_current(L6470 &motor, const uint8_t axis) {
101 101
   #endif
102 102
   sprintf_P(temp_buf, PSTR("\n...OverCurrent Threshold: %2d (%4d mA)"), overcurrent_threshold, (overcurrent_threshold + 1) * 375);
103 103
   SERIAL_ECHO(temp_buf);
104
-  sprintf_P(temp_buf, PSTR("   Stall Threshold: %2d (%7.2f mA)"), stall_threshold, (stall_threshold + 1) * 31.25);
104
+
105
+  char numstr[11];
106
+  dtostrf((stall_threshold + 1) * 31.25, 1, 2, numstr);
107
+  sprintf_P(temp_buf, PSTR("   Stall Threshold: %2d (%s mA)"), stall_threshold, numstr);
105 108
   SERIAL_ECHO(temp_buf);
109
+
106 110
   SERIAL_ECHOPGM("   Motor Status: ");
107 111
   const char * const stat_str;
108 112
   switch (motor_status) {
@@ -114,24 +118,42 @@ void L6470_report_current(L6470 &motor, const uint8_t axis) {
114 118
   }
115 119
   serialprintPGM(stat_str);
116 120
   SERIAL_EOL();
121
+
117 122
   SERIAL_ECHOPAIR("...microsteps: ", microsteps);
118 123
   SERIAL_ECHOPAIR("   ADC_OUT: ", adc_out);
119 124
   SERIAL_ECHOPGM("   Vs_compensation: ");
120 125
   serialprintPGM((motor.GetParam(L6470_CONFIG) & CONFIG_EN_VSCOMP) ? PSTR("ENABLED ") : PSTR("DISABLED"));
121
-  sprintf_P(temp_buf, PSTR("   Compensation coefficient: ~%4.2f\n"), comp_coef * 0.01f);
122
-  SERIAL_ECHO(temp_buf);
126
+
127
+  SERIAL_ECHOLNPGM("   Compensation coefficient: ", dtostrf(comp_coef * 0.01f, 7, 2, numstr));
123 128
   SERIAL_ECHOPAIR("...KVAL_HOLD: ", motor.GetParam(L6470_KVAL_HOLD));
124 129
   SERIAL_ECHOPAIR("   KVAL_RUN : ", motor.GetParam(L6470_KVAL_RUN));
125 130
   SERIAL_ECHOPAIR("   KVAL_ACC: ", motor.GetParam(L6470_KVAL_ACC));
126 131
   SERIAL_ECHOPAIR("   KVAL_DEC: ", motor.GetParam(L6470_KVAL_DEC));
127 132
   SERIAL_ECHOPGM("   V motor max =  ");
133
+  float val;
134
+  PGM_P suf;
128 135
   switch (motor_status) {
129
-    case 0: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_HOLD)\n"), float(motor.GetParam(L6470_KVAL_HOLD)) * 100 / 256); break;
130
-    case 1: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_RUN) \n"), float(motor.GetParam(L6470_KVAL_RUN)) * 100 / 256); break;
131
-    case 2: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_ACC) \n"), float(motor.GetParam(L6470_KVAL_ACC)) * 100 / 256); break;
132
-    case 3: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_DEC) \n"), float(motor.GetParam(L6470_KVAL_DEC)) * 100 / 256); break;
136
+    case 0:
137
+      val = motor.GetParam(L6470_KVAL_HOLD);
138
+      suf = PSTR("(KVAL_HOLD)");
139
+      break;
140
+    case 1:
141
+      val = motor.GetParam(L6470_KVAL_RUN);
142
+      suf = PSTR("(KVAL_RUN)");
143
+      break;
144
+    case 2:
145
+      val = motor.GetParam(L6470_KVAL_ACC);
146
+      suf = PSTR("(KVAL_ACC)");
147
+      break;
148
+    case 3:
149
+      val = motor.GetParam(L6470_KVAL_DEC);
150
+      suf = PSTR("(KVAL_DEC)");
151
+      break;
133 152
   }
134
-  SERIAL_ECHO(temp_buf);
153
+  SERIAL_ECHO(dtostrf(val * 100 / 256, 10, 2, numstr));
154
+  SERIAL_ECHO("%% ");
155
+  serialprintPGM(suf);
156
+  SERIAL_EOL();
135 157
 }
136 158
 
137 159
 void GcodeSuite::M906() {
@@ -150,7 +172,7 @@ void GcodeSuite::M906() {
150 172
     report_current = false;
151 173
 
152 174
     if (planner.has_blocks_queued() || planner.cleaning_buffer_counter) {
153
-      SERIAL_ECHOLNPGM("ERROR - can't set KVAL_HOLD while steppers are moving");
175
+      SERIAL_ECHOLNPGM("!Can't set KVAL_HOLD with steppers moving");
154 176
       return;
155 177
     }
156 178
 

+ 24
- 41
Marlin/src/gcode/feature/L6470/M916-918.cpp View File

@@ -32,6 +32,19 @@
32 32
 #define DEBUG_OUT ENABLED(L6470_CHITCHAT)
33 33
 #include "../../../core/debug_out.h"
34 34
 
35
+static void jiggle_axis(const char axis_char, const float &min, const float &max, const float &rate) {
36
+  char gcode_string[30], str1[11], str2[11];
37
+
38
+  // Turn the motor(s) both directions
39
+  sprintf_P(gcode_string, PSTR("G0 %c%s F%s"), axis_char, dtostrf(min, 1, 3, str1), dtostrf(rate, 1, 3, str2));
40
+  process_subcommands_now(gcode_string);
41
+
42
+  sprintf_P(gcode_string, PSTR("G0 %c%s F%s"), axis_char, dtostrf(max, 1, 3, str1), str2);
43
+  process_subcommands_now(gcode_string);
44
+
45
+  planner.synchronize();
46
+}
47
+
35 48
 /**
36 49
  *
37 50
  * M916: Increase KVAL_HOLD until thermal warning
@@ -85,14 +98,11 @@ void GcodeSuite::M916() {
85 98
 
86 99
   DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate);
87 100
 
88
-  planner.synchronize();                             // wait for all current movement commands to complete
101
+  planner.synchronize();                  // Wait for moves to finish
89 102
 
90 103
   for (j = 0; j < driver_count; j++)
91
-    L6470.get_status(axis_index[j]);  // clear out any pre-existing error flags
104
+    L6470.get_status(axis_index[j]);      // Clear out error flags
92 105
 
93
-  char temp_axis_string[] = " ";
94
-  temp_axis_string[0] = axis_mon[0][0];  // need to have a string for use within sprintf format section
95
-  char gcode_string[80];
96 106
   uint16_t status_composite = 0;
97 107
 
98 108
   DEBUG_ECHOLNPGM(".\n.");
@@ -104,15 +114,8 @@ void GcodeSuite::M916() {
104 114
     for (j = 0; j < driver_count; j++)
105 115
       L6470.set_param(axis_index[j], L6470_KVAL_HOLD, kval_hold);
106 116
 
107
-    // turn the motor(s) both directions
108
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f  F%4.3f"), temp_axis_string, position_min, final_feedrate);
109
-    process_subcommands_now(gcode_string);
110
-
111
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f  F%4.3f"), temp_axis_string, position_max, final_feedrate);
112
-    process_subcommands_now(gcode_string);
113
-
114
-    // get the status after the motors have stopped
115
-    planner.synchronize();
117
+    // Turn the motor(s) both directions
118
+    jiggle_axis(axis_mon[0][0], position_min, position_max, final_feedrate);
116 119
 
117 120
     status_composite = 0;    // clear out the old bits
118 121
 
@@ -201,12 +204,9 @@ void GcodeSuite::M917() {
201 204
 
202 205
   DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate);
203 206
 
204
-  planner.synchronize();                             // wait for all current movement commands to complete
207
+  planner.synchronize();                // Wait for moves to finish
205 208
   for (j = 0; j < driver_count; j++)
206
-    L6470.get_status(axis_index[j]);  // clear out any pre-existing error flags
207
-  char temp_axis_string[] = " ";
208
-  temp_axis_string[0] = axis_mon[0][0];  // need to have a string for use within sprintf format section
209
-  char gcode_string[80];
209
+    L6470.get_status(axis_index[j]);    // Clear out error flags
210 210
   uint16_t status_composite = 0;
211 211
   uint8_t test_phase = 0;
212 212
         // 0 - decreasing OCD - exit when OCD warning occurs (ignore STALL)
@@ -225,13 +225,7 @@ void GcodeSuite::M917() {
225 225
     DEBUG_ECHOPAIR("STALL threshold : ", (stall_th_val + 1) * 31.25);
226 226
     DEBUG_ECHOLNPAIR("   OCD threshold : ", (ocd_th_val + 1) * 375);
227 227
 
228
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f  F%4.3f"), temp_axis_string, position_min, final_feedrate);
229
-    process_subcommands_now(gcode_string);
230
-
231
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f  F%4.3f"), temp_axis_string, position_max, final_feedrate);
232
-    process_subcommands_now(gcode_string);
233
-
234
-    planner.synchronize();
228
+    jiggle_axis(axis_mon[0][0], position_min, position_max, final_feedrate);
235 229
 
236 230
     status_composite = 0;    // clear out the old bits
237 231
 
@@ -500,30 +494,19 @@ void GcodeSuite::M918() {
500 494
   float feedrate_inc = final_feedrate / 10, // start at 1/10 of max & go up by 1/10 per step)
501 495
         current_feedrate = 0;
502 496
 
503
-  planner.synchronize();                  // wait for all current movement commands to complete
497
+  planner.synchronize();                  // Wait for moves to finish
504 498
 
505 499
   for (j = 0; j < driver_count; j++)
506
-    L6470.get_status(axis_index[j]);      // clear all error flags
500
+    L6470.get_status(axis_index[j]);      // Clear all error flags
507 501
 
508
-  char temp_axis_string[2];
509
-  temp_axis_string[0] = axis_mon[0][0];   // need to have a string for use within sprintf format section
510
-  temp_axis_string[1] = '\n';
511
-
512
-  char gcode_string[80];
513 502
   uint16_t status_composite = 0;
514
-  DEBUG_ECHOLNPGM(".\n.\n.");            // make the feedrate prints easier to see
503
+  DEBUG_ECHOLNPGM(".\n.\n.");             // Make the feedrate prints easier to see
515 504
 
516 505
   do {
517 506
     current_feedrate += feedrate_inc;
518 507
     DEBUG_ECHOLNPAIR("...feedrate = ", current_feedrate);
519 508
 
520
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_min, current_feedrate);
521
-    process_subcommands_now(gcode_string);
522
-
523
-    sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_max, current_feedrate);
524
-    process_subcommands_now(gcode_string);
525
-
526
-    planner.synchronize();
509
+    jiggle_axis(axis_mon[0][0], position_min, position_max, current_feedrate);
527 510
 
528 511
     for (j = 0; j < driver_count; j++) {
529 512
       axis_status[j] = (~L6470.get_status(axis_index[j])) & 0x0800;    // bits of interest are all active low

+ 1
- 1
Marlin/src/gcode/host/M114.cpp View File

@@ -42,7 +42,7 @@
42 42
       SERIAL_CHAR(' ');
43 43
       SERIAL_CHAR(axis_codes[i]);
44 44
       SERIAL_CHAR(':');
45
-      SERIAL_ECHO(dtostrf(pos[i], 8, precision, str));
45
+      SERIAL_ECHO(dtostrf(pos[i], 1, precision, str));
46 46
     }
47 47
     SERIAL_EOL();
48 48
   }

+ 3
- 6
Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp View File

@@ -666,17 +666,14 @@ void ST7920_Lite_Status_Screen::draw_position(const float x, const float y, cons
666 666
   // If position is unknown, flash the labels.
667 667
   const unsigned char alt_label = position_known ? 0 : (ui.get_blink() ? ' ' : 0);
668 668
 
669
-  dtostrf(x, -4, 0, str);
670 669
   write_byte(alt_label ? alt_label : 'X');
671
-  write_str(str, 4);
670
+  write_str(dtostrf(x, -4, 0, str), 4);
672 671
 
673
-  dtostrf(y, -4, 0, str);
674 672
   write_byte(alt_label ? alt_label : 'Y');
675
-  write_str(str, 4);
673
+  write_str(dtostrf(y, -4, 0, str), 4);
676 674
 
677
-  dtostrf(z, -5, 1, str);
678 675
   write_byte(alt_label ? alt_label : 'Z');
679
-  write_str(str, 5);
676
+  write_str(dtostrf(z, -5, 1, str), 5);
680 677
 }
681 678
 
682 679
 bool ST7920_Lite_Status_Screen::indicators_changed() {

+ 8
- 8
Marlin/src/lcd/extui_malyan_lcd.cpp View File

@@ -146,10 +146,10 @@ void process_lcd_eb_command(const char* command) {
146 146
 
147 147
       char message_buffer[MAX_CURLY_COMMAND];
148 148
       sprintf_P(message_buffer,
149
-        PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}{TQ:%03i}{TT:%s}"),
150
-        thermalManager.degHotend(0), thermalManager.degTargetHotend(0),
149
+        PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}{TQ:%03i}{TT:%s}"),
150
+        int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
151 151
         #if HAS_HEATED_BED
152
-          thermalManager.degBed(), thermalManager.degTargetBed(),
152
+          int(thermalManager.degBed()), thermalManager.degTargetBed(),
153 153
         #else
154 154
           0, 0,
155 155
         #endif
@@ -199,8 +199,8 @@ void process_lcd_j_command(const char* command) {
199 199
     case 'X': {
200 200
       // G0 <AXIS><distance>
201 201
       // The M200 class UI seems to send movement in .1mm values.
202
-      char cmd[20];
203
-      sprintf_P(cmd, PSTR("G1 %c%03.1f"), axis, atof(command + 1) / 10.0);
202
+      char cmd[20], pos[6];
203
+      sprintf_P(cmd, PSTR("G1 %c%s"), axis, dtostrf(atof(command + 1) / 10.0, -5, 3, pos));
204 204
       queue.enqueue_one_now(cmd);
205 205
     } break;
206 206
     default:
@@ -305,10 +305,10 @@ void process_lcd_s_command(const char* command) {
305 305
     case 'I': {
306 306
       // temperature information
307 307
       char message_buffer[MAX_CURLY_COMMAND];
308
-      sprintf_P(message_buffer, PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}"),
309
-        thermalManager.degHotend(0), thermalManager.degTargetHotend(0),
308
+      sprintf_P(message_buffer, PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}"),
309
+        int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0),
310 310
         #if HAS_HEATED_BED
311
-          thermalManager.degBed(), thermalManager.degTargetBed()
311
+          int(thermalManager.degBed()), thermalManager.degTargetBed()
312 312
         #else
313 313
           0, 0
314 314
         #endif

+ 1
- 2
Marlin/src/lcd/menu/menu_motion.cpp View File

@@ -285,8 +285,7 @@ void _menu_move_distance(const AxisEnum axis, const screenFunc_t func, const int
285 285
         // Determine digits needed right of decimal
286 286
         const uint8_t digs = !UNEAR_ZERO((SHORT_MANUAL_Z_MOVE) * 1000 - int((SHORT_MANUAL_Z_MOVE) * 1000)) ? 4 :
287 287
                              !UNEAR_ZERO((SHORT_MANUAL_Z_MOVE) *  100 - int((SHORT_MANUAL_Z_MOVE) *  100)) ? 3 : 2;
288
-        dtostrf(SHORT_MANUAL_Z_MOVE, 1, digs, numstr);
289
-        sprintf_P(tmp, PSTR(MSG_MOVE_Z_DIST), numstr);
288
+        sprintf_P(tmp, PSTR(MSG_MOVE_Z_DIST), dtostrf(SHORT_MANUAL_Z_MOVE, 1, digs, numstr));
290 289
         LCDPRINT(tmp);
291 290
       MENU_ITEM_ADDON_END();
292 291
     }

Loading…
Cancel
Save