forked from am32-firmware/ConfigTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.cpp
2277 lines (1998 loc) · 71.2 KB
/
widget.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
#include "widget.h"
#include "BF_ROOTLOADER.h"
#include "defaults.h"
#include "fourwayif.h"
#include "ui_widget.h"
#include "bluejaymelody.h"
#include <QComboBox>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QScrollBar>
#include <QtSerialPort/QSerialPort>
#include <QSerialPortInfo>
#include <QTextStream>
#include <QTimer>
#include <QTimerEvent>
#include <QTextStream>
Widget::Widget(QWidget *parent)
: QWidget(parent), ui(new Ui::Widget), four_way(new FourWayIF),
RL(new BF_ROOTLOADER),
// msg_console(new OutConsole),
m_serial(new QSerialPort(this)), input_buffer(new QByteArray),
bluejay_tune(new QByteArray), eeprom_buffer(new QByteArray),
music_buffer(new QByteArray) {
ui->setupUi(this);
ui->tabWidget->removeTab(4); // todo make these visible
ui->tabWidget->removeTab(4); // remove led tab for now
this->setWindowTitle("Multi ESC Config Tool 1.86");
serialInfoStuff();
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(serialInfoStuff()));
timer->start(2000);
// connect(m_serial, &QSerialPort::readyRead, this, &Widget::readData); //
// an interrupt for reading serial
// connect(msg_console, &OutConsole::getData, this, &Widget::writeData);
// myButton->seCheckable(true);
hide4wayButtons(true);
hideESCSettings(true);
hideEEPROMSettings(true);
ui->writeBinary->setHidden(true);
ui->VerifyFlash->setHidden(true);
ui->MusicTextEdit->setHidden(true);
ui->uploadMusic->setHidden(true);
ui->passthoughButton->setHidden(true);
ui->endPassthrough->setHidden(true);
// ui->devFrame->setHidden(true);
}
Widget::~Widget() { delete ui; }
void Widget::on_sendButton_clicked() { connectSerial(); }
void Widget::closeSerialPort() {
if (m_serial->isOpen())
m_serial->close();
showStatusMessage(tr("Disconnected"));
}
void Widget::loadBinFile() {
filename = QFileDialog::getOpenFileName(this, tr("Open File"),
"c:", tr("All Files (*.*)"));
// ui->textEdit->setPlainText(filename);
ui->writeBinary->setHidden(false);
// ui->VerifyFlash->setHidden(false);
}
void Widget::showStatusMessage(const QString &message) {
ui->label_2->setText(message);
}
void Widget::serialInfoStuff() {
if (m_serial->isOpen()) {
return;
}
// qInfo("called serial info");
const auto infos = QSerialPortInfo::availablePorts();
// qInfo("number of ports : %d ", infos.size());
if (infos.size() == number_of_ports) {
return;
}
number_of_ports = infos.size();
ui->serialSelectorBox->clear();
ui->serialSelectorBox->addItem("Select Port");
QString s;
for (const QSerialPortInfo &info :
infos) { // here we should add to drop down menu
ui->serialSelectorBox->addItem(info.portName());
// m_serial->setPortName(info.portName());
s = s + QObject::tr("Port: ") + info.portName() + "\n" +
QObject::tr("Location: ") + info.systemLocation() + "\n" +
QObject::tr("Description: ") + info.description() + "\n" +
QObject::tr("Manufacturer: ") + info.manufacturer() + "\n" +
QObject::tr("Serial number: ") + info.serialNumber() + "\n" +
QObject::tr("Vendor Identifier: ") +
(info.hasVendorIdentifier()
? QString::number(info.vendorIdentifier(), 16)
: QString()) +
"\n" + QObject::tr("Product Identifier: ") +
(info.hasProductIdentifier()
? QString::number(info.productIdentifier(), 16)
: QString()) +
"\n";
}
// ui->textEdit->setPlainText(s);
}
void Widget::connectSerial() {
if (ui->checkBox_2->isChecked()) {
four_way->direct = true;
m_serial->setBaudRate(m_serial->Baud19200);
if (ui->tabWidget->count() == 4) {
ui->tabWidget->removeTab(2);
showSingleMotor(true);
}
} else {
ui->tabWidget->insertTab(2, ui->tab_3, "Motor Control");
showSingleMotor(false);
four_way->direct = false;
m_serial->setBaudRate(m_serial->Baud19200);
}
m_serial->setPortName(ui->serialSelectorBox->currentText());
m_serial->setDataBits(m_serial->Data8);
m_serial->setParity(m_serial->NoParity);
m_serial->setStopBits(m_serial->OneStop);
m_serial->setFlowControl(m_serial->NoFlowControl);
if (m_serial->open(QIODevice::ReadWrite)) {
ui->ConnectedButton->setCheckable(true);
ui->ConnectedButton->setChecked(true);
ui->escStatusLabel->setText("Select Motor");
showStatusMessage(tr("Connected to %1 : %2, %3, %4, %5, %6")
.arg(m_serial->portName())
.arg(m_serial->dataBits())
.arg(m_serial->baudRate())
.arg(m_serial->parity())
.arg(m_serial->stopBits())
.arg(m_serial->flowControl()));
hide4wayButtons(false);
QByteArray passthroughenable2; // payload empty here
four_way->passthrough_started = true;
four_way->ack_required = false;
if (four_way->direct == false) {
parseMSPMessage = true;
send_mspCommand(0x68, passthroughenable2);
m_serial->waitForBytesWritten(100);
while (m_serial->waitForReadyRead(100)) {
}
readData();
send_mspCommand(0xf5, passthroughenable2);
m_serial->waitForBytesWritten(100);
}
} else {
QMessageBox::critical(this, tr("Error"), m_serial->errorString());
showStatusMessage(tr("Open error"));
}
}
void Widget::on_disconnectButton_clicked() {
if (m_serial->isOpen()) {
hide4wayButtons(true);
hideESCSettings(true);
hideEEPROMSettings(true);
writeData(four_way->makeFourWayCommand(0x34, 0x00));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
QByteArray data = m_serial->readAll();
}
send_mspCommand(
0x44,
0x00); // reset the FC, otherwise it can hold the last throttle value;
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
QByteArray data = m_serial->readAll();
}
parseMSPMessage = true;
readData();
four_way->passthrough_started = false;
ui->ConnectedButton->setChecked(false);
ui->ConnectedButton->setCheckable(false);
allup();
}
closeSerialPort();
}
void Widget::readInitData() {
QByteArray data = m_serial->readAll();
if(data.size() != 0){
qInfo("read data size next");
if (data.size() > 21) {
data.remove(0, 21);
}
if (data[8] == (char)0x30) {
if (data[4] == (char)0x2b) {
qInfo("G071ESC_2KB_PAGE");
four_way->memory_divider_required_four = true;
four_way->eeprom_address =
0x7e00; // this equals an eeprom address of 0x1f800 126kb
}
if (data[4] == (char)0x1f) {
qInfo("F0ESC_1KB_PAGE");
four_way->memory_divider_required_four = false;
four_way->eeprom_address = 0x7c00; // eeprom address of 0x7c00 31kb
}
if (data[4] == (char)0x35) {
qInfo("F3ESC_2KB_PAGE");
four_way->memory_divider_required_four = false;
four_way->eeprom_address = 0xF800; // eeprom address of 0xf800 62kb
}
ui->escStatusLabel->setText("Connected");
four_way->ESC_connected = true;
hideESCSettings(false);
} else {
hideESCSettings(true);
four_way->ESC_connected = false;
}
}
}
void Widget::readData() {
qInfo("reading");
QByteArray data = m_serial->readAll();
if(data.size() != 0){
if (four_way->passthrough_started) {
qInfo("passthrough started");
}
if (!four_way->passthrough_started) {
qInfo("no-passthrough started");
}
// qInfo("size of data : %d ", data.size());
// QByteArray data_hex_string = data.toHex();
if (four_way->passthrough_started) {
if (four_way->ack_required == true) {
if (data.size() < 3) {
ui->StatusLabel->setText("No Response From ESC");
return;
}
if (four_way->checkCRC(data, data.size())) {
if (data[data.size() - 3] == (char)0x00) { // ACK OK!!
four_way->ack_required = false;
four_way->ack_type = ACK_OK;
qInfo("line 271");
ui->StatusLabel->setText("GOOD ACK FROM IF");
if (data[1] == (char)0x3a) {
// if verifying flash
input_buffer->clear();
for (int i = 0; i < (uint8_t)data[4]; i++) {
input_buffer->append(
data[i + 5]); // first 4 byte are package header
}
qInfo("GOOD ACK FROM ESC -- read");
hideESCSettings(false);
hideEEPROMSettings(false);
// QApplication::processEvents();
}
if (data[1] == (char)0x3b) {
qInfo("GOOD ACK FROM ESC -- WRITE");
}
if (data[1] == (char)0x37) {
hideESCSettings(false);
// qInfo("ID 6: %d",data[6]);
if (data[6] == (char)0x2b) {
qInfo("G071ESC_2KB_PAGE");
four_way->memory_divider_required_four = true;
four_way->eeprom_address =
0x7e00; // this equals an eeprom address of 0x1f800 126kb
}
if (data[6] == (char)0x1f) {
qInfo("F0ESC_1KB_PAGE");
four_way->memory_divider_required_four = false;
four_way->eeprom_address =
0x7c00; // eeprom address of 0x7c00 31kb
}
if (data[6] == (char)0x35) {
qInfo("F3ESC_2KB_PAGE");
four_way->memory_divider_required_four = false;
four_way->eeprom_address =
0xF800; // eeprom address of 0x7c00 62kb
}
ui->escStatusLabel->setText("Connected");
four_way->ESC_connected = true;
}
} else { // bad ack
qInfo("line 319");
if (data[1] == (char)0x37) {
hideESCSettings(true);
four_way->ESC_connected = false;
}
if (data[1] == (char)0x3b) {
qInfo("BAD ACK FROM ESC -- WRITE");
}
hideEEPROMSettings(true);
qInfo("BAD OR NO ACK FROM ESC");
ui->StatusLabel->setText("BAD OR NO ACK FROM IF");
four_way->ack_type = BAD_ACK;
// four_way->ack_required = false;
}
} else {
qInfo("4WAY CRC ERROR");
ui->StatusLabel->setText("BAD OR NO ACK FROM ESC");
four_way->ack_type = CRC_ERROR;
}
} else {
qInfo("no ack required");
}
}
}
if (parseMSPMessage) {
parseMSPMessage = false;
//// if(data.size() == 59){
//// if(data[0] == 0x24 && data[2] == 0x3e){
//// rpm = (uint8_t)data[6] | (uint8_t(data[7])<<8);
//// qInfo("RPM : %d ", rpm);
//// }
//// }
// if(data.size() == 22){
// if(data[0] == char(0x24) && data[2] == char(0x3e)){
// motor1throttle = (uint8_t)data[5] | (uint8_t(data[6])<<8);
// motor2throttle = (uint8_t)data[7] | (uint8_t(data[8])<<8);
// motor3throttle = (uint8_t)data[9] | (uint8_t(data[10])<<8);
// motor4throttle = (uint8_t)data[11] | (uint8_t(data[12])<<8);
// qInfo("Throttle 1 : %d ", motor1throttle);
// qInfo("Throttle 2 : %d ", motor2throttle);
// qInfo("Throttle 3: %d ", motor3throttle);
// qInfo("Throttle 4 : %d ", motor4throttle);
// ui->horizontalSlider->setValue(motor1throttle);
// ui->m1MSPSlider->setValue(motor1throttle);
// ui->m2MSPSlider->setValue(motor2throttle);
// ui->m3MSPSlider->setValue(motor3throttle);
// ui->m4MSPSlider->setValue(motor4throttle);
// QString s = QString::number(motor1throttle);
// ui->lineEdit->setText(s);
// }
// }
}
}
void Widget::writeData(const QByteArray &data) { m_serial->write(data); }
void Widget::on_sendMessageButton_clicked() {
// const QByteArray data = ui->plainTextEdit->toPlainText().toLocal8Bit();
// writeData(data);
}
uint8_t Widget::mspSerialChecksumBuf(uint8_t checksum, const uint8_t *data,
int len) {
while (len-- > 0) {
checksum ^= *data++;
}
return checksum;
}
void Widget::on_pushButton_clicked() {
four_way->ack_required = true;
writeData(four_way->makeFourWayCommand(0x3f, 0x04));
}
void Widget::on_pushButton_2_clicked() {
four_way->ack_required = true;
writeData(four_way->makeFourWayCommand(0x37, 0x00));
}
void Widget::on_passthoughButton_clicked() {
hide4wayButtons(false);
QByteArray passthroughenable2; // payload empty here
four_way->passthrough_started = true;
parseMSPMessage = false;
send_mspCommand(0xf5, passthroughenable2);
}
void Widget::on_horizontalSlider_sliderMoved(int position) {
char highByteThrottle = (position >> 8) & 0xff;
;
char lowByteThrottle = position & 0xff;
QString s = QString::number(position);
ui->lineEdit->setText(s);
// 24 4d 3c 10 d6 d0 07 d0 07 d0 07 d0 07 00 00 00 00 00 00 00 00 c6
QByteArray sliderThrottle;
sliderThrottle.append((char)lowByteThrottle); // motor 1
sliderThrottle.append((char)highByteThrottle); //
sliderThrottle.append((char)lowByteThrottle); // motor 2
sliderThrottle.append((char)highByteThrottle);
sliderThrottle.append((char)lowByteThrottle);
sliderThrottle.append((char)highByteThrottle);
sliderThrottle.append((char)lowByteThrottle);
sliderThrottle.append((char)highByteThrottle);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
sliderThrottle.append((char)0x00);
if (ui->checkBox->isChecked()) {
send_mspCommand(0xd6, sliderThrottle);
m_serial->waitForBytesWritten(200);
}
}
void Widget::on_serialSelectorBox_currentTextChanged(const QString &arg1) {}
void Widget::send_mspCommand(uint8_t cmd, QByteArray payload) {
QByteArray mspMsgOut;
mspMsgOut.append((char)0x24);
mspMsgOut.append((char)0x4d);
mspMsgOut.append((char)0x3c);
mspMsgOut.append((char)payload.length());
mspMsgOut.append((char)cmd);
if (payload.length() > 0) {
mspMsgOut.append(payload);
}
uint8_t checksum = 0;
for (int i = 3; i < mspMsgOut.length(); i++) {
checksum ^= mspMsgOut[i];
}
mspMsgOut.append((char)checksum);
writeData(mspMsgOut);
}
QByteArray Widget::convertFromHex() {
QFile inputHex(filename);
uint16_t last_address = 0;
uint16_t last_size = 0;
QByteArray rawData;
if (inputHex.open(QIODevice::ReadOnly)) {
QTextStream in(&inputHex);
while (!in.atEnd()) {
QString line = in.readLine();
QByteArray lineArray;
uint16_t crc = 0;
for (int i = 1; i < line.size(); i = i + 2) {
QString word = line.at(i);
word.append(line.at(i + 1));
uint16_t num = word.toLongLong(nullptr, 16);
crc = crc + num;
// qInfo("byte: %d", num);
lineArray.append(num);
}
qInfo("crc line %d", crc);
if (crc % 256) {
qInfo("crc error");
ui->StatusLabel->setText("CRC ERROR IN HEX FILE!");
break;
}
// 0 size
// 1 address high
// 2 adress low
// 3 data type
uint16_t data_type = (char)lineArray.at(3);
if (data_type == (char)0x00) { // data
uint16_t address = (((uint8_t)lineArray.at(1) << 8) & 0xffff) |
((uint8_t)lineArray.at(2) & 0xff);
// qInfo("address: %d", address);
// qInfo("last size: %d", last_size);
if ((address - last_address > last_size) && (last_size > 0)) {
for (int i = 0; i < address - last_address - last_size; i++) {
rawData.append(char(0x00));
}
}
last_address = address;
last_size = (char)lineArray.at(0);
lineArray.remove(0, 4);
lineArray.chop(1);
rawData.append(lineArray);
}
}
inputHex.close();
}
return rawData;
}
void Widget::resetESC() {
if (four_way->direct) {
QByteArray reset;
reset.append(char(0x00));
reset.append(char(0x00));
reset.append(char(0x00));
reset.append(char(0x00));
writeData(reset);
} else {
writeData(four_way->makeFourWayCommand(0x35, four_way->connected_motor));
}
}
void Widget::on_loadBinary_clicked() { loadBinFile(); }
void Widget::on_writeBinary_clicked() {
uint16_t chunk_size = 128;
if (four_way->ESC_connected == false) {
ui->StatusLabel->setText("NOT CONNECTED");
return;
}
four_way->ack_required = true;
if(eeprom_buffer->size() != 0){
QByteArray eeprom_out;
for (int i = 0; i < 48; i++) {
eeprom_out.append(eeprom_buffer->at(i));
}
eeprom_out[0] = 0x00;
if (four_way->direct) {
sendDirect(eeprom_out, 48, four_way->eeprom_address);
chunk_size = 128;
} else {
writeData(four_way->makeFourWayWriteCommand(eeprom_out, 48,
four_way->eeprom_address));
chunk_size = 256;
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(1000)) {
}
readData();
}
if (four_way->ack_required == false) { // good ack received from esc
ui->escStatusLabel->setText("WRITE EEPROM SUCCESSFUL");
} else {
ui->escStatusLabel->setText("Unable to set safety bit");
return;
}
}
QFileInfo fileInfo(filename);
QByteArray line;
QString ext = fileInfo.suffix(); // ext = "tar.gz"
if (ext == "hex") {
qInfo("hex");
line = convertFromHex();
} else if (ext == "bin") {
QFile inputFile(filename);
qInfo("bin");
inputFile.open(QIODevice::ReadOnly);
line = inputFile.readAll();
// qInfo("size of original: %d", line.size());
inputFile.close();
} else {
ui->StatusLabel->setText("NOT A VALID FILE");
ui->escStatusLabel->setText("Select a .bin or .hex file");
return;
}
uint32_t sizeofBin = line.size();
uint16_t index = 0;
ui->progressBar->setValue(0);
uint8_t pages = sizeofBin / 2048;
// uint8_t bytes_in_last_page = sizeofBin % 1024;
uint8_t max_retries = 8;
uint8_t retries = 0;
for (int i = 0; i <= pages;
i++) { // for each page ( including partial page at end)
for (int j = 0; j < 2048 / chunk_size; j++) { // 8 or 16 buffers per page
QByteArray onetwentyeight;
// for debugging limit to 50
for (int k = 0; k < chunk_size; k++) { // transfer 256 bytes each buffer
onetwentyeight.append(line.at(k + (i * 2048) + (j * chunk_size)));
index++;
if (index >= sizeofBin) {
break;
}
}
four_way->ack_required = true;
// four_way->ack_received = false;
retries = 0;
while (four_way->ack_required) {
if (four_way->memory_divider_required_four) {
if (four_way->direct) {
sendDirect(onetwentyeight, onetwentyeight.size(),
(4096 + (i * 2048) + (j * chunk_size)) >> 2);
} else {
writeData(four_way->makeFourWayWriteCommand(
onetwentyeight, onetwentyeight.size(),
(4096 + (i * 2048) + (j * chunk_size)) >> 2));
}
} else {
if (four_way->direct) {
sendDirect(onetwentyeight, onetwentyeight.size(),
(4096 + (i * 2048) + (j * chunk_size)));
} else {
writeData(four_way->makeFourWayWriteCommand(
onetwentyeight, onetwentyeight.size(),
4096 + (i * 2048) +
(j * chunk_size))); // increment address every i and j
}
}
if (!four_way->direct) {
m_serial->waitForBytesWritten(200);
while (m_serial->waitForReadyRead(100)) {
}
readData();
}
retries++;
if (retries > max_retries) { // after 8 tries to get an ack
break;
}
}
if (four_way->ack_type == BAD_ACK) {
ui->escStatusLabel_2->setText("FLASH FAILURE");
return; //
}
if (four_way->ack_type == CRC_ERROR) {
ui->escStatusLabel_2->setText("FLASH FAILURE");
return;
// index = index -(256*j);
// i--;// go back to beggining of page to erase in case data
// has been written.
break;
}
ui->progressBar->setValue((index * 100) / sizeofBin);
QApplication::processEvents();
if (index >= sizeofBin) {
ui->escStatusLabel_2->setText("FLASH SUCCESS");
ui->progressBar->setValue(0);
four_way->ack_required = true;
QByteArray another_eeprom_out;
if(eeprom_buffer->size() != 0) {
for (int i = 0; i < 48; i++) {
another_eeprom_out.append(eeprom_buffer->at(i));
}
another_eeprom_out[00] = 0x01;
if (ui->crawlerFlash->isChecked()) {
sendFirstEeprom(1);
resetESC();
return;
}else{
if ((eeprom_buffer->at(2) ==
char(0x00))) { // no eeprom ever sent, will be set to zero at
// beggining of flash.
sendFirstEeprom(0);
resetESC();
return;
} else {
if (four_way->direct) {
sendDirect(another_eeprom_out, 48, four_way->eeprom_address);
} else {
writeData(four_way->makeFourWayWriteCommand(
another_eeprom_out, 48, four_way->eeprom_address));
m_serial->waitForBytesWritten(1000);
while (m_serial->waitForReadyRead(1000)) {
}
// QByteArray data = m_serial->readAll();
// four_way->ack_required = false;
readData();
}
}
if (four_way->ack_required == false) { // good ack received from esc
ui->escStatusLabel->setText("WRITE EEPROM SUCCESSFUL");
writeMusic();
return;
} else {
ui->escStatusLabel->setText("Unable to set safety bit");
return;
}
// resetESC();
break;
}
}
}
}
}
qInfo("what is going on? size : %d ", sizeofBin);
}
bool Widget::getMusic() {
four_way->ack_required = true;
if (four_way->direct) {
writeData(RL->setAddress(four_way->eeprom_address + 48));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
QByteArray data = m_serial->readAll();
if (data[data.size() - 1] == char(0x30)) {
qInfo("good ack !!!!");
} else {
return false;
}
writeData(RL->readFlash(128));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
QByteArray music = m_serial->readAll();
music.remove(0, 4);
// qInfo("size of music : %d ", music.size());
if (music[music.size() - 1] == char(0x30)) {
qInfo("good ack read !!!!");
} else {
return false;
}
if ((uint8_t)music.at(0) == 0xFF) {
musicBufferFull = false;
} else {
musicBufferFull = true;
}
music_buffer->clear();
for (int i = 0; i < music.size() - 2; i++) {
music_buffer->append(music[i]);
}
} else {
while (four_way->ack_required) {
writeData(
four_way->makeFourWayReadCommand(128, four_way->eeprom_address + 48));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(1000)) {
}
readData();
}
if ((uint8_t)input_buffer->at(0) == 0xFF) {
musicBufferFull = false;
} else {
for (int i = 0; i < 128; i++) {
music_buffer->append(input_buffer->at(i));
}
musicBufferFull = true;
}
}
return true;
}
bool Widget::writeMusic() {
if (musicBufferFull) {
QByteArray musicBufferOut;
for (int i = 0; i < 128; i++) {
musicBufferOut.append(music_buffer->at(i));
}
if (four_way->direct) {
sendDirect(musicBufferOut, 128, four_way->eeprom_address + 48);
} else {
writeData(four_way->makeFourWayWriteCommand(
musicBufferOut, 128, four_way->eeprom_address + 48));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
readData();
}
if (four_way->ack_required == false) { // good ack received from esc
ui->escStatusLabel->setText("WRITE EEPROM + SUCCESSFUL");
return true;
} else {
return false;
}
} else {
return false;
}
}
void Widget::on_VerifyFlash_clicked() {
QFile inputFile(filename);
inputFile.open(QIODevice::ReadOnly);
// QTextStream in(&inputFile);
QByteArray line = inputFile.readAll();
inputFile.close();
uint16_t bin_size = line.size();
uint16_t K128Chunks = bin_size / 128;
uint32_t index = 0;
for (int i = 0; i < K128Chunks + 1; i++) {
retries = 0;
four_way->ack_required = true;
while (four_way->ack_required) {
if (four_way->memory_divider_required_four) {
writeData(
four_way->makeFourWayReadCommand(128, (4096 + (i * 128)) >> 2));
} else {
writeData(four_way->makeFourWayReadCommand(128, 4096 + (i * 128)));
}
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
readData();
retries++;
if (retries > max_retries) { // after 8 tries to get an ack
return;
}
}
for (int j = 0; j < input_buffer->size(); j++) {
if (input_buffer->at(j) == line.at(j + i * 128)) {
qInfo("the same! index : %d", index);
index++;
if (index >= bin_size) {
qInfo("all memory verified in flash memory");
break;
}
} else {
qInfo("data error in flash memory");
return;
}
}
ui->progressBar->setValue((index * 100) / bin_size);
QApplication::processEvents();
}
}
bool Widget::connectMotor(uint8_t motor) {
uint16_t buffer_length = 48;
ui->escStatusLabel->setText("Connecting to ESC...");
ui->escStatusLabel_2->setText("Connecting to ESC...");
QApplication::processEvents();
four_way->ack_required = true;
retries = 0;
// while(four_way->ack_required){
if (four_way->direct) {
uint8_t init[21] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0x0D, 'B', 'L', 'H', 'e', 'l', 'i', 0xF4, 0x7D};
QByteArray BootInit;
for (int i = 0; i < 21; i++) {
BootInit.append(init[i]);
}
writeData(BootInit);
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
readInitData();
if (four_way->ESC_connected == false) {
qInfo("not connected");
ui->escStatusLabel->setText("Can Not Connect");
ui->escStatusLabel_2->setText("Can Not Connect");
return false;
}
writeData(RL->setAddress(four_way->eeprom_address));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
QByteArray data = m_serial->readAll();
if (data[data.size() - 1] == char(0x30)) {
qInfo("good ack !!!!");
} else {
return false;
}
writeData(RL->readFlash(48));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(500)) {
}
QByteArray flash = m_serial->readAll();
if(flash.size() == 55){
flash.remove(0, 4);
}
qInfo("size of flash : %d ", flash.size());
if (flash[flash.size() - 1] == char(0x30)) {
qInfo("good ack read !!!!");
} else {
return false;
}
if (RL->checkCRC(flash, flash.size() - 1)) { // last byte ack 0x30
qInfo("GOOD crc FROM ESC -- read");
hideESCSettings(false);
hideEEPROMSettings(false);
ui->sendFirstEEPROM->setHidden(false);
ui->crawler_default_button->setHidden(false);
input_buffer->clear();
for (int i = 0; i < flash.size() - 2; i++) {
input_buffer->append(flash[i]);
}
}
} else {
writeData(four_way->makeFourWayCommand(0x37, motor));
m_serial->waitForBytesWritten(100);
while (m_serial->waitForReadyRead(200)) {
}
readData();
while (m_serial->waitForReadyRead(50)) {
}
if (four_way->ESC_connected == false) {
qInfo("not connected");
ui->escStatusLabel->setText("Can Not Connect");
ui->escStatusLabel_2->setText("Can Not Connect");
return false;
}
four_way->ack_required = true;
while (four_way->ack_required) {
writeData(four_way->makeFourWayReadCommand(buffer_length,
four_way->eeprom_address));
m_serial->waitForBytesWritten(500);
while (m_serial->waitForReadyRead(1000)) {
}
readData();
retries++;
if (retries > max_retries / 4) { // after 8 tries to get an ack
return false;
}
}
}
//
qInfo("inputBUFFERAT0 : %d ", input_buffer->at(0));
//return false;
if ((input_buffer->at(0) == (char)0xFF)) {
QMessageBox::warning( // no settings area found
this, tr("Application Name"),
tr("Boot bit set to 0xFF"));
return 0;
}
if ((input_buffer->at(0) == (char)0x01)) {
if ((input_buffer->at(1) == (char)0xFF) ||
(input_buffer->at(2) ==
(char)0x00)) { // if eeprom version is 0xff it means boot bit is set
// but no defaults have been sent
QMessageBox::warning( // also if bootloader version is 00 it means it has
// been written