My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

e_parser.h 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * e_parser.h - Intercept special commands directly in the serial stream
  25. */
  26. #include "../inc/MarlinConfigPre.h"
  27. #if ENABLED(HOST_PROMPT_SUPPORT)
  28. #include "host_actions.h"
  29. #endif
  30. // External references
  31. extern bool wait_for_user, wait_for_heatup;
  32. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  33. // From motion.h, which cannot be included here
  34. void report_current_position_moving();
  35. void quickpause_stepper();
  36. void quickresume_stepper();
  37. #endif
  38. #if ENABLED(SOFT_RESET_VIA_SERIAL)
  39. void HAL_reboot();
  40. #endif
  41. class EmergencyParser {
  42. public:
  43. // Currently looking for: M108, M112, M410, M524, M876 S[0-9], S000, P000, R000
  44. enum State : uint8_t {
  45. EP_RESET,
  46. EP_N,
  47. EP_M,
  48. EP_M1,
  49. EP_M10, EP_M108,
  50. EP_M11, EP_M112,
  51. EP_M4, EP_M41, EP_M410,
  52. #if ENABLED(SDSUPPORT)
  53. EP_M5, EP_M52, EP_M524,
  54. #endif
  55. #if ENABLED(HOST_PROMPT_SUPPORT)
  56. EP_M8, EP_M87, EP_M876, EP_M876S, EP_M876SN,
  57. #endif
  58. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  59. EP_S, EP_S0, EP_S00, EP_GRBL_STATUS,
  60. EP_R, EP_R0, EP_R00, EP_GRBL_RESUME,
  61. EP_P, EP_P0, EP_P00, EP_GRBL_PAUSE,
  62. #endif
  63. #if ENABLED(SOFT_RESET_VIA_SERIAL)
  64. EP_ctrl,
  65. EP_K, EP_KI, EP_KIL, EP_KILL,
  66. #endif
  67. EP_IGNORE // to '\n'
  68. };
  69. static bool killed_by_M112;
  70. static bool quickstop_by_M410;
  71. #if ENABLED(SDSUPPORT)
  72. static bool sd_abort_by_M524;
  73. #endif
  74. #if ENABLED(HOST_PROMPT_SUPPORT)
  75. static uint8_t M876_reason;
  76. #endif
  77. EmergencyParser() { enable(); }
  78. FORCE_INLINE static void enable() { enabled = true; }
  79. FORCE_INLINE static void disable() { enabled = false; }
  80. FORCE_INLINE static void update(State &state, const uint8_t c) {
  81. switch (state) {
  82. case EP_RESET:
  83. switch (c) {
  84. case ' ': case '\n': case '\r': break;
  85. case 'N': state = EP_N; break;
  86. case 'M': state = EP_M; break;
  87. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  88. case 'S': state = EP_S; break;
  89. case 'P': state = EP_P; break;
  90. case 'R': state = EP_R; break;
  91. #endif
  92. #if ENABLED(SOFT_RESET_VIA_SERIAL)
  93. case '^': state = EP_ctrl; break;
  94. case 'K': state = EP_K; break;
  95. #endif
  96. default: state = EP_IGNORE;
  97. }
  98. break;
  99. case EP_N:
  100. switch (c) {
  101. case '0' ... '9':
  102. case '-': case ' ': break;
  103. case 'M': state = EP_M; break;
  104. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  105. case 'S': state = EP_S; break;
  106. case 'P': state = EP_P; break;
  107. case 'R': state = EP_R; break;
  108. #endif
  109. default: state = EP_IGNORE;
  110. }
  111. break;
  112. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  113. case EP_S: state = (c == '0') ? EP_S0 : EP_IGNORE; break;
  114. case EP_S0: state = (c == '0') ? EP_S00 : EP_IGNORE; break;
  115. case EP_S00: state = (c == '0') ? EP_GRBL_STATUS : EP_IGNORE; break;
  116. case EP_R: state = (c == '0') ? EP_R0 : EP_IGNORE; break;
  117. case EP_R0: state = (c == '0') ? EP_R00 : EP_IGNORE; break;
  118. case EP_R00: state = (c == '0') ? EP_GRBL_RESUME : EP_IGNORE; break;
  119. case EP_P: state = (c == '0') ? EP_P0 : EP_IGNORE; break;
  120. case EP_P0: state = (c == '0') ? EP_P00 : EP_IGNORE; break;
  121. case EP_P00: state = (c == '0') ? EP_GRBL_PAUSE : EP_IGNORE; break;
  122. #endif
  123. #if ENABLED(SOFT_RESET_VIA_SERIAL)
  124. case EP_ctrl: state = (c == 'X') ? EP_KILL : EP_IGNORE; break;
  125. case EP_K: state = (c == 'I') ? EP_KI : EP_IGNORE; break;
  126. case EP_KI: state = (c == 'L') ? EP_KIL : EP_IGNORE; break;
  127. case EP_KIL: state = (c == 'L') ? EP_KILL : EP_IGNORE; break;
  128. #endif
  129. case EP_M:
  130. switch (c) {
  131. case ' ': break;
  132. case '1': state = EP_M1; break;
  133. case '4': state = EP_M4; break;
  134. #if ENABLED(SDSUPPORT)
  135. case '5': state = EP_M5; break;
  136. #endif
  137. #if ENABLED(HOST_PROMPT_SUPPORT)
  138. case '8': state = EP_M8; break;
  139. #endif
  140. default: state = EP_IGNORE;
  141. }
  142. break;
  143. case EP_M1:
  144. switch (c) {
  145. case '0': state = EP_M10; break;
  146. case '1': state = EP_M11; break;
  147. default: state = EP_IGNORE;
  148. }
  149. break;
  150. case EP_M10: state = (c == '8') ? EP_M108 : EP_IGNORE; break;
  151. case EP_M11: state = (c == '2') ? EP_M112 : EP_IGNORE; break;
  152. case EP_M4: state = (c == '1') ? EP_M41 : EP_IGNORE; break;
  153. case EP_M41: state = (c == '0') ? EP_M410 : EP_IGNORE; break;
  154. #if ENABLED(SDSUPPORT)
  155. case EP_M5: state = (c == '2') ? EP_M52 : EP_IGNORE; break;
  156. case EP_M52: state = (c == '4') ? EP_M524 : EP_IGNORE; break;
  157. #endif
  158. #if ENABLED(HOST_PROMPT_SUPPORT)
  159. case EP_M8: state = (c == '7') ? EP_M87 : EP_IGNORE; break;
  160. case EP_M87: state = (c == '6') ? EP_M876 : EP_IGNORE; break;
  161. case EP_M876:
  162. switch (c) {
  163. case ' ': break;
  164. case 'S': state = EP_M876S; break;
  165. default: state = EP_IGNORE; break;
  166. }
  167. break;
  168. case EP_M876S:
  169. switch (c) {
  170. case ' ': break;
  171. case '0' ... '9':
  172. state = EP_M876SN;
  173. M876_reason = uint8_t(c - '0');
  174. break;
  175. }
  176. break;
  177. #endif
  178. case EP_IGNORE:
  179. if (ISEOL(c)) state = EP_RESET;
  180. break;
  181. default:
  182. if (ISEOL(c)) {
  183. if (enabled) switch (state) {
  184. case EP_M108: wait_for_user = wait_for_heatup = false; break;
  185. case EP_M112: killed_by_M112 = true; break;
  186. case EP_M410: quickstop_by_M410 = true; break;
  187. #if ENABLED(SDSUPPORT)
  188. case EP_M524: sd_abort_by_M524 = true; break;
  189. #endif
  190. #if ENABLED(HOST_PROMPT_SUPPORT)
  191. case EP_M876SN: hostui.handle_response(M876_reason); break;
  192. #endif
  193. #if ENABLED(REALTIME_REPORTING_COMMANDS)
  194. case EP_GRBL_STATUS: report_current_position_moving(); break;
  195. case EP_GRBL_PAUSE: quickpause_stepper(); break;
  196. case EP_GRBL_RESUME: quickresume_stepper(); break;
  197. #endif
  198. #if ENABLED(SOFT_RESET_VIA_SERIAL)
  199. case EP_KILL: HAL_reboot(); break;
  200. #endif
  201. default: break;
  202. }
  203. state = EP_RESET;
  204. }
  205. }
  206. }
  207. private:
  208. static bool enabled;
  209. };
  210. extern EmergencyParser emergency_parser;