Merge pull request #16 from logxen/multicommand
[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 currentline = -1;
25 }
26
27 // When a command is received, if it is a Gcode, dispatch it as an object via an event
28 void GcodeDispatch::on_console_line_received(void * line){
29 SerialMessage new_message = *static_cast<SerialMessage*>(line);
30 string possible_command = new_message.message;
31
32 char first_char = possible_command[0];
33 int ln = 0;
34 int cs = 0;
35 if( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'S' || first_char == 'N' ){
36
37 //Get linenumber
38 if( first_char == 'N' ){
39 Gcode full_line = Gcode();
40 full_line.command = possible_command;
41 full_line.stream = new_message.stream;
42 ln = (int) full_line.get_value('N');
43 int chksum = (int) full_line.get_value('*');
44
45 //Catch message if it is M110: Set Current Line Number
46 if( full_line.has_letter('M') ){
47 if( ((int) full_line.get_value('M')) == 110 ){
48 currentline = ln;
49 new_message.stream->printf("ok\r\n");
50 return;
51 }
52 }
53
54 //Strip checksum value from possible_command
55 size_t chkpos = possible_command.find_first_of("*");
56 possible_command = possible_command.substr(0, chkpos);
57 //Calculate checksum
58 if( chkpos != string::npos ){
59 for(int i = 0; possible_command[i] != '*' && possible_command[i] != NULL; i++)
60 cs = cs ^ possible_command[i];
61 cs &= 0xff; // Defensive programming...
62 cs -= chksum;
63 }
64 //Strip line number value from possible_command
65 size_t lnsize = possible_command.find_first_of(" ") + 1;
66 possible_command = possible_command.substr(lnsize);
67
68 }else{
69 //Assume checks succeeded
70 cs = 0x00;
71 ln = currentline + 1;
72 }
73
74 //Remove comments
75 size_t comment = possible_command.find_first_of(";");
76 if( comment != string::npos ){ possible_command = possible_command.substr(0, comment); }
77
78 //If checksum passes then process message, else request resend
79 int nextline = currentline + 1;
80 if( cs == 0x00 && ln == nextline ){
81 if( first_char == 'N' ) {
82 currentline = nextline;
83 }
84
85 while(possible_command.size() > 0) {
86 size_t nextcmd = possible_command.find_first_of("GMTS", possible_command.find_first_of("GMTS")+1);
87 string single_command;
88 if(nextcmd == string::npos) {
89 single_command = possible_command;
90 possible_command = "";
91 }
92 else {
93 single_command = possible_command.substr(0,nextcmd);
94 possible_command = possible_command.substr(nextcmd);
95 }
96 //Prepare gcode for dispatch
97 Gcode gcode = Gcode();
98 gcode.command = single_command;
99 gcode.stream = new_message.stream;
100
101 //Dispatch message!
102 this->kernel->call_event(ON_GCODE_RECEIVED, &gcode );
103 new_message.stream->printf("ok\r\n");
104 }
105 }else{
106 //Request resend
107 new_message.stream->printf("rs N%d\r\n", nextline);
108 }
109
110 // Ignore comments and blank lines
111 }else if( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ){
112 new_message.stream->printf("ok\r\n");
113 }
114 }
115