Allow T1 M200 on same line with Tn as first command
[clinton/Smoothieware.git] / src / modules / communication / GcodeDispatch.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 Smoothie 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.
4 Smoothie 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8 #include "GcodeDispatch.h"
9
10 #include "libs/Kernel.h"
11 #include "utils/Gcode.h"
12 #include "Pauser.h"
13 #include "libs/nuts_bolts.h"
14 #include "modules/robot/Conveyor.h"
15 #include "libs/SerialMessage.h"
16 #include "libs/StreamOutput.h"
17 #include "libs/StreamOutputPool.h"
18 #include "libs/FileStream.h"
19 #include "libs/AppendFileStream.h"
20 #include "Config.h"
21 #include "checksumm.h"
22 #include "ConfigValue.h"
23
24 #define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode")
25
26 // goes in Flash, list of Mxxx codes that are allowed when in Halted state
27 static const int allowed_mcodes[]= {105,114}; // get temp, get pos
28 static bool is_allowed_mcode(int m) {
29 for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) {
30 if(allowed_mcodes[i] == m) return true;
31 }
32 return false;
33 }
34
35 GcodeDispatch::GcodeDispatch()
36 {
37 halted= false;
38 uploading = false;
39 currentline = -1;
40 last_g= 255;
41 }
42
43 // Called when the module has just been loaded
44 void GcodeDispatch::on_module_loaded()
45 {
46 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
47 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
48 this->register_for_event(ON_HALT);
49 }
50
51 void GcodeDispatch::on_halt(void *arg)
52 {
53 // set halt stream and ignore everything until M999
54 this->halted= (arg == nullptr);
55 }
56
57 // When a command is received, if it is a Gcode, dispatch it as an object via an event
58 void GcodeDispatch::on_console_line_received(void *line)
59 {
60 SerialMessage new_message = *static_cast<SerialMessage *>(line);
61 string possible_command = new_message.message;
62
63 int ln = 0;
64 int cs = 0;
65
66 try_again:
67
68 char first_char = possible_command[0];
69 unsigned int n;
70 if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) {
71
72 //Get linenumber
73 if ( first_char == 'N' ) {
74 Gcode full_line = Gcode(possible_command, new_message.stream, false);
75 ln = (int) full_line.get_value('N');
76 int chksum = (int) full_line.get_value('*');
77
78 //Catch message if it is M110: Set Current Line Number
79 if ( full_line.has_m ) {
80 if ( full_line.m == 110 ) {
81 currentline = ln;
82 new_message.stream->printf("ok\r\n");
83 return;
84 }
85 }
86
87 //Strip checksum value from possible_command
88 size_t chkpos = possible_command.find_first_of("*");
89 possible_command = possible_command.substr(0, chkpos);
90 //Calculate checksum
91 if ( chkpos != string::npos ) {
92 for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++)
93 cs = cs ^ *c;
94 cs &= 0xff; // Defensive programming...
95 cs -= chksum;
96 }
97 //Strip line number value from possible_command
98 size_t lnsize = possible_command.find_first_not_of("N0123456789.,- ");
99 possible_command = possible_command.substr(lnsize);
100
101 } else {
102 //Assume checks succeeded
103 cs = 0x00;
104 ln = currentline + 1;
105 }
106
107 //Remove comments
108 size_t comment = possible_command.find_first_of(";(");
109 if( comment != string::npos ) {
110 possible_command = possible_command.substr(0, comment);
111 }
112
113 //If checksum passes then process message, else request resend
114 int nextline = currentline + 1;
115 if( cs == 0x00 && ln == nextline ) {
116 if( first_char == 'N' ) {
117 currentline = nextline;
118 }
119
120 while(possible_command.size() > 0) {
121 size_t nextcmd = possible_command.find_first_of("GM", 1);
122 string single_command;
123 if(nextcmd == string::npos) {
124 single_command = possible_command;
125 possible_command = "";
126 } else {
127 single_command = possible_command.substr(0, nextcmd);
128 possible_command = possible_command.substr(nextcmd);
129 }
130
131
132 if(!uploading) {
133 //Prepare gcode for dispatch
134 Gcode *gcode = new Gcode(single_command, new_message.stream);
135
136 if(halted) {
137 // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp)
138 if(gcode->has_m && gcode->m == 999) {
139 THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
140 halted= false;
141 // fall through and pass onto other modules
142
143 }else if(!is_allowed_mcode(gcode->m)) {
144 // ignore everything, return error string to host
145 new_message.stream->printf("!!\r\n");
146 delete gcode;
147 continue;
148 }
149 }
150
151 if(gcode->has_g) {
152 last_g= gcode->g;
153 }
154
155 if(gcode->has_m) {
156 switch (gcode->m) {
157 case 28: // start upload command
158 delete gcode;
159
160 this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename
161 // open file
162 upload_fd = fopen(this->upload_filename.c_str(), "w");
163 if(upload_fd != NULL) {
164 this->uploading = true;
165 new_message.stream->printf("Writing to file: %s\r\nok\r\n", this->upload_filename.c_str());
166 } else {
167 new_message.stream->printf("open failed, File: %s.\r\nok\r\n", this->upload_filename.c_str());
168 }
169 //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd);
170 continue;
171
172 case 112: // emergency stop, do the best we can with this
173 // TODO this really needs to be handled out-of-band
174 // disables heaters and motors, ignores further incoming Gcode and clears block queue
175 THEKERNEL->call_event(ON_HALT, nullptr);
176 THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to continue\r\n");
177 delete gcode;
178 return;
179
180 case 500: // M500 save volatile settings to config-override
181 THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run
182 //remove(THEKERNEL->config_override_filename()); // seems to cause a hang every now and then
183 {
184 FileStream fs(THEKERNEL->config_override_filename());
185 fs.printf("; DO NOT EDIT THIS FILE\n");
186 // this also will truncate the existing file instead of deleting it
187 }
188 // replace stream with one that writes to config-override file
189 gcode->stream = new AppendFileStream(THEKERNEL->config_override_filename());
190 // dispatch the M500 here so we can free up the stream when done
191 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
192 delete gcode->stream;
193 delete gcode;
194 new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename());
195 continue;
196
197 case 502: // M502 deletes config-override so everything defaults to what is in config
198 remove(THEKERNEL->config_override_filename());
199 new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename());
200 delete gcode;
201 continue;
202
203 case 503: { // M503 display live settings and indicates if there is an override file
204 FILE *fd = fopen(THEKERNEL->config_override_filename(), "r");
205 if(fd != NULL) {
206 fclose(fd);
207 new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename());
208
209 } else {
210 new_message.stream->printf("; No config override\n");
211 }
212 gcode->add_nl= true;
213 break; // fall through to process by modules
214 }
215 }
216 }
217
218 //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
219 //Dispatch message!
220 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
221 if(gcode->add_nl)
222 new_message.stream->printf("\r\n");
223
224 if( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false)
225 new_message.stream->printf("ok (command unclaimed)\r\n");
226 else if(!gcode->txt_after_ok.empty()) {
227 new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str());
228 gcode->txt_after_ok.clear();
229 } else
230 new_message.stream->printf("ok\r\n");
231
232 delete gcode;
233
234 } else {
235 // we are uploading a file so save it
236 if(single_command.substr(0, 3) == "M29") {
237 // done uploading, close file
238 fclose(upload_fd);
239 upload_fd = NULL;
240 uploading = false;
241 upload_filename.clear();
242 new_message.stream->printf("Done saving file.\r\nok\r\n");
243 continue;
244 }
245
246 if(upload_fd == NULL) {
247 // error detected writing to file so discard everything until it stops
248 new_message.stream->printf("ok\r\n");
249 continue;
250 }
251
252 single_command.append("\n");
253 static int cnt = 0;
254 if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) {
255 // error writing to file
256 new_message.stream->printf("Error:error writing to file.\r\n");
257 fclose(upload_fd);
258 upload_fd = NULL;
259 continue;
260
261 } else {
262 cnt += single_command.size();
263 if (cnt > 400) {
264 // HACK ALERT to get around fwrite corruption close and re open for append
265 fclose(upload_fd);
266 upload_fd = fopen(upload_filename.c_str(), "a");
267 cnt = 0;
268 }
269 new_message.stream->printf("ok\r\n");
270 //printf("uploading file write ok\n");
271 }
272 }
273 }
274
275 } else {
276 //Request resend
277 new_message.stream->printf("rs N%d\r\n", nextline);
278 }
279
280 } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) {
281 // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line
282 if(last_g != 0 && last_g != 1) {
283 //if no last G1 or G0 ignore
284 //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str());
285 return;
286 }
287 char buf[6];
288 snprintf(buf, sizeof(buf), "G%d ", last_g);
289 possible_command.insert(0, buf);
290 goto try_again;
291
292 // Ignore comments and blank lines
293 } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) {
294 new_message.stream->printf("ok\r\n");
295 }
296 }
297