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