Added M501 to load config-override
authorJim Morris <morris@wolfman.com>
Thu, 2 Jan 2014 02:04:24 +0000 (18:04 -0800)
committerJim Morris <morris@wolfman.com>
Thu, 2 Jan 2014 02:04:24 +0000 (18:04 -0800)
renamed old M501 to M502 as it should have been for removing config-override
Added M504 xxx to save to config-override.xxx
added load/save to load and save names config override files
Make Filestream open for "w" not "a"

src/libs/FileStream.h
src/modules/communication/GcodeDispatch.cpp
src/modules/utils/simpleshell/SimpleShell.cpp
src/modules/utils/simpleshell/SimpleShell.h

index b49d0a1..10078ec 100644 (file)
@@ -6,10 +6,11 @@
 
 class FileStream : public StreamOutput {
     public:
-        FileStream(const char *filename) { fd= fopen(filename, "a"); }
+        FileStream(const char *filename) { fd= fopen(filename, "w"); }
         virtual ~FileStream(){ close(); }
         int puts(const char *str){ return (fd == NULL) ? 0 : fwrite(str, 1, strlen(str), fd); }
         void close() { if(fd != NULL) fclose(fd); fd= NULL; }
+        bool is_open() { return fd != NULL; }
 
     private:
         FILE *fd;
index b84da87..654a468 100644 (file)
@@ -121,8 +121,6 @@ void GcodeDispatch::on_console_line_received(void *line)
                                 continue;
 
                             case 500: // M500 save volatile settings to config-override
-                                // delete the existing file
-                                remove(THEKERNEL->config_override_filename());
                                 // replace stream with one that writes to config-override file
                                 gcode->stream = new FileStream(THEKERNEL->config_override_filename());
                                 // dispatch the M500 here so we can free up the stream when done
@@ -132,7 +130,7 @@ void GcodeDispatch::on_console_line_received(void *line)
                                 new_message.stream->printf("Settings Stored to %s\r\nok\r\n", THEKERNEL->config_override_filename());
                                 continue;
 
-                            case 501: // M501 deletes config-override so everything defaults to what is in config
+                            case 502: // M502 deletes config-override so everything defaults to what is in config
                                 remove(THEKERNEL->config_override_filename());
                                 new_message.stream->printf("config override file deleted %s, reboot needed\r\nok\r\n", THEKERNEL->config_override_filename());
                                 delete gcode;
@@ -147,7 +145,7 @@ void GcodeDispatch::on_console_line_received(void *line)
                                 } else {
                                     new_message.stream->printf("; No config override\n");
                                 }
-                                break; // fal through to process by modules
+                                break; // fall through to process by modules
                             }
                         }
                     }
index 3b6af10..b0f3521 100644 (file)
@@ -17,6 +17,7 @@
 #include "mri.h"
 #include "version.h"
 #include "PublicDataRequest.h"
+#include "FileStream.h"
 
 #include "modules/tools/temperaturecontrol/TemperatureControlPublicAccess.h"
 #include "modules/robot/RobotPublicAccess.h"
@@ -54,6 +55,8 @@ SimpleShell::ptentry_t SimpleShell::commands_table[] = {
     {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},
 
     // unknown command
     {0, NULL}
@@ -150,6 +153,22 @@ void SimpleShell::on_gcode_received(void *argument)
         } 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();
+            if(args.empty()) {
+                load_command("/sd/config-override", gcode->stream);
+            }else{
+                load_command("/sd/config-override." + args, gcode->stream);
+            }
+
+        }else if(gcode->m == 504) { // save to specific config override file
+            gcode->mark_as_taken();
+            if(args.empty()) {
+                save_command("/sd/config-override", gcode->stream);
+            }else{
+                save_command("/sd/config-override." + args, gcode->stream);
+            }
         }
     }
 }
@@ -287,6 +306,58 @@ void SimpleShell::cat_command( string parameters, StreamOutput *stream )
 
 }
 
+// loads the specified config-override file
+void SimpleShell::load_command( string parameters, StreamOutput *stream )
+{
+    // Get parameters ( filename )
+    string filename = this->absolute_from_relative(parameters);
+    if(filename == "/") {
+        filename = THEKERNEL->config_override_filename();
+    }
+
+    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};
+            THEKERNEL->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
+        }
+        stream->printf("config override file executed\n");
+        fclose(fp);
+
+    }else{
+        stream->printf("File not found: %s\n", filename.c_str());
+    }
+}
+
+// saves the specified config-override file
+void SimpleShell::save_command( string parameters, StreamOutput *stream )
+{
+    // Get parameters ( filename )
+    string filename = this->absolute_from_relative(parameters);
+    if(filename == "/") {
+        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;
+    }
+
+    // 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;
+
+    stream->printf("Settings Stored to %s\r\n", filename.c_str());
+}
+
 // show free memory
 void SimpleShell::mem_command( string parameters, StreamOutput *stream)
 {
@@ -437,5 +508,7 @@ void SimpleShell::help_command( string parameters, StreamOutput *stream )
     stream->printf("set_temp bed|hotend 185\r\n");
     stream->printf("get pos\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");
 }
 
index 39039df..3022503 100644 (file)
@@ -43,6 +43,9 @@ private:
 
     void net_command( string parameters, StreamOutput *stream);
 
+    void load_command( string parameters, StreamOutput *stream);
+    void save_command( string parameters, StreamOutput *stream);
+
     bool parse_command(unsigned short cs, string args, StreamOutput *stream);
 
     typedef void (SimpleShell::*PFUNC)(string parameters, StreamOutput *stream);