solved the block end problem, commiting mostly to save all those neat debug statement...
[clinton/Smoothieware.git] / src / libs / utils.cpp
CommitLineData
cd011f58
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
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
AW
15
16
17uint16_t get_checksum(string to_check){
18 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum
19 uint16_t sum1 = 0;
20 uint16_t sum2 = 0;
21 for( int index = 0; index < to_check.length(); ++index ){
22 sum1 = (sum1 + to_check[index]) % 255;
23 sum2 = (sum2 + sum1) % 255;
24 }
25 return (sum2 << 8) | sum1;
26}
27
4464301d 28void get_checksums(uint16_t check_sums[],string key){
ced5fce8 29 key = key.append(" ");
4464301d
AW
30 check_sums[0] = 0x0000;
31 check_sums[1] = 0x0000;
32 check_sums[2] = 0x0000;
5efaa1b1 33 size_t begin_key = 0;
4464301d 34 char counter = 0;
ced5fce8 35 while( begin_key < key.size()-1 ){
5efaa1b1
L
36 size_t end_key = key.find_first_of(" .", begin_key);
37 string key_node = key.substr(begin_key, end_key - begin_key);
4464301d 38 check_sums[counter] = get_checksum(key_node);
5efaa1b1 39 begin_key = end_key + 1;
4464301d 40 counter++;
5efaa1b1 41 }
5efaa1b1
L
42}
43
4eb9c745
AW
44// Convert to lowercase
45string lc(string str){
46 for (int i=0;i<strlen(str.c_str());i++)
47 if (str[i] >= 0x41 && str[i] <= 0x5A)
48 str[i] = str[i] + 0x20;
49 return str;
50}
51
a699b669
AW
52// Remove non-number characters
53string remove_non_number( string str ){
54 string number_mask = "0123456789-.";
55 size_t found=str.find_first_not_of(number_mask);
56 while (found!=string::npos){
57 //str[found]='*';
58 str.replace(found,1,"");
59 found=str.find_first_not_of(number_mask);
60 }
61 return str;
62}
63
4eb9c745
AW
64// Get the first parameter, and remove it from the original string
65string shift_parameter( string &parameters ){
66 size_t beginning = parameters.find_first_of(" ");
67 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; }
68 string temp = parameters.substr( 0, beginning );
69 parameters = parameters.substr(beginning+1, parameters.size());
70 return temp;
71}
cebe90b6
AW
72
73// Separate command from arguments
74string get_arguments( string possible_command ){
75 size_t beginning = possible_command.find_first_of(" ");
76 if( beginning == string::npos ){ return ""; }
77 return possible_command.substr( beginning+1, possible_command.size() - beginning);
78}
79
8d857d2e
L
80// Returns true if the file exists
81bool file_exists( string file_name ){
82 bool exists = false;
83 FILE *lp = fopen(file_name.c_str(), "r");
84 if(lp){ exists = true; }
85 fclose(lp);
86 return exists;
87}
88
4de76b51
L
89// Prepares and executes a watchdog reset
90void system_reset( void ){
91 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK
92 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4
93 LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second
94 LPC_WDT->WDMOD = 0x3; // Enabled and Reset
95 LPC_WDT->WDFEED = 0xAA; // Kick the dog!
96 LPC_WDT->WDFEED = 0x55;
97}
cebe90b6 98