2018-04-25 06:44:26 -05:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 08:00:57 -06:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-04-25 06:44:26 -05:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-27 23:57:50 -05:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2018-04-25 06:44:26 -05:00
|
|
|
*
|
|
|
|
* 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
|
2020-07-22 22:20:14 -05:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-04-25 06:44:26 -05:00
|
|
|
*
|
|
|
|
*/
|
2018-10-30 19:44:12 -05:00
|
|
|
#pragma once
|
2018-04-25 06:44:26 -05:00
|
|
|
|
|
|
|
/**
|
2020-08-06 08:14:00 -05:00
|
|
|
* e_parser.h - Intercept special commands directly in the serial stream
|
2018-04-25 06:44:26 -05:00
|
|
|
*/
|
|
|
|
|
2019-09-09 17:53:23 -05:00
|
|
|
#include "../inc/MarlinConfigPre.h"
|
2018-04-25 06:44:26 -05:00
|
|
|
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
|
|
|
#include "host_actions.h"
|
|
|
|
#endif
|
|
|
|
|
2019-03-10 09:13:54 -05:00
|
|
|
// External references
|
|
|
|
extern bool wait_for_user, wait_for_heatup;
|
2018-04-25 20:58:00 -05:00
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
// From motion.h, which cannot be included here
|
|
|
|
void report_current_position_moving();
|
|
|
|
void quickpause_stepper();
|
|
|
|
void quickresume_stepper();
|
|
|
|
#endif
|
|
|
|
|
2021-04-24 02:53:52 -05:00
|
|
|
void HAL_reboot();
|
|
|
|
|
2018-04-25 06:44:26 -05:00
|
|
|
class EmergencyParser {
|
|
|
|
|
2018-04-25 20:58:00 -05:00
|
|
|
public:
|
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
// Currently looking for: M108, M112, M410, M876 S[0-9], S000, P000, R000
|
|
|
|
enum State : uint8_t {
|
2018-04-25 06:54:17 -05:00
|
|
|
EP_RESET,
|
|
|
|
EP_N,
|
|
|
|
EP_M,
|
|
|
|
EP_M1,
|
2021-04-16 01:59:28 -05:00
|
|
|
EP_M10, EP_M108,
|
|
|
|
EP_M11, EP_M112,
|
|
|
|
EP_M4, EP_M41, EP_M410,
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
2021-04-16 01:59:28 -05:00
|
|
|
EP_M8, EP_M87, EP_M876, EP_M876S, EP_M876SN,
|
|
|
|
#endif
|
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
EP_S, EP_S0, EP_S00, EP_GRBL_STATUS,
|
|
|
|
EP_R, EP_R0, EP_R00, EP_GRBL_RESUME,
|
|
|
|
EP_P, EP_P0, EP_P00, EP_GRBL_PAUSE,
|
2019-02-12 15:55:47 -06:00
|
|
|
#endif
|
2021-04-24 02:53:52 -05:00
|
|
|
#if ENABLED(SOFT_RESET_VIA_SERIAL)
|
|
|
|
EP_ctrl,
|
|
|
|
EP_K, EP_KI, EP_KIL, EP_KILL,
|
|
|
|
#endif
|
2018-04-25 06:54:17 -05:00
|
|
|
EP_IGNORE // to '\n'
|
2018-04-25 06:44:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
static bool killed_by_M112;
|
2020-10-16 16:11:00 -05:00
|
|
|
static bool quickstop_by_M410;
|
2018-04-25 06:44:26 -05:00
|
|
|
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
|
|
|
static uint8_t M876_reason;
|
|
|
|
#endif
|
|
|
|
|
2018-10-30 19:44:12 -05:00
|
|
|
EmergencyParser() { enable(); }
|
2018-04-25 06:44:26 -05:00
|
|
|
|
2018-10-30 19:44:12 -05:00
|
|
|
FORCE_INLINE static void enable() { enabled = true; }
|
|
|
|
FORCE_INLINE static void disable() { enabled = false; }
|
|
|
|
|
|
|
|
FORCE_INLINE static void update(State &state, const uint8_t c) {
|
2018-04-25 20:58:00 -05:00
|
|
|
switch (state) {
|
|
|
|
case EP_RESET:
|
|
|
|
switch (c) {
|
2020-03-13 13:06:53 -05:00
|
|
|
case ' ': case '\n': case '\r': break;
|
2021-04-16 01:59:28 -05:00
|
|
|
case 'N': state = EP_N; break;
|
|
|
|
case 'M': state = EP_M; break;
|
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
case 'S': state = EP_S; break;
|
|
|
|
case 'P': state = EP_P; break;
|
|
|
|
case 'R': state = EP_R; break;
|
|
|
|
#endif
|
2021-04-24 02:53:52 -05:00
|
|
|
#if ENABLED(SOFT_RESET_VIA_SERIAL)
|
|
|
|
case '^': state = EP_ctrl; break;
|
|
|
|
case 'K': state = EP_K; break;
|
|
|
|
#endif
|
2021-04-16 01:59:28 -05:00
|
|
|
default: state = EP_IGNORE;
|
2018-04-25 20:58:00 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EP_N:
|
|
|
|
switch (c) {
|
2020-10-09 06:25:23 -05:00
|
|
|
case '0' ... '9':
|
2021-04-16 01:59:28 -05:00
|
|
|
case '-': case ' ': break;
|
|
|
|
case 'M': state = EP_M; break;
|
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
case 'S': state = EP_S; break;
|
|
|
|
case 'P': state = EP_P; break;
|
|
|
|
case 'R': state = EP_R; break;
|
|
|
|
#endif
|
|
|
|
default: state = EP_IGNORE;
|
2018-04-25 20:58:00 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
case EP_S: state = (c == '0') ? EP_S0 : EP_IGNORE; break;
|
|
|
|
case EP_S0: state = (c == '0') ? EP_S00 : EP_IGNORE; break;
|
|
|
|
case EP_S00: state = (c == '0') ? EP_GRBL_STATUS : EP_IGNORE; break;
|
|
|
|
|
|
|
|
case EP_R: state = (c == '0') ? EP_R0 : EP_IGNORE; break;
|
|
|
|
case EP_R0: state = (c == '0') ? EP_R00 : EP_IGNORE; break;
|
|
|
|
case EP_R00: state = (c == '0') ? EP_GRBL_RESUME : EP_IGNORE; break;
|
|
|
|
|
|
|
|
case EP_P: state = (c == '0') ? EP_P0 : EP_IGNORE; break;
|
|
|
|
case EP_P0: state = (c == '0') ? EP_P00 : EP_IGNORE; break;
|
|
|
|
case EP_P00: state = (c == '0') ? EP_GRBL_PAUSE : EP_IGNORE; break;
|
|
|
|
#endif
|
|
|
|
|
2021-04-24 02:53:52 -05:00
|
|
|
#if ENABLED(SOFT_RESET_VIA_SERIAL)
|
|
|
|
case EP_ctrl: state = (c == 'X') ? EP_KILL : EP_IGNORE; break;
|
|
|
|
case EP_K: state = (c == 'I') ? EP_KI : EP_IGNORE; break;
|
|
|
|
case EP_KI: state = (c == 'L') ? EP_KIL : EP_IGNORE; break;
|
|
|
|
case EP_KIL: state = (c == 'L') ? EP_KILL : EP_IGNORE; break;
|
|
|
|
#endif
|
|
|
|
|
2018-04-25 20:58:00 -05:00
|
|
|
case EP_M:
|
|
|
|
switch (c) {
|
|
|
|
case ' ': break;
|
|
|
|
case '1': state = EP_M1; break;
|
|
|
|
case '4': state = EP_M4; break;
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
|
|
|
case '8': state = EP_M8; break;
|
|
|
|
#endif
|
2018-04-25 20:58:00 -05:00
|
|
|
default: state = EP_IGNORE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EP_M1:
|
|
|
|
switch (c) {
|
|
|
|
case '0': state = EP_M10; break;
|
|
|
|
case '1': state = EP_M11; break;
|
|
|
|
default: state = EP_IGNORE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
case EP_M10: state = (c == '8') ? EP_M108 : EP_IGNORE; break;
|
|
|
|
case EP_M11: state = (c == '2') ? EP_M112 : EP_IGNORE; break;
|
|
|
|
case EP_M4: state = (c == '1') ? EP_M41 : EP_IGNORE; break;
|
|
|
|
case EP_M41: state = (c == '0') ? EP_M410 : EP_IGNORE; break;
|
2018-04-25 20:58:00 -05:00
|
|
|
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
case EP_M8: state = (c == '7') ? EP_M87 : EP_IGNORE; break;
|
|
|
|
case EP_M87: state = (c == '6') ? EP_M876 : EP_IGNORE; break;
|
2019-02-12 15:55:47 -06:00
|
|
|
|
2021-04-16 01:59:28 -05:00
|
|
|
case EP_M876:
|
|
|
|
switch (c) {
|
|
|
|
case ' ': break;
|
|
|
|
case 'S': state = EP_M876S; break;
|
|
|
|
default: state = EP_IGNORE; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EP_M876S:
|
|
|
|
switch (c) {
|
|
|
|
case ' ': break;
|
|
|
|
case '0' ... '9':
|
|
|
|
state = EP_M876SN;
|
|
|
|
M876_reason = uint8_t(c - '0');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2019-02-12 15:55:47 -06:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-04-25 20:58:00 -05:00
|
|
|
case EP_IGNORE:
|
2020-03-13 16:29:29 -05:00
|
|
|
if (ISEOL(c)) state = EP_RESET;
|
2018-04-25 20:58:00 -05:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-03-13 16:29:29 -05:00
|
|
|
if (ISEOL(c)) {
|
2018-10-30 19:44:12 -05:00
|
|
|
if (enabled) switch (state) {
|
2019-02-04 03:37:26 -06:00
|
|
|
case EP_M108: wait_for_user = wait_for_heatup = false; break;
|
|
|
|
case EP_M112: killed_by_M112 = true; break;
|
2020-10-16 16:11:00 -05:00
|
|
|
case EP_M410: quickstop_by_M410 = true; break;
|
2019-02-12 15:55:47 -06:00
|
|
|
#if ENABLED(HOST_PROMPT_SUPPORT)
|
|
|
|
case EP_M876SN: host_response_handler(M876_reason); break;
|
|
|
|
#endif
|
2021-04-16 01:59:28 -05:00
|
|
|
#if ENABLED(REALTIME_REPORTING_COMMANDS)
|
|
|
|
case EP_GRBL_STATUS: report_current_position_moving(); break;
|
|
|
|
case EP_GRBL_PAUSE: quickpause_stepper(); break;
|
|
|
|
case EP_GRBL_RESUME: quickresume_stepper(); break;
|
|
|
|
#endif
|
2021-04-24 02:53:52 -05:00
|
|
|
#if ENABLED(SOFT_RESET_VIA_SERIAL)
|
|
|
|
case EP_KILL: HAL_reboot(); break;
|
|
|
|
#endif
|
2019-02-04 03:37:26 -06:00
|
|
|
default: break;
|
2018-04-25 20:58:00 -05:00
|
|
|
}
|
|
|
|
state = EP_RESET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 06:44:26 -05:00
|
|
|
|
2018-10-30 19:44:12 -05:00
|
|
|
private:
|
|
|
|
static bool enabled;
|
2018-04-25 06:44:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
extern EmergencyParser emergency_parser;
|