added grbl commands when in grbl_mode (set in config with grbl_mode true)
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
index 0129f92..193fae1 100644 (file)
 #include "PublicData.h"
 #include "Gcode.h"
 #include "Robot.h"
+#include "ToolManagerPublicAccess.h"
+#include "GcodeDispatch.h"
 
-#include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
+#include "TemperatureControlPublicAccess.h"
 #include "NetworkPublicAccess.h"
 #include "platform_memory.h"
 #include "SwitchPublicAccess.h"
@@ -203,18 +205,53 @@ bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stre
 void SimpleShell::on_console_line_received( void *argument )
 {
     SerialMessage new_message = *static_cast<SerialMessage *>(argument);
+    string possible_command = new_message.message;
 
-    // ignore comments and blank lines and if this is a G code then also ignore it
-    char first_char = new_message.message[0];
-    if(strchr(";( \n\rGMTN", first_char) != NULL) return;
+    // ignore anything that is not lowercase or a $ as it is not a command
+    if(possible_command.size() == 0 || (!islower(possible_command[0]) && possible_command[0] != '$')) {
+        return;
+    }
 
-    string possible_command = new_message.message;
+    // it is a grbl compatible command
+    if(possible_command[0] == '$' && possible_command.size() >= 2) {
+        switch(possible_command[1]) {
+            case 'G':
+                // issue get state
+                get_command("state", new_message.stream);
+                break;
+
+            case 'X':
+                THEKERNEL->call_event(ON_HALT, (void *)1); // clears on_halt
+                new_message.stream->printf("[Caution: Unlocked]\n");
+                break;
+
+            case '#':
+                grblDP_command("", new_message.stream);
+                break;
+
+            case 'H':
+                {
+                    // issue G28.3 which is force homing cycle
+                    Gcode gcode("G28.3", new_message.stream);
+                    THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode);
+                }
+                break;
 
-    //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
-    string cmd = shift_parameter(possible_command);
+            default:
+                new_message.stream->printf("error: Invalid statement\n");
+                break;
+        }
+
+    }else{
 
-    // find command and execute it
-    parse_command(cmd.c_str(), possible_command, new_message.stream);
+        //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
+        string cmd = shift_parameter(possible_command);
+
+        // find command and execute it
+        if(!parse_command(cmd.c_str(), possible_command, new_message.stream)) {
+            new_message.stream->printf("error: Unsupported command\n");
+        }
+    }
 }
 
 // Act upon an ls command
