Finish handling Pycam style gcode files
[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 <string>
9 using std::string;
10 #include "libs/Module.h"
11 #include "libs/Kernel.h"
12 #include "utils/Gcode.h"
13 #include "libs/nuts_bolts.h"
14 #include "GcodeDispatch.h"
15 #include "modules/robot/Conveyor.h"
16 #include "libs/SerialMessage.h"
17 #include "libs/StreamOutput.h"
18 #include "libs/FileStream.h"
19
20 GcodeDispatch::GcodeDispatch() {}
21
22 // Called when the module has just been loaded
23 void GcodeDispatch::on_module_loaded()
24 {
25 return_error_on_unhandled_gcode = THEKERNEL->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool();
26 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
27 currentline = -1;
28 uploading = false;
29 last_g= 255;
30 }
31
32 // When a command is received, if it is a Gcode, dispatch it as an object via an event
33 void GcodeDispatch::on_console_line_received(void *line)
34 {
35 SerialMessage new_message = *static_cast<SerialMessage *>(line);
36 string possible_command = new_message.message;
37
38 int ln = 0;
39 int cs = 0;
40
41 try_again:
42
43 char first_char = possible_command[0];
44 unsigned int n;
45 if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) {
46
47 //Get linenumber
48 if ( first_char == 'N' ) {
49 Gcode full_line = Gcode(possible_command, new_message.stream);
50 ln = (int) full_line.get_value('N');
51 int chksum = (int) full_line.get_value('*');
52
53 //Catch message if it is M110: Set Current Line Number
54 if ( full_line.has_letter('M') ) {
55 if ( ((int) full_line.get_value('M')) == 110 ) {
56 currentline = ln;
57 new_message.stream->printf("ok\r\n");
58 return;
59 }
60 }
61
62 //Strip checksum value from possible_command
63 size_t chkpos = possible_command.find_first_of("*");
64 possible_command = possible_command.substr(0, chkpos);
65 //Calculate checksum
66 if ( chkpos != string::npos ) {
67 for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++)
68 cs = cs ^ *c;
69 cs &= 0xff; // Defensive programming...
70 cs -= chksum;
71 }
72 //Strip line number value from possible_command
73 size_t lnsize = possible_command.find_first_not_of("N0123456789.,- ");
74 possible_command = possible_command.substr(lnsize);
75
76 } else {
77 //Assume checks succeeded
78 cs = 0x00;
79 ln = currentline + 1;
80 }
81
82 //Remove comments
83 size_t comment = possible_command.find_first_of(";(");
84 if( comment != string::npos ) {
85 possible_command = possible_command.substr(0, comment);
86 }
87
88 //If checksum passes then process message, else request resend
89 int nextline = currentline + 1;
90 if( cs == 0x00 && ln == nextline ) {
91 if( first_char == 'N' ) {
92 currentline = nextline;
93 }
94
95 while(possible_command.size() > 0) {
96 size_t nextcmd = possible_command.find_first_of("GMT", possible_command.find_first_of("GMT") + 1);
97 string single_command;
98 if(nextcmd == string::npos) {
99 single_command = possible_command;
100 possible_command = "";
101 } else {
102 single_command = possible_command.substr(0, nextcmd);
103 possible_command = possible_command.substr(nextcmd);
104 }
105
106 if(!uploading) {
107 //Prepare gcode for dispatch
108 Gcode *gcode = new Gcode(single_command, new_message.stream);
109
110 if(gcode->has_g) {
111 last_g= gcode->g;
112 }
113 if(gcode->has_m) {
114 switch (gcode->m) {
115 case 28: // start upload command
116 delete gcode;
117
118 this->upload_filename = "/sd/" + single_command.substr(4); // rest of line is filename
119 // open file
120 upload_fd = fopen(this->upload_filename.c_str(), "w");
121 if(upload_fd != NULL) {
122 this->uploading = true;
123 new_message.stream->printf("Writing to file: %s\r\n", this->upload_filename.c_str());
124 } else {
125 new_message.stream->printf("open failed, File: %s.\r\n", this->upload_filename.c_str());
126 }
127 //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd);
128 continue;
129
130 case 500: // M500 save volatile settings to config-override
131 // replace stream with one that writes to config-override file
132 gcode->stream = new FileStream(THEKERNEL->config_override_filename());
133 // dispatch the M500 here so we can free up the stream when done
134 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
135 delete gcode->stream;
136 delete gcode;
137 new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename());
138 continue;
139
140 case 502: // M502 deletes config-override so everything defaults to what is in config
141 remove(THEKERNEL->config_override_filename());
142 new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename());
143 delete gcode;
144 continue;
145
146 case 503: { // M503 display live settings and indicates if there is an override file
147 FILE *fd = fopen(THEKERNEL->config_override_filename(), "r");
148 if(fd != NULL) {
149 fclose(fd);
150 new_message.stream->printf("; config override present: %s\n", THEKERNEL->config_override_filename());
151
152 } else {
153 new_message.stream->printf("; No config override\n");
154 }
155 break; // fall through to process by modules
156 }
157 }
158 }
159
160 //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
161 //Dispatch message!
162 THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
163 if(gcode->add_nl)
164 new_message.stream->printf("\r\n");
165
166 if( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false)
167 new_message.stream->printf("ok (command unclaimed)\r\n");
168 else if(!gcode->txt_after_ok.empty()) {
169 new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str());
170 gcode->txt_after_ok.clear();
171 } else
172 new_message.stream->printf("ok\r\n");
173
174 delete gcode;
175
176 } else {
177 // we are uploading a file so save it
178 if(single_command.substr(0, 3) == "M29") {
179 // done uploading, close file
180 fclose(upload_fd);
181 upload_fd = NULL;
182 uploading = false;
183 upload_filename.clear();
184 new_message.stream->printf("Done saving file.\r\n");
185 continue;
186 }
187
188 if(upload_fd == NULL) {
189 // error detected writing to file so discard everything until it stops
190 new_message.stream->printf("ok\r\n");
191 continue;
192 }
193
194 single_command.append("\n");
195 static int cnt = 0;
196 if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) {
197 // error writing to file
198 new_message.stream->printf("Error:error writing to file.\r\n");
199 fclose(upload_fd);
200 upload_fd = NULL;
201 continue;
202
203 } else {
204 cnt += single_command.size();
205 if (cnt > 400) {
206 // HACK ALERT to get around fwrite corruption close and re open for append
207 fclose(upload_fd);
208 upload_fd = fopen(upload_filename.c_str(), "a");
209 cnt = 0;
210 }
211 new_message.stream->printf("ok\r\n");
212 //printf("uploading file write ok\n");
213 }
214 }
215 }
216
217 } else {
218 //Request resend
219 new_message.stream->printf("rs N%d\r\n", nextline);
220 }
221
222 } else if( (n=possible_command.find_first_of("XYZF")) == 0 || (first_char == ' ' && n != string::npos) ) {
223 // handle pycam syntax, use last G0 or G1 and resubmit if an X Y Z or F is found on its own line
224 if(last_g != 0 && last_g != 1) {
225 //if no last G1 or G0 ignore
226 //THEKERNEL->streams->printf("ignored: %s\r\n", possible_command.c_str());
227 return;
228 }
229 char buf[6];
230 snprintf(buf, sizeof(buf), "G%d ", last_g);
231 possible_command.insert(0, buf);
232 goto try_again;
233
234 // Ignore comments and blank lines
235 } else if ( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ) {
236 new_message.stream->printf("ok\r\n");
237 }
238 }
239