Subversion Repositories MK-Marlin

Rev

Details | Last modification | View Log | RSS feed

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