added grbl commands when in grbl_mode (set in config with grbl_mode true)
[clinton/Smoothieware.git] / src / modules / utils / simpleshell / SimpleShell.cpp
index afc7ca0..193fae1 100644 (file)
@@ -6,8 +6,8 @@
 */
 
 
-#include "libs/Kernel.h"
 #include "SimpleShell.h"
+#include "libs/Kernel.h"
 #include "libs/nuts_bolts.h"
 #include "libs/utils.h"
 #include "libs/SerialMessage.h"
 #include "mri.h"
 #include "version.h"
 #include "PublicDataRequest.h"
+#include "AppendFileStream.h"
 #include "FileStream.h"
-
-#include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
-#include "modules/robot/RobotPublicAccess.h"
+#include "checksumm.h"
+#include "PublicData.h"
+#include "Gcode.h"
+#include "Robot.h"
+#include "ToolManagerPublicAccess.h"
+#include "GcodeDispatch.h"
+
+#include "TemperatureControlPublicAccess.h"
 #include "NetworkPublicAccess.h"
 #include "platform_memory.h"
+#include "SwitchPublicAccess.h"
+#include "SDFAT.h"
+#include "Thermistor.h"
+#include "md5.h"
+
+#include "system_LPC17xx.h"
+#include "LPC17xx.h"
+
+#include "mbed.h" // for wait_ms()
 
 extern unsigned int g_maximumHeapAddress;
 
@@ -35,33 +50,39 @@ extern "C" uint32_t  __end__;
 extern "C" uint32_t  __malloc_free_list;
 extern "C" uint32_t  _sbrk(int size);
 
-#define get_temp_command_checksum CHECKSUM("temp")
-#define get_pos_command_checksum  CHECKSUM("pos")
-
 // command lookup table
