-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
677 lines (495 loc) · 17.2 KB
/
main.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
// Header files
#include <iostream>
#include <fstream>
#include <cstring>
#include <signal.h>
#include <unistd.h>
#include "printer.h"
using namespace std;
// Global variables
Printer printer;
// Function prototypes
void breakHandler(int signal);
// Main function
int main(int argc, char *argv[]) {
// Initialize variables
string response, line, firmwareRom, inputFile, outputFile;
ifstream file;
bool translate = false, forceFlash = false, settings = false, provided = false, exit = false;
// Attach break handler
signal(SIGINT, breakHandler);
// Display version
cout << "M33 Linux V0.16" << endl << endl;
// Go through all commands
for(uint8_t i = 0; i < argc; i++) {
// Check if help is requested
if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
// Display help
cout << "Usage: m33-linux -v -c -p -w -t -b -l -f -x -r firmware.rom -a -i input.gcode -s -o output.gcode -e -d" << endl;
cout << "-v | --validation: Use validation pre-processor" << endl;
cout << "-p | --preparation: Use preparation pre-processor" << endl;
cout << "-w | --wavebonding: Use wave bonding pre-processor" << endl;
cout << "-t | --thermalbonding: Use thermal bonding pre-processor" << endl;
cout << "-b | --bedcompensation: Use bed compensation pre-processor" << endl;
cout << "-l | --backlashcompensation: Use backlash compensation pre-processor" << endl;
cout << "-f | --feedrateconversion: Use feed rate conversion pre-processor" << endl;
cout << "-c | --centermodel: Use center model pre-processor" << endl;
cout << "-r | --firmwarerom: Use the following parameter as the firmware ROM in case firmware is corrupt or outdated" << endl;
cout << "-x | --exit: Switches printer into firmware mode and exits if firmware is updated" << endl;
cout << "-a | --forceflash: Forces the firmware to update to the provided ROM" << endl;
cout << "-i | --inputfile: Use the following parameter as the G-code file to processes and send to the printer. G-code commands can be manually entered if no G-code file is not provided." << endl;
cout << "-s | --translate: Uses the program as a middle man to communicate between the printer and other software" << endl;
cout << "-o | --outputfile: Use the following parameter as the G-code file to output after the input file has been processed by all the desired pre-processor stages." << endl;
cout << "-e | --settings: Uses values from settings file instead of obtaining them from the printer" << endl;
cout << "-d | --provided: Uses provided values instead of obtaining them from the printer" << endl;
cout << "--backlashX: Provide backlash X" << endl;
cout << "--backlashY: Provide backlash Y" << endl;
cout << "--backlashSpeed: Provide backlash speed" << endl;
cout << "--filamentTemperature: Provide filament temperature" << endl;
cout << "--filamentType: Provide filament type" << endl;
cout << "--bedHeightOffset: Provide bed height offset" << endl;
cout << "--backRightOffset: Provide back right offset" << endl;
cout << "--backLeftOffset: Provide back left offset" << endl;
cout << "--frontLeftOffset: Provide front left offset" << endl;
cout << "--frontRightOffset: Provide front right offset" << endl << endl;
return 0;
}
// Otherwise check if using validation pre-processor
else if(!strcmp(argv[i], "-v") || !strcmp(argv[i], "--validation"))
// Set validation preprocessor
printer.setValidationPreprocessor();
// Otherwise check if using preparation pre-processor
else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--preparation"))
// Set preparation preprocessor
printer.setPreparationPreprocessor();
// Otherwise check if using wave bonding pre-processor
else if(!strcmp(argv[i], "-w") || !strcmp(argv[i], "--wavebonding"))
// Set wave bonding preprocessor
printer.setWaveBondingPreprocessor();
// Otherwise check if using thermal bonding pre-processor
else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--thermalbonding"))
// Set thermal bonding preprocessor
printer.setThermalBondingPreprocessor();
// Otherwise check if using bed compensation pre-processor
else if(!strcmp(argv[i], "-b") || !strcmp(argv[i], "--bedcompensation"))
// Set bed compensation preprocessor
printer.setBedCompensationPreprocessor();
// Otherwise check if using backlash compensation pre-processor
else if(!strcmp(argv[i], "-l") || !strcmp(argv[i], "--backlashcompensation"))
// Set backlash compensation preprocessor
printer.setBacklashCompensationPreprocessor();
// Otherwise check if using feed rate conversion pre-processor
else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--feedrateconversion"))
// Set feed rate conversion preprocessor
printer.setFeedRateConversionPreprocessor();
// Otherwise check if using center model pre-processor
else if(!strcmp(argv[i], "-c") || !strcmp(argv[i], "--centermodel"))
// Set center model preprocessor
printer.setCenterModelPreprocessor();
// Otherwise check if a firmware rom is provided
else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--firmwarerom")) {
// Check if firmware rom parameter exists
if(i < argc - 1)
// Set firmware rom
firmwareRom = argv[++i];
// Otherwise
else {
// Display error
cout << "No firmware ROM provided" << endl;
return 0;
}
}
// Otherwise check if using force flash
else if(!strcmp(argv[i], "-a") || !strcmp(argv[i], "--forceflash"))
// Set force flash
forceFlash = true;
// Otherwise check if using exiting after flash
else if(!strcmp(argv[i], "-x") || !strcmp(argv[i], "--exit"))
// Set exit
exit = true;
// Otherwise check if an input file is provided
else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--inputfile")) {
// Check if input file parameter exists
if(i < argc - 1)
// Set input file
inputFile = argv[++i];
// Otherwise
else {
// Display error
cout << "No input file provided" << endl;
return 0;
}
}
// Otherwise check if an output file is provided
else if(!strcmp(argv[i], "-o") || !strcmp(argv[i], "--outputfile")) {
// Check if output file parameter exists
if(i < argc - 1)
// Set output file
outputFile = argv[++i];
// Otherwise
else {
// Display error
cout << "No output file provided" << endl;
return 0;
}
}
// Otherwise check if using with translate
else if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--translate"))
// Set translate
translate = true;
// Otherwise check if using settings
else if(!strcmp(argv[i], "-e") || !strcmp(argv[i], "--settings"))
// Set settings
settings = true;
// Otherwise check if using provided
else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--provided"))
// Set provided
provided = true;
// Otherwise check if a backlash X is provided
else if(!strcmp(argv[i], "--backlashX")) {
// Check if backlash X parameter exists
if(i < argc - 1)
// Set backlash X
printer.setBacklashX(argv[++i]);
// Otherwise
else {
// Display error
cout << "No backlash X provided" << endl;
return 0;
}
}
// Otherwise check if a backlash Y is provided
else if(!strcmp(argv[i], "--backlashY")) {
// Check if backlash Y parameter exists
if(i < argc - 1)
// Set backlash Y
printer.setBacklashY(argv[++i]);
// Otherwise
else {
// Display error
cout << "No backlash Y provided" << endl;
return 0;
}
}
// Otherwise check if a backlash speed is provided
else if(!strcmp(argv[i], "--backlashSpeed")) {
// Check if backlash speed parameter exists
if(i < argc - 1)
// Set backlash speed
printer.setBacklashSpeed(argv[++i]);
// Otherwise
else {
// Display error
cout << "No backlash speed provided" << endl;
return 0;
}
}
// Otherwise check if a filament type is provided
else if(!strcmp(argv[i], "--filamentType")) {
// Check if filament type parameter exists
if(i < argc - 1)
// Set filament type
printer.setFilamentType(argv[++i]);
// Otherwise
else {
// Display error
cout << "No filament type provided" << endl;
return 0;
}
}
// Otherwise check if a filament temperature is provided
else if(!strcmp(argv[i], "--filamentTemperature")) {
// Check if filament temperature parameter exists
if(i < argc - 1)
// Set filament temperature
printer.setFilamentTemperature(argv[++i]);
// Otherwise
else {
// Display error
cout << "No filament temperature provided" << endl;
return 0;
}
}
// Otherwise check if a bed height offset is provided
else if(!strcmp(argv[i], "--bedHeightOffset")) {
// Check if bed height offset parameter exists
if(i < argc - 1)
// Set bed height offset
printer.setBedHeightOffset(argv[++i]);
// Otherwise
else {
// Display error
cout << "No bed height offset provided" << endl;
return 0;
}
}
// Otherwise check if a back right offset is provided
else if(!strcmp(argv[i], "--backRightOffset")) {
// Check if back right offset parameter exists
if(i < argc - 1)
// Set back right offset
printer.setBackRightOffset(argv[++i]);
// Otherwise
else {
// Display error
cout << "No back right offset provided" << endl;
return 0;
}
}
// Otherwise check if a back left offset is provided
else if(!strcmp(argv[i], "--backLeftOffset")) {
// Check if back left offset parameter exists
if(i < argc - 1)
// Set back left offset
printer.setBackLeftOffset(argv[++i]);
// Otherwise
else {
// Display error
cout << "No back left offset provided" << endl;
return 0;
}
}
// Otherwise check if a front left offset is provided
else if(!strcmp(argv[i], "--frontLeftOffset")) {
// Check if front left offset parameter exists
if(i < argc - 1)
// Set front left offset
printer.setFrontLeftOffset(argv[++i]);
// Otherwise
else {
// Display error
cout << "No front left offset provided" << endl;
return 0;
}
}
// Otherwise check if a front right offset is provided
else if(!strcmp(argv[i], "--frontRightOffset")) {
// Check if front right offset parameter exists
if(i < argc - 1)
// Set front right offset
printer.setFrontRightOffset(argv[++i]);
// Otherwise
else {
// Display error
cout << "No front right offset provided" << endl;
return 0;
}
}
}
// Check if a firmware rom is provided
if(!firmwareRom.empty()) {
// Check if firmware rom doesn't exists
file.open(firmwareRom);
if(!file.good()) {
// Display error
cout << "Firmware ROM doesn't exist" << endl;
return 0;
}
// Go through the file name
uint8_t i = 0;
if(firmwareRom.find(' ') != string::npos)
i = firmwareRom.find(' ') + 1;
for(; i < firmwareRom.size(); i++) {
// Check if extension is occuring
if(firmwareRom[i] == '.') {
// Break if file name beings with 10 numbers
if(firmwareRom.find(' ') != string::npos && i - firmwareRom.find(' ') - 1 == 10)
break;
if(i == 10)
break;
// Display error
cout << "Invalid firmware ROM name" << endl;
return 0;
}
// Check if current character isn't a digit or length is invalid
if(firmwareRom[i] < '0' || firmwareRom[i] > '9' || (i == firmwareRom.size() - 1 && i < 9)) {
// Display error
cout << "Invalid firmware ROM name" << endl;
return 0;
}
}
// Close file
file.close();
}
// Check if an input file is provided
if(!inputFile.empty()) {
// Check if the input file doesn't exists
file.open(inputFile);
if(!file.good()) {
// Display error
cout << "Input file doesn't exist" << endl;
return 0;
}
// Close file
file.close();
}
// Check if an output file is provided without an input file
if(!outputFile.empty() && inputFile.empty()) {
// Display error
cout << "An output file cannot be generated without an input file" << endl;
return 0;
}
// Check if force flash without a rom
if(forceFlash && firmwareRom.empty()) {
// Display error
cout << "Cannot force flash when no ROM is provided" << endl;
return 0;
}
// Check if using output file with settings file
if(!outputFile.empty() && settings) {
// Display message
cout << "Using printer values from settings file" << endl;
// Check if gettings settings from file failed
if(!printer.useSettingsFile()) {
// Display error
cout << "Failed to get settings from file" << endl;
return 0;
}
}
// Otherwise check if using output file with provided values
else if(!outputFile.empty() && provided)
// Display message
cout << "Using printer values that were provided" << endl;
// Otherwise
else {
// Check if not root and not just updating firmware
if(getuid() && (firmwareRom.empty() || !forceFlash || !exit)) {
// Display error
cout << "Elevated privileges required" << endl;
return 0;
}
// Wait for device to be connected
cout << "Attempting to connect to the printer" << endl;
while(!printer.connect())
cout << "Printer not detected" << endl;
// Display message
cout << "Connected to printer" << endl;
// Check if a rom is provided and printer isn't in bootloader mode
if(!firmwareRom.empty() && !printer.isBootloaderMode())
// Enter bootloader mode
printer.sendRequest("M115 S628");
// Check if printer's firmware isn't valid
if(!printer.isFirmwareValid() || forceFlash) {
// Display error
if(!forceFlash)
cout << "Printer's firmware is corrupt" << endl;
// Check if a firmware rom is provided
if(!firmwareRom.empty()) {
// Display message
cout << "Updating firmware" << endl;
// Check if updating printer's firmware failed
if(!printer.updateFirmware(firmwareRom.c_str())) {
// Display error
cout << "Failed to update firmware" << endl;
return 0;
}
// Display message
cout << "Done updating firmware" << endl;
// Put printer into firmware mode and exit if set
if(exit) {
printer.sendRequest("Q");
return 0;
}
}
// Otherwise
else
// Return 0
return 0;
}
// Check if printer's firmware is outdated
if(!firmwareRom.empty() && !printer.getFirmwareVersion().empty() && stoi(printer.getFirmwareVersion()) < stoi(firmwareRom)) {
// Display error
cout << "Printer's firmware is outdated" << endl << "Updating firmware" << endl;
// Check if updating printer's firmware failed
if(!printer.updateFirmware(firmwareRom.c_str())) {
// Display error
cout << "Failed to update firmware" << endl;
return 0;
}
// Display message
cout << "Done updating firmware" << endl;
// Put printer into firmware mode and exit if set
if(exit) {
printer.sendRequest("Q");
return 0;
}
}
// Check if printer'a firmware is incompatible
if(!printer.getFirmwareVersion().empty() && stoi(printer.getFirmwareVersion()) < 2015080602) {
// Display error
cout << "Printer's firmware is incompatible" << endl;
return 0;
}
// Check if collect printer information failed
if(!printer.collectInformation()) {
// Display error
cout << "Failed to collect printer information" << endl;
return 0;
}
// Check if printer Z isn't valid
if(!printer.isZValid()) {
// Display error
cout << "Printer's Z is invalid" << endl << "Calibrating Z" << endl;
// Calibrate Z
printer.calibrateZ();
}
// Check if printer bed orientation isn't valid
if(!printer.isBedOrientationValid()) {
// Display error
cout << "Printer's bed orientation is invalid" << endl << "Calibrating bed orientation" << endl;
// Calibrate bed orientation
printer.calibrateBedOrientation();
}
}
// Check if an output file was provided
if(!outputFile.empty())
// Process file
printer.processFile(inputFile.c_str(), outputFile.c_str());
// Otherwise check if an input file was provided
else if(!inputFile.empty())
// Print file
printer.printFile(inputFile.c_str());
// Otherwise check if translating
else if(translate)
// Enter translator mode
printer.translatorMode();
// Otherwise
else {
// Display message
cout << "Entering manual G-code mode" << endl << "Enter 'quit' to exit" << endl;
// Loop forever
while(1) {
// Get g-code from user
cout << "Enter command: ";
getline(cin, line);
// End if requested
if(line == "quit")
break;
// Send g-code to device
do {
// Get next command if line didn't contain valid g-code
if(!printer.sendRequest(line)) {
cout << "Failed to parse command" << endl << endl;
break;
}
// Display command
cout << "Send: " << line << endl;
// Get valid response
do {
response = printer.receiveResponse();
while(response == "Info:Too small" || response.substr(0, 2) == "T:")
response = printer.receiveResponse();
} while(response.empty());
// Display response
cout << "Receive: " << response << endl << endl;
} while(response.substr(0, 2) != "ok");
}
}
// Return 0
return 0;
}
// Supporting function implementation
void breakHandler(int signal) {
// Exit so that destructor is called on printer
exit(0);
}