Add a debounce count to the reset triggered state
[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
728477c4 46 this->halted= (arg == nullptr);
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) {
728477c4 131 THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
b375ba1d 132 halted= false;
76217df5 133 // fall through and pass onto other modules
b375ba1d 134 }else{
76217df5 135 // ignore everything, return error string to host
b375ba1d 136 new_message.stream->printf("!!\r\n");
76217df5
JM
137 delete gcode;
138 continue;
b375ba1d 139 }
b375ba1d
JM
140 }
141
c2885de8
JM
142 if(gcode->has_g) {
143 last_g= gcode->g;
144 }
b375ba1d 145
adf89655
JM
146 if(gcode->has_m) {
147 switch (gcode->m) {
148 case 28: // start upload command
149 delete gcode;
150
151 this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename
152 // open file
153 upload_fd = fopen(this->upload_filename.c_str(), "w");
154 if(upload_fd != NULL) {
155 this->uploading = true;
156 new_message.stream->printf("Writing to file: %s\r\n", this->upload_filename.c_str());
157 } else {
158 new_message.stream->printf("open failed, File: %s.\r\n", this->upload_filename.c_str());
159 }
160 //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd);
161 continue;
162
17704c93
JM
163 case 112: // emergency stop, do the best we can with this
164 // TODO this really needs to be handled out-of-band
b375ba1d 165 // disables heaters and motors, ignores further incoming Gcode and clears block queue
728477c4 166 THEKERNEL->call_event(ON_HALT, nullptr);
b375ba1d
JM
167 THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to continue\r\n");
168 delete gcode;
17704c93
JM
169 return;
170
adf89655 171 case 500: // M500 save volatile settings to config-override
f2f0dfed 172 // replace stream with one that writes to config-override file
347854ff 173 gcode->stream = new FileStream(THEKERNEL->config_override_filename());
adf89655 174 // dispatch the M500 here so we can free up the stream when done
314ab8f7 175 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
adf89655
JM
176 delete gcode->stream;
177 delete gcode;
347854ff 178 new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename());
adf89655
JM
179 continue;
180
618c9b0f 181 case 502: // M502 deletes config-override so everything defaults to what is in config
347854ff
MM
182 remove(THEKERNEL->config_override_filename());
183 new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename());
adf89655
JM
184 delete gcode;
185 continue;
186
187 case 503: { // M503 display live settings and indicates if there is an override file
347854ff 188 FILE *fd = fopen(THEKERNEL->config_override_filename(), "r");
adf89655
JM
189 if(fd != NULL) {
190 fclose(fd);
347854ff 191 new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename());
adf89655
JM
192
193 } else {
194 new_message.stream->printf("; No config override\n");
195 }
08d9afc6 196 gcode->add_nl= true;
618c9b0f 197 break; // fall through to process by modules
adf89655 198 }
44e8c359 199 }
33e4cc02
JM
200 }
201
202 //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
44e8c359 203 //Dispatch message!
314ab8f7 204 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
adf89655 205 if(gcode->add_nl)
44e8c359
JM
206 new_message.stream->printf("\r\n");
207
adf89655 208 if( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false)
44e8c359
JM
209 new_message.stream->printf("ok (command unclaimed)\r\n");
210 else if(!gcode->txt_after_ok.empty()) {
211 new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str());
212 gcode->txt_after_ok.clear();
adf89655 213 } else
44e8c359
JM
214 new_message.stream->printf("ok\r\n");
215
216 delete gcode;
217
adf89655 218 } else {
44e8c359
JM
219 // we are uploading a file so save it
220 if(single_command.substr(0, 3) == "M29") {
221 // done uploading, close file
222 fclose(upload_fd);
adf89655
JM
223 upload_fd = NULL;
224 uploading = false;
44e8c359
JM
225 upload_filename.clear();
226 new_message.stream->printf("Done saving file.\r\n");
227 continue;
228 }
adf89655 229
44e8c359
JM
230 if(upload_fd == NULL) {
231 // error detected writing to file so discard everything until it stops
232 new_message.stream->printf("ok\r\n");
233 continue;
234 }
adf89655 235
44e8c359 236 single_command.append("\n");
adf89655 237 static int cnt = 0;
44e8c359
JM
238 if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) {
239 // error writing to file
240 new_message.stream->printf("Error:error writing to file.\r\n");
241 fclose(upload_fd);
adf89655 242 upload_fd = NULL;
44e8c359 243 continue;
adf89655
JM
244
245 } else {
246 cnt += single_command.size();
247 if (cnt > 400) {
44e8c359
JM
248 // HACK ALERT to get around fwrite corruption close and re open for append
249 fclose(upload_fd);
adf89655
JM
250 upload_fd = fopen(upload_filename.c_str(), "a");
251 cnt = 0;
44e8c359
JM
252 }
253 new_message.stream->printf("ok\r\n");
254 //printf("uploading file write ok\n");
255 }
256 }
d6190d18 257 }
adf89655
JM
258
259 } else {
62567509 260 //Request resend
f337d8fd 261 new_message.stream->printf("rs N%d\r\n", nextline);
62567509 262 }
2bb8b390 263
c2885de8 264 } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) {
c4c70e85
JM
265 // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line
266 if(last_g != 0 && last_g != 1) {
267 //if no last G1 or G0 ignore
268 //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str());
269 return;
270 }
c2885de8
JM
271 char buf[6];
272 snprintf(buf, sizeof(buf), "G%d ", last_g);
273 possible_command.insert(0, buf);
274 goto try_again;
275
adf89655
JM
276 // Ignore comments and blank lines
277 } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) {
b6c86164 278 new_message.stream->printf("ok\r\n");
4cff3ded
AW
279 }
280}
281