Merge pull request #27 from logxen/xhume
[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 "libs/Kernel.h"
11 #include "libs/utils.h"
12 #include "libs/Pin.h"
13
14 #define error(...) (fprintf(stderr, __VA_ARGS__), exit(1))
15
16 using namespace std;
17 #include <vector>
18 #include <string>
19 #include <stdio.h>
20
21 #define config_get_checksum 46310
22 #define config_set_checksum 55538
23 #define config_load_checksum 3143
24
25 class ConfigValue{
26 public:
27 ConfigValue(){
28 this->found = false;
29 this->default_set = false;
30 };
31
32 ConfigValue* required(){
33 if( this->found == true ){
34 return this;
35 }else{
36 error("could not find config setting with checksum %u, please see http://smoothieware.org/configuring-smoothie\r\n", this->check_sum );
37 }
38 }
39
40 double as_number(){
41 if( this->found == false && this->default_set == true ){
42 return this->default_double;
43 }else{
44 double result = atof(remove_non_number(this->value).c_str());
45 if( result == 0.0 && this->value.find_first_not_of("0.") != string::npos ){
46 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() );
47 }
48 return result;
49 }
50
51 }
52
53 std::string as_string(){
54 if( this->found == false && this->default_set == true ){
55 return this->default_string;
56 }else{
57 return this->value;
58 }
59 }
60
61 bool as_bool(){
62 if( this->found == false && this->default_set == true ){
63 return this->default_double;
64 }else{
65 if( this->value.find_first_of("t1") != string::npos ){
66 return true;
67 }else{
68 return false;
69 }
70 }
71 }
72
73 Pin* as_pin(){
74 Pin* pin = new Pin();
75 pin->from_string(this->as_string());
76 return pin;
77 }
78
79 ConfigValue* by_default(double value){
80 this->default_set = true;
81 this->default_double = value;
82 return this;
83 }
84
85 ConfigValue* by_default(std::string value){
86 this->default_set = true;
87 this->default_string = value;
88 return this;
89 }
90
91 bool has_characters( string mask ){
92 if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; }
93 }
94
95 bool is_inverted(){
96 return this->has_characters(string("!"));
97 }
98
99 string value;
100 string key;
101 uint16_t check_sum;
102 bool found;
103 bool default_set;
104 double default_double;
105 string default_string;
106 };
107
108
109 class Config : public Module {
110 public:
111 Config();
112
113 void on_module_loaded();
114 void on_console_line_received( void* argument );
115 void config_get_command( string parameters );
116 void config_set_command( string parameters );
117 void config_load_command(string parameters );
118 void set_string( string setting , string value);
119
120 ConfigValue* value(uint16_t check_sum);
121 ConfigValue* value(uint16_t check_sum_a, uint16_t check_sum_b);
122 ConfigValue* value(uint16_t check_sum_a, uint16_t check_sum_b, uint16_t check_sum_c );
123 ConfigValue* value(vector<uint16_t> check_sums );
124
125 void get_module_list(vector<uint16_t>* list, uint16_t family);
126
127 bool has_characters(uint16_t check_sum, string str );
128 string get_config_file();
129 bool has_config_file();
130 void try_config_file(string candidate);
131
132 string config_file; // Path to the config file
133 bool config_file_found; // Wether or not the config file's location is known
134 };
135
136 #endif