X-Git-Url: https://git.hcoop.net/clinton/Smoothieware.git/blobdiff_plain/e799360878781d1aaa1a5e4be42def59cd24834a..27fdf0205958dcbc13241679f16587d79842c879:/src/modules/communication/GcodeDispatch.cpp diff --git a/src/modules/communication/GcodeDispatch.cpp b/src/modules/communication/GcodeDispatch.cpp index 26592bf1..708628a9 100644 --- a/src/modules/communication/GcodeDispatch.cpp +++ b/src/modules/communication/GcodeDispatch.cpp @@ -25,13 +25,13 @@ #include "SimpleShell.h" #include "utils.h" #include "LPC17xx.h" +#include "version.h" -#define return_error_on_unhandled_gcode_checksum CHECKSUM("return_error_on_unhandled_gcode") #define panel_display_message_checksum CHECKSUM("display_message") #define panel_checksum CHECKSUM("panel") // goes in Flash, list of Mxxx codes that are allowed when in Halted state -static const int allowed_mcodes[]= {105,114,119,80,81,911,503,106,107}; // get temp, get pos, get endstops etc +static const int allowed_mcodes[]= {2,5,9,30,105,114,119,80,81,911,503,106,107}; // get temp, get pos, get endstops etc static bool is_allowed_mcode(int m) { for (size_t i = 0; i < sizeof(allowed_mcodes)/sizeof(int); ++i) { if(allowed_mcodes[i] == m) return true; @@ -61,11 +61,27 @@ void GcodeDispatch::on_console_line_received(void *line) int ln = 0; int cs = 0; + // just reply ok to empty lines + if(possible_command.empty()) { + new_message.stream->printf("ok\r\n"); + return; + } + try_again: char first_char = possible_command[0]; unsigned int n; - if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'N' ) { + + if(first_char == '$') { + // ignore as simpleshell will handle it + return; + + }else if(islower(first_char)) { + // ignore all lowercase as they are simpleshell commands + return; + } + + if ( first_char == 'G' || first_char == 'M' || first_char == 'T' || first_char == 'S' || first_char == 'N' ) { //Get linenumber if ( first_char == 'N' ) { @@ -84,17 +100,24 @@ try_again: //Strip checksum value from possible_command size_t chkpos = possible_command.find_first_of("*"); - possible_command = possible_command.substr(0, chkpos); - //Calculate checksum + + //Calculate checksum if ( chkpos != string::npos ) { + possible_command = possible_command.substr(0, chkpos); for (auto c = possible_command.cbegin(); *c != '*' && c != possible_command.cend(); c++) cs = cs ^ *c; cs &= 0xff; // Defensive programming... cs -= chksum; - } + } + //Strip line number value from possible_command - size_t lnsize = possible_command.find_first_not_of("N0123456789.,- "); - possible_command = possible_command.substr(lnsize); + size_t lnsize = possible_command.find_first_not_of("N0123456789.,- "); + if(lnsize != string::npos) { + possible_command = possible_command.substr(lnsize); + }else{ + // it is a blank line + possible_command.clear(); + } } else { //Assume checks succeeded @@ -135,13 +158,22 @@ try_again: if(THEKERNEL->is_halted()) { // we ignore all commands until M999, unless it is in the exceptions list (like M105 get temp) if(gcode->has_m && gcode->m == 999) { - THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt - - // fall through and pass onto other modules + if(THEKERNEL->is_halted()) { + THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt + new_message.stream->printf("WARNING: After HALT you should HOME as position is currently unknown\n"); + } + new_message.stream->printf("ok\n"); + delete gcode; + continue; }else if(!is_allowed_mcode(gcode->m)) { // ignore everything, return error string to host - new_message.stream->printf("!!\r\n"); + if(THEKERNEL->is_grbl_mode()) { + new_message.stream->printf("error:Alarm lock\n"); + + }else{ + new_message.stream->printf("!!\r\n"); + } delete gcode; continue; } @@ -150,7 +182,7 @@ try_again: if(gcode->has_g) { if(gcode->g == 53) { // G53 makes next movement command use machine coordinates // this is ugly to implement as there may or may not be a G0/G1 on the same line - // valid vesion seem to include G53 G0 X1 Y2 Z3 G53 X1 Y2 + // valid version seem to include G53 G0 X1 Y2 Z3 G53 X1 Y2 if(possible_command.empty()) { // use last gcode G1 or G0 if none on the line, and pass through as if it was a G0/G1 // TODO it is really an error if the last is not G0 thru G3 @@ -175,7 +207,7 @@ try_again: } } // makes it handle the parameters as a machine position - THEKERNEL->robot->next_command_is_MCS= true; + THEROBOT->next_command_is_MCS= true; } @@ -206,14 +238,48 @@ try_again: //printf("Start Uploading file: %s, %p\n", upload_filename.c_str(), upload_fd); continue; + case 30: // end of program + if(!THEKERNEL->is_grbl_mode()) break; // Special case M30 as it is also delete sd card file so only do this if in grbl mode + // fall through to M2 + case 2: + { + modal_group_1= 1; // set to G1 + // issue M5 and M9 in case spindle and coolant are being used + Gcode gc1("M5", &StreamOutput::NullStream); + THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc1); + Gcode gc2("M9", &StreamOutput::NullStream); + THEKERNEL->call_event(ON_GCODE_RECEIVED, &gc2); + } + break; + case 112: // emergency stop, do the best we can with this - // TODO this really needs to be handled out-of-band + // this is also handled out-of-band (it is now with ^X in the serial driver) // disables heaters and motors, ignores further incoming Gcode and clears block queue THEKERNEL->call_event(ON_HALT, nullptr); - THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to continue\r\n"); + THEKERNEL->streams->printf("ok Emergency Stop Requested - reset or M999 required to exit HALT state\r\n"); delete gcode; return; + case 115: // M115 Get firmware version and capabilities + Version vers; + + new_message.stream->printf("FIRMWARE_NAME:Smoothieware, FIRMWARE_URL:http%%3A//smoothieware.org, X-SOURCE_CODE_URL:https://github.com/Smoothieware/Smoothieware, FIRMWARE_VERSION:%s, X-FIRMWARE_BUILD_DATE:%s, X-SYSTEM_CLOCK:%ldMHz, X-AXES:%d", vers.get_build(), vers.get_build_date(), SystemCoreClock / 1000000, MAX_ROBOT_ACTUATORS); + + #ifdef CNC + new_message.stream->printf(", X-CNC:1"); + #else + new_message.stream->printf(", X-CNC:0"); + #endif + + #ifdef DISABLEMSD + new_message.stream->printf(", X-MSD:0"); + #else + new_message.stream->printf(", X-MSD:1"); + #endif + + new_message.stream->printf("\nok\n"); + return; + case 117: // M117 is a special non compliant Gcode as it allows arbitrary text on the line following the command { // concatenate the command again and send to panel if enabled string str= single_command.substr(4) + possible_command; @@ -223,7 +289,7 @@ try_again: return; } - case 1000: // M1000 is a special comanad that will pass thru the raw lowercased command to the simpleshell (for hosts that do not allow such things) + case 1000: // M1000 is a special command that will pass thru the raw lowercased command to the simpleshell (for hosts that do not allow such things) { // reconstruct entire command line again string str= single_command.substr(5) + possible_command; @@ -248,7 +314,7 @@ try_again: } case 500: // M500 save volatile settings to config-override - THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run + THEKERNEL->conveyor->wait_for_idle(); //just to be safe as it can take a while to run //remove(THEKERNEL->config_override_filename()); // seems to cause a hang every now and then __disable_irq(); { @@ -266,6 +332,19 @@ try_again: new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename()); continue; + case 501: // load config override + case 504: // save to specific config override file + { + string arg= get_arguments(single_command + possible_command); // rest of line is filename + if(arg.empty()) arg= "/sd/config-override"; + else arg= "/sd/config-override." + arg; + //new_message.stream->printf("args: <%s>\n", arg.c_str()); + SimpleShell::parse_command((gcode->m == 501) ? "load_command" : "save_command", arg, new_message.stream); + } + delete gcode; + new_message.stream->printf("ok\r\n"); + return; + case 502: // M502 deletes config-override so everything defaults to what is in config remove(THEKERNEL->config_override_filename()); delete gcode; @@ -284,20 +363,54 @@ try_again: gcode->add_nl= true; break; // fall through to process by modules } + } } //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m); //Dispatch message! THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode ); - if(gcode->add_nl) - new_message.stream->printf("\r\n"); - if(!gcode->txt_after_ok.empty()) { - new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str()); - gcode->txt_after_ok.clear(); - } else - new_message.stream->printf("ok\r\n"); + if (gcode->is_error) { + // report error + if(THEKERNEL->is_grbl_mode()) { + new_message.stream->printf("error: "); + }else{ + new_message.stream->printf("Error: "); + } + + if(!gcode->txt_after_ok.empty()) { + new_message.stream->printf("%s\r\n", gcode->txt_after_ok.c_str()); + gcode->txt_after_ok.clear(); + + }else{ + new_message.stream->printf("unknown\r\n"); + } + + // we cannot continue safely after an error so we enter HALT state + new_message.stream->printf("Entering Alarm/Halt state\n"); + THEKERNEL->call_event(ON_HALT, nullptr); + + }else{ + + if(gcode->add_nl) + new_message.stream->printf("\r\n"); + + if(!gcode->txt_after_ok.empty()) { + new_message.stream->printf("ok %s\r\n", gcode->txt_after_ok.c_str()); + gcode->txt_after_ok.clear(); + + } else { + if(THEKERNEL->is_ok_per_line() || THEKERNEL->is_grbl_mode()) { + // only send ok once per line if this is a multi g code line send ok on the last one + if(possible_command.empty()) + new_message.stream->printf("ok\r\n"); + } else { + // maybe should do the above for all hosts? + new_message.stream->printf("ok\r\n"); + } + } + } delete gcode; @@ -321,7 +434,6 @@ try_again: } single_command.append("\n"); - static int cnt = 0; if(fwrite(single_command.c_str(), 1, single_command.size(), upload_fd) != single_command.size()) { // error writing to file new_message.stream->printf("Error:error writing to file.\r\n"); @@ -330,14 +442,7 @@ try_again: continue; } else { - cnt += single_command.size(); - if (cnt > 400) { - // HACK ALERT to get around fwrite corruption close and re open for append - fclose(upload_fd); - upload_fd = fopen(upload_filename.c_str(), "a"); - cnt = 0; - } - new_message.stream->printf("ok\r\n"); + new_message.stream->printf("ok\r\n"); //printf("uploading file write ok\n"); } }