From 4abf1f436a43fa4a0ae7b5237acdc251c85d85d5 Mon Sep 17 00:00:00 2001 From: Alex Voinea Date: Mon, 26 Oct 2020 09:42:26 +0200 Subject: [PATCH] Gracefully dump the queue + fixes to fancheck --- Firmware/Marlin.h | 11 ----------- Firmware/Marlin_main.cpp | 17 +++++++---------- Firmware/cardreader.cpp | 1 + Firmware/cmdqueue.cpp | 20 +++++++++++++++----- Firmware/cmdqueue.h | 5 +++-- Firmware/first_lay_cal.cpp | 1 + Firmware/menu.cpp | 1 + Firmware/mmu.cpp | 1 + Firmware/temperature.cpp | 2 +- Firmware/ultralcd.cpp | 16 ++++++++++------ 10 files changed, 40 insertions(+), 35 deletions(-) diff --git a/Firmware/Marlin.h b/Firmware/Marlin.h index 5a38cae52..19635c178 100755 --- a/Firmware/Marlin.h +++ b/Firmware/Marlin.h @@ -240,23 +240,12 @@ void Stop(); bool IsStopped(); void finishAndDisableSteppers(); -//put an ASCII command at the end of the current buffer. -void enquecommand(const char *cmd, bool from_progmem = false); - //put an ASCII command at the end of the current buffer, read from flash #define enquecommand_P(cmd) enquecommand(cmd, true) -//put an ASCII command at the begin of the current buffer -void enquecommand_front(const char *cmd, bool from_progmem = false); - //put an ASCII command at the begin of the current buffer, read from flash #define enquecommand_front_P(cmd) enquecommand_front(cmd, true) -void repeatcommand_front(); - -// Remove all lines from the command queue. -void cmdqueue_reset(); - void prepare_arc_move(char isclockwise); void clamp_to_software_endstops(float target[3]); void refresh_cmd_timeout(void); diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 260b9a30b..5376daefe 100755 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -631,7 +631,7 @@ void crashdet_cancel() lcd_print_stop(); }else if(saved_printing_type == PRINTING_TYPE_USB){ SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL); //for Octoprint: works the same as clicking "Abort" button in Octoprint GUI - SERIAL_PROTOCOLLNRPGM(MSG_OK); + cmdqueue_reset(); } } @@ -3678,14 +3678,12 @@ There are reasons why some G Codes aren't in numerical order. void process_commands() { #ifdef FANCHECK - if(fan_check_error){ - if(fan_check_error == EFCE_DETECTED){ - fan_check_error = EFCE_REPORTED; - // SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); - lcd_pause_print(); - } // otherwise it has already been reported, so just ignore further processing - return; //ignore usb stream. It is reenabled by selecting resume from the lcd. - } + if(fan_check_error == EFCE_DETECTED){ + fan_check_error = EFCE_REPORTED; + // SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); + lcd_pause_print(); + cmdqueue_serial_disabled = true; + } #endif if (!buflen) return; //empty command @@ -11459,7 +11457,6 @@ void restore_print_from_ram_and_continue(float e_move) //not sd printing nor usb printing } - // SERIAL_PROTOCOLLNRPGM(MSG_OK); //dummy response because of octoprint is waiting for this lcd_setstatuspgm(_T(WELCOME_MSG)); saved_printing_type = PRINTING_TYPE_NONE; saved_printing = false; diff --git a/Firmware/cardreader.cpp b/Firmware/cardreader.cpp index 0a6fdeea8..94077f4cd 100644 --- a/Firmware/cardreader.cpp +++ b/Firmware/cardreader.cpp @@ -1,4 +1,5 @@ #include "Marlin.h" +#include "cmdqueue.h" #include "cardreader.h" #include "ultralcd.h" #include "stepper.h" diff --git a/Firmware/cmdqueue.cpp b/Firmware/cmdqueue.cpp index 9167aaaa6..5c5a71731 100755 --- a/Firmware/cmdqueue.cpp +++ b/Firmware/cmdqueue.cpp @@ -18,6 +18,10 @@ int buflen = 0; // Therefore don't remove the command from the queue in the loop() function. bool cmdbuffer_front_already_processed = false; +// Used for temporarely preventing accidental adding of Serial commands to the queue. +// For now only check_file and the fancheck pause use this. +bool cmdqueue_serial_disabled = false; + int serial_count = 0; //index of character read from serial line boolean comment_mode = false; char *strchr_pointer; // just a pointer to find chars in the command string like X, Y, Z, E, etc @@ -91,14 +95,20 @@ bool cmdqueue_pop_front() void cmdqueue_reset() { - bufindr = 0; - bufindw = 0; - buflen = 0; + while (buflen) + { + uint8_t cmdqueue_current_type = *(cmdbuffer+bufindr); + // printf_P(PSTR("dumping: \"%s\" of type %hu\n"), cmdbuffer+bufindr+CMDHDRSIZE, cmdqueue_current_type); + ClearToSend(); + cmdqueue_pop_front(); + } + bufindr = 0; + bufindw = 0; //commands are removed from command queue after process_command() function is finished //reseting command queue and enqueing new commands during some (usually long running) command processing would cause that new commands are immediately removed from queue (or damaged) //this will ensure that all new commands which are enqueued after cmdqueue reset, will be always executed - cmdbuffer_front_already_processed = true; + cmdbuffer_front_already_processed = true; } // How long a string could be pushed to the front of the command queue? @@ -390,7 +400,7 @@ void get_command() } // start of serial line processing loop - while ((MYSERIAL.available() > 0 && !saved_printing) || (MYSERIAL.available() > 0 && isPrintPaused)) { //is print is saved (crash detection or filament detection), dont process data from serial line + while (((MYSERIAL.available() > 0 && !saved_printing) || (MYSERIAL.available() > 0 && isPrintPaused)) && !cmdqueue_serial_disabled) { //is print is saved (crash detection or filament detection), dont process data from serial line char serial_char = MYSERIAL.read(); /* if (selectedSerialPort == 1) diff --git a/Firmware/cmdqueue.h b/Firmware/cmdqueue.h index 13185f179..017452a2a 100644 --- a/Firmware/cmdqueue.h +++ b/Firmware/cmdqueue.h @@ -35,6 +35,7 @@ extern char cmdbuffer[BUFSIZE * (MAX_CMD_SIZE + 1) + CMDBUFFER_RESERVE_FRONT]; extern size_t bufindr; extern int buflen; extern bool cmdbuffer_front_already_processed; +extern bool cmdqueue_serial_disabled; // Type of a command, which is to be executed right now. #define CMDBUFFER_CURRENT_TYPE (cmdbuffer[bufindr]) @@ -65,8 +66,8 @@ extern void cmdqueue_dump_to_serial_single_line(int nr, const char *p); extern void cmdqueue_dump_to_serial(); #endif /* CMDBUFFER_DEBUG */ extern bool cmd_buffer_empty(); -extern void enquecommand(const char *cmd, bool from_progmem); -extern void enquecommand_front(const char *cmd, bool from_progmem); +extern void enquecommand(const char *cmd, bool from_progmem = false); +extern void enquecommand_front(const char *cmd, bool from_progmem = false); extern void repeatcommand_front(); extern bool is_buffer_empty(); extern void get_command(); diff --git a/Firmware/first_lay_cal.cpp b/Firmware/first_lay_cal.cpp index a6b5f109f..d7ddc5c8d 100644 --- a/Firmware/first_lay_cal.cpp +++ b/Firmware/first_lay_cal.cpp @@ -7,6 +7,7 @@ #include "Configuration_prusa.h" #include "language.h" #include "Marlin.h" +#include "cmdqueue.h" #include "mmu.h" #include diff --git a/Firmware/menu.cpp b/Firmware/menu.cpp index 3c4e89260..9c7704e6f 100755 --- a/Firmware/menu.cpp +++ b/Firmware/menu.cpp @@ -8,6 +8,7 @@ #include "lcd.h" #include "Configuration.h" #include "Marlin.h" +#include "cmdqueue.h" #include "ultralcd.h" #include "language.h" #include "static_assert.h" diff --git a/Firmware/mmu.cpp b/Firmware/mmu.cpp index ab4af5e29..18b5d5e79 100755 --- a/Firmware/mmu.cpp +++ b/Firmware/mmu.cpp @@ -9,6 +9,7 @@ #include "Configuration_prusa.h" #include "fsensor.h" #include "cardreader.h" +#include "cmdqueue.h" #include "ultralcd.h" #include "sound.h" #include "printers.h" diff --git a/Firmware/temperature.cpp b/Firmware/temperature.cpp index cb4fc8aee..b4977c405 100755 --- a/Firmware/temperature.cpp +++ b/Firmware/temperature.cpp @@ -30,6 +30,7 @@ #include "Marlin.h" +#include "cmdqueue.h" #include "ultralcd.h" #include "sound.h" #include "temperature.h" @@ -632,7 +633,6 @@ void fanSpeedError(unsigned char _fan) { fanSpeedErrorBeep(PSTR("Print fan speed is lower than expected"), MSG_FANCHECK_PRINT); break; } - // SERIAL_PROTOCOLLNRPGM(MSG_OK); //This ok messes things up with octoprint. } #endif //(defined(TACH_0) && TACH_0 >-1) || (defined(TACH_1) && TACH_1 > -1) diff --git a/Firmware/ultralcd.cpp b/Firmware/ultralcd.cpp index 63fea0e3f..e93047465 100755 --- a/Firmware/ultralcd.cpp +++ b/Firmware/ultralcd.cpp @@ -1582,6 +1582,7 @@ void lcd_return_to_status() //! @brief Pause print, disable nozzle heater, move to park position void lcd_pause_print() { + SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); //pause for octoprint stop_and_save_print_to_ram(0.0, -default_retraction); lcd_return_to_status(); isPrintPaused = true; @@ -1589,7 +1590,6 @@ void lcd_pause_print() { lcd_commands_type = LcdCommands::LongPause; } - SERIAL_PROTOCOLLNRPGM(MSG_OCTOPRINT_PAUSED); //pause for octoprint } @@ -6740,6 +6740,7 @@ void lcd_resume_print() lcd_return_to_status(); lcd_reset_alert_level(); //for fan speed error if (fan_error_selftest()) return; //abort if error persists + cmdqueue_serial_disabled = false; lcd_setstatuspgm(_T(MSG_FINISHING_MOVEMENTS)); st_synchronize(); @@ -7352,6 +7353,7 @@ void lcd_print_stop() if (!card.sdprinting) { SERIAL_ECHOLNRPGM(MSG_OCTOPRINT_CANCEL); // for Octoprint } + cmdqueue_serial_disabled = false; //for when canceling a print with a fancheck CRITICAL_SECTION_START; @@ -8780,27 +8782,29 @@ static void lcd_selftest_screen_step(int _row, int _col, int _state, const char /** Menu action functions **/ static bool check_file(const char* filename) { - if (farm_mode) return true; + if (farm_mode) + return true; bool result = false; uint32_t filesize; card.openFile((char*)filename, true); filesize = card.getFileSize(); if (filesize > END_FILE_SECTION) { card.setIndex(filesize - END_FILE_SECTION); - } + cmdqueue_reset(); + cmdqueue_serial_disabled = true; - while (!card.eof() && !result) { + while (!card.eof() && !result) { card.sdprinting = true; get_command(); result = check_commands(); - } + + cmdqueue_serial_disabled = false; card.printingHasFinished(); strncpy_P(lcd_status_message, _T(WELCOME_MSG), LCD_WIDTH); lcd_finishstatus(); return result; - } static void menu_action_sdfile(const char* filename)