Skip to content

Commit

Permalink
Print abort improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
kisslorand committed Sep 24, 2023
1 parent cd69550 commit eda7999
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 63 deletions.
71 changes: 35 additions & 36 deletions TFT/src/User/API/Printing.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,13 @@ void resumeAndContinue(void)
sendEmergencyCmd("M876 S1\n");
}

void abortAndTerminate(void)
static void sendPrintAbortCmd(void)
{
clearQueueAndMore();

if (infoMachineSettings.firmwareType != FW_REPRAPFW)
{
// clear the command queue and send the M524 gcode immediately if there is an already pending gcode waiting for an ACK message.
// Otherwise, store the gcode on command queue to send it waiting for its related ACK message
//
if (isPendingCmd())
sendEmergencyCmd("M524\n");
else
mustStoreCmd("M524\n");
sendEmergencyCmd("M524\n");
}
else // if RepRap
{
Expand All @@ -98,14 +92,35 @@ void abortAndTerminate(void)
}
}

void loopBreakToCondition(CONDITION_CALLBACK condCallback)
void breakHost(void)
{
// M108 is sent to Marlin because consecutive blocking operations such as heating bed, extruder may defer processing of other gcodes.
// If there's any ongoing blocking command, "M108" will take that out from the closed loop and a response will be received
// from that command. Than another "M108" will be sent to unlock a next possible blocking command.
// This way enough "M108" will be sent to unlock all possible blocking command(s) (ongoing or enqueued) but not too much and
// not too fast one after another to overload/overrun the serial communication
TASK_LOOP_WHILE(condCallback(), if (Serial_Available(SERIAL_PORT) != 0) sendEmergencyCmd("M108\n"));
switch (infoFile.source)
{
case FS_REMOTE_HOST:
// - forward a print cancel notification to all hosts (so also the one handling the print) asking to cancel the print
// - the host handling the print should respond to this notification with "M118 P0 A1 action:cancel" that will
// trigger setPrintAbort() in parseACK() once the following loop does its job (stopping all blocking operations)
mustStoreCmd("M118 P0 A1 action:notification remote cancel\n");
break;

case FS_TFT_SD:
case FS_TFT_USB:
infoHost.status = HOST_STATUS_ABORTING;

case FS_ONBOARD_MEDIA:
case FS_ONBOARD_MEDIA_REMOTE:
// triggers setPrintAbort() in parseACK() once the following loop does its job (stopping all blocking operations)
mustStoreCmd("M118 P0 A1 action:cancel\n");
break;
}

// M108 is sent to Marlin because consecutive blocking operations such as heating bed, heating extruder
// may defer processing of other gcodes. If there's any ongoing blocking command, "M108" will take that
// out from the closed loop and a response will be received from such a blocking command.
//
// This will unlock all possible blocking command(s) (ongoing or enqueued) but not too much
// and not too fast one after another to overload/overrun the serial communication.
TASK_LOOP_WHILE(infoHost.status != HOST_STATUS_IDLE, if (dmaL1Data[SERIAL_PORT].wIndex != dmaL1Data[SERIAL_PORT].rIndex) sendEmergencyCmd("M108\n"))

// remove any enqueued command that could come from a supplementary serial port or TFT media
// (if printing from remote host or TFT media) during the loop above
Expand Down Expand Up @@ -567,36 +582,20 @@ void abortPrint(void)
{
case FS_TFT_SD:
case FS_TFT_USB:
loopBreakToCondition(&isPendingCmd); // break a pending gcode waiting for an ACK message, if any
setPrintAbort(); // finalize the print abort
setPrintAbort();
breakHost();
break;

case FS_ONBOARD_MEDIA:
case FS_ONBOARD_MEDIA_REMOTE:
popupSplash(DIALOG_TYPE_INFO, LABEL_SCREEN_INFO, LABEL_BUSY);
loopPopup(); // trigger the immediate draw of the above popup

// clear the command queue and send the M524 gcode immediately if there is an already pending gcode waiting for an ACK message.
// Otherwise, store the gcode on command queue to send it waiting for its related ACK message.
// Furthermore, forward the print cancel action to all hosts (also TFT) to notify the print cancelation
//
// NOTE: the print cancel action received by the TFT always guarantees the invokation of setPrintAbort() in parseAck.c
// to finalize the print (e.g. stats) in case the ACK messages "Not SD printing" and/or "//action:cancel"
// are not received from Marlin
//
abortAndTerminate();
mustStoreCmd("M118 P0 A1 action:cancel\n");

// loop on break until infoHost.status is set to "HOST_STATUS_IDLE" by setPrintAbort() in parseAck.c
loopBreakToCondition(&isPrintingFromOnboard);
sendPrintAbortCmd();
breakHost();
break;

case FS_REMOTE_HOST:
loopBreakToCondition(&isPendingCmd); // break a pending gcode waiting for an ACK message, if any

// forward a print cancel notification to all hosts (so also the one handling the print) asking to cancel the print
mustStoreCmd("M118 P0 A1 action:notification remote cancel\n");

breakHost();
loopDetected = false; // finally, remove lock and exit
return;
}
Expand Down
6 changes: 0 additions & 6 deletions TFT/src/User/API/Printing.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ void breakAndContinue(void);
void resumeAndPurge(void);
void resumeAndContinue(void);

//
// commented because NOT externally invoked
//
//void abortAndTerminate(void);
//void loopBreakToCondition(CONDITION_CALLBACK condCallback);

void setPrintExpectedTime(uint32_t expectedTime);
uint32_t getPrintExpectedTime(void);

Expand Down
8 changes: 0 additions & 8 deletions TFT/src/User/API/SerialConnection.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,6 @@ void Serial_Forward(SERIAL_PORT_INDEX portIndex, const char * msg)
}
}

