play command now works
authorArthur Wolf <wolf.arthur@gmail.com>
Wed, 28 Sep 2011 20:31:02 +0000 (22:31 +0200)
committerArthur Wolf <wolf.arthur@gmail.com>
Wed, 28 Sep 2011 20:31:02 +0000 (22:31 +0200)
src/libs/Config.cpp
src/modules/utils/simpleshell/SimpleShell.cpp
src/modules/utils/simpleshell/SimpleShell.h

index 078a319..d544bcb 100644 (file)
@@ -27,7 +27,6 @@ void Config::on_console_line_received( void* argument ){
 
     // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
     unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) );  // todo: put this method somewhere more convenient
-    //this->kernel->serial->printf("checksum>%u\r\n", check_sum);   
 
     // Act depending on command
     switch( check_sum ){
@@ -66,7 +65,6 @@ void Config::set_string( uint16_t check_sum, string value ){
             size_t begin_value = buffer.find_first_not_of(" ", buffer.find_first_of(" ", begin_key));
             // If this line matches the checksum 
             if(get_checksum(buffer.substr(begin_key,  buffer.find_first_of(" ", begin_key) - begin_key)) != check_sum){ buffer.clear(); continue; }
-            //this->kernel->serial->printf("avaliable length: %d, new value length: %d, complete line: %s\r\n", int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value), value.length(), buffer.c_str()); 
             if( int(value.length()) >= int(int(buffer.find_first_of("\r\n#", begin_value+1))-begin_value) ){ this->kernel->serial->printf("ERROR: Not enough room for value\r\n"); fclose(lp); return; }
             // Update value
             fpos_t pos;
@@ -74,7 +72,6 @@ void Config::set_string( uint16_t check_sum, string value ){
             int start = pos - buffer.length() + begin_value - 1;
             fseek(lp, start, SEEK_SET); 
             fputs(value.c_str(), lp);
-            
             fclose(lp);
             return;
         }else{
index 1dadd2d..baf056e 100644 (file)
@@ -16,12 +16,13 @@ void SimpleShell::on_console_line_received( void* argument ){
 
     // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
     unsigned short check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) );  // todo: put this method somewhere more convenient
-    
+
     // Act depending on command
     switch( check_sum ){
-        case ls_command_checksum      : this->ls_command( get_arguments(possible_command)); break;
-        case cd_command_checksum      : this->cd_command( get_arguments(possible_command)); break;
-        case cat_command_checksum     : this->cat_command(get_arguments(possible_command)); break;
+        case ls_command_checksum      : this->ls_command(  get_arguments(possible_command)); break;
+        case cd_command_checksum      : this->cd_command(  get_arguments(possible_command)); break;
+        case cat_command_checksum     : this->cat_command( get_arguments(possible_command)); break;
+        case play_command_checksum    : this->play_command(get_arguments(possible_command)); break; 
     }
 }
 
@@ -85,5 +86,29 @@ void SimpleShell::cat_command( string parameters ){
 
 }
 
+// Play a gcode file by considering each line as if it was received on the serial console
+void SimpleShell::play_command( string parameters ){
 
+    // Get filename
+    string filename          = this->absolute_from_relative(shift_parameter( parameters ));
+    // Open file 
+    FILE *lp = fopen(filename.c_str(), "r");
+    string buffer;
+    int c;
+    
+    // Print each line of the file
+    while ((c = fgetc (lp)) != EOF){
+        if (c == '\n'){
+            this->kernel->serial->printf("%s\n", buffer.c_str());
+            this->kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &buffer); 
+            buffer.clear();
+        }else{
+            buffer += c;
+        }
+    }; 
+    fclose(lp);
 
+
+
+}
index c82ab1b..da1ed14 100644 (file)
@@ -10,7 +10,7 @@
 #define ls_command_checksum      19679 
 #define cd_command_checksum      11207
 #define cat_command_checksum     24889
-
+#define play_command_checksum    17335
 
 class SimpleShell : public Module {
     public: 
@@ -19,10 +19,11 @@ class SimpleShell : public Module {
         void on_module_loaded();
         void on_console_line_received( void* argument );
         string absolute_from_relative( string path );
-        void ls_command( string parameters );
-        void cd_command( string parameters );
-        void cat_command( string parameters );
-        
+        void ls_command(   string parameters );
+        void cd_command(   string parameters );
+        void cat_command(  string parameters );
+        void play_command( string parameters );       
+
         string current_path;
 };