-
Notifications
You must be signed in to change notification settings - Fork 998
/
Gcode.cpp
206 lines (187 loc) · 6.44 KB
/
Gcode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
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.
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.
You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Gcode.h"
#include "libs/StreamOutput.h"
#include "utils.h"
#include <stdlib.h>
#include <algorithm>
// This is a gcode object. It reprensents a GCode string/command, an caches some important values about that command for the sake of performance.
// It gets passed around in events, and attached to the queue ( that'll change )
Gcode::Gcode(const string &command, StreamOutput *stream, bool strip)
{
this->command= strdup(command.c_str());
this->m= 0;
this->g= 0;
this->add_nl= false;
this->stream= stream;
this->millimeters_of_travel = 0.0F;
this->accepted_by_module = false;
prepare_cached_values(strip);
}
Gcode::~Gcode()
{
if(command != nullptr) {
// TODO we can reference count this so we share copies, may save more ram than the extra count we need to store
free(command);
}
}
Gcode::Gcode(const Gcode &to_copy)
{
this->command = strdup(to_copy.command); // TODO we can reference count this so we share copies, may save more ram than the extra count we need to store
this->millimeters_of_travel = to_copy.millimeters_of_travel;
this->has_m = to_copy.has_m;
this->has_g = to_copy.has_g;
this->m = to_copy.m;
this->g = to_copy.g;
this->add_nl = to_copy.add_nl;
this->stream = to_copy.stream;
this->accepted_by_module = false;
this->txt_after_ok.assign( to_copy.txt_after_ok );
}
Gcode &Gcode::operator= (const Gcode &to_copy)
{
if( this != &to_copy ) {
this->command = strdup(to_copy.command); // TODO we can reference count this so we share copies, may save more ram than the extra count we need to store
this->millimeters_of_travel = to_copy.millimeters_of_travel;
this->has_m = to_copy.has_m;
this->has_g = to_copy.has_g;
this->m = to_copy.m;
this->g = to_copy.g;
this->add_nl = to_copy.add_nl;
this->stream = to_copy.stream;
this->txt_after_ok.assign( to_copy.txt_after_ok );
}
this->accepted_by_module = false;
return *this;
}
// Whether or not a Gcode has a letter
bool Gcode::has_letter( char letter ) const
{
for (size_t i = 0; i < strlen(this->command); ++i) {
if( command[i] == letter ) {
return true;
}
}
return false;
}
// Retrieve the value for a given letter
float Gcode::get_value( char letter, char **ptr ) const
{
const char *cs = command;
char *cn = NULL;
for (; *cs; cs++) {
if( letter == *cs ) {
cs++;
float r = strtof(cs, &cn);
if(ptr != nullptr) *ptr= cn;
if (cn > cs)
return r;
}
}
if(ptr != nullptr) *ptr= nullptr;
return 0;
}
int Gcode::get_int( char letter, char **ptr ) const
{
const char *cs = command;
char *cn = NULL;
for (; *cs; cs++) {
if( letter == *cs ) {
cs++;
int r = strtol(cs, &cn, 10);
if(ptr != nullptr) *ptr= cn;
if (cn > cs)
return r;
}
}
if(ptr != nullptr) *ptr= nullptr;
return 0;
}
uint32_t Gcode::get_uint( char letter, char **ptr ) const
{
const char *cs = command;
char *cn = NULL;
for (; *cs; cs++) {
if( letter == *cs ) {
cs++;
int r = strtoul(cs, &cn, 10);
if(ptr != nullptr) *ptr= cn;
if (cn > cs)
return r;
}
}
if(ptr != nullptr) *ptr= nullptr;
return 0;
}
int Gcode::get_num_args() const
{
int count = 0;
for(size_t i = 1; i < strlen(command); i++) {
if( this->command[i] >= 'A' && this->command[i] <= 'Z' ) {
count++;
}
}
return count;
}
// Cache some of this command's properties, so we don't have to parse the string every time we want to look at them
void Gcode::prepare_cached_values(bool strip)
{
char *p= nullptr;
if( this->has_letter('G') ) {
this->has_g = true;
this->g = this->get_int('G', &p);
} else {
this->has_g = false;
}
if( this->has_letter('M') ) {
this->has_m = true;
this->m = this->get_int('M', &p);
} else {
this->has_m = false;
}
if(!strip) return;
// remove the Gxxx or Mxxx from string
if (p != nullptr) {
char *n= strdup(p); // create new string starting at end of the numeric value
free(command);
command= n;
}
}
void Gcode::mark_as_taken()
{
this->accepted_by_module = true;
}
// strip off X Y Z I J K parameters if G0/1/2/3
void Gcode::strip_parameters()
{
if(has_g && g < 4){
// strip the command of the XYZIJK parameters
string newcmd;
char *cn= command;
// find the start of each parameter
char *pch= strpbrk(cn, "XYZIJK");
while (pch != nullptr) {
if(pch > cn) {
// copy non parameters to new string
newcmd.append(cn, pch-cn);
}
// find the end of the parameter and its value
char *eos;
strtof(pch+1, &eos);
cn= eos; // point to end of last parameter
pch= strpbrk(cn, "XYZIJK"); // find next parameter
}
// append anything left on the line
newcmd.append(cn);
// strip whitespace to save even more, this causes problems so don't do it
//newcmd.erase(std::remove_if(newcmd.begin(), newcmd.end(), ::isspace), newcmd.end());
// release the old one
free(command);
// copy the new shortened one
command= strdup(newcmd.c_str());
}
}