smoothie port to mbed online compiler (smoothieware.org)
For documentation, license, ..., please check http://smoothieware.org/
This version has been tested with a 3 axis machine
libs/utils.cpp@0:31e91bb0ef3c, 2012-07-31 (annotated)
- Committer:
- scachat
- Date:
- Tue Jul 31 21:11:18 2012 +0000
- Revision:
- 0:31e91bb0ef3c
smoothie port to mbed online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
scachat | 0:31e91bb0ef3c | 1 | /* |
scachat | 0:31e91bb0ef3c | 2 | This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl). |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 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. |
scachat | 0:31e91bb0ef3c | 5 | You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>. |
scachat | 0:31e91bb0ef3c | 6 | */ |
scachat | 0:31e91bb0ef3c | 7 | |
scachat | 0:31e91bb0ef3c | 8 | #include "libs/Kernel.h" |
scachat | 0:31e91bb0ef3c | 9 | #include "libs/utils.h" |
scachat | 0:31e91bb0ef3c | 10 | #include "system_LPC17xx.h" |
scachat | 0:31e91bb0ef3c | 11 | using namespace std; |
scachat | 0:31e91bb0ef3c | 12 | #include <string> |
scachat | 0:31e91bb0ef3c | 13 | using std::string; |
scachat | 0:31e91bb0ef3c | 14 | #include <cstring> |
scachat | 0:31e91bb0ef3c | 15 | |
scachat | 0:31e91bb0ef3c | 16 | |
scachat | 0:31e91bb0ef3c | 17 | uint16_t get_checksum(string to_check){ |
scachat | 0:31e91bb0ef3c | 18 | // From: http://en.wikipedia.org/wiki/Fletcher%27s_checksum |
scachat | 0:31e91bb0ef3c | 19 | uint16_t sum1 = 0; |
scachat | 0:31e91bb0ef3c | 20 | uint16_t sum2 = 0; |
scachat | 0:31e91bb0ef3c | 21 | for( int index = 0; index < to_check.length(); ++index ){ |
scachat | 0:31e91bb0ef3c | 22 | sum1 = (sum1 + to_check[index]) % 255; |
scachat | 0:31e91bb0ef3c | 23 | sum2 = (sum2 + sum1) % 255; |
scachat | 0:31e91bb0ef3c | 24 | } |
scachat | 0:31e91bb0ef3c | 25 | return (sum2 << 8) | sum1; |
scachat | 0:31e91bb0ef3c | 26 | } |
scachat | 0:31e91bb0ef3c | 27 | |
scachat | 0:31e91bb0ef3c | 28 | vector<uint16_t> get_checksums(string key){ |
scachat | 0:31e91bb0ef3c | 29 | key = key.append(" "); |
scachat | 0:31e91bb0ef3c | 30 | vector<uint16_t> check_sums; |
scachat | 0:31e91bb0ef3c | 31 | size_t begin_key = 0; |
scachat | 0:31e91bb0ef3c | 32 | while( begin_key < key.size()-1 ){ |
scachat | 0:31e91bb0ef3c | 33 | size_t end_key = key.find_first_of(" .", begin_key); |
scachat | 0:31e91bb0ef3c | 34 | string key_node = key.substr(begin_key, end_key - begin_key); |
scachat | 0:31e91bb0ef3c | 35 | check_sums.push_back(get_checksum(key_node)); |
scachat | 0:31e91bb0ef3c | 36 | begin_key = end_key + 1; |
scachat | 0:31e91bb0ef3c | 37 | } |
scachat | 0:31e91bb0ef3c | 38 | return check_sums; |
scachat | 0:31e91bb0ef3c | 39 | } |
scachat | 0:31e91bb0ef3c | 40 | |
scachat | 0:31e91bb0ef3c | 41 | // Convert to lowercase |
scachat | 0:31e91bb0ef3c | 42 | string lc(string str){ |
scachat | 0:31e91bb0ef3c | 43 | for (int i=0;i<strlen(str.c_str());i++) |
scachat | 0:31e91bb0ef3c | 44 | if (str[i] >= 0x41 && str[i] <= 0x5A) |
scachat | 0:31e91bb0ef3c | 45 | str[i] = str[i] + 0x20; |
scachat | 0:31e91bb0ef3c | 46 | return str; |
scachat | 0:31e91bb0ef3c | 47 | } |
scachat | 0:31e91bb0ef3c | 48 | |
scachat | 0:31e91bb0ef3c | 49 | // Remove non-number characters |
scachat | 0:31e91bb0ef3c | 50 | string remove_non_number( string str ){ |
scachat | 0:31e91bb0ef3c | 51 | string number_mask = "0123456789-."; |
scachat | 0:31e91bb0ef3c | 52 | size_t found=str.find_first_not_of(number_mask); |
scachat | 0:31e91bb0ef3c | 53 | while (found!=string::npos){ |
scachat | 0:31e91bb0ef3c | 54 | //str[found]='*'; |
scachat | 0:31e91bb0ef3c | 55 | str.replace(found,1,""); |
scachat | 0:31e91bb0ef3c | 56 | found=str.find_first_not_of(number_mask); |
scachat | 0:31e91bb0ef3c | 57 | } |
scachat | 0:31e91bb0ef3c | 58 | return str; |
scachat | 0:31e91bb0ef3c | 59 | } |
scachat | 0:31e91bb0ef3c | 60 | |
scachat | 0:31e91bb0ef3c | 61 | // Get the first parameter, and remove it from the original string |
scachat | 0:31e91bb0ef3c | 62 | string shift_parameter( string ¶meters ){ |
scachat | 0:31e91bb0ef3c | 63 | size_t beginning = parameters.find_first_of(" "); |
scachat | 0:31e91bb0ef3c | 64 | if( beginning == string::npos ){ string temp = parameters; parameters = ""; return temp; } |
scachat | 0:31e91bb0ef3c | 65 | string temp = parameters.substr( 0, beginning ); |
scachat | 0:31e91bb0ef3c | 66 | parameters = parameters.substr(beginning+1, parameters.size()); |
scachat | 0:31e91bb0ef3c | 67 | return temp; |
scachat | 0:31e91bb0ef3c | 68 | } |
scachat | 0:31e91bb0ef3c | 69 | |
scachat | 0:31e91bb0ef3c | 70 | // Separate command from arguments |
scachat | 0:31e91bb0ef3c | 71 | string get_arguments( string possible_command ){ |
scachat | 0:31e91bb0ef3c | 72 | size_t beginning = possible_command.find_first_of(" "); |
scachat | 0:31e91bb0ef3c | 73 | if( beginning == string::npos ){ return ""; } |
scachat | 0:31e91bb0ef3c | 74 | return possible_command.substr( beginning+1, possible_command.size() - beginning); |
scachat | 0:31e91bb0ef3c | 75 | } |
scachat | 0:31e91bb0ef3c | 76 | |
scachat | 0:31e91bb0ef3c | 77 | // Returns true if the file exists |
scachat | 0:31e91bb0ef3c | 78 | bool file_exists( string file_name ){ |
scachat | 0:31e91bb0ef3c | 79 | bool exists = false; |
scachat | 0:31e91bb0ef3c | 80 | FILE *lp = fopen(file_name.c_str(), "r"); |
scachat | 0:31e91bb0ef3c | 81 | if(lp){ exists = true; } |
scachat | 0:31e91bb0ef3c | 82 | fclose(lp); |
scachat | 0:31e91bb0ef3c | 83 | return exists; |
scachat | 0:31e91bb0ef3c | 84 | } |
scachat | 0:31e91bb0ef3c | 85 | |
scachat | 0:31e91bb0ef3c | 86 | // Prepares and executes a watchdog reset |
scachat | 0:31e91bb0ef3c | 87 | void system_reset( void ){ |
scachat | 0:31e91bb0ef3c | 88 | LPC_WDT->WDCLKSEL = 0x1; // Set CLK src to PCLK |
scachat | 0:31e91bb0ef3c | 89 | uint32_t clk = SystemCoreClock / 16; // WD has a fixed /4 prescaler, PCLK default is /4 |
scachat | 0:31e91bb0ef3c | 90 | LPC_WDT->WDTC = 1 * (float)clk; // Reset in 1 second |
scachat | 0:31e91bb0ef3c | 91 | LPC_WDT->WDMOD = 0x3; // Enabled and Reset |
scachat | 0:31e91bb0ef3c | 92 | LPC_WDT->WDFEED = 0xAA; // Kick the dog! |
scachat | 0:31e91bb0ef3c | 93 | LPC_WDT->WDFEED = 0x55; |
scachat | 0:31e91bb0ef3c | 94 | } |
scachat | 0:31e91bb0ef3c | 95 |