Michael Spencer / Smoothie

Dependencies:   mbed

Fork of Smoothie by Stéphane Cachat

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ConfigValue.h Source File

ConfigValue.h

00001 /*
00002       This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
00003       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.
00004       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.
00005       You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
00006 */
00007 
00008 #ifndef CONFIGVALUE_H
00009 #define CONFIGVALUE_H
00010 
00011 #include "libs/Kernel.h"
00012 #include "libs/utils.h"
00013 #include "libs/Pin.h"
00014 #include "Pwm.h"
00015 
00016 
00017 #define error(...) (fprintf(stderr, __VA_ARGS__), exit(1))
00018 
00019 using namespace std;
00020 #include <vector>
00021 #include <string>
00022 #include <stdio.h>
00023 
00024 
00025 class ConfigValue{
00026     public:
00027         ConfigValue(){
00028             this->found = false;
00029             this->default_set = false;
00030             this->check_sums[0] = 0x0000;
00031             this->check_sums[1] = 0x0000;
00032             this->check_sums[2] = 0x0000;
00033         };
00034 
00035         ConfigValue* required(){
00036             if( this->found == true ){
00037                 return this;
00038             }else{
00039                 error("could not find config setting, please see http://smoothieware.org/configuring-smoothie\r\n");
00040             }
00041         }
00042 
00043         float as_number(){
00044             if( this->found == false && this->default_set == true ){
00045                 return this->default_double;
00046             }else{
00047                 char* endptr = NULL;
00048                 float result = strtof(remove_non_number(this->value).c_str(), &endptr);
00049                 if( endptr <= remove_non_number(this->value).c_str() ){
00050                     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] );
00051                 }
00052                 return result;
00053             }
00054         }
00055 
00056         int as_int()
00057         {
00058             if( this->found == false && this->default_set == true ){
00059                 return this->default_int;
00060             }else{
00061                 char* endptr = NULL;
00062                 int result = strtol(remove_non_number(this->value).c_str(), &endptr, 10);
00063                 if( endptr <= remove_non_number(this->value).c_str() ){
00064                     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] );
00065                 }
00066                 return result;
00067             }
00068         }
00069 
00070         std::string as_string(){
00071             return this->value;
00072         }
00073 
00074         bool as_bool(){
00075             if( this->found == false && this->default_set == true ){
00076                 return this->default_double;
00077             }else{
00078                 if( this->value.find_first_of("ty1") != string::npos ){
00079                     return true;
00080                 }else{
00081                     return false;
00082                 }
00083             }
00084         }
00085 
00086         ConfigValue* by_default(int val)
00087         {
00088             this->default_set = true;
00089             this->default_int = val;
00090             this->default_double = val;
00091             return this;
00092         }
00093 
00094         ConfigValue* by_default(float val){
00095             this->default_set = true;
00096             this->default_double = val;
00097             return this;
00098         }
00099 
00100         ConfigValue* by_default(std::string val){
00101             if( this->found ){ return this; }
00102             this->default_set = true;
00103             this->value = val;
00104             return this;
00105         }
00106 
00107         bool has_characters( string mask ){
00108             if( this->value.find_first_of(mask) != string::npos ){ return true; }else{ return false; }
00109         }
00110 
00111         bool is_inverted(){
00112             return this->has_characters(string("!"));
00113         }
00114 
00115         int default_int;
00116         float default_double;
00117         uint16_t check_sums[3];
00118         string value;
00119         bool found;
00120         bool default_set;
00121 };
00122 
00123 
00124 
00125 
00126 
00127 
00128 
00129 
00130 #endif