enabled specifying numeric config values using all strtof capabilities
[clinton/Smoothieware.git] / src / libs / utils.cpp
CommitLineData
df27a6a3 1/*
cd011f58
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
cd011f58
AW
6*/
7
4de76b51 8#include "libs/Kernel.h"
4eb9c745 9#include "libs/utils.h"
4de76b51 10#include "system_LPC17xx.h"
423df6df 11using namespace std;
4eb9c745
AW
12#include <string>
13using std::string;
423df6df 14#include <cstring>
4eb9c745 15
4df07f88 16volatile bool _isr_context = false;
4eb9c745 17
d5d3d675
JM
18uint16_t get_checksum(const string& to_check){
19 return get_checksum(to_check.c_str());
20}
21
22uint16_t get_checksum(const char* to_check){
df27a6a3 23 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
4eb9c745
AW
24 uint16_t sum1 = 0;
25 uint16_t sum2 = 0;
d5d3d675
JM
26 const char* p= to_check;
27 char c;
28 while((c= *p++) != 0) {
29 sum1 = (sum1 + c) % 255;
4eb9c745
AW
30 sum2 = (sum2 + sum1) % 255;
31 }
32 return (sum2 << 8) | sum1;
33}
34
35e773a4
BG
35void get_checksums(uint16_t check_sums[], const string key){
36 const string k = key+" ";
4464301d
AW
37 check_sums[0] = 0x0000;
38 check_sums[1] = 0x0000;
39 check_sums[2] = 0x0000;
5efaa1b1 40 size_t begin_key = 0;
df27a6a3 41 unsigned int counter = 0;
ced5fce8 42 while( begin_key < key.size()-1 ){
35e773a4
BG
43 const size_t end_key = k.find_first_of(" .", begin_key);
44 const string key_node = k.substr(begin_key, end_key - begin_key);
4464301d 45 check_sums[counter] = get_checksum(key_node);
5efaa1b1 46 begin_key = end_key + 1;
4464301d 47 counter++;
5efaa1b1 48 }
5efaa1b1
L
49}
50
a2970685
MM
51bool is_alpha(int c)
52{
53 if ((c >= 'a') && (c <= 'z')) return true;
54 if ((c >= 'A') && (c <= 'Z')) return true;
55 if ((c == '_')) return true;
56 return false;
57}
58
59bool is_digit(int c)
60{
61 if ((c >= '0') && (c <= '9')) return true;
62 return false;
63}
64
65bool is_numeric(int c)
66{
67 if (is_digit(c)) return true;
68 if ((c == '.') || (c == '-')) return true;
69 if ((c == 'e')) return true;
70 return false;
71}
72
73bool is_alphanum(int c)
74{
75 return is_alpha(c) || is_numeric(c);
76}
77
78bool is_whitespace(int c)
79{
80 if ((c == ' ') || (c == '\t')) return true;
81 return false;
82}
83
4eb9c745
AW
84// Convert to lowercase
85string lc(string str){
36ddf400 86 for (unsigned int i=0; i<strlen(str.c_str()); i++)
4eb9c745
AW
87 if (str[i] >= 0x41 && str[i] <= 0x5A)
88 str[i] = str[i] + 0x20;
89 return str;
90}
91
a699b669
AW
92// Remove non-number characters
93string remove_non_number( string str ){
dfb53c73 94 string number_mask = "0123456789-.abcdefpxABCDEFPX";
a699b669
AW
95 size_t found=str.find_first_not_of(number_mask);
96 while (found!=string::npos){
97 //str[found]='*';
df27a6a3 98 str.replace(found,1,"");
a699b669
AW
99 found=str.find_first_not_of(number_mask);
100 }
101 return str;
102}
103
4eb9c745
AW
104// Get the first parameter, and remove it from the original string
105string shift_parameter( string &parameters ){
106 size_t beginning = parameters.find_first_of(" ");
df27a6a3 107 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; }
4eb9c745
AW
108 string temp = parameters.substr( 0, beginning );
109 parameters = parameters.substr(beginning+1, parameters.size());
110 return temp;
111}
cebe90b6
AW
112
113// Separate command from arguments
114string get_arguments( string possible_command ){
115 size_t beginning = possible_command.find_first_of(" ");
df27a6a3 116 if( beginning == string::npos ){ return ""; }
5b81ce13 117 return possible_command.substr( beginning + 1, possible_command.size() - beginning + 1);
cebe90b6
AW
118}
119
8d857d2e 120// Returns true if the file exists
35e773a4 121bool file_exists( const string file_name ){
8d857d2e
L
122 bool exists = false;
123 FILE *lp = fopen(file_name.c_str(), "r");
124 if(lp){ exists = true; }
125 fclose(lp);
126 return exists;
127}
128
2742fca9
JM
129// Prepares and executes a watchdog reset for dfu or reboot
130void system_reset( bool dfu ){
ed7c5844
JM
131 if(dfu) {
132 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
133 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
134 LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second
135 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
136 LPC_WDT->WDFEED = 0xAA; // Kick the dog!
137 LPC_WDT->WDFEED = 0x55;
138 }else{
139 NVIC_SystemReset();
140 }
4de76b51 141}
cebe90b6 142
75f4581c
JM
143// Convert a path indication ( absolute or relative ) into a path ( absolute )
144string absolute_from_relative( string path )
145{
146 string cwd = THEKERNEL->current_path;
147
148 if ( path.empty() ) {
149 return THEKERNEL->current_path;
150 }
151
152 if ( path[0] == '/' ) {
153 return path;
154 }
155
156 string match = "../" ;
157 while ( path.substr(0,3) == match ) {
158 path = path.substr(3);
159 unsigned found = cwd.find_last_of("/");
160 cwd = cwd.substr(0,found);
161 }
162
163 match = ".." ;
164 if ( path.substr(0,2) == match ) {
165 path = path.substr(2);
166 unsigned found = cwd.find_last_of("/");
167 cwd = cwd.substr(0,found);
168 }
169
170 if ( cwd[cwd.length() - 1] == '/' ) {
171 return cwd + path;
172 }
173
174 return cwd + '/' + path;
175}