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