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.

emergency_parser.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. /**
  24. * emergency_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. void quickstop_stepper();
  33. class EmergencyParser {
  34. public:
  35. // Currently looking for: M108, M112, M410, M876
  36. enum State : char {
  37. EP_RESET,
  38. EP_N,
  39. EP_M,
  40. EP_M1,
  41. EP_M10,
  42. EP_M108,
  43. EP_M11,
  44. EP_M112,
  45. EP_M4,
  46. EP_M41,
  47. EP_M410,
  48. #if ENABLED(HOST_PROMPT_SUPPORT)
  49. EP_M8,
  50. EP_M87,
  51. EP_M876,
  52. EP_M876S,
  53. EP_M876SN,
  54. #endif
  55. EP_IGNORE // to '\n'
  56. };
  57. static bool killed_by_M112;
  58. #if ENABLED(HOST_PROMPT_SUPPORT)
  59. static uint8_t M876_reason;
  60. #endif
  61. EmergencyParser() { enable(); }
  62. FORCE_INLINE static void enable() { enabled = true; }
  63. FORCE_INLINE static void disable() { enabled = false; }
  64. FORCE_INLINE static void update(State &state, const uint8_t c) {
  65. switch (state) {
  66. case EP_RESET:
  67. switch (c) {
  68. case ' ': case '\n': case '\r': break;
  69. case 'N': state = EP_N; break;
  70. case 'M': state = EP_M; break;
  71. default: state = EP_IGNORE;
  72. }
  73. break;
  74. case EP_N:
  75. switch (c) {
  76. case '0': case '1': case '2':
  77. case '3': case '4': case '5':
  78. case '6': case '7': case '8':
  79. case '9': case '-': case ' ': break;
  80. case 'M': state = EP_M; break;
  81. default: state = EP_IGNORE;
  82. }
  83. break;
  84. case EP_M:
  85. switch (c) {
  86. case ' ': break;
  87. case '1': state = EP_M1; break;
  88. case '4': state = EP_M4; break;
  89. #if ENABLED(HOST_PROMPT_SUPPORT)
  90. case '8': state = EP_M8; break;
  91. #endif
  92. default: state = EP_IGNORE;
  93. }
  94. break;
  95. case EP_M1:
  96. switch (c) {
  97. case '0': state = EP_M10; break;
  98. case '1': state = EP_M11; break;
  99. default: state = EP_IGNORE;
  100. }
  101. break;
  102. case EP_M10:
  103. state = (c == '8') ? EP_M108 : EP_IGNORE;
  104. break;
  105. case EP_M11:
  106. state = (c == '2') ? EP_M112 : EP_IGNORE;
  107. break;
  108. case EP_M4:
  109. state = (c == '1') ? EP_M41 : EP_IGNORE;
  110. break;
  111. case EP_M41:
  112. state = (c == '0') ? EP_M410 : EP_IGNORE;
  113. break;
  114. #if ENABLED(HOST_PROMPT_SUPPORT)
  115. case EP_M8:
  116. state = (c == '7') ? EP_M87 : EP_IGNORE;
  117. break;
  118. case EP_M87:
  119. state = (c == '6') ? EP_M876 : EP_IGNORE;
  120. break;
  121. case EP_M876:
  122. switch (c) {
  123. case ' ': break;
  124. case 'S': state = EP_M876S; break;
  125. default: state = EP_IGNORE; break;
  126. }
  127. break;
  128. case EP_M876S:
  129. switch (c) {
  130. case ' ': break;
  131. case '0': case '1': case '2':
  132. case '3': case '4': case '5':
  133. case '6': case '7': case '8':
  134. case '9':
  135. state = EP_M876SN;
  136. M876_reason = (uint8_t)(c - '0');
  137. break;
  138. }
  139. break;
  140. #endif
  141. case EP_IGNORE:
  142. if (c == '\n' || c == '\r') state = EP_RESET;
  143. break;
  144. default:
  145. if (c == '\n' || c == '\r') {
  146. if (enabled) switch (state) {
  147. case EP_M108: wait_for_user = wait_for_heatup = false; break;
  148. case EP_M112: killed_by_M112 = true; break;
  149. case EP_M410: quickstop_stepper(); break;
  150. #if ENABLED(HOST_PROMPT_SUPPORT)
  151. case EP_M876SN: host_response_handler(M876_reason); break;
  152. #endif
  153. default: break;
  154. }
  155. state = EP_RESET;
  156. }
  157. }
  158. }
  159. private:
  160. static bool enabled;
  161. };
  162. extern EmergencyParser emergency_parser;