diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h index feda4bb06d..8fb742e0d2 100644 --- a/Marlin/Marlin.h +++ b/Marlin/Marlin.h @@ -65,6 +65,8 @@ typedef unsigned long millis_t; #include "WString.h" +#include "stopwatch.h" + #ifdef USBCON #if ENABLED(BLUETOOTH) #define MYSERIAL bluetoothSerial @@ -356,8 +358,8 @@ extern bool axis_homed[3]; // axis[n].is_homed extern float retract_recover_length, retract_recover_length_swap, retract_recover_feedrate; #endif -extern millis_t print_job_start_ms; -extern millis_t print_job_stop_ms; +// Print job timer +extern Stopwatch print_job_timer; // Handling multiple extruders pins extern uint8_t active_extruder; @@ -373,9 +375,4 @@ extern uint8_t active_extruder; extern void calculate_volumetric_multipliers(); -// Print job timer related functions -millis_t print_job_timer(); -bool print_job_start(millis_t t = 0); -bool print_job_stop(bool force = false); - #endif //MARLIN_H diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 863b6515e2..e59c8d46d9 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -298,8 +298,7 @@ const int sensitive_pins[] = SENSITIVE_PINS; ///< Sensitive pin list for M42 millis_t previous_cmd_ms = 0; static millis_t max_inactive_time = 0; static millis_t stepper_inactive_time = (DEFAULT_STEPPER_DEACTIVE_TIME) * 1000L; -millis_t print_job_start_ms = 0; ///< Print job start time -millis_t print_job_stop_ms = 0; ///< Print job stop time +Stopwatch print_job_timer = Stopwatch(); static uint8_t target_extruder; #if ENABLED(AUTO_BED_LEVELING_FEATURE) @@ -1011,9 +1010,9 @@ inline void get_serial_commands() { ) { if (card_eof) { SERIAL_PROTOCOLLNPGM(MSG_FILE_PRINTED); - print_job_stop(true); + print_job_timer.stop(); char time[30]; - millis_t t = print_job_timer(); + millis_t t = print_job_timer.duration(); int hours = t / 60 / 60, minutes = (t / 60) % 60; sprintf_P(time, PSTR("%i " MSG_END_HOUR " %i " MSG_END_MINUTE), hours, minutes); SERIAL_ECHO_START; @@ -3571,7 +3570,7 @@ inline void gcode_M17() { */ inline void gcode_M24() { card.startFileprint(); - print_job_start(); + print_job_timer.start(); } /** @@ -3627,7 +3626,7 @@ inline void gcode_M17() { * M31: Get the time since the start of SD Print (or last M109) */ inline void gcode_M31() { - millis_t t = print_job_timer(); + millis_t t = print_job_timer.duration(); int min = t / 60, sec = t % 60; char time[30]; sprintf_P(time, PSTR("%i min, %i sec"), min, sec); @@ -3663,7 +3662,7 @@ inline void gcode_M31() { card.startFileprint(); // Procedure calls count as normal print time. - if (!call_procedure) print_job_start(); + if (!call_procedure) print_job_timer.start(); } } @@ -4030,6 +4029,27 @@ inline void gcode_M42() { #endif // AUTO_BED_LEVELING_FEATURE && Z_MIN_PROBE_REPEATABILITY_TEST +/** + * M75: Start print timer + */ +inline void gcode_M75() { + print_job_timer.start(); +} + +/** + * M76: Pause print timer + */ +inline void gcode_M76() { + print_job_timer.pause(); +} + +/** + * M77: Stop print timer + */ +inline void gcode_M77() { + print_job_timer.stop(); +} + /** * M104: Set hot end temperature */ @@ -4037,9 +4057,6 @@ inline void gcode_M104() { if (setTargetedHotend(104)) return; if (DEBUGGING(DRYRUN)) return; - // Start hook must happen before setTargetHotend() - print_job_start(); - if (code_seen('S')) { float temp = code_value(); setTargetHotend(temp, target_extruder); @@ -4048,10 +4065,24 @@ inline void gcode_M104() { setTargetHotend1(temp == 0.0 ? 0.0 : temp + duplicate_extruder_temp_offset); #endif + /** + * We use half EXTRUDE_MINTEMP here to allow nozzles to be put into hot + * stand by mode, for instance in a dual extruder setup, without affecting + * the running print timer. + */ + if (temp <= (EXTRUDE_MINTEMP)/2) { + print_job_timer.stop(); + LCD_MESSAGEPGM(WELCOME_MSG); + } + /** + * We do not check if the timer is already running because this check will + * be done for us inside the Stopwatch::start() method thus a running timer + * will not restart. + */ + else print_job_timer.start(); + if (temp > degHotend(target_extruder)) LCD_MESSAGEPGM(MSG_HEATING); } - - if (print_job_stop()) LCD_MESSAGEPGM(WELCOME_MSG); } #if HAS_TEMP_HOTEND || HAS_TEMP_BED @@ -4179,9 +4210,6 @@ inline void gcode_M109() { if (setTargetedHotend(109)) return; if (DEBUGGING(DRYRUN)) return; - // Start hook must happen before setTargetHotend() - print_job_start(); - no_wait_for_cooling = code_seen('S'); if (no_wait_for_cooling || code_seen('R')) { float temp = code_value(); @@ -4191,11 +4219,25 @@ inline void gcode_M109() { setTargetHotend1(temp == 0.0 ? 0.0 : temp + duplicate_extruder_temp_offset); #endif + /** + * We use half EXTRUDE_MINTEMP here to allow nozzles to be put into hot + * stand by mode, for instance in a dual extruder setup, without affecting + * the running print timer. + */ + if (temp <= (EXTRUDE_MINTEMP)/2) { + print_job_timer.stop(); + LCD_MESSAGEPGM(WELCOME_MSG); + } + /** + * We do not check if the timer is already running because this check will + * be done for us inside the Stopwatch::start() method thus a running timer + * will not restart. + */ + else print_job_timer.start(); + if (temp > degHotend(target_extruder)) LCD_MESSAGEPGM(MSG_HEATING); } - if (print_job_stop()) LCD_MESSAGEPGM(WELCOME_MSG); - #if ENABLED(AUTOTEMP) autotemp_enabled = code_seen('F'); if (autotemp_enabled) autotemp_factor = code_value(); @@ -6223,6 +6265,18 @@ void process_next_command() { break; #endif // AUTO_BED_LEVELING_FEATURE && Z_MIN_PROBE_REPEATABILITY_TEST + case 75: // Start print timer + gcode_M75(); + break; + + case 76: // Pause print timer + gcode_M76(); + break; + + case 77: // Stop print timer + gcode_M77(); + break; + #if ENABLED(M100_FREE_MEMORY_WATCHER) case 100: gcode_M100(); @@ -7639,50 +7693,3 @@ void calculate_volumetric_multipliers() { for (int i = 0; i < EXTRUDERS; i++) volumetric_multiplier[i] = calculate_volumetric_multiplier(filament_size[i]); } - -/** - * Start the print job timer - * - * The print job is only started if all extruders have their target temp at zero - * otherwise the print job timew would be reset everytime a M109 is received. - * - * @param t start timer timestamp - * - * @return true if the timer was started at function call - */ -bool print_job_start(millis_t t /* = 0 */) { - for (int i = 0; i < EXTRUDERS; i++) if (degTargetHotend(i) > 0) return false; - print_job_start_ms = (t) ? t : millis(); - print_job_stop_ms = 0; - return true; -} - -/** - * Check if the running print job has finished and stop the timer - * - * When the target temperature for all extruders is zero then we assume that the - * print job has finished printing. There are some special conditions under which - * this assumption may not be valid: If during a print job for some reason the - * user decides to bring a nozzle temp down and only then heat the other afterwards. - * - * @param force stops the timer ignoring all pre-checks - * - * @return boolean true if the print job has finished printing - */ -bool print_job_stop(bool force /* = false */) { - if (!print_job_start_ms) return false; - if (!force) for (int i = 0; i < EXTRUDERS; i++) if (degTargetHotend(i) > 0) return false; - print_job_stop_ms = millis(); - return true; -} - -/** - * Output the print job timer in seconds - * - * @return the number of seconds - */ -millis_t print_job_timer() { - if (!print_job_start_ms) return 0; - return (((print_job_stop_ms > print_job_start_ms) - ? print_job_stop_ms : millis()) - print_job_start_ms) / 1000; -} diff --git a/Marlin/dogm_lcd_implementation.h b/Marlin/dogm_lcd_implementation.h index 4807fff56f..4da13cd297 100644 --- a/Marlin/dogm_lcd_implementation.h +++ b/Marlin/dogm_lcd_implementation.h @@ -334,9 +334,8 @@ static void lcd_implementation_status_screen() { } u8g.setPrintPos(80,48); - if (print_job_start_ms != 0) { - uint16_t time = (((print_job_stop_ms > print_job_start_ms) - ? print_job_stop_ms : millis()) - print_job_start_ms) / 60000; + uint16_t time = print_job_timer.duration() / 60; + if (time != 0) { lcd_print(itostr2(time/60)); lcd_print(':'); lcd_print(itostr2(time%60)); diff --git a/Marlin/stopwatch.cpp b/Marlin/stopwatch.cpp new file mode 100644 index 0000000000..5bc0a280fd --- /dev/null +++ b/Marlin/stopwatch.cpp @@ -0,0 +1,77 @@ +/* + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#include "Marlin.h" +#include "stopwatch.h" + +Stopwatch::Stopwatch() { + this->reset(); + } + +void Stopwatch::stop() { + if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("Stopwatch::stop()"); + if (!this->isRunning()) return; + + this->status = STPWTCH_STOPPED; + this->stopTimestamp = millis(); +} + +void Stopwatch::pause() { + if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("Stopwatch::pause()"); + if (!this->isRunning()) return; + + this->status = STPWTCH_PAUSED; + this->stopTimestamp = millis(); +} + +void Stopwatch::start() { + if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("Stopwatch::start()"); + if (this->isRunning()) return; + + if (this->isPaused()) this->accumulator = this->duration(); + else this->reset(); + + this->status = STPWTCH_RUNNING; + this->startTimestamp = millis(); +} + +void Stopwatch::reset() { + if (DEBUGGING(INFO)) SERIAL_ECHOLNPGM("Stopwatch::reset()"); + + this->status = STPWTCH_STOPPED; + this->startTimestamp = 0; + this->stopTimestamp = 0; + this->accumulator = 0; +} + +bool Stopwatch::isRunning() { + return (this->status == STPWTCH_RUNNING) ? true : false; +} + +bool Stopwatch::isPaused() { + return (this->status == STPWTCH_PAUSED) ? true : false; +} + +uint16_t Stopwatch::duration() { + return (((this->isRunning()) ? millis() : this->stopTimestamp) + - this->startTimestamp) / 1000 + this->accumulator; +} diff --git a/Marlin/stopwatch.h b/Marlin/stopwatch.h new file mode 100644 index 0000000000..d6ef8a7444 --- /dev/null +++ b/Marlin/stopwatch.h @@ -0,0 +1,99 @@ +/* + * Marlin 3D Printer Firmware + * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin] + * + * Based on Sprinter and grbl. + * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +#ifndef STOPWATCH_H +#define STOPWATCH_H + +enum StopwatchStatus { + STPWTCH_STOPPED = 0x0, + STPWTCH_RUNNING = 0x1, + STPWTCH_PAUSED = 0x2 +}; + +/** + * @brief Stopwatch class + * @details This class acts as a timer proving stopwatch functionality including + * the ability to pause the running time counter. + */ +class Stopwatch { + private: + StopwatchStatus status; + uint16_t accumulator; + uint32_t startTimestamp; + uint32_t stopTimestamp; + + public: + /** + * @brief Class constructor + */ + Stopwatch(); + + /** + * @brief Stops the stopwatch + * @details Stops the running timer, it will silently ignore the request if + * no timer is currently running. + */ + void stop(); + + /** + * @brief Pauses the stopwatch + * @details Pauses the running timer, it will silently ignore the request if + * no timer is currently running. + */ + void pause(); + + /** + * @brief Starts the stopwatch + * @details Starts the timer, it will silently ignore the request if the + * timer is already running. + */ + void start(); + + /** + * @brief Resets the stopwatch + * @details Resets all settings to their default values. + */ + void reset(); + + /** + * @brief Checks if the timer is running + * @details Returns true if the timer is currently running, false otherwise. + * @return bool + */ + bool isRunning(); + + /** + * @brief Checks if the timer is paused + * @details Returns true if the timer is currently paused, false otherwise. + * @return bool + */ + bool isPaused(); + + /** + * @brief Gets the running time + * @details Returns the total number of seconds the timer has been running. + * @return uint16_t + */ + uint16_t duration(); +}; + +#endif //STOPWATCH_H diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index a6898368ec..d9a5895f44 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -1175,7 +1175,7 @@ void disable_all_heaters() { setTargetBed(0); // If all heaters go down then for sure our print job has stopped - print_job_stop(true); + print_job_timer.stop(); #define DISABLE_HEATER(NR) { \ setTargetHotend(NR, 0); \ diff --git a/Marlin/ultralcd_implementation_hitachi_HD44780.h b/Marlin/ultralcd_implementation_hitachi_HD44780.h index 5fc0316d7e..26399fbad4 100644 --- a/Marlin/ultralcd_implementation_hitachi_HD44780.h +++ b/Marlin/ultralcd_implementation_hitachi_HD44780.h @@ -716,9 +716,9 @@ static void lcd_implementation_status_screen() { lcd.setCursor(LCD_WIDTH - 6, 2); lcd.print(LCD_STR_CLOCK[0]); - if (print_job_start_ms != 0) { - uint16_t time = (((print_job_stop_ms > print_job_start_ms) - ? print_job_stop_ms : millis()) - print_job_start_ms) / 60000; + + uint16_t time = print_job_timer.duration() / 60; + if (time != 0) { lcd.print(itostr2(time / 60)); lcd.print(':'); lcd.print(itostr2(time % 60));