uint16_t Serial_Available(SERIAL_PORT_INDEX portIndex)
{
if (!WITHIN(portIndex, PORT_1, SERIAL_PORT_COUNT - 1))
return 0;

return (dmaL1Data[portIndex].cacheSize + dmaL1Data[portIndex].wIndex - dmaL1Data[portIndex].rIndex) % dmaL1Data[portIndex].cacheSize;
}

uint16_t Serial_Get(SERIAL_PORT_INDEX portIndex, char * buf, uint16_t bufSize)
{
// if port index is out of range or no data to read from L1 cache
Expand Down
6 changes: 0 additions & 6 deletions TFT/src/User/API/SerialConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ void Serial_DeInit(SERIAL_PORT_INDEX portIndex);
// - specific port index: specific serial port
void Serial_Forward(SERIAL_PORT_INDEX portIndex, const char * msg);

// retrieve the number of bytes available on the provided serial port:
// - portIndex: index of serial port
//
// - return value: number of bytes available on serial port
uint16_t Serial_Available(SERIAL_PORT_INDEX portIndex);

// retrieve a message from the provided serial port, if any:
// - portIndex: index of serial port where data are read from
// - buf: buffer where data are stored
Expand Down
5 changes: 0 additions & 5 deletions TFT/src/User/API/interfaceCmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ uint8_t cmd_index;
WRITING_MODE writing_mode = NO_WRITING;
FIL file;

bool isPendingCmd(void)
{
return infoHost.wait;
}

bool isFullCmdQueue(void)
{
return (cmdQueue.count >= CMD_QUEUE_SIZE);
Expand Down
1 change: 0 additions & 1 deletion TFT/src/User/API/interfaceCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ extern "C" {

typedef char CMD[CMD_MAX_SIZE];

bool isPendingCmd(void); // also usable as condition callback for loopProcessToCondition()
bool isFullCmdQueue(void); // also usable as condition callback for loopProcessToCondition()
bool isNotEmptyCmdQueue(void); // also usable as condition callback for loopProcessToCondition()
bool isEnqueued(const CMD cmd);
Expand Down
3 changes: 2 additions & 1 deletion TFT/src/User/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ typedef enum
HOST_STATUS_PRINTING,
HOST_STATUS_RESUMING,
HOST_STATUS_PAUSED,
HOST_STATUS_PAUSING
HOST_STATUS_PAUSING,
HOST_STATUS_ABORTING
} HOST_STATUS;

typedef struct
Expand Down

0 comments on commit eda7999

Please sign in to comment.