added missing files
[clinton/Smoothieware.git] / src / modules / communication / utils / Gcode.cpp
1 #include <string>
2 using std::string;
3 #include "Gcode.h"
4 #include "mbed.h"
5
6 Gcode::Gcode(){}
7
8 // Whether or not a Gcode has a letter
9 bool Gcode::has_letter( char letter ){
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;
17 }
18
19 // Retrieve the value for a given letter
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
32 double Gcode::get_value( char letter ){
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;
53 }