-
Notifications
You must be signed in to change notification settings - Fork 17
/
gcode.cpp
executable file
·375 lines (272 loc) · 8.58 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Header files
extern "C" {
#include <asf.h>
}
#include <string.h>
#include "common.h"
#include "gcode.h"
// Definitions
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
#define PARAMETER_ORDER PSTR("gmtspxyzfen")
#else
#define PARAMETER_ORDER "gmtspxyzfen"
#endif
// Function prototypes
/*
Name: Is whitespace
Purpose: Returns if a provided character is whitespace
*/
inline bool isWhitespace(char value) noexcept;
// Supporting function implementation
bool isWhitespace(char value) noexcept {
// Return if character is whitespace
return value == ' ' || (value >= '\t' && value <= '\r');
}
void Gcode::parseCommand(const char *command) noexcept {
// Set that command has been parsed
isParsed = true;
// Clear command parameters
commandParameters = 0;
// Remove leading whitespace
const char *firstValidCharacter = command;
for(; isWhitespace(*firstValidCharacter); ++firstValidCharacter);
// Get last valid character
const char *lastValidCharacter = firstValidCharacter;
for(; *lastValidCharacter && *lastValidCharacter != ';' && *lastValidCharacter != '*' && *lastValidCharacter != '\n'; ++lastValidCharacter);
// Remove trailing white space
for(--lastValidCharacter; lastValidCharacter >= firstValidCharacter && isWhitespace(*lastValidCharacter); --lastValidCharacter);
// Check if command is empty
if(++lastValidCharacter != firstValidCharacter) {
// Set start and stop parsing offsets
uint8_t startParsingOffset = firstValidCharacter - command;
uint8_t stopParsingOffset = lastValidCharacter - command;
// Check if command is a host command
if(*firstValidCharacter == '@') {
// Check if host commands are enabled
#if ENABLE_HOST_COMMANDS == true
// Set command length
uint8_t commandLength = min(static_cast<uint8_t>(stopParsingOffset - startParsingOffset - 1), sizeof(hostCommand) - 1);
// Check if host command isn't empty
if(commandLength) {
// Save host command
strncpy(hostCommand, firstValidCharacter + 1, commandLength);
hostCommand[commandLength] = 0;
// Set command parameters
commandParameters |= PARAMETER_HOST_COMMAND_OFFSET;
}
#endif
}
// Otherwise
else {
// Go through each valid character in the command
for(uint8_t i = startParsingOffset; i < stopParsingOffset; ++i) {
// Check if character is a valid parameter
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
const char *parameterIndex = strchr_P(PARAMETER_ORDER, lowerCase(command[i]));
#else
const char *parameterIndex = strchr(PARAMETER_ORDER, lowerCase(command[i]));
#endif
if(parameterIndex) {
// Check if parameter hasn't been obtained yet
#if STORE_CONSTANTS_IN_PROGRAM_SPACE == true
gcodeParameterOffset parameterBit = 1 << (parameterIndex - reinterpret_cast<PGM_P>(pgm_read_ptr(PARAMETER_ORDER)));
#else
gcodeParameterOffset parameterBit = 1 << (parameterIndex - PARAMETER_ORDER);
#endif
if(!(commandParameters & parameterBit)) {
// Save parameter value
char *lastParameterCharacter;
switch(parameterBit) {
case PARAMETER_G_OFFSET:
valueG = strtoull(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_M_OFFSET:
valueM = strtoull(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_T_OFFSET:
valueT = strtoull(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_S_OFFSET:
valueS = strtoll(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_P_OFFSET:
valueP = strtoll(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_X_OFFSET:
valueX = strtof(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_Y_OFFSET:
valueY = strtof(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_Z_OFFSET:
valueZ = strtof(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_F_OFFSET:
valueF = strtof(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_E_OFFSET:
valueE = strtof(&command[++i], &lastParameterCharacter);
break;
case PARAMETER_N_OFFSET:
default:
valueN = strtoull(&command[++i], &lastParameterCharacter);
}
// Check if parameter exists
if(lastParameterCharacter != &command[i]) {
// Set command parameters
commandParameters |= parameterBit;
// Set index
i = lastParameterCharacter - command;
}
// Decrement index
--i;
}
}
}
// Check if command contains a checksum
const char *checksumCharacter = strchr(lastValidCharacter, '*');
if(checksumCharacter) {
// Check if checksum exists
char *lastChecksumCharacter;
uint8_t providedChecksum = strtoull(++checksumCharacter, &lastChecksumCharacter);
if(lastChecksumCharacter != checksumCharacter) {
// Calculate checksum
uint8_t calculatedChecksum = 0;
for(uint8_t i = 0; command[i] != '*'; ++i)
calculatedChecksum ^= command[i];
// Set valid checksum
if(calculatedChecksum == providedChecksum)
commandParameters |= VALID_CHECKSUM_OFFSET;
}
}
}
}
}
void Gcode::clearCommand() noexcept {
// Set values to defaults
isParsed = false;
commandParameters = 0;
valueG = 0;
valueM = 0;
valueT = 0;
valueS = 0;
valueP = 0;
valueX = 0;
valueY = 0;
valueZ = 0;
valueF = 0;
valueE = 0;
valueN = 0;
// Check if host commands are enabled
#if ENABLE_HOST_COMMANDS == true
// Clear host command
*hostCommand = 0;
#endif
}
bool Gcode::isEmpty() const noexcept {
// Return if command hasn't been parsed
return !isParsed;
}
bool Gcode::hasParameterG() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_G_OFFSET;
}
uint8_t Gcode::getParameterG() const noexcept {
// Return parameter's value
return valueG;
}
bool Gcode::hasParameterM() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_M_OFFSET;
}
uint16_t Gcode::getParameterM() const noexcept {
// Return parameter's value
return valueM;
}
bool Gcode::hasParameterT() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_T_OFFSET;
}
uint8_t Gcode::getParameterT() const noexcept {
// Return parameter's value
return valueT;
}
bool Gcode::hasParameterS() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_S_OFFSET;
}
int32_t Gcode::getParameterS() const noexcept {
// Return parameter's value
return valueS;
}
bool Gcode::hasParameterP() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_P_OFFSET;
}
int32_t Gcode::getParameterP() const noexcept {
// Return parameter's value
return valueP;
}
bool Gcode::hasParameterX() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_X_OFFSET;
}
float Gcode::getParameterX() const noexcept {
// Return parameter's value
return valueX;
}
bool Gcode::hasParameterY() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_Y_OFFSET;
}
float Gcode::getParameterY() const noexcept {
// Return parameter's value
return valueY;
}
bool Gcode::hasParameterZ() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_Z_OFFSET;
}
float Gcode::getParameterZ() const noexcept {
// Return parameter's value
return valueZ;
}
bool Gcode::hasParameterF() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_F_OFFSET;
}
float Gcode::getParameterF() const noexcept {
// Return parameter's value
return valueF;
}
bool Gcode::hasParameterE() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_E_OFFSET;
}
float Gcode::getParameterE() const noexcept {
// Return parameter's value
return valueE;
}
bool Gcode::hasParameterN() const noexcept {
// Return is parameter is set
return commandParameters & PARAMETER_N_OFFSET;
}
uint64_t Gcode::getParameterN() const noexcept {
// Return parameter's value
return valueN;
}
// Check if host commands are enabled
#if ENABLE_HOST_COMMANDS == true
bool Gcode::hasHostCommand() const noexcept {
// Return is host command is set
return commandParameters & PARAMETER_HOST_COMMAND_OFFSET;
}
const char *Gcode::getHostCommand() const noexcept {
// Return host command
return hostCommand;
}
#endif
bool Gcode::hasValidChecksum() const noexcept {
// Return if checksum is valid
return commandParameters & VALID_CHECKSUM_OFFSET;
}