12dfdfd841415c03443c741873ca2457f1be50ad
[clinton/Smoothieware.git] / src / modules / utils / configurator / Configurator.cpp
1 /*
2 This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
3 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.
4 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.
5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
6 */
7
8
9 #include "libs/Kernel.h"
10 #include "Configurator.h"
11 #include "libs/nuts_bolts.h"
12 #include "libs/utils.h"
13 #include "libs/SerialMessage.h"
14 #include "libs/StreamOutput.h"
15
16
17 void Configurator::on_module_loaded(){
18 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
19 // this->register_for_event(ON_GCODE_RECEIVED);
20 // this->register_for_event(ON_MAIN_LOOP);
21 }
22
23 // When a new line is received, check if it is a command, and if it is, act upon it
24 void Configurator::on_console_line_received( void* argument ){
25 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
26
27 // ignore comments
28 if(new_message.message[0] == ';') return;
29
30 string possible_command = new_message.message;
31
32 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
33 uint16_t check_sum = get_checksum( possible_command.substr(0,possible_command.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
34
35 // Act depending on command
36 if (check_sum == config_get_command_checksum)
37 this->config_get_command( get_arguments(possible_command), new_message.stream );
38 else if (check_sum == config_set_command_checksum)
39 this->config_set_command( get_arguments(possible_command), new_message.stream );
40 else if (check_sum == config_load_command_checksum)
41 this->config_load_command( get_arguments(possible_command), new_message.stream );
42 }
43
44 // Process and respond to eeprom gcodes (M50x)
45 void Configurator::on_gcode_received(void* argument){
46 Gcode* gcode = static_cast<Gcode*>(argument);
47 if( gcode->has_letter('G') ){
48 int code = gcode->get_value('G');
49 switch( code ){
50 }
51 }
52 else if( gcode->has_letter('M') ){
53 int code = gcode->get_value('M');
54 switch( code ){
55 }
56 }
57 }
58
59 void Configurator::on_main_loop(void* argument){}
60
61 // Output a ConfigValue from the specified ConfigSource to the stream
62 void Configurator::config_get_command( string parameters, StreamOutput* stream ){
63 string source = shift_parameter(parameters);
64 string setting = shift_parameter(parameters);
65 if (setting == "") { // output live setting
66 setting = source;
67 source = "";
68 uint16_t setting_checksums[3];
69 get_checksums(setting_checksums, setting );
70 ConfigValue* cv = THEKERNEL->config->value(setting_checksums);
71 string value = "";
72 if(cv->found){ value = cv->as_string(); }
73 stream->printf( "live: %s is set to %s\r\n", setting.c_str(), value.c_str() );
74 } else { // output setting from specified source
75 uint16_t source_checksum = get_checksum( source );
76 uint16_t setting_checksums[3];
77 get_checksums(setting_checksums, setting );
78 for(unsigned int i=0; i < THEKERNEL->config->config_sources.size(); i++){
79 if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ){
80 string value = THEKERNEL->config->config_sources[i]->read(setting_checksums);
81 stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
82 break;
83 }
84 }
85 }
86 }
87
88 // Write the specified setting to the specified ConfigSource
89 void Configurator::config_set_command( string parameters, StreamOutput* stream ){
90 string source = shift_parameter(parameters);
91 string setting = shift_parameter(parameters);
92 string value = shift_parameter(parameters);
93 if (value == "") {
94 value = setting;
95 setting = source;
96 source = "";
97 THEKERNEL->config->set_string(setting, value);
98 stream->printf( "live: %s has been set to %s\r\n", setting.c_str(), value.c_str() );
99 } else {
100 uint16_t source_checksum = get_checksum(source);
101 for(unsigned int i=0; i < THEKERNEL->config->config_sources.size(); i++){
102 if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ){
103 THEKERNEL->config->config_sources[i]->write(setting, value);
104 stream->printf( "%s: %s has been set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
105 break;
106 }
107 }
108 }
109 }
110
111 // Reload config values from the specified ConfigSource
112 void Configurator::config_load_command( string parameters, StreamOutput* stream ){
113 string source = shift_parameter(parameters);
114 if(source == ""){
115 THEKERNEL->config->config_cache_load();
116 THEKERNEL->call_event(ON_CONFIG_RELOAD);
117 stream->printf( "Reloaded settings\r\n" );
118 } else if(file_exists(source)){
119 FileConfigSource fcs(source);
120 fcs.transfer_values_to_cache(&THEKERNEL->config->config_cache);
121 THEKERNEL->call_event(ON_CONFIG_RELOAD);
122 stream->printf( "Loaded settings from %s\r\n", source.c_str() );
123 } else {
124 uint16_t source_checksum = get_checksum(source);
125 for(unsigned int i=0; i < THEKERNEL->config->config_sources.size(); i++){
126 if( THEKERNEL->config->config_sources[i]->is_named(source_checksum) ){
127 THEKERNEL->config->config_sources[i]->transfer_values_to_cache(&THEKERNEL->config->config_cache);
128 THEKERNEL->call_event(ON_CONFIG_RELOAD);
129 stream->printf( "Loaded settings from %s\r\n", source.c_str() );
130 break;
131 }
132 }
133 }
134 }
135