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/>.
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 #include "checksumm.h"
19 void Configurator::on_module_loaded(){
20 this->register_for_event(ON_CONSOLE_LINE_RECEIVED
);
21 // this->register_for_event(ON_GCODE_RECEIVED);
22 // this->register_for_event(ON_MAIN_LOOP);
25 // When a new line is received, check if it is a command, and if it is, act upon it
26 void Configurator::on_console_line_received( void* argument
){
27 SerialMessage new_message
= *static_cast<SerialMessage
*>(argument
);
30 if(new_message
.message
[0] == ';') return;
32 string possible_command
= new_message
.message
;
34 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
35 uint16_t check_sum
= get_checksum( possible_command
.substr(0,possible_command
.find_first_of(" \r\n")) ); // todo: put this method somewhere more convenient
37 // Act depending on command
38 if (check_sum
== config_get_command_checksum
)
39 this->config_get_command( get_arguments(possible_command
), new_message
.stream
);
40 else if (check_sum
== config_set_command_checksum
)
41 this->config_set_command( get_arguments(possible_command
), new_message
.stream
);
42 else if (check_sum
== config_load_command_checksum
)
43 this->config_load_command( get_arguments(possible_command
), new_message
.stream
);
46 // Process and respond to eeprom gcodes (M50x)
47 void Configurator::on_gcode_received(void* argument
){
48 Gcode
* gcode
= static_cast<Gcode
*>(argument
);
49 if( gcode
->has_letter('G') ){
50 int code
= gcode
->get_value('G');
54 else if( gcode
->has_letter('M') ){
55 int code
= gcode
->get_value('M');
61 void Configurator::on_main_loop(void* argument
){}
63 // Output a ConfigValue from the specified ConfigSource to the stream
64 void Configurator::config_get_command( string parameters
, StreamOutput
* stream
){
65 string source
= shift_parameter(parameters
);
66 string setting
= shift_parameter(parameters
);
67 if (setting
== "") { // output live setting
70 uint16_t setting_checksums
[3];
71 get_checksums(setting_checksums
, setting
);
72 ConfigValue
* cv
= THEKERNEL
->config
->value(setting_checksums
);
74 if(cv
->found
){ value
= cv
->as_string(); }
75 stream
->printf( "live: %s is set to %s\r\n", setting
.c_str(), value
.c_str() );
76 } else { // output setting from specified source
77 uint16_t source_checksum
= get_checksum( source
);
78 uint16_t setting_checksums
[3];
79 get_checksums(setting_checksums
, setting
);
80 for(unsigned int i
=0; i
< THEKERNEL
->config
->config_sources
.size(); i
++){
81 if( THEKERNEL
->config
->config_sources
[i
]->is_named(source_checksum
) ){
82 string value
= THEKERNEL
->config
->config_sources
[i
]->read(setting_checksums
);
83 stream
->printf( "%s: %s is set to %s\r\n", source
.c_str(), setting
.c_str(), value
.c_str() );
90 // Write the specified setting to the specified ConfigSource
91 void Configurator::config_set_command( string parameters
, StreamOutput
* stream
){
92 string source
= shift_parameter(parameters
);
93 string setting
= shift_parameter(parameters
);
94 string value
= shift_parameter(parameters
);
99 THEKERNEL
->config
->set_string(setting
, value
);
100 stream
->printf( "live: %s has been set to %s\r\n", setting
.c_str(), value
.c_str() );
102 uint16_t source_checksum
= get_checksum(source
);
103 for(unsigned int i
=0; i
< THEKERNEL
->config
->config_sources
.size(); i
++){
104 if( THEKERNEL
->config
->config_sources
[i
]->is_named(source_checksum
) ){
105 THEKERNEL
->config
->config_sources
[i
]->write(setting
, value
);
106 stream
->printf( "%s: %s has been set to %s\r\n", source
.c_str(), setting
.c_str(), value
.c_str() );
113 // Reload config values from the specified ConfigSource
114 void Configurator::config_load_command( string parameters
, StreamOutput
* stream
){
115 string source
= shift_parameter(parameters
);
117 THEKERNEL
->config
->config_cache_load();
118 THEKERNEL
->call_event(ON_CONFIG_RELOAD
);
119 stream
->printf( "Reloaded settings\r\n" );
120 } else if(file_exists(source
)){
121 FileConfigSource
fcs(source
);
122 fcs
.transfer_values_to_cache(&THEKERNEL
->config
->config_cache
);
123 THEKERNEL
->call_event(ON_CONFIG_RELOAD
);
124 stream
->printf( "Loaded settings from %s\r\n", source
.c_str() );
126 uint16_t source_checksum
= get_checksum(source
);
127 for(unsigned int i
=0; i
< THEKERNEL
->config
->config_sources
.size(); i
++){
128 if( THEKERNEL
->config
->config_sources
[i
]->is_named(source_checksum
) ){
129 THEKERNEL
->config
->config_sources
[i
]->transfer_values_to_cache(&THEKERNEL
->config
->config_cache
);
130 THEKERNEL
->call_event(ON_CONFIG_RELOAD
);
131 stream
->printf( "Loaded settings from %s\r\n", source
.c_str() );