report error message when a command was not taken
[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"
436a2cd1 13#include "libs/nuts_bolts.h"
4cff3ded 14#include "GcodeDispatch.h"
3fceb8eb 15#include "modules/robot/Conveyor.h"
423df6df 16#include "libs/SerialMessage.h"
838b33b4 17#include "libs/StreamOutput.h"
4cff3ded
AW
18
19GcodeDispatch::GcodeDispatch(){}
20
21// Called when the module has just been loaded
22void GcodeDispatch::on_module_loaded() {
74b6303c
DD
23 if( this->kernel->config->value( return_error_on_unhandled_gcode_checksum )->by_default(false)->as_bool() == false )
24 return_error_on_unhandled_gcode = false;
25 else
26 return_error_on_unhandled_gcode = true;
27
4cff3ded 28 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
534e17b9 29 currentline = -1;
4cff3ded
AW
30}
31
32// When a command is received, if it is a Gcode, dispatch it as an object via an event
33void GcodeDispatch::on_console_line_received(void * line){
b6c86164 34 SerialMessage new_message = *static_cast<SerialMessage*>(line);
de91760a 35 string possible_command = new_message.message;
b6c86164 36
4cff3ded 37 char first_char = possible_command[0];
62567509
MC
38 int ln = 0;
39 int cs = 0;
5c07c6dc 40 if( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ){
9a29161f 41
62567509 42 //Get linenumber
de91760a 43 if( first_char == 'N' ){
702023f3 44 Gcode full_line = Gcode(possible_command, new_message.stream);
534e17b9
MC
45 ln = (int) full_line.get_value('N');
46 int chksum = (int) full_line.get_value('*');
62567509 47
f337d8fd
MC
48 //Catch message if it is M110: Set Current Line Number
49 if( full_line.has_letter('M') ){
50 if( ((int) full_line.get_value('M')) == 110 ){
51 currentline = ln;
52 new_message.stream->printf("ok\r\n");
53 return;
54 }
55 }
56
534e17b9 57 //Strip checksum value from possible_command
62567509 58 size_t chkpos = possible_command.find_first_of("*");
de91760a 59 possible_command = possible_command.substr(0, chkpos);
534e17b9 60 //Calculate checksum
de91760a 61 if( chkpos != string::npos ){
7a2ca498
BG
62 for(auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++)
63 cs = cs ^ *c;
62567509 64 cs &= 0xff; // Defensive programming...
534e17b9 65 cs -= chksum;
62567509 66 }
534e17b9 67 //Strip line number value from possible_command
9883efb8 68 size_t lnsize = possible_command.find_first_not_of("N0123456789.,- ");
de91760a 69 possible_command = possible_command.substr(lnsize);
f337d8fd 70
534e17b9
MC
71 }else{
72 //Assume checks succeeded
73 cs = 0x00;
74 ln = currentline + 1;
9a29161f
MC
75 }
76
436a2cd1 77 //Remove comments
30c006fd 78 size_t comment = possible_command.find_first_of(";(");
436a2cd1
AW
79 if( comment != string::npos ){ possible_command = possible_command.substr(0, comment); }
80
62567509
MC
81 //If checksum passes then process message, else request resend
82 int nextline = currentline + 1;
83 if( cs == 0x00 && ln == nextline ){
f337d8fd 84 if( first_char == 'N' ) {
534e17b9 85 currentline = nextline;
f337d8fd
MC
86 }
87
d6190d18 88 while(possible_command.size() > 0) {
7790a7c4 89 size_t nextcmd = possible_command.find_first_of("GMT", possible_command.find_first_of("GMT")+1);
d6190d18 90 string single_command;
85bb8e89 91 if(nextcmd == string::npos) {
d6190d18
MC
92 single_command = possible_command;
93 possible_command = "";
94 }
95 else {
96 single_command = possible_command.substr(0,nextcmd);
2c4285ec 97 possible_command = possible_command.substr(nextcmd);
d6190d18
MC
98 }
99 //Prepare gcode for dispatch
702023f3
MM
100 Gcode* gcode = new Gcode(single_command, new_message.stream);
101 gcode->prepare_cached_values();
d6190d18 102
702023f3 103// printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
d6190d18 104 //Dispatch message!
702023f3
MM
105 this->kernel->call_event(ON_GCODE_RECEIVED, gcode );
106 if (gcode->add_nl)
ac9f9b7a 107 new_message.stream->printf("\r\n");
74b6303c
DD
108 if ( return_error_on_unhandled_gcode == true && gcode->accepted_by_module == false)
109 new_message.stream->printf("error: Command hasn't been processed.\r\n");
110 else
111 new_message.stream->printf("ok\r\n");
702023f3 112
00669816 113 delete gcode;
d149c730 114
d6190d18 115 }
85bb8e89 116 }else{
62567509 117 //Request resend
f337d8fd 118 new_message.stream->printf("rs N%d\r\n", nextline);
62567509 119 }
2bb8b390 120
9a29161f
MC
121 // Ignore comments and blank lines
122 }else if( first_char == ';' || first_char == '(' || first_char == ' ' || first_char == '\n' || first_char == '\r' ){
b6c86164 123 new_message.stream->printf("ok\r\n");
4cff3ded
AW
124 }
125}
126