GcodeDisplay: Use iterator instead of indexing
[clinton/Smoothieware.git] / src / modules / communication / utils / Gcode.cpp
CommitLineData
df27a6a3 1/*
cd011f58
AW
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.
df27a6a3 5 You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
cd011f58
AW
6*/
7
8
4cff3ded
AW
9#include <string>
10using std::string;
11#include "Gcode.h"
838b33b4 12#include "libs/StreamOutput.h"
adc927a8 13#include "utils.h"
838b33b4 14
838b33b4 15#include <stdlib.h>
4cff3ded 16
8b8b3339 17
702023f3
MM
18Gcode::Gcode()
19{
20 queued = g = m = 0;
21 add_nl = false;
22}
23
24Gcode::Gcode(string& command, StreamOutput* stream)
25{
26 queued = g = m = 0;
27 add_nl = false;
28
29 this->command = command;
30 this->stream = stream;
31
32 prepare_cached_values();
33}
4cff3ded 34
2bb8b390 35// Whether or not a Gcode has a letter
4cff3ded 36bool Gcode::has_letter( char letter ){
13e4a3f9 37 //return ( this->command->find( letter ) != string::npos );
f36ddd7d
BG
38 for (std::string::const_iterator c = this->command.cbegin(); c != this->command.cend(); c++) {
39 if( *c == letter ){
13e4a3f9
AW
40 return true;
41 }
42 }
43 return false;
4cff3ded
AW
44}
45
13e4a3f9
AW
46// Retrieve the value for a given letter
47// 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 48double Gcode::get_value( char letter ){
838b33b4 49 //__disable_irq();
13e4a3f9 50 for (size_t i=0; i <= this->command.length()-1; i++){
fbb2bb71 51 const char& c = this->command.at(i);
f36ddd7d 52 if( letter == c ){
13e4a3f9
AW
53 size_t beginning = i+1;
54 char buffer[20];
55 for(size_t j=beginning; j <= this->command.length(); j++){
56 char c;
57 if( j == this->command.length() ){ c = ';'; }else{ c = this->command.at(j); }
58 if( c != '.' && c != '-' && ( c < '0' || c > '9' ) ){
59 buffer[j-beginning] = '\0';
838b33b4 60 //__enable_irq();
df27a6a3 61 return atof(buffer);
13e4a3f9
AW
62 }else{
63 buffer[j-beginning] = c;
64 }
65 }
66 }
67 }
838b33b4 68 //__enable_irq();
df27a6a3 69 return 0;
4cff3ded 70}
e6b5ae25 71
adc927a8
MM
72int Gcode::get_int( char letter )
73{
74 const char* buffer = command.c_str();
75 for (int i = 0; buffer[i]; i++)
76 {
77 if( letter == buffer[i] )
78 {
79 for(int j = i + 1; buffer[j]; j++)
80 {
81 if( is_numeric(buffer[j]) )
82 {
83 const char* endptr = &buffer[j];
fcd27c01 84 int r = strtol(&buffer[j], const_cast<char**>(&endptr), 10);
adc927a8
MM
85 if (endptr > command.c_str())
86 return r;
87 return 0;
88 }
89 else if ( is_whitespace(buffer[j]) == false )
90 return 0;
91 }
92 }
93 }
94 return 0;
95}
96
d8f8bbf3
L
97int Gcode::get_num_args(){
98 int count = 0;
99 for(size_t i=1; i<this->command.length(); i++){
100 if( this->command.at(i) >= 'A' && this->command.at(i) <= 'Z' ){
101 count++;
102 }
103 }
104 return count;
105}
106
e6b5ae25
AW
107void Gcode::prepare_cached_values(){
108 if( this->has_letter('G') ){
109 this->has_g = true;
110 this->g = this->get_value('G');
111 }else{
112 this->has_g = false;
113 }
114 if( this->has_letter('M') ){
115 this->has_m = true;
db453125 116 this->m = this->get_value('M');
e6b5ae25
AW
117 }else{
118 this->has_m = false;
119 }
120}