Allow TABS in config
[clinton/Smoothieware.git] / src / modules / utils / configurator / Configurator.cpp
CommitLineData
6c9499fd 1/*
d3a6143b
L
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
d3a6143b
L
6*/
7
8
9#include "libs/Kernel.h"
6c9499fd 10#include "Configurator.h"
d3a6143b
L
11#include "libs/nuts_bolts.h"
12#include "libs/utils.h"
13#include "libs/SerialMessage.h"
14#include "libs/StreamOutput.h"
61134a65
JM
15#include "checksumm.h"
16#include "Config.h"
17#include "Gcode.h"
d3a6143b
L
18
19void Configurator::on_module_loaded(){
2b394436 20 this->register_for_event(ON_CONSOLE_LINE_RECEIVED);
e6b5ae25 21// this->register_for_event(ON_GCODE_RECEIVED);
d3a6143b
L
22// this->register_for_event(ON_MAIN_LOOP);
23}
24
25// When a new line is received, check if it is a command, and if it is, act upon it
26void Configurator::on_console_line_received( void* argument ){
27 SerialMessage new_message = *static_cast<SerialMessage*>(argument);
7f613782
JM
28
29 // ignore comments
30 if(new_message.message[0] == ';') return;
31
d3a6143b
L
32 string possible_command = new_message.message;
33
34 // We don't compare to a string but to a checksum of that string, this saves some space in flash memory
7b49793d 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
d3a6143b
L
36
37 // Act depending on command
d9664db4
AG
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 );
d3a6143b
L
44}
45
46// Process and respond to eeprom gcodes (M50x)
e6b5ae25 47void Configurator::on_gcode_received(void* argument){
d3a6143b 48 Gcode* gcode = static_cast<Gcode*>(argument);
62e5b0ca
L
49 if( gcode->has_letter('G') ){
50 int code = gcode->get_value('G');
51 switch( code ){
52 }
53 }
54 else if( gcode->has_letter('M') ){
d3a6143b
L
55 int code = gcode->get_value('M');
56 switch( code ){
d3a6143b
L
57 }
58 }
59}
60
2b394436
L
61void Configurator::on_main_loop(void* argument){}
62
63// Output a ConfigValue from the specified ConfigSource to the stream
64void Configurator::config_get_command( string parameters, StreamOutput* stream ){
5efaa1b1
L
65 string source = shift_parameter(parameters);
66 string setting = shift_parameter(parameters);
67 if (setting == "") { // output live setting
68 setting = source;
69 source = "";
4464301d
AW
70 uint16_t setting_checksums[3];
71 get_checksums(setting_checksums, setting );
314ab8f7 72 ConfigValue* cv = THEKERNEL->config->value(setting_checksums);
ced5fce8
L
73 string value = "";
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() );
2b394436 76 } else { // output setting from specified source
5efaa1b1 77 uint16_t source_checksum = get_checksum( source );
4464301d
AW
78 uint16_t setting_checksums[3];
79 get_checksums(setting_checksums, setting );
314ab8f7
MM
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);
ced5fce8 83 stream->printf( "%s: %s is set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
5efaa1b1
L
84 break;
85 }
86 }
2b394436
L
87 }
88}
89
90// Write the specified setting to the specified ConfigSource
91void 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);
95 if (value == "") {
96 value = setting;
97 setting = source;
98 source = "";
314ab8f7 99 THEKERNEL->config->set_string(setting, value);
ced5fce8 100 stream->printf( "live: %s has been set to %s\r\n", setting.c_str(), value.c_str() );
2b394436
L
101 } else {
102 uint16_t source_checksum = get_checksum(source);
314ab8f7
MM
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);
ced5fce8
L
106 stream->printf( "%s: %s has been set to %s\r\n", source.c_str(), setting.c_str(), value.c_str() );
107 break;
108 }
109 }
2b394436 110 }
d3a6143b
L
111}
112
2b394436 113// Reload config values from the specified ConfigSource
89c31ddb
L
114void Configurator::config_load_command( string parameters, StreamOutput* stream ){
115 string source = shift_parameter(parameters);
116 if(source == ""){
314ab8f7
MM
117 THEKERNEL->config->config_cache_load();
118 THEKERNEL->call_event(ON_CONFIG_RELOAD);
89c31ddb 119 stream->printf( "Reloaded settings\r\n" );
8d857d2e
L
120 } else if(file_exists(source)){
121 FileConfigSource fcs(source);
314ab8f7
MM
122 fcs.transfer_values_to_cache(&THEKERNEL->config->config_cache);
123 THEKERNEL->call_event(ON_CONFIG_RELOAD);
8d857d2e 124 stream->printf( "Loaded settings from %s\r\n", source.c_str() );
89c31ddb
L
125 } else {
126 uint16_t source_checksum = get_checksum(source);
314ab8f7
MM
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);
89c31ddb
L
131 stream->printf( "Loaded settings from %s\r\n", source.c_str() );
132 break;
133 }
134 }
135 }
136}
41424456 137