forked from Duet3D/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCodeBuffer.cpp
More file actions
364 lines (320 loc) · 7.93 KB
/
GCodeBuffer.cpp
File metadata and controls
364 lines (320 loc) · 7.93 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* GCodeBuffer.cpp
*
* Created on: 6 Feb 2015
* Author: David
*/
//*************************************************************************************
#include "RepRapFirmware.h"
// This class stores a single G Code and provides functions to allow it to be parsed
GCodeBuffer::GCodeBuffer(Platform* p, const char* id)
{
platform = p;
identity = id;
writingFileDirectory = NULL; // Has to be done here as Init() is called every line.
toolNumberAdjust = 0;
checksumRequired = false;
}
void GCodeBuffer::Init()
{
gcodePointer = 0;
readPointer = -1;
inComment = false;
state = idle;
}
int GCodeBuffer::CheckSum() const
{
int cs = 0;
for (int i = 0; gcodeBuffer[i] != '*' && gcodeBuffer[i] != 0; i++)
{
cs = cs ^ gcodeBuffer[i];
}
cs &= 0xff; // Defensive programming...
return cs;
}
// Add a byte to the code being assembled. If false is returned, the code is
// not yet complete. If true, it is complete and ready to be acted upon.
bool GCodeBuffer::Put(char c)
{
if (c == '\r')
{
// Ignore carriage return, it messes up filenames sometimes if it appears in macro files etc.
// Alternatively, we could handle it in the same way as linefeed, and add an optimisation to ignore blank lines.
return false;
}
if (c == ';')
{
inComment = true;
}
else if (c == '\n' || !c)
{
gcodeBuffer[gcodePointer] = 0;
if (reprap.Debug(moduleGcodes) && gcodeBuffer[0] && !writingFileDirectory) // Don't bother with blank/comment lines
{
platform->Message(HOST_MESSAGE, "%s%s\n", identity, gcodeBuffer);
}
// Deal with line numbers and checksums
if (Seen('*'))
{
int csSent = GetIValue();
int csHere = CheckSum();
Seen('N');
if (csSent != csHere)
{
snprintf(gcodeBuffer, GcodeLength, "M998 P%d", GetIValue());
Init();
return true;
}
// Strip out the line number and checksum
gcodePointer = 0;
while (gcodeBuffer[gcodePointer] != ' ' && gcodeBuffer[gcodePointer])
{
gcodePointer++;
}
// Anything there?
if (!gcodeBuffer[gcodePointer])
{
// No...
gcodeBuffer[0] = 0;
Init();
return false;
}
// Yes...
gcodePointer++;
int gp2 = 0;
while (gcodeBuffer[gcodePointer] != '*' && gcodeBuffer[gcodePointer])
{
gcodeBuffer[gp2] = gcodeBuffer[gcodePointer++];
gp2++;
}
gcodeBuffer[gp2] = 0;
}
else if (checksumRequired)
{
gcodeBuffer[0] = 0;
Init();
return false;
}
Init();
state = executing;
return true;
}
else if (!inComment || writingFileDirectory)
{
gcodeBuffer[gcodePointer++] = c;
if (gcodePointer >= (int)GcodeLength)
{
platform->Message(BOTH_ERROR_MESSAGE, "G-Code buffer length overflow.\n");
gcodePointer = 0;
gcodeBuffer[0] = 0;
}
}
return false;
}
bool GCodeBuffer::Put(const char *str, size_t len)
{
for(size_t i=0; i<=len; i++)
{
if (Put(str[i]))
{
return true;
}
}
return false;
}
// Does this buffer contain any code?
bool GCodeBuffer::IsEmpty() const
{
const char *buf = gcodeBuffer;
while (*buf != 0 && strchr(" \t\n\r", *buf) != nullptr)
{
buf++;
}
return *buf == 0;
}
// Is 'c' in the G Code string?
// Leave the pointer there for a subsequent read.
bool GCodeBuffer::Seen(char c)
{
readPointer = 0;
for (;;)
{
char b = gcodeBuffer[readPointer];
if (b == 0 || b == ';') break;
if (b == c) return true;
++readPointer;
}
readPointer = -1;
return false;
}
// Get a float after a G Code letter found by a call to Seen()
float GCodeBuffer::GetFValue()
{
if (readPointer < 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode float before a search.\n");
readPointer = -1;
return 0.0;
}
float result = (float) strtod(&gcodeBuffer[readPointer + 1], 0);
readPointer = -1;
return result;
}
// Get a :-separated list of floats after a key letter
const void GCodeBuffer::GetFloatArray(float a[], int& returnedLength)
{
int length = 0;
if(readPointer < 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode float array before a search.\n");
readPointer = -1;
returnedLength = 0;
return;
}
bool inList = true;
while(inList)
{
if(length >= returnedLength) // Array limit has been set in here
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode float array that is too long: %s\n", gcodeBuffer);
readPointer = -1;
returnedLength = 0;
return;
}
a[length] = (float)strtod(&gcodeBuffer[readPointer + 1], 0);
length++;
readPointer++;
while(gcodeBuffer[readPointer] && (gcodeBuffer[readPointer] != ' ') && (gcodeBuffer[readPointer] != LIST_SEPARATOR))
{
readPointer++;
}
if(gcodeBuffer[readPointer] != LIST_SEPARATOR)
{
inList = false;
}
}
// Special case if there is one entry and returnedLength requests several.
// Fill the array with the first entry.
if(length == 1 && returnedLength > 1)
{
for(int i = 1; i < returnedLength; i++)
{
a[i] = a[0];
}
}
else
{
returnedLength = length;
}
readPointer = -1;
}
// Get a :-separated list of longs after a key letter
const void GCodeBuffer::GetLongArray(long l[], int& returnedLength)
{
if(readPointer < 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode long array before a search.\n");
readPointer = -1;
return;
}
int length = 0;
bool inList = true;
while(inList)
{
if(length >= returnedLength) // Array limit has been set in here
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode long array that is too long: %s\n", gcodeBuffer);
readPointer = -1;
returnedLength = 0;
return;
}
l[length] = strtol(&gcodeBuffer[readPointer + 1], 0, 0);
length++;
readPointer++;
while(gcodeBuffer[readPointer] && (gcodeBuffer[readPointer] != ' ') && (gcodeBuffer[readPointer] != LIST_SEPARATOR))
{
readPointer++;
}
if(gcodeBuffer[readPointer] != LIST_SEPARATOR)
{
inList = false;
}
}
returnedLength = length;
readPointer = -1;
}
// Get a string after a G Code letter found by a call to Seen().
// It will be the whole of the rest of the GCode string, so strings
// should always be the last parameter.
const char* GCodeBuffer::GetString()
{
if (readPointer < 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode string before a search.\n");
readPointer = -1;
return "";
}
const char* result = &gcodeBuffer[readPointer + 1];
readPointer = -1;
return result;
}
// This returns a pointer to the end of the buffer where a
// string starts. It assumes that an M or G search has
// been done followed by a GetIValue(), so readPointer will
// be -1. It absorbs "M/Gnnn " (including the space) from the
// start and returns a pointer to the next location.
// This is provided for legacy use, in particular in the M23
// command that sets the name of a file to be printed. In
// preference use GetString() which requires the string to have
// been preceded by a tag letter.
const char* GCodeBuffer::GetUnprecedentedString(bool optional)
{
readPointer = 0;
while (gcodeBuffer[readPointer] && gcodeBuffer[readPointer] != ' ')
{
readPointer++;
}
if (!gcodeBuffer[readPointer])
{
readPointer = -1;
if (optional)
{
return NULL;
}
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: String expected but not seen.\n");
return gcodeBuffer; // Good idea?
}
const char* result = &gcodeBuffer[readPointer + 1];
readPointer = -1;
return result;
}
// Get an long after a G Code letter
long GCodeBuffer::GetLValue()
{
if (readPointer < 0)
{
platform->Message(BOTH_ERROR_MESSAGE, "GCodes: Attempt to read a GCode int before a search.\n");
readPointer = -1;
return 0;
}
long result = strtol(&gcodeBuffer[readPointer + 1], 0, 0);
readPointer = -1;
return result;
}
// Return true if this buffer contains a poll request or empty request that can be executed while macros etc. from another source are being completed
bool GCodeBuffer::IsPollRequest()
{
if (state == executing)
{ if (IsEmpty())
{
return true;
}
if (Seen('M'))
{
int i = GetIValue();
return i == 105 || i == 408;
}
}
return false;
}
// End