in-file config of the TemperatureControl, and simple adjustments to make
[clinton/Smoothieware.git] / src / libs / Config.h
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 #ifndef CONFIG_H
9 #define CONFIG_H
10 #include "mbed.h"
11 #include "libs/Kernel.h"
12 #include "libs/utils.h"
13 #include <string>
14 using std::string;
15
16 #define config_get_checksum 46310
17 #define config_set_checksum 55538
18 #define config_load_checksum 3143
19
20 class ConfigValue{
21 public:
22 ConfigValue(){
23 this->found = false;
24 this->default_set = false;
25 };
26
27 ConfigValue* required(){
28 if( this->found == true ){
29 return this;
30 }else{
31 error("could not find config setting with checksum %u, please see http://smoothieware.org/configuring-smoothie\r\n", this->check_sum );
32 }
33 }
34
35 double as_number(){
36 if( this->found == false && this->default_set == true ){
37 return this->default_double;
38 }else{
39 double result = atof(remove_non_number(value).c_str());
40 if( result == 0.0 && this->value.find_first_not_of("0.") != string::npos ){
41 error("config setting '%s' with value '%s' is not a valid number, please see http://smoothieware.org/configuring-smoothie\r\n", this->key.c_str(), this->value.c_str() );
42 }
43 return result;
44 }
45 }
46
47 bool as_bool(){
48 if( this->found == false && this->default_set == true ){
49 return this->default_double;
50 }else{
51 if( this->value.find_first_of("t1") != string::npos ){
52 return true;
53 }else{
54 return false;
55 }
56 }
57
58 }
59
60 ConfigValue* by_default(double value){
61 this->default_set = true;
62 this->default_double = value;
63 return this;
64 }
65
66 bool has_characters( string mask ){
67 if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; }
68 }
69
70 bool is_inverted(){
71 return this->has_characters(string("!"));
72 }
73
74 string value;
75 string key;
76 uint16_t check_sum;
77 bool found;
78 bool default_set;
79 double default_double;
80 };
81
82
83 class Config : public Module {
84 public:
85 Config();
86
87 void on_module_loaded();
88 void on_console_line_received( void* argument );
89 void config_get_command( string parameters );
90 void config_set_command( string parameters );
91 void config_load_command(string parameters );
92 void set_string( uint16_t check_sum, string value);
93 ConfigValue* value(uint16_t check_sum);
94 bool has_characters(uint16_t check_sum, string str );
95 string get_config_file();
96 bool has_config_file();
97 void try_config_file(string candidate);
98
99 string config_file; // Path to the config file
100 bool config_file_found; // Wether or not the config file's location is known
101 };
102
103 #endif