emergency_parser.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. /**
  23. * emergency_parser.h - Intercept special commands directly in the serial stream
  24. */
  25. #ifndef _EMERGENCY_PARSER_H_
  26. #define _EMERGENCY_PARSER_H_
  27. // External references
  28. extern volatile bool wait_for_user, wait_for_heatup;
  29. void quickstop_stepper();
  30. class EmergencyParser {
  31. public:
  32. // Currently looking for: M108, M112, M410
  33. enum State : char {
  34. EP_RESET,
  35. EP_N,
  36. EP_M,
  37. EP_M1,
  38. EP_M10,
  39. EP_M108,
  40. EP_M11,
  41. EP_M112,
  42. EP_M4,
  43. EP_M41,
  44. EP_M410,
  45. EP_IGNORE // to '\n'
  46. };
  47. static bool killed_by_M112;
  48. static State state;
  49. EmergencyParser() {}
  50. __attribute__((always_inline)) inline
  51. static void update(const uint8_t c) {
  52. switch (state) {
  53. case EP_RESET:
  54. switch (c) {
  55. case ' ': break;
  56. case 'N': state = EP_N; break;
  57. case 'M': state = EP_M; break;
  58. default: state = EP_IGNORE;
  59. }
  60. break;
  61. case EP_N:
  62. switch (c) {
  63. case '0': case '1': case '2':
  64. case '3': case '4': case '5':
  65. case '6': case '7': case '8':
  66. case '9': case '-': case ' ': break;
  67. case 'M': state = EP_M; break;
  68. default: state = EP_IGNORE;
  69. }
  70. break;
  71. case EP_M:
  72. switch (c) {
  73. case ' ': break;
  74. case '1': state = EP_M1; break;
  75. case '4': state = EP_M4; break;
  76. default: state = EP_IGNORE;
  77. }
  78. break;
  79. case EP_M1:
  80. switch (c) {
  81. case '0': state = EP_M10; break;
  82. case '1': state = EP_M11; break;
  83. default: state = EP_IGNORE;
  84. }
  85. break;
  86. case EP_M10:
  87. state = (c == '8') ? EP_M108 : EP_IGNORE;
  88. break;
  89. case EP_M11:
  90. state = (c == '2') ? EP_M112 : EP_IGNORE;
  91. break;
  92. case EP_M4:
  93. state = (c == '1') ? EP_M41 : EP_IGNORE;
  94. break;
  95. case EP_M41:
  96. state = (c == '0') ? EP_M410 : EP_IGNORE;
  97. break;
  98. case EP_IGNORE:
  99. if (c == '\n') state = EP_RESET;
  100. break;
  101. default:
  102. if (c == '\n') {
  103. switch (state) {
  104. case EP_M108:
  105. wait_for_user = wait_for_heatup = false;
  106. break;
  107. case EP_M112:
  108. killed_by_M112 = true;
  109. break;
  110. case EP_M410:
  111. quickstop_stepper();
  112. break;
  113. default:
  114. break;
  115. }
  116. state = EP_RESET;
  117. }
  118. }
  119. }
  120. };
  121. extern EmergencyParser emergency_parser;
  122. #endif // _EMERGENCY_PARSER_H_