-
Notifications
You must be signed in to change notification settings - Fork 7
/
gcode.cpp
804 lines (588 loc) · 22.1 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// Header files
#include <cstring>
#include <cmath>
#include "gcode.h"
// Definitions
#define ORDER "NMGXYZE FTSP IJRD"
// Supporting function implementation
Gcode::Gcode() {
// Set parameter value size
parameterValue.resize(strlen(ORDER));
// Set inital data type
dataType = 0x1080;
}
Gcode::Gcode(Gcode &value) {
// Copy data type
value.dataType = this->dataType;
// Copy host command
value.hostCommand = this->hostCommand;
// Copy parameter values
value.parameterValue = this->parameterValue;
// Copy original command
value.originalCommand = this->originalCommand;
}
bool Gcode::parseLine(const char *line) {
// Initialize variables
string command = line, currentValue;
size_t characterOffset;
char parameterIdentifier = 0;
const char *parameterOffset;
// Reset data type
dataType = 0x1080;
// Reset parameter values
parameterValue.clear();
parameterValue.resize(strlen(ORDER));
// Clear host command
hostCommand.clear();
// Check if command contains a checksum
if((characterOffset = command.find('*')) != string::npos)
// Remove checksum
command = command.substr(0, characterOffset);
// Remove leading and trailing whitespace
if((characterOffset = command.find_first_not_of(" \t\n\r")) != string::npos)
command = command.substr(characterOffset);
if((characterOffset = command.find_last_not_of(" \t\n\r")) != string::npos)
command = command.substr(0, characterOffset + 1);
// Set original command
originalCommand = command;
// Check if command contains a comment
if((characterOffset = command.find(';')) != string::npos)
// Remove comment
command = command.substr(0, characterOffset);
// Check if command is empty
if(!command.length())
// Return false
return false;
// Check if command is a host command
if(command[0] == '@') {
// Set host command
hostCommand = command;
// Return true
return true;
}
// Go through data
for(uint8_t i = 0; i <= command.length(); i++) {
// Check if a parameter is detected
if(i == 0 || isupper(command[i]) || command[i] == ' ' || !command[i]) {
// Check if valid value has been obtained for the parameter
if(i && parameterIdentifier != ' ' && (parameterOffset = strchr(ORDER, parameterIdentifier))) {
// Set data type
dataType |= (1 << (parameterOffset - ORDER));
// Store parameter value
parameterValue[parameterOffset - ORDER] = currentValue;
}
// Reset current value
currentValue.clear();
// Check if a string is required
if(parameterIdentifier == 'M' && (parameterValue[1] == "23" || parameterValue[1] == "28" || parameterValue[1] == "29" || parameterValue[1] == "30" || parameterValue[1] == "32" || parameterValue[1] == "117")) {
// Get string data
for(; i < command.length(); i++)
currentValue.push_back(command[i]);
// Remove leading and trailing whitespace
if((characterOffset = currentValue.find_first_not_of(" \t\n\r")) != string::npos)
currentValue = currentValue.substr(characterOffset);
if((characterOffset = currentValue.find_last_not_of(" \t\n\r")) != string::npos)
currentValue = currentValue.substr(0, characterOffset + 1);
// Set string parameter
parameterValue[15] = currentValue;
// Check if string exists
if(!parameterValue[15].empty())
// Set data type
dataType |= (1 << 15);
// Stop parsing line
break;
}
// Set parameter identifier
parameterIdentifier = command[i];
}
// Otherwise check if value isn't whitespace
else if(!isspace(command[i]))
// Get current value
currentValue.push_back(command[i]);
}
// Return if data wasn't empty
return dataType != 0x1080;
}
bool Gcode::parseLine(const string &line) {
// Return parse line
return parseLine(line.c_str());
}
bool Gcode::parseBinary(const char *line) {
// Initialize variables
uint8_t index = 4;
int32_t *tempPointer;
float tempFloat;
// Set data type
dataType = (line[0] & 0xF0) + (line[0] & 0x0F) + (((line[1] & 0xF0) + (line[1] & 0x0F)) << 8) + (((line[2] & 0xF0) + (line[2] & 0x0F)) << 16) + (((line[3] & 0xF0) + (line[3] & 0x0F)) << 24);
// Check if command contains no data
if(!dataType || dataType == 0x1080)
// Return false
return false;
// Reset parameter values
parameterValue.clear();
parameterValue.resize(strlen(ORDER));
// Clear host command
hostCommand.clear();
// Check if command contains a string parameter
if(dataType & (1 << 15))
// Increment parameter location index
index++;
// Check if command contains an N value
if(dataType & 1) {
// Set parameter value
parameterValue[0] = to_string((line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8));
// Increment parameter location index
index += 2;
}
// Check if command contains an M value
if(dataType & (1 << 1)) {
// Set parameter value
parameterValue[1] = to_string((line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8));
// Increment parameter location index
index += 2;
}
// Check if command contains an G value
if(dataType & (1 << 2)) {
// Set parameter value
parameterValue[2] = to_string((line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8));
// Increment parameter location index
index += 2;
}
// Check if command contains an X value
if(dataType & (1 << 3)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[3] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a Y value
if(dataType & (1 << 4)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[4] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a Z value
if(dataType & (1 << 5)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[5] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains an E value
if(dataType & (1 << 6)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[6] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains an F value
if(dataType & (1 << 8)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[8] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a T value
if(dataType & (1 << 9)) {
// Set parameter value
parameterValue[9] = to_string((line[index] & 0xF0) + (line[index] & 0x0F));
// Increment parameter location index
index++;
}
// Check if command contains an S value
if(dataType & (1 << 10)) {
// Set parameter value
parameterValue[10] = to_string((line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24));
// Increment parameter location index
index += 4;
}
// Check if command contains a P value
if(dataType & (1 << 11)) {
// Set parameter value
parameterValue[11] = to_string((line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24));
// Increment parameter location index
index += 4;
}
// Check if command contains an I value
if(dataType & (1 << 16)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[16] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a J value
if(dataType & (1 << 17)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[17] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains an R value
if(dataType & (1 << 18)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[18] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a D value
if(dataType & (1 << 19)) {
// Set parameter value
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
*tempPointer = (line[index] & 0xF0) + (line[index] & 0x0F) + (((line[index + 1] & 0xF0) + (line[index + 1] & 0x0F)) << 8) + (((line[index + 2] & 0xF0) + (line[index + 2] & 0x0F)) << 16) + (((line[index + 3] & 0xF0) + (line[index + 3] & 0x0F)) << 24);
parameterValue[19] = to_string(tempFloat);
// Increment parameter location index
index += 4;
}
// Check if command contains a string parameter
if(dataType & (1 << 15))
// Set string parameter value
for(uint8_t i = 0; i < (line[4] & 0xF0) + (line[4] & 0x0F); i++)
parameterValue[15].push_back(line[index + i]);
// Set original command
originalCommand = getAscii();
// Return true
return true;
}
string Gcode::getOriginalCommand() const {
// Return original command
return originalCommand;
}
vector<uint8_t> Gcode::getBinary() const {
// Initialize request
vector<uint8_t> request;
// Check if host command
if(!hostCommand.empty())
// Set request to host command
for(uint8_t i = 0; i < hostCommand.length(); i++)
request.push_back(hostCommand[i]);
// Otherwise
else {
// Initialize variables
uint16_t sum1 = 0, sum2 = 0;
int32_t tempNumber, *tempPointer;
float tempFloat;
// Fill first four bytes of request to data type
request.push_back(dataType);
request.push_back(dataType >> 8);
request.push_back(dataType >> 16);
request.push_back(dataType >> 24);
// Check if command contains a string parameter
if(!parameterValue[15].empty())
// Set fifth byte of request to string length
request.push_back(parameterValue[15].length());
// Check if command contains an N value
if(!parameterValue[0].empty()) {
// Set 2 byte integer parameter value
tempNumber = stoi(parameterValue[0]);
request.push_back(tempNumber & 0xFF);
request.push_back((tempNumber >> 8) & 0xFF);
}
// Check if command contains an M value
if(!parameterValue[1].empty()) {
// Set 2 byte integer parameter value
tempNumber = stoi(parameterValue[1]);
request.push_back(tempNumber & 0xFF);
request.push_back((tempNumber >> 8) & 0xFF);
}
// CCheck if command contains a G value
if(!parameterValue[2].empty()) {
// Set 2 byte integer parameter value
tempNumber = stoi(parameterValue[2]);
request.push_back(tempNumber & 0xFF);
request.push_back((tempNumber >> 8) & 0xFF);
}
// Check if command contains an X value
if(!parameterValue[3].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[3]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a Y value
if(!parameterValue[4].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[4]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a Z value
if(!parameterValue[5].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[5]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains an E value
if(!parameterValue[6].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[6]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains an F value
if(!parameterValue[8].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[8]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a T value
if(!parameterValue[9].empty()) {
// Set 1 byte integer parameter value
tempNumber = stoi(parameterValue[9]);
request.push_back(tempNumber & 0xFF);
}
// Check if command contains an S value
if(!parameterValue[10].empty()) {
// Set 4 byte integer parameter value
tempNumber = static_cast<int>(round(stod(parameterValue[10])));
request.push_back(tempNumber & 0xFF);
request.push_back((tempNumber >> 8) & 0xFF);
request.push_back((tempNumber >> 16) & 0xFF);
request.push_back((tempNumber >> 24) & 0xFF);
}
// Check if command contains a P value
if(!parameterValue[11].empty()) {
// Set 4 byte integer parameter value
tempNumber = static_cast<int>(round(stod(parameterValue[11])));
request.push_back(tempNumber & 0xFF);
request.push_back((tempNumber >> 8) & 0xFF);
request.push_back((tempNumber >> 16) & 0xFF);
request.push_back((tempNumber >> 24) & 0xFF);
}
// Check if command contains an I value
if(!parameterValue[16].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[16]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a J value
if(!parameterValue[17].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[17]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains an R value
if(!parameterValue[18].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[18]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a D value
if(!parameterValue[19].empty()) {
// Set 4 byte float parameter value
tempFloat = stof(parameterValue[19]);
tempPointer = reinterpret_cast<int32_t *>(&tempFloat);
request.push_back(*tempPointer & 0xFF);
request.push_back((*tempPointer >> 8) & 0xFF);
request.push_back((*tempPointer >> 16) & 0xFF);
request.push_back((*tempPointer >> 24) & 0xFF);
}
// Check if command contains a string
if(!parameterValue[15].empty())
// Set string parameter value
for(uint8_t i = 0; i < parameterValue[15].length(); i++)
request.push_back(parameterValue[15][i]);
// Go through all values
for(uint8_t index = 0; index < request.size(); index++) {
// Set sums
sum1 = (sum1 + request[index]) % 0xFF;
sum2 = (sum1 + sum2) % 0xFF;
}
// Append Fletcher 16 checksum checksum to request
request.push_back(sum1);
request.push_back(sum2);
}
// Return request
return request;
}
string Gcode::getAscii() const {
// Check if host command
if(!hostCommand.empty())
// Return host command
return hostCommand;
// Initialize request
string request;
// Go through all values
for(uint8_t i = 0; i < strlen(ORDER); i++)
// Check if command contains value and value is valid
if(dataType & (1 << i) && 0xF0F7F & (1 << i)) {
// Append parameter identifier and value
request += ORDER[i] + parameterValue[i] + ' ';
// Check if M command contains a string
if(i == 1 && dataType & (1 << 15))
// Append string to request
request += parameterValue[15] + ' ';
}
// Remove last space from request
if(!request.empty())
request.pop_back();
// Return request
return request;
}
uint32_t Gcode::getDataType() const {
// Return data type
return dataType;
}
bool Gcode::hasParameter(char parameter) const {
// Check if parameter isn't a space
if(parameter != ' ') {
// Check if parameter is valid
const char *parameterOffset = strchr(ORDER, parameter);
if(parameterOffset)
// Return if value is set
return dataType & (1 << (parameterOffset - ORDER));
}
// Return false
return false;
}
void Gcode::removeParameter(char parameter) {
// Check if parameter isn't a space
if(parameter != ' ') {
// Check if parameter is valid
const char *parameterOffset = strchr(ORDER, parameter);
if(parameterOffset) {
// Clear data type
dataType &= ~(1 << (parameterOffset - ORDER));
// Clear parameter value
parameterValue[parameterOffset - ORDER].clear();
// Check if command is now empty
if(dataType == 0x1080)
// Clear original command
originalCommand = "";
}
}
}
bool Gcode::hasValue(char parameter) const {
// Check if parameter isn't a space
if(parameter != ' ') {
// Check if parameter is valid
const char *parameterOffset = strchr(ORDER, parameter);
if(parameterOffset)
// Return if parameter's value isn't empty
return !parameterValue[parameterOffset - ORDER].empty();
}
// Return false
return false;
}
string Gcode::getValue(char parameter) const {
// Check if parameter isn't a space
if(parameter != ' ') {
// Check if parameter is valid
const char *parameterOffset = strchr(ORDER, parameter);
if(parameterOffset)
// Return parameter's value
return parameterValue[parameterOffset - ORDER];
}
// Return empty
return "";
}
void Gcode::setValue(char parameter, const string &value) {
// Check if parameter isn't a space
if(parameter != ' ') {
// Check if parameter is valid
const char *parameterOffset = strchr(ORDER, parameter);
if(parameterOffset) {
// Set data type
dataType |= (1 << (parameterOffset - ORDER));
// Set parameter value
parameterValue[parameterOffset - ORDER] = value;
}
}
}
bool Gcode::hasString() const {
// Return if string is set
return dataType & (1 << 15);
}
string Gcode::getString() const {
// Return string
return parameterValue[15];
}
void Gcode::setString(const string &value) {
// Set data type
dataType |= (1 << 15);
// Set string value
parameterValue[15] = value;
}
void Gcode::clear() {
// Set inital data type
dataType = 0x1080;
// Set parameter value size
parameterValue.clear();
parameterValue.resize(strlen(ORDER));
// Clear host command
hostCommand.clear();
// Clear original command
originalCommand.clear();
}
bool Gcode::isHostCommand() const {
// Return if host command is set
return !hostCommand.empty();
}
bool Gcode::isEmpty() const {
// Return if doesn't contain any values
return dataType == 0x1080;
}
Gcode &Gcode::operator=(const Gcode &value) {
// Return self if calling on self
if(this == &value)
return *this;
// Copy data type
dataType = value.dataType;
// Copy host command
hostCommand = value.hostCommand;
// Copy parameter values
parameterValue = value.parameterValue;
// Copy original command
originalCommand = value.originalCommand;
// Return self
return *this;
}
ostream &operator<<(ostream &output, const Gcode &gcode) {
// Return command sent to output
return output << (gcode.isEmpty() ? gcode.getOriginalCommand() : gcode.getAscii());
}