From c8e30b663973be40855b46560616edd916477bb2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 8 Aug 2019 01:51:37 -0500 Subject: [PATCH] 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. --- Marlin/src/feature/power_loss_recovery.cpp | 13 ++-- Marlin/src/gcode/calibrate/M48.cpp | 3 +- Marlin/src/gcode/feature/L6470/M906.cpp | 40 +++++++++--- Marlin/src/gcode/feature/L6470/M916-918.cpp | 65 +++++++------------ Marlin/src/gcode/host/M114.cpp | 2 +- .../lcd/dogm/status_screen_lite_ST7920.cpp | 9 +-- Marlin/src/lcd/extui_malyan_lcd.cpp | 16 ++--- Marlin/src/lcd/menu/menu_motion.cpp | 3 +- 8 files changed, 75 insertions(+), 76 deletions(-) diff --git a/Marlin/src/feature/power_loss_recovery.cpp b/Marlin/src/feature/power_loss_recovery.cpp index 305b79f522..5f826166a5 100644 --- a/Marlin/src/feature/power_loss_recovery.cpp +++ b/Marlin/src/feature/power_loss_recovery.cpp @@ -332,8 +332,7 @@ void PrintJobRecovery::resume() { // Restore leveling state before 'G92 Z' to ensure // the Z stepper count corresponds to the native Z. if (info.fade || info.leveling) { - dtostrf(info.fade, 1, 1, str_1); - sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), str_1); + sprintf_P(cmd, PSTR("M420 S%i Z%s"), int(info.leveling), dtostrf(info.fade, 1, 1, str_1)); gcode.process_subcommands_now(cmd); } #endif @@ -355,9 +354,10 @@ void PrintJobRecovery::resume() { #endif // Move back to the saved XY - dtostrf(info.current_position[X_AXIS], 1, 3, str_1); - dtostrf(info.current_position[Y_AXIS], 1, 3, str_2); - sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), str_1, str_2); + sprintf_P(cmd, PSTR("G1 X%s Y%s F3000"), + dtostrf(info.current_position[X_AXIS], 1, 3, str_1), + dtostrf(info.current_position[Y_AXIS], 1, 3, str_2) + ); gcode.process_subcommands_now(cmd); // Move back to the saved Z @@ -382,8 +382,7 @@ void PrintJobRecovery::resume() { gcode.process_subcommands_now(cmd); // Restore E position with G92.9 - dtostrf(info.current_position[E_AXIS], 1, 3, str_1); - sprintf_P(cmd, PSTR("G92.9 E%s"), str_1); + sprintf_P(cmd, PSTR("G92.9 E%s"), dtostrf(info.current_position[E_AXIS], 1, 3, str_1)); gcode.process_subcommands_now(cmd); // Relative mode diff --git a/Marlin/src/gcode/calibrate/M48.cpp b/Marlin/src/gcode/calibrate/M48.cpp index 5201e7c10c..2b5edb0919 100644 --- a/Marlin/src/gcode/calibrate/M48.cpp +++ b/Marlin/src/gcode/calibrate/M48.cpp @@ -252,8 +252,7 @@ void GcodeSuite::M48() { #if HAS_SPI_LCD // Display M48 results in the status bar char sigma_str[8]; - dtostrf(sigma, 2, 6, sigma_str); - ui.status_printf_P(0, PSTR(MSG_M48_DEVIATION ": %s"), sigma_str); + ui.status_printf_P(0, PSTR(MSG_M48_DEVIATION ": %s"), dtostrf(sigma, 2, 6, sigma_str)); #endif } diff --git a/Marlin/src/gcode/feature/L6470/M906.cpp b/Marlin/src/gcode/feature/L6470/M906.cpp index 94c17f010a..df0c085bc6 100644 --- a/Marlin/src/gcode/feature/L6470/M906.cpp +++ b/Marlin/src/gcode/feature/L6470/M906.cpp @@ -101,8 +101,12 @@ void L6470_report_current(L6470 &motor, const uint8_t axis) { #endif sprintf_P(temp_buf, PSTR("\n...OverCurrent Threshold: %2d (%4d mA)"), overcurrent_threshold, (overcurrent_threshold + 1) * 375); SERIAL_ECHO(temp_buf); - sprintf_P(temp_buf, PSTR(" Stall Threshold: %2d (%7.2f mA)"), stall_threshold, (stall_threshold + 1) * 31.25); + + char numstr[11]; + dtostrf((stall_threshold + 1) * 31.25, 1, 2, numstr); + sprintf_P(temp_buf, PSTR(" Stall Threshold: %2d (%s mA)"), stall_threshold, numstr); SERIAL_ECHO(temp_buf); + SERIAL_ECHOPGM(" Motor Status: "); const char * const stat_str; switch (motor_status) { @@ -114,24 +118,42 @@ void L6470_report_current(L6470 &motor, const uint8_t axis) { } serialprintPGM(stat_str); SERIAL_EOL(); + SERIAL_ECHOPAIR("...microsteps: ", microsteps); SERIAL_ECHOPAIR(" ADC_OUT: ", adc_out); SERIAL_ECHOPGM(" Vs_compensation: "); serialprintPGM((motor.GetParam(L6470_CONFIG) & CONFIG_EN_VSCOMP) ? PSTR("ENABLED ") : PSTR("DISABLED")); - sprintf_P(temp_buf, PSTR(" Compensation coefficient: ~%4.2f\n"), comp_coef * 0.01f); - SERIAL_ECHO(temp_buf); + + SERIAL_ECHOLNPGM(" Compensation coefficient: ", dtostrf(comp_coef * 0.01f, 7, 2, numstr)); SERIAL_ECHOPAIR("...KVAL_HOLD: ", motor.GetParam(L6470_KVAL_HOLD)); SERIAL_ECHOPAIR(" KVAL_RUN : ", motor.GetParam(L6470_KVAL_RUN)); SERIAL_ECHOPAIR(" KVAL_ACC: ", motor.GetParam(L6470_KVAL_ACC)); SERIAL_ECHOPAIR(" KVAL_DEC: ", motor.GetParam(L6470_KVAL_DEC)); SERIAL_ECHOPGM(" V motor max = "); + float val; + PGM_P suf; switch (motor_status) { - case 0: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_HOLD)\n"), float(motor.GetParam(L6470_KVAL_HOLD)) * 100 / 256); break; - case 1: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_RUN) \n"), float(motor.GetParam(L6470_KVAL_RUN)) * 100 / 256); break; - case 2: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_ACC) \n"), float(motor.GetParam(L6470_KVAL_ACC)) * 100 / 256); break; - case 3: sprintf_P(temp_buf, PSTR(" %4.1f%% (KVAL_DEC) \n"), float(motor.GetParam(L6470_KVAL_DEC)) * 100 / 256); break; + case 0: + val = motor.GetParam(L6470_KVAL_HOLD); + suf = PSTR("(KVAL_HOLD)"); + break; + case 1: + val = motor.GetParam(L6470_KVAL_RUN); + suf = PSTR("(KVAL_RUN)"); + break; + case 2: + val = motor.GetParam(L6470_KVAL_ACC); + suf = PSTR("(KVAL_ACC)"); + break; + case 3: + val = motor.GetParam(L6470_KVAL_DEC); + suf = PSTR("(KVAL_DEC)"); + break; } - SERIAL_ECHO(temp_buf); + SERIAL_ECHO(dtostrf(val * 100 / 256, 10, 2, numstr)); + SERIAL_ECHO("%% "); + serialprintPGM(suf); + SERIAL_EOL(); } void GcodeSuite::M906() { @@ -150,7 +172,7 @@ void GcodeSuite::M906() { report_current = false; if (planner.has_blocks_queued() || planner.cleaning_buffer_counter) { - SERIAL_ECHOLNPGM("ERROR - can't set KVAL_HOLD while steppers are moving"); + SERIAL_ECHOLNPGM("!Can't set KVAL_HOLD with steppers moving"); return; } diff --git a/Marlin/src/gcode/feature/L6470/M916-918.cpp b/Marlin/src/gcode/feature/L6470/M916-918.cpp index 66679a2e0e..0e79d5f233 100644 --- a/Marlin/src/gcode/feature/L6470/M916-918.cpp +++ b/Marlin/src/gcode/feature/L6470/M916-918.cpp @@ -32,6 +32,19 @@ #define DEBUG_OUT ENABLED(L6470_CHITCHAT) #include "../../../core/debug_out.h" +static void jiggle_axis(const char axis_char, const float &min, const float &max, const float &rate) { + char gcode_string[30], str1[11], str2[11]; + + // Turn the motor(s) both directions + sprintf_P(gcode_string, PSTR("G0 %c%s F%s"), axis_char, dtostrf(min, 1, 3, str1), dtostrf(rate, 1, 3, str2)); + process_subcommands_now(gcode_string); + + sprintf_P(gcode_string, PSTR("G0 %c%s F%s"), axis_char, dtostrf(max, 1, 3, str1), str2); + process_subcommands_now(gcode_string); + + planner.synchronize(); +} + /** * * M916: Increase KVAL_HOLD until thermal warning @@ -85,14 +98,11 @@ void GcodeSuite::M916() { DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate); - planner.synchronize(); // wait for all current movement commands to complete + planner.synchronize(); // Wait for moves to finish for (j = 0; j < driver_count; j++) - L6470.get_status(axis_index[j]); // clear out any pre-existing error flags + L6470.get_status(axis_index[j]); // Clear out error flags - char temp_axis_string[] = " "; - temp_axis_string[0] = axis_mon[0][0]; // need to have a string for use within sprintf format section - char gcode_string[80]; uint16_t status_composite = 0; DEBUG_ECHOLNPGM(".\n."); @@ -104,15 +114,8 @@ void GcodeSuite::M916() { for (j = 0; j < driver_count; j++) L6470.set_param(axis_index[j], L6470_KVAL_HOLD, kval_hold); - // turn the motor(s) both directions - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_min, final_feedrate); - process_subcommands_now(gcode_string); - - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_max, final_feedrate); - process_subcommands_now(gcode_string); - - // get the status after the motors have stopped - planner.synchronize(); + // Turn the motor(s) both directions + jiggle_axis(axis_mon[0][0], position_min, position_max, final_feedrate); status_composite = 0; // clear out the old bits @@ -201,12 +204,9 @@ void GcodeSuite::M917() { DEBUG_ECHOLNPAIR("feedrate = ", final_feedrate); - planner.synchronize(); // wait for all current movement commands to complete + planner.synchronize(); // Wait for moves to finish for (j = 0; j < driver_count; j++) - L6470.get_status(axis_index[j]); // clear out any pre-existing error flags - char temp_axis_string[] = " "; - temp_axis_string[0] = axis_mon[0][0]; // need to have a string for use within sprintf format section - char gcode_string[80]; + L6470.get_status(axis_index[j]); // Clear out error flags uint16_t status_composite = 0; uint8_t test_phase = 0; // 0 - decreasing OCD - exit when OCD warning occurs (ignore STALL) @@ -225,13 +225,7 @@ void GcodeSuite::M917() { DEBUG_ECHOPAIR("STALL threshold : ", (stall_th_val + 1) * 31.25); DEBUG_ECHOLNPAIR(" OCD threshold : ", (ocd_th_val + 1) * 375); - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_min, final_feedrate); - process_subcommands_now(gcode_string); - - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_max, final_feedrate); - process_subcommands_now(gcode_string); - - planner.synchronize(); + jiggle_axis(axis_mon[0][0], position_min, position_max, final_feedrate); status_composite = 0; // clear out the old bits @@ -500,30 +494,19 @@ void GcodeSuite::M918() { float feedrate_inc = final_feedrate / 10, // start at 1/10 of max & go up by 1/10 per step) current_feedrate = 0; - planner.synchronize(); // wait for all current movement commands to complete + planner.synchronize(); // Wait for moves to finish for (j = 0; j < driver_count; j++) - L6470.get_status(axis_index[j]); // clear all error flags + L6470.get_status(axis_index[j]); // Clear all error flags - char temp_axis_string[2]; - temp_axis_string[0] = axis_mon[0][0]; // need to have a string for use within sprintf format section - temp_axis_string[1] = '\n'; - - char gcode_string[80]; uint16_t status_composite = 0; - DEBUG_ECHOLNPGM(".\n.\n."); // make the feedrate prints easier to see + DEBUG_ECHOLNPGM(".\n.\n."); // Make the feedrate prints easier to see do { current_feedrate += feedrate_inc; DEBUG_ECHOLNPAIR("...feedrate = ", current_feedrate); - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_min, current_feedrate); - process_subcommands_now(gcode_string); - - sprintf_P(gcode_string, PSTR("G0 %s%4.3f F%4.3f"), temp_axis_string, position_max, current_feedrate); - process_subcommands_now(gcode_string); - - planner.synchronize(); + jiggle_axis(axis_mon[0][0], position_min, position_max, current_feedrate); for (j = 0; j < driver_count; j++) { axis_status[j] = (~L6470.get_status(axis_index[j])) & 0x0800; // bits of interest are all active low diff --git a/Marlin/src/gcode/host/M114.cpp b/Marlin/src/gcode/host/M114.cpp index 1a53406288..b4b2f58d33 100644 --- a/Marlin/src/gcode/host/M114.cpp +++ b/Marlin/src/gcode/host/M114.cpp @@ -42,7 +42,7 @@ SERIAL_CHAR(' '); SERIAL_CHAR(axis_codes[i]); SERIAL_CHAR(':'); - SERIAL_ECHO(dtostrf(pos[i], 8, precision, str)); + SERIAL_ECHO(dtostrf(pos[i], 1, precision, str)); } SERIAL_EOL(); } diff --git a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp index a4001f67d1..f651945beb 100644 --- a/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp +++ b/Marlin/src/lcd/dogm/status_screen_lite_ST7920.cpp @@ -666,17 +666,14 @@ void ST7920_Lite_Status_Screen::draw_position(const float x, const float y, cons // If position is unknown, flash the labels. const unsigned char alt_label = position_known ? 0 : (ui.get_blink() ? ' ' : 0); - dtostrf(x, -4, 0, str); write_byte(alt_label ? alt_label : 'X'); - write_str(str, 4); + write_str(dtostrf(x, -4, 0, str), 4); - dtostrf(y, -4, 0, str); write_byte(alt_label ? alt_label : 'Y'); - write_str(str, 4); + write_str(dtostrf(y, -4, 0, str), 4); - dtostrf(z, -5, 1, str); write_byte(alt_label ? alt_label : 'Z'); - write_str(str, 5); + write_str(dtostrf(z, -5, 1, str), 5); } bool ST7920_Lite_Status_Screen::indicators_changed() { diff --git a/Marlin/src/lcd/extui_malyan_lcd.cpp b/Marlin/src/lcd/extui_malyan_lcd.cpp index dde1a4c8ff..979e159598 100644 --- a/Marlin/src/lcd/extui_malyan_lcd.cpp +++ b/Marlin/src/lcd/extui_malyan_lcd.cpp @@ -146,10 +146,10 @@ void process_lcd_eb_command(const char* command) { char message_buffer[MAX_CURLY_COMMAND]; sprintf_P(message_buffer, - PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}{TQ:%03i}{TT:%s}"), - thermalManager.degHotend(0), thermalManager.degTargetHotend(0), + PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}{TQ:%03i}{TT:%s}"), + int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0), #if HAS_HEATED_BED - thermalManager.degBed(), thermalManager.degTargetBed(), + int(thermalManager.degBed()), thermalManager.degTargetBed(), #else 0, 0, #endif @@ -199,8 +199,8 @@ void process_lcd_j_command(const char* command) { case 'X': { // G0 // The M200 class UI seems to send movement in .1mm values. - char cmd[20]; - sprintf_P(cmd, PSTR("G1 %c%03.1f"), axis, atof(command + 1) / 10.0); + char cmd[20], pos[6]; + sprintf_P(cmd, PSTR("G1 %c%s"), axis, dtostrf(atof(command + 1) / 10.0, -5, 3, pos)); queue.enqueue_one_now(cmd); } break; default: @@ -305,10 +305,10 @@ void process_lcd_s_command(const char* command) { case 'I': { // temperature information char message_buffer[MAX_CURLY_COMMAND]; - sprintf_P(message_buffer, PSTR("{T0:%03.0f/%03i}{T1:000/000}{TP:%03.0f/%03i}"), - thermalManager.degHotend(0), thermalManager.degTargetHotend(0), + sprintf_P(message_buffer, PSTR("{T0:%03i/%03i}{T1:000/000}{TP:%03i/%03i}"), + int(thermalManager.degHotend(0)), thermalManager.degTargetHotend(0), #if HAS_HEATED_BED - thermalManager.degBed(), thermalManager.degTargetBed() + int(thermalManager.degBed()), thermalManager.degTargetBed() #else 0, 0 #endif diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp index 223d1077aa..5885001372 100644 --- a/Marlin/src/lcd/menu/menu_motion.cpp +++ b/Marlin/src/lcd/menu/menu_motion.cpp @@ -285,8 +285,7 @@ void _menu_move_distance(const AxisEnum axis, const screenFunc_t func, const int // Determine digits needed right of decimal const uint8_t digs = !UNEAR_ZERO((SHORT_MANUAL_Z_MOVE) * 1000 - int((SHORT_MANUAL_Z_MOVE) * 1000)) ? 4 : !UNEAR_ZERO((SHORT_MANUAL_Z_MOVE) * 100 - int((SHORT_MANUAL_Z_MOVE) * 100)) ? 3 : 2; - dtostrf(SHORT_MANUAL_Z_MOVE, 1, digs, numstr); - sprintf_P(tmp, PSTR(MSG_MOVE_Z_DIST), numstr); + sprintf_P(tmp, PSTR(MSG_MOVE_Z_DIST), dtostrf(SHORT_MANUAL_Z_MOVE, 1, digs, numstr)); LCDPRINT(tmp); MENU_ITEM_ADDON_END(); }