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/>.
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"
19 GcodeDispatch::GcodeDispatch(){}
21 // Called when the module has just been loaded
22 void GcodeDispatch::on_module_loaded() {
23 return_error_on_unhandled_gcode
= this->kernel
->config
->value( return_error_on_unhandled_gcode_checksum
)->by_default(false)->as_bool();
24 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
28 // When a command is received, if it is a Gcode, dispatch it as an object via an event
29 void GcodeDispatch::on_console_line_received(void * line
){
30 SerialMessage new_message
= *static_cast<SerialMessage
*>(line
);
31 string possible_command
= new_message
.message
;
33 char first_char
= possible_command
[0];
36 if( first_char
== 'G' || first_char
== 'M' || first_char
== 'T' || first_char
== 'N' ){
39 if( first_char
== 'N' ){
40 Gcode full_line
= Gcode(possible_command
, new_message
.stream
);
41 ln
= (int) full_line
.get_value('N');
42 int chksum
= (int) full_line
.get_value('*');
44 //Catch message if it is M110: Set Current Line Number
45 if( full_line
.has_letter('M') ){
46 if( ((int) full_line
.get_value('M')) == 110 ){
48 new_message
.stream
->printf("ok\r\n");
53 //Strip checksum value from possible_command
54 size_t chkpos
= possible_command
.find_first_of("*");
55 possible_command
= possible_command
.substr(0, chkpos
);
57 if( chkpos
!= string::npos
){
58 for(auto c
= possible_command
.cbegin(); *c
!= '*' && c
!= possible_command
.cend(); c
++)
60 cs
&= 0xff; // Defensive programming...
63 //Strip line number value from possible_command
64 size_t lnsize
= possible_command
.find_first_not_of("N0123456789.,- ");
65 possible_command
= possible_command
.substr(lnsize
);
68 //Assume checks succeeded
74 size_t comment
= possible_command
.find_first_of(";(");
75 if( comment
!= string::npos
){ possible_command
= possible_command
.substr(0, comment
); }
77 //If checksum passes then process message, else request resend
78 int nextline
= currentline
+ 1;
79 if( cs
== 0x00 && ln
== nextline
){
80 if( first_char
== 'N' ) {
81 currentline
= nextline
;
84 while(possible_command
.size() > 0) {
85 size_t nextcmd
= possible_command
.find_first_of("GMT", possible_command
.find_first_of("GMT")+1);
86 string single_command
;
87 if(nextcmd
== string::npos
) {
88 single_command
= possible_command
;
89 possible_command
= "";
92 single_command
= possible_command
.substr(0,nextcmd
);
93 possible_command
= possible_command
.substr(nextcmd
);
95 //Prepare gcode for dispatch
96 Gcode
* gcode
= new Gcode(single_command
, new_message
.stream
);
97 gcode
->prepare_cached_values();
99 // printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
101 this->kernel
->call_event(ON_GCODE_RECEIVED
, gcode
);
103 new_message
.stream
->printf("\r\n");
105 if ( return_error_on_unhandled_gcode
== true && gcode
->accepted_by_module
== false)
106 new_message
.stream
->printf("ok (command unclaimed)\r\n");
107 else if(!gcode
->txt_after_ok
.empty()) {
108 new_message
.stream
->printf("ok %s\r\n", gcode
->txt_after_ok
.c_str());
109 gcode
->txt_after_ok
.clear();
111 new_message
.stream
->printf("ok\r\n");
118 new_message
.stream
->printf("rs N%d\r\n", nextline
);
121 // Ignore comments and blank lines
122 }else if( first_char
== ';' || first_char
== '(' || first_char
== ' ' || first_char
== '\n' || first_char
== '\r' ){
123 new_message
.stream
->printf("ok\r\n");