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/>.
13 #include "libs/Kernel.h"
15 #include "ConfigValue.h"
16 #include "ConfigSource.h"
17 #include "ConfigCache.h"
18 #include "libs/nuts_bolts.h"
19 #include "libs/utils.h"
20 #include "libs/SerialMessage.h"
21 #include "libs/ConfigSources/FileConfigSource.h"
24 this->config_cache_loaded
= false;
26 // Config source for */config files
27 this->config_sources
.push_back( new FileConfigSource("/local/config", LOCAL_CONFIGSOURCE_CHECKSUM
) );
28 this->config_sources
.push_back( new FileConfigSource("/sd/config", SD_CONFIGSOURCE_CHECKSUM
) );
30 // Pre-load the config cache
31 this->config_cache_load();
35 void Config::on_module_loaded(){}
37 void Config::on_console_line_received( void* argument
){}
39 void Config::set_string( string setting
, string value
){
40 ConfigValue
* cv
= new ConfigValue
;
42 get_checksums(cv
->check_sums
, setting
);
45 this->config_cache
.replace_or_push_back(cv
);
47 this->kernel
->call_event(ON_CONFIG_RELOAD
);
50 void Config::get_module_list(vector
<uint16_t>* list
, uint16_t family
){
51 for( unsigned int i
=1; i
<this->config_cache
.size(); i
++){
52 ConfigValue
* value
= this->config_cache
.at(i
);
53 //if( value->check_sums.size() == 3 && value->check_sums.at(2) == 29545 && value->check_sums.at(0) == family ){
54 if( value
->check_sums
[2] == 29545 && value
->check_sums
[0] == family
){
55 // We found a module enable for this family, add it's number
56 list
->push_back(value
->check_sums
[1]);
62 // Command to load config cache into buffer for multiple reads during init
63 void Config::config_cache_load(){
65 this->config_cache_clear();
67 // First element is a special empty ConfigValue for values not found
68 ConfigValue
* result
= new ConfigValue
;
69 this->config_cache
.push_back(result
);
71 // For each ConfigSource in our stack
72 for( unsigned int i
= 0; i
< this->config_sources
.size(); i
++ ){
73 ConfigSource
* source
= this->config_sources
[i
];
74 source
->transfer_values_to_cache(&this->config_cache
);
77 this->config_cache_loaded
= true;
80 // Command to clear the config cache after init
81 void Config::config_cache_clear(){
82 while( ! this->config_cache
.empty() ){
83 delete this->config_cache
.back();
84 this->config_cache
.pop_back();
86 this->config_cache_loaded
= false;
90 ConfigValue
* Config::value(uint16_t check_sum_a
, uint16_t check_sum_b
, uint16_t check_sum_c
){
91 uint16_t check_sums
[3];
92 check_sums
[0] = check_sum_a
;
93 check_sums
[1] = check_sum_b
;
94 check_sums
[2] = check_sum_c
;
95 return this->value(check_sums
);
98 ConfigValue
* Config::value(uint16_t check_sum_a
, uint16_t check_sum_b
){
99 uint16_t check_sums
[3];
100 check_sums
[0] = check_sum_a
;
101 check_sums
[1] = check_sum_b
;
102 check_sums
[2] = 0x0000;
103 return this->value(check_sums
);
106 ConfigValue
* Config::value(uint16_t check_sum
){
107 uint16_t check_sums
[3];
108 check_sums
[0] = check_sum
;
109 check_sums
[1] = 0x0000;
110 check_sums
[2] = 0x0000;
111 return this->value(check_sums
);
114 // Get a value from the configuration as a string
115 // Because we don't like to waste space in Flash with lengthy config parameter names, we take a checksum instead so that the name does not have to be stored
117 ConfigValue
* Config::value(uint16_t check_sums
[]){
118 ConfigValue
* result
= this->config_cache
[0];
119 bool cache_preloaded
= this->config_cache_loaded
;
120 if( !cache_preloaded
){ this->config_cache_load(); }
122 for( unsigned int i
=1; i
<this->config_cache
.size(); i
++){
123 // If this line matches the checksum
125 unsigned int counter
= 0;
126 while(check_sums
[counter
] != 0x0000 && counter
<= 2 ){
127 if(this->config_cache
[i
]->check_sums
[counter
] != check_sums
[counter
] ){
133 if( match
== false ){
136 result
= this->config_cache
[i
];
140 if( !cache_preloaded
){
141 this->config_cache_clear();