solved the block end problem, commiting mostly to save all those neat debug statement...
[clinton/Smoothieware.git] / src / libs / Config.cpp
CommitLineData
4cff3ded
AW
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
3c132bd0
AW
8
9using namespace std;
10#include <vector>
4cff3ded 11#include <string>
3c132bd0 12
4cff3ded
AW
13#include "libs/Kernel.h"
14#include "Config.h"
c295905f
AW
15#include "ConfigValue.h"
16#include "ConfigSource.h"
17#include "ConfigCache.h"
4eb9c745
AW
18#include "libs/nuts_bolts.h"
19#include "libs/utils.h"
6268c527 20#include "libs/SerialMessage.h"
c295905f 21#include "libs/ConfigSources/FileConfigSource.h"
4cff3ded
AW
22
23Config::Config(){
d272dd3c 24 this->config_cache_loaded = false;
fc82a1ee 25
c295905f 26 // Config source for */config files
32891c60 27 this->config_sources.push_back( new FileConfigSource("/local/config", LOCAL_CONFIGSOURCE_CHECKSUM) );
26c569a2 28 this->config_sources.push_back( new FileConfigSource("/sd/config", SD_CONFIGSOURCE_CHECKSUM ) );
4cff3ded 29
c295905f
AW
30 // Pre-load the config cache
31 this->config_cache_load();
cebe90b6 32
cebe90b6
AW
33}
34
c295905f 35void Config::on_module_loaded(){}
26806df9 36
c295905f 37void Config::on_console_line_received( void* argument ){}
ced5fce8
L
38
39void Config::set_string( string setting, string value ){
40 ConfigValue* cv = new ConfigValue;
41 cv->found = true;
4464301d 42 get_checksums(cv->check_sums, setting);
ced5fce8
L
43 cv->value = value;
44
45 this->config_cache.replace_or_push_back(cv);
46
47 this->kernel->call_event(ON_CONFIG_RELOAD);
48}
49
26c569a2
AW
50void Config::get_module_list(vector<uint16_t>* list, uint16_t family){
51 for( int i=1; i<this->config_cache.size(); i++){
52 ConfigValue* value = this->config_cache.at(i);
4464301d
AW
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 ){
26c569a2 55 // We found a module enable for this family, add it's number
4464301d 56 list->push_back(value->check_sums[1]);
26c569a2
AW
57 }
58 }
59}
26806df9 60
d272dd3c
L
61
62// Command to load config cache into buffer for multiple reads during init
63void Config::config_cache_load(){
c295905f 64
d272dd3c 65 this->config_cache_clear();
c295905f
AW
66
67 // First element is a special empty ConfigValue for values not found
d272dd3c 68 ConfigValue* result = new ConfigValue;
fc82a1ee 69 this->config_cache.push_back(result);
b03ad2ef 70
c295905f
AW
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);
75 }
c295905f 76
d272dd3c
L
77 this->config_cache_loaded = true;
78}
79
80// Command to clear the config cache after init
81void Config::config_cache_clear(){
fc82a1ee 82 while( ! this->config_cache.empty() ){
d272dd3c
L
83 delete this->config_cache.back();
84 this->config_cache.pop_back();
85 }
d272dd3c 86 this->config_cache_loaded = false;
da24d6ae
AW
87}
88
cebe90b6 89
3c132bd0 90ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b, uint16_t check_sum_c ){
4464301d
AW
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;
3c132bd0
AW
95 return this->value(check_sums);
96}
97
98ConfigValue* Config::value(uint16_t check_sum_a, uint16_t check_sum_b){
4464301d
AW
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;
3c132bd0
AW
103 return this->value(check_sums);
104}
105
106ConfigValue* Config::value(uint16_t check_sum){
4464301d
AW
107 uint16_t check_sums[3];
108 check_sums[0] = check_sum;
109 check_sums[1] = 0x0000;
110 check_sums[2] = 0x0000;
3c132bd0
AW
111 return this->value(check_sums);
112}
113
4cff3ded
AW
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
116// See get_checksum
4464301d 117ConfigValue* Config::value(uint16_t check_sums[]){
fc82a1ee 118 ConfigValue* result = this->config_cache[0];
d272dd3c 119 bool cache_preloaded = this->config_cache_loaded;
c295905f
AW
120 if( !cache_preloaded ){ this->config_cache_load(); }
121
fc82a1ee 122 for( int i=1; i<this->config_cache.size(); i++){
d272dd3c
L
123 // If this line matches the checksum
124 bool match = true;
4464301d
AW
125 char counter = 0;
126 while(check_sums[counter] != 0x0000 && counter <= 2 ){
127 if(this->config_cache[i]->check_sums[counter] != check_sums[counter] ){
d272dd3c 128 match = false;
d272dd3c
L
129 break;
130 }
4464301d 131 counter++;
d272dd3c
L
132 }
133 if( match == false ){
d272dd3c 134 continue;
4cff3ded 135 }
fc82a1ee 136 result = this->config_cache[i];
d272dd3c
L
137 break;
138 }
c295905f 139
d272dd3c
L
140 if( !cache_preloaded ){
141 this->config_cache_clear();
142 }
b66fb830 143 return result;
4cff3ded
AW
144}
145
3c132bd0 146
4cff3ded 147