@@ -309,15 +346,36 @@ void SimpleShell::cat_command( string parameters, StreamOutput *stream )
 {
     // Get parameters ( filename and line limit )
     string filename          = absolute_from_relative(shift_parameter( parameters ));
-    string limit_paramater   = shift_parameter( parameters );
+    string limit_parameter   = shift_parameter( parameters );
     int limit = -1;
-    if ( limit_paramater != "" ) {
+    int delay= 0;
+    bool send_eof= false;
+    if ( limit_parameter == "-d" ) {
+        string d= shift_parameter( parameters );
+        char *e = NULL;
+        delay = strtol(d.c_str(), &e, 10);
+        if (e <= d.c_str()) {
+            delay = 0;
+
+        } else {
+            send_eof= true; // we need to terminate file send with an eof
+        }
+
+    }else if ( limit_parameter != "" ) {
         char *e = NULL;
-        limit = strtol(limit_paramater.c_str(), &e, 10);
-        if (e <= limit_paramater.c_str())
+        limit = strtol(limit_parameter.c_str(), &e, 10);
+        if (e <= limit_parameter.c_str())
             limit = -1;
     }
 
+    // we have been asked to delay before cat, probably to allow time to issue upload command
+    while(delay-- > 0) {
+        for (int i = 0; i < 10; ++i) {
+            wait_ms(100);
+            THEKERNEL->call_event(ON_IDLE);
+        }
+    }
+
     // Open file
     FILE *lp = fopen(filename.c_str(), "r");
     if (lp == NULL) {
@@ -331,8 +389,8 @@ void SimpleShell::cat_command( string parameters, StreamOutput *stream )
     // Print each line of the file
     while ((c = fgetc (lp)) != EOF) {
         buffer.append((char *)&c, 1);
-        if ( char(c) == '\n' || ++linecnt > 80) {
-            newlines++;
+        if ( c == '\n' || ++linecnt > 80) {
+            if(c == '\n') newlines++;
             stream->puts(buffer.c_str());
             buffer.clear();
             if(linecnt > 80) linecnt = 0;
@@ -344,6 +402,10 @@ void SimpleShell::cat_command( string parameters, StreamOutput *stream )
         }
     };
     fclose(lp);
+
+    if(send_eof) {
+        stream->puts("\032"); // ^Z terminates the upload
+    }
 }
 
 void SimpleShell::upload_command( string parameters, StreamOutput *stream )
@@ -559,6 +621,50 @@ void SimpleShell::break_command( string parameters, StreamOutput *stream)
     __debugbreak();
 }
 
+static int get_active_tool()
+{
+    void *returned_data;
+    bool ok = PublicData::get_value(tool_manager_checksum, get_active_tool_checksum, &returned_data);
+    if (ok) {
+         int active_tool=  *static_cast<int *>(returned_data);
+        return active_tool;
+    } else {
+        return 0;
+    }
+}
+
+void SimpleShell::grblDP_command( string parameters, StreamOutput *stream)
+{
+    /*
+    [G54:95.000,40.000,-23.600]
+    [G55:0.000,0.000,0.000]
+    [G56:0.000,0.000,0.000]
+    [G57:0.000,0.000,0.000]
+    [G58:0.000,0.000,0.000]
+    [G59:0.000,0.000,0.000]
+    [G28:0.000,0.000,0.000]
+    [G30:0.000,0.000,0.000]
+    [G92:0.000,0.000,0.000]
+    [TLO:0.000]
+    [PRB:0.000,0.000,0.000:0]
+    */
+    std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
+    int n= std::get<1>(v[0]);
+    for (int i = 1; i <= n; ++i) {
+        stream->printf("[%s:%1.3f,%1.3f,%1.3f]\n", wcs2gcode(i-1).c_str(), std::get<0>(v[i]), std::get<1>(v[i]), std::get<2>(v[i]));
+    }
+
+    stream->printf("[G28:%1.3f,%1.3f,%1.3f]\n",  0.0F, 0.0F, 0.0F);
+    stream->printf("[G30:%1.3f,%1.3f,%1.3f]\n",  0.0F, 0.0F, 0.0F);
+    stream->printf("[G92:%1.3f,%1.3f,%1.3f]\n", std::get<0>(v[n+1]), std::get<1>(v[n+1]), std::get<2>(v[n+1]));
+    stream->printf("[TL0:%1.3f]\n", std::get<2>(v[n+2]));
+
+    // TODO this shoul dbe the last probe position, which will be this if probe was the last thing done
+    float current_machine_pos[3];
+    THEKERNEL->robot->get_axis_position(current_machine_pos);
+    stream->printf("[PRB:%1.3f,%1.3f,%1.3f:%d]\n", current_machine_pos[X_AXIS], current_machine_pos[Y_AXIS], current_machine_pos[Z_AXIS], 0);
+}
+
 // used to test out the get public data events
 void SimpleShell::get_command( string parameters, StreamOutput *stream)
 {
@@ -591,9 +697,44 @@ void SimpleShell::get_command( string parameters, StreamOutput *stream)
         }
 
     } else if (what == "pos") {
-        float pos[3];
-        THEKERNEL->robot->get_axis_position(pos);
-        stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
+        // convenience to call all the various M114 variants
+        char buf[64];
+        THEKERNEL->robot->print_position(0, buf, sizeof buf); stream->printf("last %s\n", buf);
+        THEKERNEL->robot->print_position(1, buf, sizeof buf); stream->printf("realtime %s\n", buf);
+        THEKERNEL->robot->print_position(2, buf, sizeof buf); stream->printf("%s\n", buf);
+        THEKERNEL->robot->print_position(3, buf, sizeof buf); stream->printf("%s\n", buf);
+        THEKERNEL->robot->print_position(4, buf, sizeof buf); stream->printf("%s\n", buf);
+        THEKERNEL->robot->print_position(5, buf, sizeof buf); stream->printf("%s\n", buf);
+
+    } else if (what == "wcs") {
+        // print the wcs state
+        std::vector<Robot::wcs_t> v= THEKERNEL->robot->get_wcs_state();
+        char current_wcs= std::get<0>(v[0]);
+        stream->printf("current WCS: %s\n", wcs2gcode(current_wcs).c_str());
+        int n= std::get<1>(v[0]);
+        for (int i = 1; i <= n; ++i) {
+            stream->printf("%s: %1.4f, %1.4f, %1.4f\n", wcs2gcode(i-1).c_str(), std::get<0>(v[i]), std::get<1>(v[i]), std::get<2>(v[i]));
+        }
+
+        stream->printf("G92: %1.4f, %1.4f, %1.4f\n", std::get<0>(v[n+1]), std::get<1>(v[n+1]), std::get<2>(v[n+1]));
+        stream->printf("ToolOffset: %1.4f, %1.4f, %1.4f\n", std::get<0>(v[n+2]), std::get<1>(v[n+2]), std::get<2>(v[n+2]));
+
+    } else if (what == "state") {
+        // also $G
+        // [G0 G54 G17 G21 G90 G94 M0 M5 M9 T0 F0.]
+        stream->printf("[G%d %s G%d G%d G%d G94 M0 M5 M9 T%d F%1.1f]\n",
+            THEKERNEL->gcode_dispatch->get_modal_command(),
+            wcs2gcode(THEKERNEL->robot->get_current_wcs()).c_str(),
+            THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Y_AXIS && THEKERNEL->robot->plane_axis_2 == Z_AXIS ? 17 :
+              THEKERNEL->robot->plane_axis_0 == X_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == Y_AXIS ? 18 :
+              THEKERNEL->robot->plane_axis_0 == Y_AXIS && THEKERNEL->robot->plane_axis_1 == Z_AXIS && THEKERNEL->robot->plane_axis_2 == X_AXIS ? 19 : 17,
+            THEKERNEL->robot->inch_mode ? 20 : 21,
+            THEKERNEL->robot->absolute_mode ? 90 : 91,
+            get_active_tool(),
+            THEKERNEL->robot->get_feed_rate());
+
+    } else {
+        stream->printf("error: unknown option %s\n", what.c_str());
     }
 }
 
@@ -703,7 +844,7 @@ void SimpleShell::help_command( string parameters, StreamOutput *stream )
     stream->printf("ls [-s] [folder]\r\n");
     stream->printf("cd folder\r\n");
     stream->printf("pwd\r\n");
-    stream->printf("cat file [limit]\r\n");
+    stream->printf("cat file [limit] [-d 10]\r\n");
     stream->printf("rm file\r\n");
     stream->printf("mv file newfile\r\n");
     stream->printf("remount\r\n");
@@ -718,6 +859,8 @@ void SimpleShell::help_command( string parameters, StreamOutput *stream )
     stream->printf("get temp [bed|hotend]\r\n");
     stream->printf("set_temp bed|hotend 185\r\n");
     stream->printf("get pos\r\n");
+    stream->printf("get wcs\r\n");
+    stream->printf("get state\r\n");
     stream->printf("net\r\n");
     stream->printf("load [file] - loads a configuration override file from soecified name or config-override\r\n");
     stream->printf("save [file] - saves a configuration override file as specified filename or as config-override\r\n");