smoothie port to mbed online compiler (smoothieware.org)

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Configurator.cpp Source File

Configurator.cpp

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
00004       Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. 
00006 */
00007 
00008 
00009 #include "libs/Kernel.h"
00010 #include "Configurator.h"
00011 #include "libs/nuts_bolts.h"
00012 #include "libs/utils.h"
00013 #include "libs/SerialMessage.h"
00014 #include "libs/StreamOutput.h"
00015 
00016 
00017 void Configurator::on_module_loaded(){
00018     this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
00019 //    this->register_for_event(ON_GCODE_EXECUTE);
00020 //    this->register_for_event(ON_MAIN_LOOP);
00021 }
00022 
00023 // When a new line is received, check if it is a command, and if it is, act upon it
00024 void Configurator::on_console_line_received( void* argument ){
00025     SerialMessage new_message = *static_cast<SerialMessage*>(argument);
00026     string possible_command = new_message.message;
00027 
00028     // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
00029     uint16_t check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) );  // todo: put this method somewhere more convenient
00030 
00031     // Act depending on command
00032     switch( check_sum ){
00033         case config_get_command_checksum: this->config_get_command(  get_arguments(possible_command), new_message.stream ); break;
00034         case config_set_command_checksum: this->config_set_command(  get_arguments(possible_command), new_message.stream ); break;
00035         case config_load_command_checksum: this->config_load_command(  get_arguments(possible_command), new_message.stream ); break;
00036     }
00037 }
00038 
00039 // Process and respond to eeprom gcodes (M50x)
00040 void Configurator::on_gcode_execute(void* argument){
00041     Gcode* gcode = static_cast<Gcode*>(argument);
00042     if( gcode->has_letter('G') ){
00043         int code = gcode->get_value('G');
00044         switch( code ){
00045         }
00046     }
00047     else if( gcode->has_letter('M') ){
00048         int code = gcode->get_value('M');
00049         switch( code ){
00050         }
00051     }
00052 }
00053 
00054 void Configurator::on_main_loop(void* argument){}
00055 
00056 // Output a ConfigValue from the specified ConfigSource to the stream
00057 void Configurator::config_get_command( string parameters, StreamOutput* stream ){
00058     string source = shift_parameter(parameters);
00059     string setting = shift_parameter(parameters);
00060     if (setting == "") { // output live setting
00061         setting = source;
00062         source = "";
00063         vector<uint16_t> setting_checksums = get_checksums( setting );
00064         ConfigValue* cv = this->kernel->config->value(setting_checksums);
00065         string value = "";
00066         if(cv->found){ value = cv->as_string(); }
00067         stream->printf( "live: %s is set to %s\r\n", setting.c_str(), value.c_str() );
00068     } else { // output setting from specified source
00069         uint16_t source_checksum = get_checksum( source );
00070         vector<uint16_t> setting_checksums = get_checksums( setting );
00071         for(int i=0; i < this->kernel->config->config_sources.size(); i++){
00072             if( this->kernel->config->config_sources[i]->is_named(source_checksum) ){
00073                 string value = this->kernel->config->config_sources[i]->read(setting_checksums);
00074                 stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
00075                 break;
00076             }
00077         }
00078     }
00079 }
00080 
00081 // Write the specified setting to the specified ConfigSource
00082 void Configurator::config_set_command( string parameters, StreamOutput* stream ){
00083     string source = shift_parameter(parameters);
00084     string setting = shift_parameter(parameters);
00085     string value = shift_parameter(parameters);
00086     if (value == "") {
00087         value = setting;
00088         setting = source;
00089         source = "";
00090         this->kernel->config->set_string(setting, value);
00091         stream->printf( "live: %s has been set to %s\r\n", setting.c_str(), value.c_str() );
00092     } else {
00093         uint16_t source_checksum = get_checksum(source);
00094         for(int i=0; i < this->kernel->config->config_sources.size(); i++){
00095             if( this->kernel->config->config_sources[i]->is_named(source_checksum) ){
00096                 this->kernel->config->config_sources[i]->write(setting, value);
00097                 stream->printf( "%s: %s has been set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
00098                 break;
00099             }
00100         }
00101     }
00102 }
00103 
00104 // Reload config values from the specified ConfigSource
00105 void Configurator::config_load_command( string parameters, StreamOutput* stream ){
00106     string source = shift_parameter(parameters);
00107     if(source == ""){
00108         this->kernel->config->config_cache_load();
00109         this->kernel->call_event(ON_CONFIG_RELOAD);
00110         stream->printf( "Reloaded settings\r\n" );
00111     } else if(file_exists(source)){
00112         FileConfigSource fcs(source);
00113         fcs.transfer_values_to_cache(&this->kernel->config->config_cache);
00114         this->kernel->call_event(ON_CONFIG_RELOAD);
00115         stream->printf( "Loaded settings from %s\r\n", source.c_str() );
00116     } else {
00117         uint16_t source_checksum = get_checksum(source);
00118         for(int i=0; i < this->kernel->config->config_sources.size(); i++){
00119             if( this->kernel->config->config_sources[i]->is_named(source_checksum) ){
00120                 this->kernel->config->config_sources[i]->transfer_values_to_cache(&this->kernel->config->config_cache);
00121                 this->kernel->call_event(ON_CONFIG_RELOAD);
00122                 stream->printf( "Loaded settings from %s\r\n", source.c_str() );
00123                 break;
00124             }
00125         }
00126     }
00127 }
00128