Skip to content

Commit

Permalink
Add printer_state.cpp/.h
Browse files Browse the repository at this point in the history
  • Loading branch information
3d-gussner committed Nov 17, 2023
1 parent 29e3ea5 commit a65f8b1
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 33 deletions.
14 changes: 1 addition & 13 deletions Firmware/Marlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "pins.h"
#include "Timer.h"
#include "mmu2.h"
#include "printer_state.h"

#ifndef AT90USB
#define HardwareSerial_h // trick to disable the standard HWserial
Expand Down Expand Up @@ -316,19 +317,6 @@ bool printJobOngoing();

bool printer_active();

enum class PrinterStatus : uint8_t
{
NotReady = 0,
IsReady = 1,
Idle = 2,
SDPrintingFinished = 3,
HostPrintingFinished = 4,
IsSDPrinting = 5,
IsHostPrinting = 6,
};

extern PrinterStatus printer_status;

//! Beware - mcode_in_progress is set as soon as the command gets really processed,
//! which is not the same as posting the M600 command into the command queue
//! There can be a considerable lag between posting M600 and its real processing which might result
Expand Down
41 changes: 30 additions & 11 deletions Firmware/Marlin_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ bool __attribute__((noinline)) printer_active() {
|| mesh_bed_leveling_flag;
}

PrinterStatus printer_status;

// Currently only used in one place, allowed to be inlined
bool check_fsensor() {
return printJobOngoing()
Expand Down Expand Up @@ -1735,7 +1733,7 @@ void loop()
usb_timer.start();
}
else if (usb_timer.expired(10000)) { //just need to check if it expired. Nothing else is needed to be done.
printer_status = PrinterStatus::HostPrintingFinished; //set printer state to show LCD menu after finished SD print and report correctly M862.7 Q when USB times out
SetPrinterState(PrinterState::HostPrintingFinished); //set printer state to show LCD menu after finished SD print and report correctly M862.7 Q when USB times out
}

#ifdef PRUSA_M28
Expand Down Expand Up @@ -5841,17 +5839,40 @@ SERIAL_PROTOCOLPGM("\n\n");
#endif // ENABLE_AUTO_BED_LEVELING

/*!
### M72 - Set Ready <a href="https://reprap.org/wiki/G-code#M73:_Set_Ready">M72: Set Ready</a>
### M72 - Set/get Printer State <a href="https://reprap.org/wiki/G-code#M72:_Set.2FGet_Printer_State">M72: Set/get Printer State</a>
Without any parameter get printer state
- 0 = NotReady Used by PrusaConnect
- 1 = IsReady Used by PrusaConnect
- 2 = Idle
- 3 = SD printing finished
- 4 = Host printing finished
- 5 = SD printing
- 6 = Host printing
#### Usage
M72 [ S ]
#### Parameters
- `S` - Set printer ready
- `Snnn` - Set printer state 0 = not_ready, 1 = ready
*/
case 72:
{
if(code_seen('S')) printer_status = PrinterStatus(code_value_uint8());
if(code_seen('S')){
switch (code_value_uint8()){
case 0:
SetPrinterState(PrinterState::NotReady);
break;
case 1:
SetPrinterState(PrinterState::IsReady);
break;
default:
break;
}
} else {
printf_P(_N("PrinterState: %d\n"),uint8_t(GetPrinterState()));
break;
}
break;
}

