Commit | Line | Data |
---|---|---|
4cff3ded AW |
1 | #include <string> |
2 | using std::string; | |
3 | #include "Gcode.h" | |
4 | #include "mbed.h" | |
5 | ||
2bb8b390 | 6 | Gcode::Gcode(){} |
4cff3ded | 7 | |
2bb8b390 | 8 | // Whether or not a Gcode has a letter |
4cff3ded | 9 | bool Gcode::has_letter( char letter ){ |
13e4a3f9 AW |
10 | //return ( this->command->find( letter ) != string::npos ); |
11 | for (size_t i=0; i < this->command.length(); i++){ | |
12 | if( this->command.at(i) == letter ){ | |
13 | return true; | |
14 | } | |
15 | } | |
16 | return false; | |
4cff3ded AW |
17 | } |
18 | ||
2bb8b390 | 19 | // Retrieve the value for a given letter |
13e4a3f9 AW |
20 | //double Gcode::get_value_old( char letter ){ |
21 | // size_t start = this->command.find(letter); | |
22 | // size_t end = this->command.find_first_not_of("1234567890.-", start+1); | |
23 | // if( end == string::npos ){ end = this->command.length()+1; } | |
24 | // string extracted = this->command.substr( start+1, end-start-1 ); | |
25 | // double value = atof(extracted.c_str()); | |
26 | // return value; | |
27 | //} | |
28 | ||
29 | ||
30 | // Retrieve the value for a given letter | |
31 | // We don't use the high-level methods of std::string because they call malloc and it's very bad to do that inside of interrupts | |
4cff3ded | 32 | double Gcode::get_value( char letter ){ |
13e4a3f9 AW |
33 | __disable_irq(); |
34 | for (size_t i=0; i <= this->command.length()-1; i++){ | |
35 | if( letter == this->command.at(i) ){ | |
36 | size_t beginning = i+1; | |
37 | char buffer[20]; | |
38 | for(size_t j=beginning; j <= this->command.length(); j++){ | |
39 | char c; | |
40 | if( j == this->command.length() ){ c = ';'; }else{ c = this->command.at(j); } | |
41 | if( c != '.' && c != '-' && ( c < '0' || c > '9' ) ){ | |
42 | buffer[j-beginning] = '\0'; | |
43 | __enable_irq(); | |
44 | return atof(buffer); | |
45 | }else{ | |
46 | buffer[j-beginning] = c; | |
47 | } | |
48 | } | |
49 | } | |
50 | } | |
51 | __enable_irq(); | |
52 | return 0; | |
4cff3ded | 53 | } |