-SimpleShell::ptentry_t SimpleShell::commands_table[] = {
-    {CHECKSUM("ls"),       &SimpleShell::ls_command},
-    {CHECKSUM("cd"),       &SimpleShell::cd_command},
-    {CHECKSUM("pwd"),      &SimpleShell::pwd_command},
-    {CHECKSUM("cat"),      &SimpleShell::cat_command},
-    {CHECKSUM("rm"),       &SimpleShell::rm_command},
-    {CHECKSUM("reset"),    &SimpleShell::reset_command},
-    {CHECKSUM("dfu"),      &SimpleShell::dfu_command},
-    {CHECKSUM("break"),    &SimpleShell::break_command},
-    {CHECKSUM("help"),     &SimpleShell::help_command},
-    {CHECKSUM("?"),        &SimpleShell::help_command},
-    {CHECKSUM("version"),  &SimpleShell::version_command},
-    {CHECKSUM("mem"),      &SimpleShell::mem_command},
-    {CHECKSUM("get"),      &SimpleShell::get_command},
-    {CHECKSUM("set_temp"), &SimpleShell::set_temp_command},
-    {CHECKSUM("net"),      &SimpleShell::net_command},
-    {CHECKSUM("load"),     &SimpleShell::load_command},
-    {CHECKSUM("save"),     &SimpleShell::save_command},
+const SimpleShell::ptentry_t SimpleShell::commands_table[] = {
+    {"ls",       SimpleShell::ls_command},
+    {"cd",       SimpleShell::cd_command},
+    {"pwd",      SimpleShell::pwd_command},
+    {"cat",      SimpleShell::cat_command},
+    {"rm",       SimpleShell::rm_command},
+    {"mv",       SimpleShell::mv_command},
+    {"upload",   SimpleShell::upload_command},
+    {"reset",    SimpleShell::reset_command},
+    {"dfu",      SimpleShell::dfu_command},
+    {"break",    SimpleShell::break_command},
+    {"help",     SimpleShell::help_command},
+    {"?",        SimpleShell::help_command},
+    {"version",  SimpleShell::version_command},
+    {"mem",      SimpleShell::mem_command},
+    {"get",      SimpleShell::get_command},
+    {"set_temp", SimpleShell::set_temp_command},
+    {"switch",   SimpleShell::switch_command},
+    {"net",      SimpleShell::net_command},
+    {"load",     SimpleShell::load_command},
+    {"save",     SimpleShell::save_command},
+    {"remount",  SimpleShell::remount_command},
+    {"calc_thermistor", SimpleShell::calc_thermistor_command},
+    {"thermistors", SimpleShell::print_thermistors_command},
+    {"md5sum",   SimpleShell::md5sum_command},
 
     // unknown command
-    {0, NULL}
+    {NULL, NULL}
 };
 
+int SimpleShell::reset_delay_secs = 0;
+
 // Adam Greens heap walk from http://mbed.org/forum/mbed/topic/2701/?page=4#comment-22556
 static uint32_t heapWalk(StreamOutput *stream, bool verbose)
 {
@@ -121,17 +142,17 @@ static uint32_t heapWalk(StreamOutput *stream, bool verbose)
 void SimpleShell::on_module_loaded()
 {
     this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
-       this->register_for_event(ON_GCODE_RECEIVED);
-       this->register_for_event(ON_SECOND_TICK);
+    this->register_for_event(ON_GCODE_RECEIVED);
+    this->register_for_event(ON_SECOND_TICK);
 
-    this->reset_delay_secs = 0;
+    reset_delay_secs = 0;
 }
 
 void SimpleShell::on_second_tick(void *)
 {
     // we are timing out for the reset
-    if (this->reset_delay_secs > 0) {
-        if (--this->reset_delay_secs == 0) {
+    if (reset_delay_secs > 0) {
+        if (--reset_delay_secs == 0) {
             system_reset(false);
         }
     }
@@ -140,44 +161,39 @@ void SimpleShell::on_second_tick(void *)
 void SimpleShell::on_gcode_received(void *argument)
 {
     Gcode *gcode = static_cast<Gcode *>(argument);
-    string args= get_arguments(gcode->command);
+    string args = get_arguments(gcode->get_command());
 
     if (gcode->has_m) {
         if (gcode->m == 20) { // list sd card
-            gcode->mark_as_taken();
             gcode->stream->printf("Begin file list\r\n");
             ls_command("/sd", gcode->stream);
             gcode->stream->printf("End file list\r\n");
 
         } else if (gcode->m == 30) { // remove file
-            gcode->mark_as_taken();
             rm_command("/sd/" + args, gcode->stream);
 
-        }else if(gcode->m == 501) { // load config override
-            gcode->mark_as_taken();
+        } else if(gcode->m == 501) { // load config override
             if(args.empty()) {
                 load_command("/sd/config-override", gcode->stream);
-            }else{
+            } else {
                 load_command("/sd/config-override." + args, gcode->stream);
             }
 
-        }else if(gcode->m == 504) { // save to specific config override file
-            gcode->mark_as_taken();
+        } else if(gcode->m == 504) { // save to specific config override file
             if(args.empty()) {
                 save_command("/sd/config-override", gcode->stream);
-            }else{
+            } else {
                 save_command("/sd/config-override." + args, gcode->stream);
             }
         }
     }
 }
 
-bool SimpleShell::parse_command(unsigned short cs, string args, StreamOutput *stream)
+bool SimpleShell::parse_command(const char *cmd, string args, StreamOutput *stream)
 {
-    for (ptentry_t *p = commands_table; p->pfunc != NULL; ++p) {
-        if (cs == p->command_cs) {
-            PFUNC fnc= p->pfunc;
-            (this->*fnc)(args, stream);
+    for (const ptentry_t *p = commands_table; p->command != NULL; ++p) {
+        if (strncasecmp(cmd, p->command, strlen(p->command)) == 0) {
+            p->func(args, stream);
             return true;
         }
     }
@@ -189,46 +205,121 @@ bool SimpleShell::parse_command(unsigned short cs, string args, StreamOutput *st
 void SimpleShell::on_console_line_received( void *argument )
 {
     SerialMessage new_message = *static_cast<SerialMessage *>(argument);
+    string possible_command = new_message.message;
 
-    // ignore comments
-    if (new_message.message[0] == ';') 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;
+
+            default:
+                new_message.stream->printf("error: Invalid statement\n");
+                break;
+        }
 
-    //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
+    }else{
 
-    unsigned short check_sum = get_checksum( possible_command.substr(0, possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
+        //new_message.stream->printf("Received %s\r\n", possible_command.c_str());
+        string cmd = shift_parameter(possible_command);
 
-    // find command and execute it
-    parse_command(check_sum, get_arguments(possible_command), new_message.stream);
+        // 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
 // Convert the first parameter into an absolute path, then list the files in that path
 void SimpleShell::ls_command( string parameters, StreamOutput *stream )
 {
-    string folder = absolute_from_relative( parameters );
+    string path, opts;
+    while(!parameters.empty()) {
+        string s = shift_parameter( parameters );
+        if(s.front() == '-') {
+            opts.append(s);
+        } else {
+            path = s;
+            if(!parameters.empty()) {
+                path.append(" ");
+                path.append(parameters);
+            }
+            break;
+        }
+    }
+
+    path = absolute_from_relative(path);
+
     DIR *d;
     struct dirent *p;
-    d = opendir(folder.c_str());
+    d = opendir(path.c_str());
     if (d != NULL) {
         while ((p = readdir(d)) != NULL) {
-            stream->printf("%s\r\n", lc(string(p->d_name)).c_str());
+            stream->printf("%s", lc(string(p->d_name)).c_str());
+            if(p->d_isdir) {
+                stream->printf("/");
+            } else if(opts.find("-s", 0, 2) != string::npos) {
+                stream->printf(" %d", p->d_fsize);
+            }
+            stream->printf("\r\n");
         }
         closedir(d);
     } else {
-        stream->printf("Could not open directory %s \r\n", folder.c_str());
+        stream->printf("Could not open directory %s\r\n", path.c_str());
     }
 }
 
+extern SDFAT mounter;
+
+void SimpleShell::remount_command( string parameters, StreamOutput *stream )
+{
+    mounter.remount();
+    stream->printf("remounted\r\n");
+}
+
 // Delete a file
 void SimpleShell::rm_command( string parameters, StreamOutput *stream )
 {
-    const char *fn= absolute_from_relative(shift_parameter( parameters )).c_str();
+    const char *fn = absolute_from_relative(shift_parameter( parameters )).c_str();
     int s = remove(fn);
     if (s != 0) stream->printf("Could not delete %s \r\n", fn);
 }
 
+// Rename a file
+void SimpleShell::mv_command( string parameters, StreamOutput *stream )
+{
+    string from = absolute_from_relative(shift_parameter( parameters ));
+    string to = absolute_from_relative(shift_parameter(parameters));
+    int s = rename(from.c_str(), to.c_str());
+    if (s != 0) stream->printf("Could not rename %s to %s\r\n", from.c_str(), to.c_str());
+    else stream->printf("renamed %s to %s\r\n", from.c_str(), to.c_str());
+}
+
 // Change current absolute path to provided path
 void SimpleShell::cd_command( string parameters, StreamOutput *stream )
 {
@@ -255,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;
-        limit = strtol(limit_paramater.c_str(), &e, 10);
-        if (e <= limit_paramater.c_str())
+        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_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) {
@@ -273,15 +385,17 @@ void SimpleShell::cat_command( string parameters, StreamOutput *stream )
     string buffer;
     int c;
     int newlines = 0;
-    int linecnt= 0;
+    int linecnt = 0;
     // 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;
+            if(linecnt > 80) linecnt = 0;
+            // we need to kick things or they die
+            THEKERNEL->call_event(ON_IDLE);
         }
         if ( newlines == limit ) {
             break;
@@ -289,6 +403,78 @@ 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 )
+{
+    // this needs to be a hack. it needs to read direct from serial and not allow on_main_loop run until done
+    // NOTE this will block all operation until the upload is complete, so do not do while printing
+    if(!THEKERNEL->conveyor->is_queue_empty()) {
+        stream->printf("upload not allowed while printing or busy\n");
+        return;
+    }
+
+    // open file to upload to
+    string upload_filename = absolute_from_relative( parameters );
+    FILE *fd = fopen(upload_filename.c_str(), "w");
+    if(fd != NULL) {
+        stream->printf("uploading to file: %s, send control-D or control-Z to finish\r\n", upload_filename.c_str());
+    } else {
+        stream->printf("failed to open file: %s.\r\n", upload_filename.c_str());
+        return;
+    }
+
+    int cnt = 0;
+    bool uploading = true;
+    while(uploading) {
+        if(!stream->ready()) {
+            // we need to kick things or they die
+            THEKERNEL->call_event(ON_IDLE);
+            continue;
+        }
+
+        char c = stream->_getc();
+        if( c == 4 || c == 26) { // ctrl-D or ctrl-Z
+            uploading = false;
+            // close file
+            fclose(fd);
+            stream->printf("uploaded %d bytes\n", cnt);
+            return;
+
+        } else {
+            // write character to file
+            cnt++;
+            if(fputc(c, fd) != c) {
+                // error writing to file
+                stream->printf("error writing to file. ignoring all characters until EOF\r\n");
+                fclose(fd);
+                fd = NULL;
+                uploading= false;
+
+            } else {
+                if ((cnt%400) == 0) {
+                    // HACK ALERT to get around fwrite corruption close and re open for append
+                    fclose(fd);
+                    fd = fopen(upload_filename.c_str(), "a");
+                    // we need to kick things or they die
+                    THEKERNEL->call_event(ON_IDLE);
+                }
+            }
+        }
+    }
+    // we got an error so ignore everything until EOF
+    char c;
+    do {
+        if(stream->ready()) {
+            c= stream->_getc();
+        }else{
+            THEKERNEL->call_event(ON_IDLE);
+            c= 0;
+        }
+    } while(c != 4 && c != 26);
 }
 
 // loads the specified config-override file
@@ -300,20 +486,20 @@ void SimpleShell::load_command( string parameters, StreamOutput *stream )
         filename = THEKERNEL->config_override_filename();
     }
 
-    FILE *fp= fopen(filename.c_str(), "r");
+    FILE *fp = fopen(filename.c_str(), "r");
     if(fp != NULL) {
         char buf[132];
         stream->printf("Loading config override file: %s...\n", filename.c_str());
         while(fgets(buf, sizeof buf, fp) != NULL) {
             stream->printf("  %s", buf);
             if(buf[0] == ';') continue; // skip the comments
-            struct SerialMessage message= {&(StreamOutput::NullStream), buf};
+            struct SerialMessage message = {&(StreamOutput::NullStream), buf};
             THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
         }
         stream->printf("config override file executed\n");
         fclose(fp);
 
-    }else{
+    } else {
         stream->printf("File not found: %s\n", filename.c_str());
     }
 }
@@ -327,18 +513,29 @@ void SimpleShell::save_command( string parameters, StreamOutput *stream )
         filename = THEKERNEL->config_override_filename();
     }
 
-    // replace stream with one that writes to config-override file
-    FileStream *gs = new FileStream(filename.c_str());
-    if(!gs->is_open()) {
-        stream->printf("Unable to open File %s for write\n", filename.c_str());
-        return;
+    THEKERNEL->conveyor->wait_for_empty_queue(); //just to be safe as it can take a while to run
+
+    //remove(filename.c_str()); // seems to cause a hang every now and then
+    {
+        FileStream fs(filename.c_str());
+        fs.printf("; DO NOT EDIT THIS FILE\n");
+        // this also will truncate the existing file instead of deleting it
     }
 
+    // stream that appends to file
+    AppendFileStream *gs = new AppendFileStream(filename.c_str());
+    // if(!gs->is_open()) {
+    //     stream->printf("Unable to open File %s for write\n", filename.c_str());
+    //     return;
+    // }
+
+    __disable_irq();
     // issue a M500 which will store values in the file stream
     Gcode *gcode = new Gcode("M500", gs);
     THEKERNEL->call_event(ON_GCODE_RECEIVED, gcode );
     delete gs;
     delete gcode;
+    __enable_irq();
 
     stream->printf("Settings Stored to %s\r\n", filename.c_str());
 }
@@ -351,12 +548,11 @@ void SimpleShell::mem_command( string parameters, StreamOutput *stream)
     unsigned long m = g_maximumHeapAddress - heap;
     stream->printf("Unused Heap: %lu bytes\r\n", m);
 
-    uint32_t f= heapWalk(stream, verbose);
+    uint32_t f = heapWalk(stream, verbose);
     stream->printf("Total Free RAM: %lu bytes\r\n", m + f);
 
     stream->printf("Free AHB0: %lu, AHB1: %lu\r\n", AHB0.free(), AHB1.free());
-    if (verbose)
-    {
+    if (verbose) {
         AHB0.debug(stream);
         AHB1.debug(stream);
     }
@@ -384,13 +580,13 @@ static uint32_t getDeviceType()
 void SimpleShell::net_command( string parameters, StreamOutput *stream)
 {
     void *returned_data;
-    bool ok= THEKERNEL->public_data->get_value( network_checksum, get_ipconfig_checksum, &returned_data );
+    bool ok = PublicData::get_value( network_checksum, get_ipconfig_checksum, &returned_data );
     if(ok) {
-        char *str= (char *)returned_data;
+        char *str = (char *)returned_data;
         stream->printf("%s\r\n", str);
         free(str);
 
-    }else{
+    } else {
         stream->printf("No network detected\n");
     }
 }
@@ -408,7 +604,7 @@ void SimpleShell::version_command( string parameters, StreamOutput *stream)
 void SimpleShell::reset_command( string parameters, StreamOutput *stream)
 {
     stream->printf("Smoothie out. Peace. Rebooting in 5 seconds...\r\n");
-    this->reset_delay_secs = 5; // reboot in 5 seconds
+    reset_delay_secs = 5; // reboot in 5 seconds
 }
 
 // go into dfu boot mode
@@ -425,33 +621,120 @@ 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)
 {
-    int what = get_checksum(shift_parameter( parameters ));
-    void *returned_data;
+    string what = shift_parameter( parameters );
 
-    if (what == get_temp_command_checksum) {
+    if (what == "temp") {
+        struct pad_temperature temp;
         string type = shift_parameter( parameters );
-        bool ok = THEKERNEL->public_data->get_value( temperature_control_checksum, get_checksum(type), current_temperature_checksum, &returned_data );
+        if(type.empty()) {
+            // scan all temperature controls
+            std::vector<struct pad_temperature> controllers;
+            bool ok = PublicData::get_value(temperature_control_checksum, poll_controls_checksum, &controllers);
+            if (ok) {
+                for (auto &c : controllers) {
+                   stream->printf("%s (%d) temp: %f/%f @%d\r\n", c.designator.c_str(), c.id, c.current_temperature, c.target_temperature, c.pwm);
+                }
+
+            } else {
+                stream->printf("no heaters found\r\n");
+            }
 
-        if (ok) {
-            struct pad_temperature temp =  *static_cast<struct pad_temperature *>(returned_data);
-            stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
-        } else {
-            stream->printf("%s is not a known temperature device\r\n", type.c_str());
+        }else{
+            bool ok = PublicData::get_value( temperature_control_checksum, current_temperature_checksum, get_checksum(type), &temp );
+
+            if (ok) {
+                stream->printf("%s temp: %f/%f @%d\r\n", type.c_str(), temp.current_temperature, temp.target_temperature, temp.pwm);
+            } else {
+                stream->printf("%s is not a known temperature device\r\n", type.c_str());
+            }
         }
 
-    } else if (what == get_pos_command_checksum) {
-        bool ok = THEKERNEL->public_data->get_value( robot_checksum, current_position_checksum, &returned_data );
+    } else if (what == "pos") {
+        // 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]));
+        }
 
-        if (ok) {
-            float *pos = static_cast<float *>(returned_data);
-            stream->printf("Position X: %f, Y: %f, Z: %f\r\n", pos[0], pos[1], pos[2]);
+        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("get pos command failed\r\n");
-        }
+    } else {
+        stream->printf("error: unknown option %s\n", what.c_str());
     }
 }
 
@@ -461,7 +744,7 @@ void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
     string type = shift_parameter( parameters );
     string temp = shift_parameter( parameters );
     float t = temp.empty() ? 0.0 : strtof(temp.c_str(), NULL);
-    bool ok = THEKERNEL->public_data->set_value( temperature_control_checksum, get_checksum(type), &t );
+    bool ok = PublicData::set_value( temperature_control_checksum, get_checksum(type), &t );
 
     if (ok) {
         stream->printf("%s temp set to: %3.1f\r\n", type.c_str(), t);
@@ -470,16 +753,101 @@ void SimpleShell::set_temp_command( string parameters, StreamOutput *stream)
     }
 }
 
+void SimpleShell::print_thermistors_command( string parameters, StreamOutput *stream)
+{
+    Thermistor::print_predefined_thermistors(stream);
+}
+
+void SimpleShell::calc_thermistor_command( string parameters, StreamOutput *stream)
+{
+    string s = shift_parameter( parameters );
+    int saveto= -1;
+    // see if we have -sn as first argument
+    if(s.find("-s", 0, 2) != string::npos) {
+        // save the results to thermistor n
+        saveto= strtol(s.substr(2).c_str(), nullptr, 10);
+    }else{
+        parameters= s;
+    }
+
+    std::vector<float> trl= parse_number_list(parameters.c_str());
+    if(trl.size() == 6) {
+        // calculate the coefficients
+        float c1, c2, c3;
+        std::tie(c1, c2, c3) = Thermistor::calculate_steinhart_hart_coefficients(trl[0], trl[1], trl[2], trl[3], trl[4], trl[5]);
+        stream->printf("Steinhart Hart coefficients:  I%1.18f J%1.18f K%1.18f\n", c1, c2, c3);
+        if(saveto == -1) {
+            stream->printf("  Paste the above in the M305 S0 command, then save with M500\n");
+        }else{
+            char buf[80];
+            int n = snprintf(buf, sizeof(buf), "M305 S%d I%1.18f J%1.18f K%1.18f", saveto, c1, c2, c3);
+            string g(buf, n);
+            Gcode gcode(g, &(StreamOutput::NullStream));
+            THEKERNEL->call_event(ON_GCODE_RECEIVED, &gcode );
+            stream->printf("  Setting Thermistor %d to those settings, save with M500\n", saveto);
+        }
+
+    }else{
+        // give help
+        stream->printf("Usage: calc_thermistor T1,R1,T2,R2,T3,R3\n");
+    }
+}
+
+// used to test out the get public data events for switch
+void SimpleShell::switch_command( string parameters, StreamOutput *stream)
+{
+    string type = shift_parameter( parameters );
+    string value = shift_parameter( parameters );
+    bool ok = false;
+    if(value == "on" || value == "off") {
+        bool b = value == "on";
+        ok = PublicData::set_value( switch_checksum, get_checksum(type), state_checksum, &b );
+    } else {
+        float v = strtof(value.c_str(), NULL);
+        ok = PublicData::set_value( switch_checksum, get_checksum(type), value_checksum, &v );
+    }
+    if (ok) {
+        stream->printf("switch %s set to: %s\r\n", type.c_str(), value.c_str());
+    } else {
+        stream->printf("%s is not a known switch device\r\n", type.c_str());
+    }
+}
+
+void SimpleShell::md5sum_command( string parameters, StreamOutput *stream )
+{
+    string filename = absolute_from_relative(parameters);
+
+    // Open file
+    FILE *lp = fopen(filename.c_str(), "r");
+    if (lp == NULL) {
+        stream->printf("File not found: %s\r\n", filename.c_str());
+        return;
+    }
+    MD5 md5;
+    uint8_t buf[64];
+    do {
+        size_t n= fread(buf, 1, sizeof buf, lp);
+        if(n > 0) md5.update(buf, n);
+    } while(!feof(lp));
+
+    stream->printf("%s %s\n", md5.finalize().hexdigest().c_str(), filename.c_str());
+    fclose(lp);
+}
+
+
+
 void SimpleShell::help_command( string parameters, StreamOutput *stream )
 {
     stream->printf("Commands:\r\n");
     stream->printf("version\r\n");
     stream->printf("mem [-v]\r\n");
-    stream->printf("ls [folder]\r\n");
+    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");
     stream->printf("play file [-v]\r\n");
     stream->printf("progress - shows progress of current play\r\n");
     stream->printf("abort - abort currently playing file\r\n");
@@ -488,12 +856,17 @@ void SimpleShell::help_command( string parameters, StreamOutput *stream )
     stream->printf("break - break into debugger\r\n");
     stream->printf("config-get [<configuration_source>] <configuration_setting>\r\n");
     stream->printf("config-set [<configuration_source>] <configuration_setting> <value>\r\n");
-    stream->printf("config-load [<file_name>]\r\n");
     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");
+    stream->printf("upload filename - saves a stream of text to the named file\r\n");
+    stream->printf("calc_thermistor [-s0] T1,R1,T2,R2,T3,R3 - calculate the Steinhart Hart coefficients for a thermistor\r\n");
+    stream->printf("thermistors - print out the predefined thermistors\r\n");
+    stream->printf("md5sum file - prints md5 sum of the given file\r\n");
 }