added support for blank lines and added disabled support for line numbers and checksums
[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/Player.h"
16 #include "libs/SerialMessage.h"
17 #include "libs/StreamOutput.h"
18
19 GcodeDispatch::GcodeDispatch(){}
20
21 // Called when the module has just been loaded
22 void GcodeDispatch::on_module_loaded() {
23 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
24 }
25
26 // When a command is received, if it is a Gcode, dispatch it as an object via an event
27 void GcodeDispatch::on_console_line_received(void * line){
28 SerialMessage new_message = *static_cast<SerialMessage*>(line);
29 string possible_command = new_message.message;
30
31 char first_char = possible_command[0];
32 if( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'S' || first_char == 'N' || first_char == '*' ){
33
34 //Disable 'N' linenumber for now
35 if( first_char == 'N' ){
36 size_t reallinepos = possible_command.find_first_of(" ") + 1;
37 possible_command = possible_command.substr(reallinepos);
38 }
39
40 //Disable '*' checksum for now
41 size_t chkpos = possible_command.find_first_of("*");
42 if( chkpos != string::npos ){ possible_command = possible_command.substr(0, chkpos); }
43
44 //Remove comments
45 size_t comment = possible_command.find_first_of(";");
46 if( comment != string::npos ){ possible_command = possible_command.substr(0, comment); }
47
48 // Dispatch
49 Gcode gcode = Gcode();
50 gcode.command = possible_command;
51 gcode.stream = new_message.stream;
52 this->kernel->call_event(ON_GCODE_RECEIVED, &gcode );
53
54 new_message.stream->printf("ok\r\n");
55
56 // Ignore comments and blank lines
57 }else if( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ){
58 new_message.stream->printf("ok\r\n");
59 }
60 }
61