Merge branch 'edge' into button
[clinton/Smoothieware.git] / src / libs / utils.cpp
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 #include "libs/Kernel.h"
9 #include "libs/utils.h"
10 #include "system_LPC17xx.h"
11 using namespace std;
12 #include <string>
13 using std::string;
14 #include <cstring>
15
16 volatile bool _isr_context = false;
17
18 uint16_t get_checksum(const string to_check){
19 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
20 uint16_t sum1 = 0;
21 uint16_t sum2 = 0;
22 for( unsigned int index = 0; index < to_check.length(); ++index ){
23 sum1 = (sum1 + to_check[index]) % 255;
24 sum2 = (sum2 + sum1) % 255;
25 }
26 return (sum2 << 8) | sum1;
27 }
28
29 void get_checksums(uint16_t check_sums[], const string key){
30 const string k = key+" ";
31 check_sums[0] = 0x0000;
32 check_sums[1] = 0x0000;
33 check_sums[2] = 0x0000;
34 size_t begin_key = 0;
35 unsigned int counter = 0;
36 while( begin_key < key.size()-1 ){
37 const size_t end_key = k.find_first_of(" .", begin_key);
38 const string key_node = k.substr(begin_key, end_key - begin_key);
39 check_sums[counter] = get_checksum(key_node);
40 begin_key = end_key + 1;
41 counter++;
42 }
43 }
44
45 bool is_alpha(int c)
46 {
47 if ((c >= 'a') && (c <= 'z')) return true;
48 if ((c >= 'A') && (c <= 'Z')) return true;
49 if ((c == '_')) return true;
50 return false;
51 }
52
53 bool is_digit(int c)
54 {
55 if ((c >= '0') && (c <= '9')) return true;
56 return false;
57 }
58
59 bool is_numeric(int c)
60 {
61 if (is_digit(c)) return true;
62 if ((c == '.') || (c == '-')) return true;
63 if ((c == 'e')) return true;
64 return false;
65 }
66
67 bool is_alphanum(int c)
68 {
69 return is_alpha(c) || is_numeric(c);
70 }
71
72 bool is_whitespace(int c)
73 {
74 if ((c == ' ') || (c == '\t')) return true;
75 return false;
76 }
77
78 // Convert to lowercase
79 string lc(string str){
80 for (unsigned int i=0; i<strlen(str.c_str()); i++)
81 if (str[i] >= 0x41 && str[i] <= 0x5A)
82 str[i] = str[i] + 0x20;
83 return str;
84 }
85
86 // Remove non-number characters
87 string remove_non_number( string str ){
88 string number_mask = "0123456789-.";
89 size_t found=str.find_first_not_of(number_mask);
90 while (found!=string::npos){
91 //str[found]='*';
92 str.replace(found,1,"");
93 found=str.find_first_not_of(number_mask);
94 }
95 return str;
96 }
97
98 // Get the first parameter, and remove it from the original string
99 string shift_parameter( string &parameters ){
100 size_t beginning = parameters.find_first_of(" ");
101 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; }
102 string temp = parameters.substr( 0, beginning );
103 parameters = parameters.substr(beginning+1, parameters.size());
104 return temp;
105 }
106
107 // Separate command from arguments
108 string get_arguments( string possible_command ){
109 size_t beginning = possible_command.find_first_of(" ");
110 if( beginning == string::npos ){ return ""; }
111 return possible_command.substr( beginning + 1, possible_command.size() - beginning + 1);
112 }
113
114 // Returns true if the file exists
115 bool file_exists( const string file_name ){
116 bool exists = false;
117 FILE *lp = fopen(file_name.c_str(), "r");
118 if(lp){ exists = true; }
119 fclose(lp);
120 return exists;
121 }
122
123 // Prepares and executes a watchdog reset
124 void system_reset( void ){
125 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
126 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
127 LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second
128 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
129 LPC_WDT->WDFEED = 0xAA; // Kick the dog!
130 LPC_WDT->WDFEED = 0x55;
131 }
132