Expand Down Expand Up @@ -7956,8 +7977,7 @@ SERIAL_PROTOCOLPGM("\n\n");
- M862.3 { P"<model_name>" | Q }
- M862.4 { P<fw_version> | Q }
- M862.5 { P<gcode_level> | Q }
- M862.6 Not used but reserved
- M862.7 { Q }
- M862.6 Not used but reserved by 32-bit
When run with P<> argument, the check is performed against the input value.
When run with Q argument, the current value is shown.
Expand Down Expand Up @@ -8040,10 +8060,9 @@ SERIAL_PROTOCOLPGM("\n\n");
else if(code_seen('Q'))
SERIAL_PROTOCOLLN(GCODE_LEVEL);
break;
case ClPrintChecking::_Features: // ~ .6
case ClPrintChecking::_Features: // ~ .6 used by 32-bit
break;
case ClPrintChecking::_PrinterState: // ~.7
SERIAL_PROTOCOLLN((int)printer_status);
default:
break;
}
break;
Expand Down
4 changes: 2 additions & 2 deletions Firmware/cardreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void CardReader::startFileprint()
if(cardOK)
{
sdprinting = true;
printer_status = PrinterStatus::IsSDPrinting; //set printer state to hide LCD menu and report correctly M862.7 Q while SD printing
SetPrinterState(PrinterState::IsSDPrinting); //set printer state to hide LCD menu and report correctly M862.7 Q while SD printing
#ifdef SDCARD_SORT_ALPHA
//flush_presort();
#endif
Expand Down Expand Up @@ -1019,7 +1019,7 @@ void CardReader::printingHasFinished()
else
{
sdprinting = false;
printer_status = PrinterStatus::SDPrintingFinished; //set printer state to show LCD menu after finished SD print
SetPrinterState(PrinterState::SDPrintingFinished); //set printer state to show LCD menu after finished SD print
if(SD_FINISHED_STEPPERRELEASE)
{
finishAndDisableSteppers();
Expand Down
2 changes: 1 addition & 1 deletion Firmware/cmdqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ void get_command()
// Handle the USB timer
if ((*cmd_start == 'G') && !(IS_SD_PRINTING)) {
usb_timer.start();
printer_status = PrinterStatus::IsHostPrinting; //set printer state busy printing to hide LCD menu and report correctly M862.7 Q while USB printing
SetPrinterState(PrinterState::IsHostPrinting); //set printer state busy printing to hide LCD menu and report correctly M862.7 Q while USB printing
}
if (allow_when_stopped == false && Stopped == true) {
// Stopped can be set either during error states (thermal error: cannot continue), or
Expand Down
16 changes: 16 additions & 0 deletions Firmware/printer_state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! @file
//! @brief Printer State
//! @param GetPrinterState get current printer state
//! @param SetPrinterState set printer state

#include "printer_state.h"

static PrinterState printer_state;

PrinterState GetPrinterState() {
return printer_state;
}

PrinterState SetPrinterState(PrinterState status) {
return printer_state = status;
}
27 changes: 27 additions & 0 deletions Firmware/printer_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//! @file
//! @brief Printer States
//! @param NotReady
//! @param IsReady
//! @param Idle
//! @param SDPrintingFinished
//! @param HostPrintingFinished
//! @param IsSDPrinting
//! @param IsHostPrinting
//! @todo Pause/Resume states, Heating states and more

#pragma once
#include "macros.h"

enum class PrinterState : uint8_t
{
NotReady = 0, //Lowest state to simplify queries
IsReady = 1, //
Idle = 2,
SDPrintingFinished = 3,
HostPrintingFinished = 4,
IsSDPrinting = 5,
IsHostPrinting = 6,
};

PrinterState GetPrinterState();
PrinterState SetPrinterState(PrinterState status);
17 changes: 11 additions & 6 deletions Firmware/ultralcd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ void lcd_commands()
pid_temp = DEFAULT_PID_TEMP;
lcd_commands_step = 0;
lcd_commands_type = LcdCommands::Idle;
SetPrinterState(PrinterState::Idle);
}
}

Expand Down Expand Up @@ -5127,9 +5128,9 @@ static void lcd_sheet_menu()
//! @endcode
static void lcd_printer_status_toggle()
{
if (printer_status == PrinterStatus::NotReady) printer_status = PrinterStatus::IsReady;
else printer_status = PrinterStatus::NotReady;
enquecommandf_P(PSTR("M118 A1 action:%s"), (printer_status == PrinterStatus::NotReady) ? "not_ready" : "ready");
if (GetPrinterState() == PrinterState::IsReady) SetPrinterState(PrinterState::NotReady);
else SetPrinterState(PrinterState::IsReady);
enquecommandf_P(PSTR("M118 A1 action:%s"), (GetPrinterState() == PrinterState::IsReady) ? "ready" : "not_ready");
}

//! @brief Show Main Menu
Expand Down Expand Up @@ -5204,8 +5205,12 @@ static void lcd_main_menu()
} else if (!Stopped) {
MENU_ITEM_SUBMENU_P(_i("Preheat"), lcd_preheat_menu);////MSG_PREHEAT c=18
}
if (printer_status < PrinterStatus::IsSDPrinting) {
MENU_ITEM_TOGGLE_P(_T(MSG_SET_READY), (printer_status == PrinterStatus::IsReady) ? _T(MSG_YES) : _T(MSG_NO), lcd_printer_status_toggle);
if (GetPrinterState() < PrinterState::IsSDPrinting) {
if(GetPrinterState() == PrinterState::IsReady) {
MENU_ITEM_TOGGLE_P(_T(MSG_SET_READY), _T(MSG_NO), lcd_printer_status_toggle);
} else {
MENU_ITEM_TOGGLE_P(_T(MSG_SET_READY), _T(MSG_YES), lcd_printer_status_toggle);
}
}
if (mesh_bed_leveling_flag == false && homing_flag == false && !isPrintPaused && !processing_tcode) {
if (usb_timer.running()) {
Expand Down Expand Up @@ -5664,7 +5669,7 @@ void print_stop(bool interactive)

// return to status is required to continue processing in the main loop!
lcd_commands_type = LcdCommands::StopPrint;
printer_status = PrinterStatus::NotReady; //set printer state to show LCD menu after print has been stopped and report correctly M862.7 Q
SetPrinterState(PrinterState::NotReady); //set printer state to show LCD menu after print has been stopped and report correctly M862.7 Q
lcd_return_to_status();
}

Expand Down

0 comments on commit a65f8b1

Please sign in to comment.