// 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 ){
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;
int start = pos - buffer.length() + begin_value - 1;
fseek(lp, start, SEEK_SET);
fputs(value.c_str(), lp);
-
fclose(lp);
return;
}else{
// 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;
}
}
}
+// 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);
+
+
+}
#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:
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;
};