Commit | Line | Data |
---|---|---|
c295905f 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 | ||
8 | #ifndef CONFIGVALUE_H | |
9 | #define CONFIGVALUE_H | |
10 | ||
11 | #include "libs/Kernel.h" | |
12 | #include "libs/utils.h" | |
13 | #include "libs/Pin.h" | |
14 | ||
15 | ||
16 | #define error(...) (fprintf(stderr, __VA_ARGS__), exit(1)) | |
17 | ||
18 | using namespace std; | |
19 | #include <vector> | |
20 | #include <string> | |
21 | #include <stdio.h> | |
22 | ||
23 | ||
24 | class ConfigValue{ | |
25 | public: | |
26 | ConfigValue(){ | |
27 | this->found = false; | |
4464301d AW |
28 | this->default_set = false; |
29 | this->check_sums[0] = 0x0000; | |
30 | this->check_sums[1] = 0x0000; | |
31 | this->check_sums[2] = 0x0000; | |
c295905f AW |
32 | }; |
33 | ||
34 | ConfigValue* required(){ | |
35 | if( this->found == true ){ | |
36 | return this; | |
37 | }else{ | |
4464301d | 38 | error("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n"); |
c295905f AW |
39 | } |
40 | } | |
41 | ||
42 | double as_number(){ | |
43 | if( this->found == false && this->default_set == true ){ | |
44 | return this->default_double; | |
45 | }else{ | |
46 | double result = atof(remove_non_number(this->value).c_str()); | |
47 | if( result == 0.0 && this->value.find_first_not_of("0.") != string::npos ){ | |
4464301d | 48 | error("config setting with value '%s' and checksums[%u,%u,%u] is not a valid number, please see http://smoothieware.org/configuring-smoothie\r\n", this->value.c_str(), this->check_sums[0], this->check_sums[1], this->check_sums[2] ); |
c295905f AW |
49 | } |
50 | return result; | |
51 | } | |
52 | ||
53 | } | |
54 | ||
55 | std::string as_string(){ | |
4464301d AW |
56 | //if( this->found == false && this->default_set == true ){ |
57 | // return this->default_string; | |
58 | //}else{ | |
c295905f | 59 | return this->value; |
4464301d | 60 | //} |
c295905f AW |
61 | } |
62 | ||
63 | bool as_bool(){ | |
64 | if( this->found == false && this->default_set == true ){ | |
65 | return this->default_double; | |
66 | }else{ | |
67 | if( this->value.find_first_of("t1") != string::npos ){ | |
68 | return true; | |
69 | }else{ | |
70 | return false; | |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | Pin* as_pin(){ | |
76 | Pin* pin = new Pin(); | |
77 | pin->from_string(this->as_string()); | |
78 | return pin; | |
79 | } | |
80 | ||
81 | ConfigValue* by_default(double val){ | |
82 | this->default_set = true; | |
83 | this->default_double = val; | |
84 | return this; | |
85 | } | |
86 | ||
87 | ConfigValue* by_default(std::string val){ | |
4464301d | 88 | if( this->found ){ return this; } |
c295905f | 89 | this->default_set = true; |
4464301d | 90 | this->value = val; |
c295905f AW |
91 | return this; |
92 | } | |
93 | ||
94 | bool has_characters( string mask ){ | |
95 | if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; } | |
96 | } | |
97 | ||
98 | bool is_inverted(){ | |
99 | return this->has_characters(string("!")); | |
100 | } | |
101 | ||
a39e1557 | 102 | double default_double; |
4464301d | 103 | uint16_t check_sums[3]; |
a39e1557 | 104 | string value; |
c295905f AW |
105 | bool found; |
106 | bool default_set; | |
c295905f AW |
107 | }; |
108 | ||
109 | ||
110 | ||
111 | ||
112 | ||
113 | ||
114 | ||
115 | ||
116 | #endif |