Stéphane Cachat
/
Smoothie
smoothie port to mbed online compiler (smoothieware.org)
Embed:
(wiki syntax)
Show/hide line numbers
utils.cpp
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 #include "libs/Kernel.h" 00009 #include "libs/utils.h" 00010 #include "system_LPC17xx.h" 00011 using namespace std; 00012 #include <string> 00013 using std::string; 00014 #include <cstring> 00015 00016 00017 uint16_t get_checksum(string to_check){ 00018 // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum 00019 uint16_t sum1 = 0; 00020 uint16_t sum2 = 0; 00021 for( int index = 0; index < to_check.length(); ++index ){ 00022 sum1 = (sum1 + to_check[index]) % 255; 00023 sum2 = (sum2 + sum1) % 255; 00024 } 00025 return (sum2 << 8) | sum1; 00026 } 00027 00028 vector<uint16_t> get_checksums(string key){ 00029 key = key.append(" "); 00030 vector<uint16_t> check_sums; 00031 size_t begin_key = 0; 00032 while( begin_key < key.size()-1 ){ 00033 size_t end_key = key.find_first_of(" .", begin_key); 00034 string key_node = key.substr(begin_key, end_key - begin_key); 00035 check_sums.push_back(get_checksum(key_node)); 00036 begin_key = end_key + 1; 00037 } 00038 return check_sums; 00039 } 00040 00041 // Convert to lowercase 00042 string lc(string str){ 00043 for (int i=0;i<strlen(str.c_str());i++) 00044 if (str[i] >= 0x41 && str[i] <= 0x5A) 00045 str[i] = str[i] + 0x20; 00046 return str; 00047 } 00048 00049 // Remove non-number characters 00050 string remove_non_number( string str ){ 00051 string number_mask = "0123456789-."; 00052 size_t found=str.find_first_not_of(number_mask); 00053 while (found!=string::npos){ 00054 //str[found]='*'; 00055 str.replace(found,1,""); 00056 found=str.find_first_not_of(number_mask); 00057 } 00058 return str; 00059 } 00060 00061 // Get the first parameter, and remove it from the original string 00062 string shift_parameter( string ¶meters ){ 00063 size_t beginning = parameters.find_first_of(" "); 00064 if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; } 00065 string temp = parameters.substr( 0, beginning ); 00066 parameters = parameters.substr(beginning+1, parameters.size()); 00067 return temp; 00068 } 00069 00070 // Separate command from arguments 00071 string get_arguments( string possible_command ){ 00072 size_t beginning = possible_command.find_first_of(" "); 00073 if( beginning == string::npos ){ return ""; } 00074 return possible_command.substr( beginning+1, possible_command.size() - beginning); 00075 } 00076 00077 // Returns true if the file exists 00078 bool file_exists( string file_name ){ 00079 bool exists = false; 00080 FILE *lp = fopen(file_name.c_str(), "r"); 00081 if(lp){ exists = true; } 00082 fclose(lp); 00083 return exists; 00084 } 00085 00086 // Prepares and executes a watchdog reset 00087 void system_reset( void ){ 00088 LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK 00089 uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 00090 LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second 00091 LPC_WDT->WDMOD = 0x3; // Enabled and Reset 00092 LPC_WDT->WDFEED = 0xAA; // Kick the dog! 00093 LPC_WDT->WDFEED = 0x55; 00094 } 00095
Generated on Tue Jul 12 2022 14:14:42 by 1.7.2