-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprinter.cpp
3637 lines (2715 loc) · 105 KB
/
printer.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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Header files
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <queue>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <termios.h>
#include <pwd.h>
#include "printer.h"
// Definitions
// EEPROM offsets
#define EEPROM_BACKLASH_X 0
#define EEPROM_BACKLASH_Y 1
#define EEPROM_BACK_RIGHT_ORIENTATION 2
#define EEPROM_BACK_LEFT_ORIENTATION 3
#define EEPROM_FRONT_LEFT_ORIENTATION 4
#define EEPROM_FRONT_RIGHT_ORIENTATION 5
#define EEPROM_FILAMENT_COLOR 6
#define EEPROM_FILAMENT_TYPE 7
#define EEPROM_FILAMENT_TEMPERATURE 8
#define EEPROM_FILAMENT_AMOUNT 9
#define EEPROM_BACKLASH_EXPANSION_X_PLUS 10
#define EEPROM_BACKLASH_EXPANSION_YL_PLUS 11
#define EEPROM_BACKLASH_EXPANSION_YR_PLUS 12
#define EEPROM_BACKLASH_EXPANSION_YR_MINUS 13
#define EEPROM_BACKLASH_EXPANSION_Z 14
#define EEPROM_BACKLASH_EXPANSION_E 15
#define EEPROM_BACK_LEFT_OFFSET 16
#define EEPROM_BACK_RIGHT_OFFSET 17
#define EEPROM_FRONT_RIGHT_OFFSET 18
#define EEPROM_FRONT_LEFT_OFFSET 19
#define EEPROM_BACKLASH_SPEED 22
#define EEPROM_BED_HEIGHT_OFFSET 32
// Chip details
#define CHIP_NAME ATxmega32C4
#define CHIP_PAGE_SIZE 0x80
#define CHIP_NRWW_SIZE 0x20
#define CHIP_NUMBER_OF_PAGES 0x80
#define CHIP_TOTAL_MEMORY CHIP_NUMBER_OF_PAGES * CHIP_PAGE_SIZE * 2
// Wave bonding settings
#define WAVE_PERIOD 5.0
#define WAVE_PERIOD_QUARTER (WAVE_PERIOD / 4.0)
#define WAVE_SIZE 0.15
// Bed compensation settings
#define LEVELLING_MOVE_X 104.9
#define LEVELLING_MOVE_Y 103.0
#define SEGMENT_LENGTH 2.0
// Feed rate conversion settings
#define MAX_FEED_RATE 60.0001
// Fan types
enum fanTypes {HENGLIXIN = 0x01, LISTENER = 0x02, SHENZHEW = 0x03, NO_FAN = 0xFF};
// Directions
enum directions {POSITIVE, NEGATIVE, NEITHER};
// Print tiers
enum printTiers {LOW, MEDIUM, HIGH};
// Rom decryption and encryption tables
const uint8_t romDecryptionTable[] = {0x26, 0xE2, 0x63, 0xAC, 0x27, 0xDE, 0x0D, 0x94, 0x79, 0xAB, 0x29, 0x87, 0x14, 0x95, 0x1F, 0xAE, 0x5F, 0xED, 0x47, 0xCE, 0x60, 0xBC, 0x11, 0xC3, 0x42, 0xE3, 0x03, 0x8E, 0x6D, 0x9D, 0x6E, 0xF2, 0x4D, 0x84, 0x25, 0xFF, 0x40, 0xC0, 0x44, 0xFD, 0x0F, 0x9B, 0x67, 0x90, 0x16, 0xB4, 0x07, 0x80, 0x39, 0xFB, 0x1D, 0xF9, 0x5A, 0xCA, 0x57, 0xA9, 0x5E, 0xEF, 0x6B, 0xB6, 0x2F, 0x83, 0x65, 0x8A, 0x13, 0xF5, 0x3C, 0xDC, 0x37, 0xD3, 0x0A, 0xF4, 0x77, 0xF3, 0x20, 0xE8, 0x73, 0xDB, 0x7B, 0xBB, 0x0B, 0xFA, 0x64, 0x8F, 0x08, 0xA3, 0x7D, 0xEB, 0x5C, 0x9C, 0x3E, 0x8C, 0x30, 0xB0, 0x7F, 0xBE, 0x2A, 0xD0, 0x68, 0xA2, 0x22, 0xF7, 0x1C, 0xC2, 0x17, 0xCD, 0x78, 0xC7, 0x21, 0x9E, 0x70, 0x99, 0x1A, 0xF8, 0x58, 0xEA, 0x36, 0xB1, 0x69, 0xC9, 0x04, 0xEE, 0x3B, 0xD6, 0x34, 0xFE, 0x55, 0xE7, 0x1B, 0xA6, 0x4A, 0x9A, 0x54, 0xE6, 0x51, 0xA0, 0x4E, 0xCF, 0x32, 0x88, 0x48, 0xA4, 0x33, 0xA5, 0x5B, 0xB9, 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5, 0x27, 0x7D, 0x23};
// Crc seed and table
const uint32_t crc32Seed = 0xFFFFFFFF;
const uint32_t crc32Table[] = {0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D};
// Supporting function implementation
Printer::Printer() {
// Initialize variables
char* tempPath;
// Clear file descriptor and virtual port
fd = -1;
vd = -1;
// Clear status
status = 0;
// Set bootloader mode
bootloaderMode = true;
// Get temp directory
tempPath = getenv("TEMP");
if(tempPath == NULL)
tempPath = getenv("TMP");
if(tempPath == NULL)
tempPath = getenv("TMPDIR");
// Create temporary folder
workingFolderLocation = mkdtemp(const_cast<char *>((static_cast<string>(tempPath == NULL ? P_tmpdir : tempPath) + "/m33-XXXXXX").c_str()));
// Clear all use pre-processor stages
useValidation = false;
usePreparation = false;
useWaveBonding = false;
useThermalBonding = false;
useBedCompensation = false;
useBacklashCompensation = false;
useFeedRateConversion = false;
// Set values to their defaults
backlashX = 0.3;
backlashY = 0.6;
backlashSpeed = 1500;
backRightOffset = 0;
backLeftOffset = 0;
frontLeftOffset = 0;
frontRightOffset = 0;
filamentTemperature = 200;
filamentType = PLA;
}
Printer::~Printer() {
// Close file descriptor if open
if(fd != -1)
close(fd);
// Delete temporary folder
rmdir(workingFolderLocation.c_str());
// Close virtual port descriptor if open
if(vd != -1)
close(vd);
// Delete symbolic link for virtual serial port location
if(!virtualSerialPortLocation.empty())
unlink(virtualSerialPortLocation.c_str());
}
bool Printer::connect() {
// Initialize variables
termios settings;
flock lock;
// Close file descriptor if open
if(fd != -1)
close(fd);
// Attempt to connect for 2 seconds
for(uint8_t i = 0; i < 8; i++) {
// Wait 250 milliseconds
usleep(250000);
// Check if opening device was successful
if((fd = open("/dev/micro_3d", O_RDWR | O_NONBLOCK)) != -1) {
// Create file lock
lock.l_type = F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
// Check if file is already locked by another process
if(fcntl(fd, F_SETLK, &lock) == -1)
return false;
// Set serial protocol to 8n1 with 115200 baud rate
memset(&settings, 0, sizeof(settings));
settings.c_iflag = 0;
settings.c_oflag = 0;
settings.c_cflag= CS8 | CREAD | CLOCAL;
settings.c_lflag = 0;
settings.c_cc[VMIN] = 1;
settings.c_cc[VTIME] = 5;
cfsetospeed(&settings, B115200);
cfsetispeed(&settings, B115200);
// Apply settings
tcsetattr(fd, TCSAFLUSH, &settings);
tcdrain(fd);
// Return true
return true;
}
}
// Return false
return false;
}
bool Printer::isBootloaderMode() {
// Check if printer is connected and receiving commands
if(fd != -1 && sendRequestAscii("M115"))
// Return if in bootloader mode
return bootloaderMode = (receiveResponseAscii()[0] == 'B');
// Return false
return false;
}
string Printer::getFirmwareVersion() {
// Return firmware version
return firmwareVersion;
}
bool Printer::isFirmwareValid() {
// Initialize variables
string response, eepromSerial;
uint32_t chipCrc = 0, eepromCrc = 0, eepromFirmware = 0;
fanTypes eepromFan;
uint8_t fanOffset;
float fanScale;
int32_t *tempPointer;
// Check if printer is connected and receiving commands
if(fd != -1 && sendRequestAscii("M115")) {
// Check if in bootloader mode
if(receiveResponseAscii()[0] == 'B') {
// Request crc from chip
sendRequestAscii('C');
sendRequestAscii('A');
// Get response
response = receiveResponseAscii();
// Get chip crc
for(uint8_t i = 0; i < 4; i++) {
chipCrc <<= 8;
chipCrc += static_cast<uint8_t>(response[i]);
}
// Request eeprom
sendRequestAscii('S');
// Get response
response = receiveResponseAscii();
// Check if failed to read eeprom
if(response.back() != '\r')
// Return false
return false;
// Get eeprom crc
for(uint8_t i = 0; i < 4; i++) {
eepromCrc <<= 8;
eepromCrc += static_cast<uint8_t>(response[i + 4]);
}
// Check if firmware is corrupt
if(chipCrc != eepromCrc)
// Return false
return false;
// Get eeprom firmware
for(int8_t i = 3; i >= 0; i--) {
eepromFirmware <<= 8;
eepromFirmware += static_cast<uint8_t>(response[i]);
}
// Check if firmware is deprecated
if(eepromFirmware < 150994944)
// Return false
return false;
// Set firmware version
firmwareVersion = to_string(eepromFirmware);
// Get eeprom serial
for(uint8_t i = 0; i < 13; i++)
eepromSerial.push_back(response[i + 0x2EF]);
// Get eeprom fan
eepromFan = static_cast<fanTypes>(response[0x2AB]);
// Check if fan needs updating
if(!eepromFan || eepromFan == NO_FAN) {
// Set fan to default
eepromFan = HENGLIXIN;
// Check if device is newer
if(stoi(eepromSerial.substr(2, 6)) >= 150602)
// Set fan to newer
eepromFan = SHENZHEW;
// Set fan offset and scale
if(eepromFan == HENGLIXIN) {
fanOffset = 200;
fanScale = 0.2165354;
}
else if(eepromFan == LISTENER) {
fanOffset = 145;
fanScale = 0.3333333;
}
else {
fanOffset = 82;
fanScale = 0.3843137;
}
tempPointer = reinterpret_cast<int32_t *>(&fanScale);
// Check if saving fan scale to eeprom failed
for(uint8_t i = 0; i < 4; i++)
if(!writeToEeprom(0x2AD + i, *tempPointer >> 8 * i))
// Return false
return false;
// Check if saving fan offset or fan type to eeprom failed
if(!writeToEeprom(0x2AC, fanOffset) || !writeToEeprom(0x2AB, eepromFan))
// Return false
return false;
}
// Check if extruder current needs updating
if((eepromSerial == "BK15033001100" || eepromSerial == "BK15040201050" || eepromSerial == "BK15040301050" || eepromSerial == "BK15040602050" || eepromSerial == "BK15040801050" || eepromSerial == "BK15040802100" || eepromSerial == "GR15032702100" || eepromSerial == "GR15033101100" || eepromSerial == "GR15040601100" || eepromSerial == "GR15040701100" || eepromSerial == "OR15032701100" || eepromSerial == "SL15032601050") && static_cast<uint8_t>(response[0x2E8]) + (static_cast<uint8_t>(response[0x2E9]) << 8) != 500)
// Check if saving extruder current to eeprom to be 500 failed
if(!writeToEeprom(0x2E8, static_cast<uint8_t>(500)) || !writeToEeprom(0x2E9, 500 >> 8))
// Return false
return false;
}
// Return true
return true;
}
// Return false
return false;
}
bool Printer::updateFirmware(const char *file) {
// Initialize variables
string response, romBuffer, temp;
uint32_t chipCrc = 0, eepromCrc = 0, position, romVersion = 0, romCrc = 0;
ifstream romInput;
uint16_t pagesToWrite;
uint8_t decryptedRom[CHIP_TOTAL_MEMORY];
// Check if printer is connected and receiving commands
if(fd != -1 && sendRequestAscii("M115")) {
// Check if in bootloader mode
if(receiveResponseAscii()[0] == 'B') {
// Go through the file name
uint8_t i = 0;
if(strchr(file, ' ') != NULL)
i = strchr(file, ' ') - file + 1;
for(; i < strlen(file); i++) {
// Check if extension is occuring
if(file[i] == '.') {
// Break if file name beings with 10 numbers
if(strchr(file, ' ') != NULL && i - (strchr(file, ' ') - file) - 1 == 10)
break;
if(i == 10)
break;
// Return false
return false;
}
// Check if current character isn't a digit or length is invalid
if(file[i] < '0' || file[i] > '9' || (i == strlen(file) - 1 && i < 9))
// Return false
return false;
}
// Check if opening rom failed
romInput.open(file, ios::in | ios::binary);
if(!romInput.good())
// Return false
return false;
// Set rom version
if(strchr(file, ' ') != NULL)
romVersion = atoi(strchr(file, ' ') + 1);
else
romVersion = atoi(file);
// Read in the encrypted rom
while(romInput.peek() != EOF)
romBuffer.push_back(romInput.get());
romInput.close();
// Check if rom isn't encrypted
if(static_cast<uint8_t>(romBuffer[0]) == 0x0C || static_cast<uint8_t>(romBuffer[0]) == 0xFD) {
// Encrypt the rom
for(uint16_t i = 0; i < romBuffer.length(); i++)
// Check if padding wasn't required
if(i % 2 != 0 || i != romBuffer.length() - 1)
// Encrypt the rom
temp.push_back(romEncryptionTable[static_cast<uint8_t>(romBuffer[i + (i % 2 ? -1 : 1)])]);
// Set encrypted rom
romBuffer = temp;
}
// Check if rom is too big
if(romBuffer.length() > CHIP_TOTAL_MEMORY)
// Return false
return false;
// Request that chip be erased
sendRequestAscii('E');
// Check if chip failed to be erased
do {
response = receiveResponseAscii();
} while(response.empty());
if(response != "\r")
// Return false
return false;
// Send address zero
sendRequestAscii('A');
sendRequestAscii('\x00');
sendRequestAscii('\x00');
// Check if address wasn't acknowledged
if(receiveResponseAscii() != "\r")
// Return false
return false;
// Set pages to write
pagesToWrite = romBuffer.length() / 2 / CHIP_PAGE_SIZE;
if(romBuffer.length() / 2 % CHIP_PAGE_SIZE != 0)
pagesToWrite++;
// Go through all pages to write
for(uint16_t i = 0; i < pagesToWrite; i++) {
// Send write to page request
sendRequestAscii('B');
sendRequestAscii(CHIP_PAGE_SIZE * 2 >> 8);
sendRequestAscii(static_cast<char>(CHIP_PAGE_SIZE * 2));
// Go through all values for the page
for(int j = 0; j < CHIP_PAGE_SIZE * 2; j++) {
// Check if data to be written exists
position = j + CHIP_PAGE_SIZE * i * 2;
if(position < romBuffer.length())
// Send value
sendRequestAscii(romBuffer[position + (position % 2 ? -1 : 1)]);
// Otherwise
else
// Send padding
sendRequestAscii(romEncryptionTable[0xFF]);
}
// Check if chip failed to be flashed
if(receiveResponseAscii() != "\r")
// Return false
return false;
}
// Send address zero
sendRequestAscii('A');
sendRequestAscii('\x00');
sendRequestAscii('\x00');
// Check if address wasn't acknowledged
if(receiveResponseAscii() != "\r")
// Return false
return false;
// Request eeprom
sendRequestAscii('S');
// Get response
response = receiveResponseAscii();
// Check if failed to read eeprom
if(response.back() != '\r')
// Return false
return false;
// Check if section needs to be zeroed out
if(!response[0x2E6] || chipCrc != eepromCrc)
// Check if zeroing out section failed
if(!writeToEeprom(0x08, 0) || !writeToEeprom(0x09, 0) || !writeToEeprom(0x0A, 0) || !writeToEeprom(0x0B, 0))
// Return false
return false;
// Request crc from chip
sendRequestAscii('C');
sendRequestAscii('A');
// Get response
temp = receiveResponseAscii();
// Get chip crc
for(uint8_t i = 0; i < 4; i++) {
chipCrc <<= 8;
chipCrc += static_cast<uint8_t>(temp[i]);
}
// Decrypt the rom
for(uint16_t i = 0; i < CHIP_TOTAL_MEMORY; i++) {
// Check if data exists in the rom
if (i < romBuffer.length()) {
// Check if padding is required
if(i % 2 == 0 && i == romBuffer.length() - 1)
// Put padding
decryptedRom[i] = 0xFF;
// Otherwise
else
// Decrypt the rom
decryptedRom[i] = romDecryptionTable[static_cast<uint8_t>(romBuffer[i + (i % 2 ? -1 : 1)])];
}
// Otherwise
else
// Put padding
decryptedRom[i] = 0xFF;
}
// Get rom crc
romCrc = crc32(0, decryptedRom, CHIP_TOTAL_MEMORY);
// Check if firmware update failed
if(chipCrc != __builtin_bswap32(romCrc))
// Return false
return false;
// Check if zeroing out sections in eeprom failed
if(!writeToEeprom(0x2D6, 0) || !writeToEeprom(0x2D7, 0) || !writeToEeprom(0x2D8, 0) || !writeToEeprom(0x2D9, 0) || !writeToEeprom(0x2DA, 0) || !writeToEeprom(0x2DB, 0) || !writeToEeprom(0x2DC, 0) || !writeToEeprom(0x2DD, 0) || !writeToEeprom(0x2DE, 0) || !writeToEeprom(0x2DF, 0) || !writeToEeprom(0x2E0, 0) || !writeToEeprom(0x2E1, 0) || !writeToEeprom(0x2E2, 0) || !writeToEeprom(0x2E3, 0) || !writeToEeprom(0x2E4, 0) || !writeToEeprom(0x2E5, 0))
// Return false
return false;
// Check if updating firmware version in eeprom failed
for(uint8_t i = 0; i < 4; i++)
if(!writeToEeprom(i, romVersion >> 8 * i))
// Return false
return false;
// Check if updating firmware crc in eeprom failed
for(uint8_t i = 0; i < 4; i++)
if(!writeToEeprom(i + 4, romCrc >> 8 * i))
// Return false
return false;
// Set firmware version
firmwareVersion = to_string(romVersion);
}
// Return true
return true;
}
// Return false
return false;
}
bool Printer::isZValid() {
// Retrurn valid Z
return validZ;
}
void Printer::calibrateZ() {
// Initialize variables
string response;
// Turn off heater
do {
sendRequest("M104 S0");
do {
response = receiveResponse();
while(response.substr(0, 2) == "T:")
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Delay
do {
sendRequest("G4 S10");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Relative mode
do {
sendRequest("G91");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Move to position
do {
sendRequest("G0 Y20 Z2 F150");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Set heater temperature and wait
do {
sendRequest("M109 S150");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Turn off heater
do {
sendRequest("M104 S0");
do {
response = receiveResponse();
while(response.substr(0, 2) == "T:")
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Turn off fan
do {
sendRequest("M106 S0");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Calibrate Z
do {
sendRequest("G30");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Verify results
do {
sendRequest("M577 F0");
do {
response = receiveResponse();
} while(response.empty());
} while(response.substr(0, 2) != "ok");
// Set valid Z
validZ = true;
}
bool Printer::isBedOrientationValid() {
// Retrurn valid bed orientation
return validBedOrientation;
}
void Printer::calibrateBedOrientation() {
// Set valid bed orientation
validBedOrientation = true;
}
bool Printer::collectInformation() {
// Initialize variables
string response;
char character;
uint32_t value;
float *valuePointer;
// Loop forever
while(1) {
// Check if device is already in g-code processing mode
sendRequestAscii("M115");
while(read(fd, &character, 1) == -1);
if(character == 'e')
// Break
break;
// Attempt to put device into g-code processing mode
sendRequestAscii('Q');
// Return false if failed to reconnect
if(!connect())
return false;
}
// Clear bootloader
bootloaderMode = false;
// Catch string errors
try {
// Get device info
sendRequest("M115");
response = receiveResponse();
// Set firmware and serial number
firmwareVersion = response.substr(response.find("FIRMWARE_VERSION:") + 17, response.find(" ", response.find("FIRMWARE_VERSION:")) - response.find("FIRMWARE_VERSION:") - 17);
serialNumber = response.substr(response.find("SERIAL_NUMBER:") + 14);
// Display values
cout << "Firmware Version: " << firmwareVersion << endl;
cout << "Serial Number: " << serialNumber << endl;
// Get back right offset
sendRequest("M619 S" + to_string(EEPROM_BACK_RIGHT_OFFSET));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backRightOffset = *valuePointer;
// Get back left offset
sendRequest("M619 S" + to_string(EEPROM_BACK_LEFT_OFFSET));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backLeftOffset = *valuePointer;
// Get front left offset
sendRequest("M619 S" + to_string(EEPROM_FRONT_LEFT_OFFSET));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
frontLeftOffset = *valuePointer;
// Get front right offset
sendRequest("M619 S" + to_string(EEPROM_FRONT_RIGHT_OFFSET));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
frontRightOffset = *valuePointer;
// Get bed height offset
sendRequest("M619 S" + to_string(EEPROM_BED_HEIGHT_OFFSET));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
bedHeightOffset = *valuePointer;
// Display values
cout << "Back Right Offset: " << backRightOffset << endl;
cout << "Back Left Offset: " << backLeftOffset << endl;
cout << "Front Left Offset: " << frontLeftOffset << endl;
cout << "Front Right Offset: " << frontRightOffset << endl;
cout << "Bed Height Offset: " << bedHeightOffset << endl;
// Get backlash X
sendRequest("M619 S" + to_string(EEPROM_BACKLASH_X));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backlashX = *valuePointer;
// Get backlash Y
sendRequest("M619 S" + to_string(EEPROM_BACKLASH_Y));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backlashY = *valuePointer;
// Get backlash speed
sendRequest("M619 S" + to_string(EEPROM_BACKLASH_SPEED));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backlashSpeed = *valuePointer;
// Check if backlash speed isn't valid
if(backlashSpeed <= 1 || backlashSpeed >= 5000) {
// Set backlash speed to default value
sendRequestBinary("M618 S22 P1153138688");
receiveResponse();
backlashSpeed = 1500;
}
// Display values
cout << "Backlash X: " << backlashX << endl;
cout << "Backlash Y: " << backlashY << endl;
cout << "Backlash Speed: " << backlashSpeed << endl;
// Get back right orientation
sendRequest("M619 S" + to_string(EEPROM_BACK_RIGHT_ORIENTATION));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backRightOrientation = *valuePointer;
// Get back left orientation
sendRequest("M619 S" + to_string(EEPROM_BACK_LEFT_ORIENTATION));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
backLeftOrientation = *valuePointer;
// Get front left orientation
sendRequest("M619 S" + to_string(EEPROM_FRONT_LEFT_ORIENTATION));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
frontLeftOrientation = *valuePointer;
// Get front right orientation
sendRequest("M619 S" + to_string(EEPROM_FRONT_RIGHT_ORIENTATION));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
valuePointer = reinterpret_cast<float *>(&value);
frontRightOrientation = *valuePointer;
// Set valid bed orientation
validBedOrientation = (backRightOrientation != 0 || backLeftOrientation != 0 || frontLeftOrientation != 0 || frontRightOrientation != 0) && backRightOrientation >= -3 && backRightOrientation <= 3 && backLeftOrientation >= -3 && backLeftOrientation <= 3 && frontLeftOrientation >= -3 && frontLeftOrientation <= 3 && frontRightOrientation >= -3 && frontRightOrientation <= 3;
// Display values
cout << "Back Right Orientation: " << backRightOrientation << endl;
cout << "Back Left Orientation: " << backLeftOrientation << endl;
cout << "Front Left Orientation: " << frontLeftOrientation << endl;
cout << "Front Right Orientation: " << frontRightOrientation << endl;
// Get status
sendRequestBinary("M117");
response = receiveResponse();
// Set valid Z and status
validZ = response.find("ZV:1") != string::npos;
status = stoi(response.substr(response.find("S:") + 2));
cout << "Zalid Z: " << validZ << endl;
cout << "Status: " << static_cast<unsigned int>(status) << endl;
// Get filament type
sendRequest("M619 S" + to_string(EEPROM_FILAMENT_TYPE));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
filamentLocation = (value & 0xC0) == 0x00 ? NO_LOCATION : (value & 0xC0) == 0x40 ? INTERNAL : EXTERNAL;
filamentType = (value & 0x3F) < 4 ? static_cast<filamentTypes>(value & 0x3F) : NO_TYPE;
// Get filament color
sendRequest("M619 S" + to_string(EEPROM_FILAMENT_COLOR));
response = receiveResponse();
value = stoi(response.substr(response.find("DT:") + 3));
filamentColor = value <= 0x2C ? static_cast<filamentColors>(value) : OTHER_COLOR;
// Get filament temperature
sendRequest("M619 S" + to_string(EEPROM_FILAMENT_TEMPERATURE));
response = receiveResponse();
filamentTemperature = stoi(response.substr(response.find("DT:") + 3)) + 100;
// Display values
cout << "Filament Location: " << filamentLocation << endl;
cout << "Filament Type: " << filamentType << endl;
cout << "Filament Color: " << filamentColor<< endl;
cout << "Filament Temperature: " << filamentTemperature << endl;
}
// Check if an out of range error has occured
catch(const out_of_range& exception) {
// Return false
return false;
}
// Check if an invalid argument error has occured
catch(const invalid_argument& exception) {
// Return false
return false;
}
// Check if creating settings file failes
if(!createSettingsFile()) {
// Display error
cout << "Could not create settings file" << endl;
return false;
}
//return true
return true;
}
bool Printer::sendRequest(const char *data) {
// Send data based on if in bootloader mode
return bootloaderMode ? sendRequestAscii(data) : sendRequestBinary(data);
}
bool Printer::sendRequest(const string &data) {
// Return if data was sent
return sendRequest(data.c_str());
}
bool Printer::sendRequest(const Gcode &data) {
// Send data based on if in bootloader mode
return bootloaderMode ? sendRequestAscii(data) : sendRequestBinary(data);
}
string Printer::receiveResponse() {
// Send data based on if in bootloader mode
return bootloaderMode ? receiveResponseAscii() : receiveResponseBinary();
}
bool Printer::processFile(const char *inputFile, const char *outputFile) {
// Initialize variables
fstream processedFile;
ifstream input;
ofstream output;
char userName[256];
passwd *pwd;
// Check if opening input and creating processed file wern't successful
input.open(inputFile, ios::in | ios::binary);
processedFile.open(workingFolderLocation + "/output.gcode", ios::out | ios::binary | ios::app);
if(!input.good() || !processedFile.good())
// Return false
return false;
// Read in file
processedFile << input.rdbuf();
processedFile.close();
// Display message
cout << "Processing " << inputFile << endl;
// Use center model preprocessor if set
if(useCenterModel) {
// Check if preprocessor failed
if(!centerModelPreprocessor((workingFolderLocation + "/output.gcode").c_str())) {
// Display error
cout << "Center model pre-processor failed" << endl;
return 0;
}
// Otherwise
else
// Display message
cout << "Center model pre-processor done" << endl;
}
// Check if print dimensions are out of bounds
if(!checkPrintDimensions((workingFolderLocation + "/output.gcode").c_str(), false)) {