-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
1001 lines (832 loc) · 30.3 KB
/
mainwindow.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
/***************************************************************************
* Copyright (C) 2009 by open-nandra *
* matej.haulik@gmail.com *
* marek.belisko@open-nandra.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>
#include <QTime>
#include <QProcess>
#include <QDBusConnection>
#include <QtDBus>
#include <QSettings>
#define DEBUG
/* window contructor */
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
QStringList arguments;
QString str;
int max_setup = 3;
this->dev_found = 0;
ui->setupUi(this);
/* setup dialog windows */
setup_win = new setup_dialog();
this->window = new compile_dialog((QDialog*)this);
/* read only text edits*/
ui->UploadTextEdit->setReadOnly(true);
ui->term_text_edit->setReadOnly(true);
/* set up accepted characters for line edit tx in spi*/
QRegExp rx("[0-9a-fA-F.]*");
QRegExpValidator *myValidator = new QRegExpValidator(rx, this);
ui->line_tx_data_spi->setValidator(myValidator);
/* programmer instance */
prog = new programmer();
/* thread for editor instance */
editor_thread = new Thread();
/* user has 2 possibilities to conect usb */
while (max_setup) {
/* usb initialization */
if (prog->init() == 0) {
ui->UploadTextEdit->append("USB device found");
this->dev_found = 1;
break;
} else {
if (max_setup != 1)
QMessageBox::about(this, tr("USB device not found!"),
tr("Please connect a device and press OK."));
}
max_setup--;
}
/* create timer for checking SPI status */
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_spi_status()));
/* start checking spi status device is presented */
if (ui->checkBox->isChecked()) {
if (this->dev_found) {
timer->start(500);
} else {
ui->label_3->setText("No USB device found");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: red; }");
/* disable enter prog button */
ui->EnterProgButton->setDisabled(true);
ui->ResetButton->setDisabled(true);
this->enable_tab_func(false);
}
}
/* set table parameters for system and network registers */
for (int i=0; i < ui->tableWidget->rowCount(); i++) {
ui->tableWidget->setRowHeight(i, 15);
}
ui->tableWidget->resizeColumnsToContents();
//ui->tableWidget->resizeRowsToContents();
for (int i=0; i < ui->tableWidget_2->rowCount(); i++) {
ui->tableWidget_2->setRowHeight(i, 15);
}
ui->tableWidget_2->resizeColumnsToContents();
//ui->tableWidget_2->resizeRowsToContents();
this->file_type = HEX;
readSettings();
this->setup_win->reset();
/* set table parameters for buffers */
for (int i=0; i < ui->table_buff_info->rowCount(); i++) {
ui->table_buff_info->setRowHeight(i, 18);
}
ui->table_buff_info->resizeColumnsToContents();
for (int i=0; i < ui->table_buff_com->rowCount(); i++) {
ui->table_buff_com->setRowHeight(i, 18);
}
ui->table_buff_com->resizeColumnsToContents();
for (int i=0; i < ui->table_buff_RF->rowCount(); i++) {
ui->table_buff_RF->setRowHeight(i, 18);
}
ui->table_buff_RF->resizeColumnsToContents();
/* set table parameters for eeprom */
for (int i=0; i < ui->table_eeprom->rowCount(); i++) {
ui->table_eeprom->setRowHeight(i, 18);
}
ui->table_eeprom->resizeColumnsToContents();
for (int i=0; i < ui->table_app_info->rowCount(); i++) {
ui->table_app_info->setRowHeight(i, 18);
}
ui->table_app_info->resizeColumnsToContents();
/* set table parameters for user ram */
for (int i=0; i < ui->table_user_ram->rowCount(); i++) {
ui->table_user_ram->setRowHeight(i, 18);
}
ui->table_user_ram->resizeColumnsToContents();
for (int i=0; i < ui->table_user_ram_reg->rowCount(); i++) {
ui->table_user_ram_reg->setRowHeight(i, 15);
}
ui->table_user_ram_reg->resizeColumnsToContents();
/* menu connections */
connect(ui->action_Exit, SIGNAL(triggered(bool)), this, SLOT(close()));
connect(ui->actionAbout, SIGNAL(triggered(bool)), this, SLOT(about()));
connect(ui->actionAboutQt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt()));
connect(ui->actionTools_Settings, SIGNAL(triggered(bool)), this, SLOT(toolsSetting()));
/* button connections */
connect(ui->ResetButton, SIGNAL(clicked(bool)), this, SLOT(resetModule()));
connect(ui->EnterProgButton, SIGNAL(clicked(bool)), this, SLOT(enterProgMode()));
/* connections to custom signals */
connect(this, SIGNAL(my_signal()), this, SLOT(test_signal()));
/* default values for MCU */
if(this->setup_win->select_index == 0)
this->mcu = MCU_16F88;
else
this->mcu = MCU_16F886;
if (this->mcu == MCU_16F88)
str.append("MCU 16F88");
else
str.append("MCU 16F886");
ui->label_mcu->setText(str);
/* register signal for HAL via DBus to retrieve
* notifications about usb device status
*/
if (!QDBusConnection::systemBus().isConnected()) {
qDebug() << "Cannot connect to system bus";
return;
}
QDBusConnection::systemBus().connect(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceAdded",
this, SLOT(deviceAdded(const QString &)));
QDBusConnection::systemBus().connect(
"org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
"DeviceRemoved",
this, SLOT(deviceRemoved(const QString &)));
/*FIXME: this is just temporary until debug infos will be released */
ui->TabWidget->removeTab(0);
ui->TabWidget->removeTab(1);
ui->TabWidget->removeTab(0);
ui->TabWidget->removeTab(0);
}
/* when device is disconnected disable some features in tabs
* or vice versa
*/
void MainWindow::enable_tab_func(bool en)
{
if (en) {
ui->send_spi_data->setDisabled(false);
ui->spi_data_tx->setDisabled(false);
ui->btn_teminal_spi_send->setDisabled(false);
} else {
ui->send_spi_data->setDisabled(true);
ui->spi_data_tx->setDisabled(true);
ui->btn_teminal_spi_send->setDisabled(true);
}
}
/* template to retrieve data from HAL */
template<typename T>
T dbusRequest(QDBusInterface &i, const QString &method, const QString ¶m)
{
QDBusReply<T> reply = i.call(method, param);
return reply.value();
}
void MainWindow::deviceAdded(const QString &udi)
{
QDBusInterface i("org.freedesktop.Hal",
udi,
"org.freedesktop.Hal.Device",
QDBusConnection::systemBus());
QString caps = dbusRequest<QString>(i, "GetProperty", "info.subsystem");
int vendor = dbusRequest<int>(i, "GetPropertyInteger", "usb.vendor_id");
qDebug() << "Vendor" << vendor;
int product = dbusRequest<int>(i, "GetPropertyInteger", "usb.product_id");
qDebug() << "Product" << product;
if ((vendor == 0x1de6) && (product == 0x1)) {
qDebug() << "Device added";
if (prog->init()) {
this->dev_found = 1;
} else {
qDebug() << "Device error";
goto end;
}
timer->start();
ui->EnterProgButton->setEnabled(true);
ui->ResetButton->setEnabled(true);
ui->label_3->setStyleSheet("QLabel {}");
ui->label_3->setText("USB device connected");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: green; }");
this->enable_tab_func(true);
}
end:
return;
}
void MainWindow::deviceRemoved(const QString &udi)
{
bool ok;
QString str_vendor = udi.section('_',2,2);
QString str_product = udi.section('_',3,3);
/* this is kind of hack because after remove
* HAL emmit sinal 2 times with different ui
*/
QString str = udi.section('_',5,5);
if (str != "") {
qDebug() << str_vendor << str_product;
if ((str_vendor.toInt(&ok,16) == 0x1de6) && (str_product.toInt(&ok, 16) == 0x1)) {
qDebug() << "Device removed";
/* in case of disconnection usb device */
timer->stop();
prog->release();
ui->EnterProgButton->setDisabled(true);
ui->ResetButton->setDisabled(true);
ui->label_3->setText("USB device disconnected");
ui->label_3->setStyleSheet("QLabel {background-color: red;}");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: red; }");
this->enable_tab_func(false);
}
}
}
void MainWindow::debug(QString str)
{
#ifdef DEBUG
qDebug() << str;
#else
#endif /* DEBUG */
}
void MainWindow::test_signal()
{
}
void MainWindow::toolsSetting()
{
/* show dialog with modality */
if (this->setup_win->exec()) {
switch(this->setup_win->select_index) {
case 0:
this->mcu_16f88();
break;
case 1:
this->mcu_16f886();
break;
default:
qDebug() << "Unsupported mcu type";
}
}
}
void MainWindow::about()
{
QString str = "<h2>Iqrf IDE</h2>" "<p>Version: v";
str.append(VERSION);
str.append("<p>open nandra 2009"
"<p><a href=\"http://open-nandra.com\">open-nandra.com</a>");
/* this for sure could be done easiest way */
QByteArray ba = str.toLatin1();
const char *str1 = ba.data();
QMessageBox::about(this, tr("About QTLiqrf"),
tr(str1));
}
/* reset module command */
void MainWindow::resetModule()
{
prog->reset_module();
/* start SPI status check after module reset */
if (!ui->checkBox->isChecked())
ui->checkBox->setChecked(true);
}
/* mcu type selection for compilation */
void MainWindow::mcu_16f88()
{
this->mcu = MCU_16F88;
QString str;
str.append("MCU 16F88");
ui->label_mcu->setText(str);
/* reread hex file if opened */
if (!prog->parser->hexfile.isEmpty())
if(!prog->parser->read_file(this->mcu)) {
ui->UploadTextEdit->append("Error opening file "+prog->parser->hexfile);
return;
}
}
void MainWindow::mcu_16f886()
{
this->mcu = MCU_16F886;
QString str;
str.append("MCU 16F886");
ui->label_mcu->setText(str);
// reread hex file if opened
if (!prog->parser->hexfile.isEmpty())
if(!prog->parser->read_file(this->mcu)) {
ui->UploadTextEdit->append("Error opening file "+prog->parser->hexfile);
return;
}
}
/*
parse data to retrieve modele info
data[0-3] = module id
data[4] = os version
data[5] = mcu type
data[6-7] = build number
*/
void parse_module_info(unsigned char *data, QString *str_out, QString *str1_out)
{
QString str, str1, t, s ;
int i;
if (data[3] > 0)
str.append("Coordinator: ");
else
str.append("Node: ");
t = str.number((data[4] & 0xF0)>>4, 16);
s = str.number((data[4] & 0x0F), 16);
if(s.size() < 2)
s = "0"+s;
str.append("IQRF OS ver "+t+"."+s);
str1.append("ID:");
for (i = 0; i < 4; i++) {
QString t = str1.number(data[i],16).toUpper();
if (t.size() < 2)
t = "0" + t;
str1.append(t);
}
str1.append(" ");
if (data[5] == 0x02)
str1.append(" MCU PIC16LF88");
else
str1.append(" MCU PIC16LF886");
t = str.number(data[6], 16);
if (t.size() < 2)
t = "0"+t;
s = str.number(data[7], 16);
if (s.size() < 2)
s = "0"+s;
str.append(" (0x"+s+t+")");
*str_out = str;
*str1_out = str1;
}
/* enter programming mode */
void MainWindow::enterProgMode()
{
QString str, str1;
/* maybe needs to be extended for other SPI statuses */
if ((prog->get_status() != NO_MODULE_ON_USB) && (prog->get_status() != SPI_DISABLED)) {
/* stop timer to avoid get data to wrong place */
timer->stop();
/* send some data to negotiate prog. mode */
prog->enter_prog_mode();
/* unti status == programming */
while (prog->get_status() != PROGRAMMING_MODE)
sleep(1);
/* ask for module id (checksums are wrong don't know why)*/
if (!prog->request_module_id()) {
qDebug() << "Internal error : request_module_id";
goto exit;
}
/* read module id and other info */
if (!prog->get_module_id()) {
qDebug() << "Internal error : get_module_id";
goto exit;
}
parse_module_info(prog->module_id, &str, &str1);
ui->label_os->setText(str);
ui->label_mod_id->setText(str1);
/* disable upload button */
ui->EnterProgButton->setDisabled(true);
/* enable open file button */
ui->UploadButton->setStyleSheet("QPushButton {border: 2px groove red; font-weight: bold;}");
ui->UploadButton->setEnabled(true);
}
exit:
return;
}
/* main destructor */
MainWindow::~MainWindow()
{
writeSettings();
delete prog;
delete timer;
delete editor_thread;
delete window;
}
/* timer slot (check for spi status) */
void MainWindow::update_spi_status()
{
int stat = 0, len;
unsigned char buff[35];
memset(buff, 0 , sizeof(buff));
stat = prog->get_status();
switch(stat) {
case 0x00:
ui->label_3->setText("SPI not working");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: grey; }");
break;
case 0x07:
ui->label_3->setText("SPI suspended");
break;
case 0x3F:
ui->label_3->setText("SPI not ready last CRCM O.K.)");
break;
case 0x3E:
ui->label_3->setText("SPI not ready last CRCM error)");
break;
case 0x80:
ui->label_3->setText("SPI ready (communication mode)");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: green; }");
break;
case 0x81:
ui->label_3->setText("SPI ready (programming mode)");
/* if user press on CKUSB02 button it must also trigger
* programming (disable enter prog mode btn, enable upload btn)
*/
ui->EnterProgButton->setDisabled(true);
/* avoid overwriting status */
timer->stop();
break;
case 0x82:
ui->label_3->setText("SPI ready (debugging mode)");
ui->StartDebugButton->setEnabled(true);
ui->StartDebugButton->setStyleSheet("QPushButton { border: 2px groove red;}");
break;
case 0x83:
ui->label_3->setText("SPI not working in background ");
break;
case 0xFF:
ui->label_3->setText("SPI not working (HW error)");
ui->indicatorSPI->setStyleSheet("QLineEdit { background-color: red; }");
break;
default:
if (stat >= 0x40 && stat <= 0x63) {
ui->label_3->setText("SPI data ready");
/* if data are ready print to text array in Terminal label */
len = prog->write_read_spi_data(buff, stat-0x40, false);
if (len > 0) {
buff[len] = '\0';
QString str;
QTime tm;
str.append(tm.currentTime().toString());
str.append(" RxD : \"");
str.append((char *)&buff[0]);
str.append("\"");
ui->term_text_edit->append(str);
}
} else {
printf("Unkown SPI response!\n");
}
break;
}
}
void MainWindow::on_OpenFileButton_clicked()
{
QString selected_type;
// set last used file type
switch (this->file_type) {
case HEX:
selected_type = "Hex file (*.hex)";
break;
case C:
selected_type = "C file (*.c)";
break;
default:
break;
}
// run open file dialog
opened_file = QFileDialog::getOpenFileName( this,
tr("Open file"),
this->directory,
tr("Hex file (*.hex);;C file (*.c)"),
&selected_type );
if (opened_file.isEmpty())
return;
// get directory path to opened file
int dir_index = opened_file.lastIndexOf("/");
QStringRef reference = opened_file.leftRef(dir_index);
this->directory = reference.toString();
/* HEX files */
if (opened_file.endsWith(".hex")) {
prog->parser->hexfile = opened_file;
this->file_type = HEX;
/* read with correct hex format */
if (!prog->parser->read_file(this->mcu)) {
ui->UploadTextEdit->append("Error opening file "+prog->parser->hexfile);
return;
}
ui->ApplicationCheckBox->setEnabled(true);
ui->ApplicationCheckBox->setChecked(true);
QString text = QString::number(prog->parser->flash_size);
ui->ApplicationCheckBox->setText("APPLICATION " + text + " instructions");
ui->EepromCheckBox->setEnabled(true);
ui->EepromCheckBox->setChecked(true);
text = QString::number(prog->parser->app_eeprom_size + prog->parser->usr_eeprom_size);
ui->EepromCheckBox->setText("EEPROM " + text + " bytes");
ui->CompileButton->setDisabled(true);
ui->EditFileButton->setDisabled(true);
// C files
} else if(opened_file.endsWith(".c")) {
this->file_type = C;
ui->UploadButton->setStyleSheet("QPushButton {}");
ui->UploadButton->setDisabled(true);
ui->ApplicationCheckBox->setDisabled(true);
ui->EepromCheckBox->setDisabled(true);
ui->CompileButton->setEnabled(true);
ui->EditFileButton->setEnabled(true);
// Other
} else {
ui->UploadTextEdit->append("Unsupported file format!");
ui->UploadButton->setStyleSheet("QPushButton {}");
ui->UploadButton->setDisabled(true);
ui->ApplicationCheckBox->setDisabled(true);
ui->EepromCheckBox->setDisabled(true);
ui->CompileButton->setDisabled(true);
ui->EditFileButton->setDisabled(true);
ui->file_label->setText(" ");
return;
}
ui->UploadTextEdit->append("Opened file "+opened_file);
ui->file_label->setText(opened_file.rightRef(opened_file.size() - opened_file.lastIndexOf("/") - 1).toString());
}
void MainWindow::on_checkBox_stateChanged(int )
{
if (ui->checkBox->isChecked())
timer->start(500);
else
timer->stop();
}
void MainWindow::on_clear_upload_btn_clicked()
{
ui->UploadTextEdit->clear();
}
void MainWindow::on_pushButton_clicked()
{
ui->term_text_edit->clear();
}
// Compile c program for iqrf
void MainWindow::on_CompileButton_clicked()
{
QProcess compile_process;
QStringList arguments;
QString filename; // name of file being compiled
/* display compilation output in separate window */
this->window->show();
if ((!opened_file.isEmpty()) && (opened_file.endsWith(".c"))) {
// get separate file name and directory
int dir_index = opened_file.lastIndexOf("/");
this->window->write_data("working dir is "+this->directory);
QStringRef reference = opened_file.rightRef(opened_file.size() - dir_index - 1);
filename = reference.toString();
this->window->write_data("file is "+filename);
// setup and run compile process
arguments << this->setup_win->compiler_location;
/* if it's not splitted it's represented like whole string
* what makes problems during compilation
*/
arguments << this->setup_win->compiler_options.split(" ");
if (this->mcu == MCU_16F88)
arguments << "-p16F88";
else
arguments << "-p16F886";
arguments << filename;
this->window->write_data("args are "+arguments.join(" "));
compile_process.setWorkingDirectory(this->directory);
compile_process.start("wine", arguments);
if (!compile_process.waitForStarted()) {
fprintf(stderr, "Compile error.\n");
this->window->write_data("Compilation error. Please check wine and CC5X compiler.");
return;
}
if (!compile_process.waitForFinished())
return;
// print output of compile process
QByteArray output;
output = compile_process.readAllStandardOutput();
this->window->write_data(QString(output));
output.append(compile_process.readAllStandardError());
this->window->write_data(QString(output));
this->window->write_data("\n\n\n");
/* check if compilation contain errors*/
if (output.contains("Error")) {
fprintf(stderr,"Error in compilation ( hex file not created )\n");
} else {
/* this is maybe hack and using same code 2 times */
prog->parser->hexfile = this->directory+"/"+filename.remove(".c").append(".hex");
ui->UploadTextEdit->append("File openned after compilation "+prog->parser->hexfile);
/* read with correct hex format */
if (!prog->parser->read_file(this->mcu)) {
ui->UploadTextEdit->append("Error opening file "+prog->parser->hexfile);
return;
}
ui->ApplicationCheckBox->setEnabled(true);
ui->ApplicationCheckBox->setChecked(true);
QString text = QString::number(prog->parser->flash_size);
ui->ApplicationCheckBox->setText("APPLICATION " + text + " instructions");
ui->EepromCheckBox->setEnabled(true);
ui->EepromCheckBox->setChecked(true);
text = QString::number(prog->parser->app_eeprom_size + prog->parser->usr_eeprom_size);
ui->EepromCheckBox->setText("EEPROM " + text + " bytes");
}
}
}
// start editor
void MainWindow::on_EditFileButton_clicked()
{
if (this->setup_win->editor_location.isEmpty()) {
ui->UploadTextEdit->append("Editor not set!");
return;
}
editor_thread->editor_name = this->setup_win->editor_location;
editor_thread->run(opened_file);
}
// run editor in thread
void Thread::run(QString filename)
{
QProcess editor_process;
QStringList arguments;
if ((!filename.isEmpty()) && (filename.endsWith(".c"))) {
arguments << filename;
editor_process.start(editor_name, arguments);
if (!editor_process.waitForStarted()) {
fprintf(stderr, "Editor error.\n");
return;
}
exec();
/*
if (!editor_process.waitForFinished())
return;
*/
}
}
void MainWindow::on_UploadButton_clicked()
{
int block_count, c, len;
int flash_addr = (this->mcu == MCU_16F88) ? FLASH_BASE_ADDR_16F88 : FLASH_BASE_ADDR_16F886;
int size = 0;
ui->progressBar->setMinimum(0);
ui->progressBar->setMaximum(prog->parser->usr_eeprom_size +
prog->parser->app_eeprom_size +
prog->parser->flash_size);
while (prog->get_status() != PROGRAMMING_MODE)
prog->get_status();
if (prog->parser->usr_eeprom_size) {
if (!prog->send_prog_data(EEPROM_USER, prog->parser->usr_eeprom, prog->parser->usr_eeprom_size,
USR_EEPROM_BASE_ADDR)) {
qDebug() << "Error occured";
return;
}
size += prog->parser->usr_eeprom_size;
ui->progressBar->setValue(size);
}
if (prog->parser->app_eeprom_size) {
if (!prog->send_prog_data(EEPROM_APP, prog->parser->app_eeprom, prog->parser->app_eeprom_size,
APP_EEPROM_BASE_ADDR)) {
qDebug() << "Error occured";
return;
}
size += prog->parser->app_eeprom_size;
ui->progressBar->setValue(size);
}
/* prepare flash data by 32 bytes blocks */
block_count = prog->parser->flash_size / FLASH_BLOCK_SIZE;
if (prog->parser->flash_size % FLASH_BLOCK_SIZE)
block_count++;
for (c = 0; c < block_count; c++) {
if ((prog->parser->flash_size - (c * FLASH_BLOCK_SIZE)) < FLASH_BLOCK_SIZE)
len = prog->parser->flash_size - (c * FLASH_BLOCK_SIZE);
else
len = FLASH_BLOCK_SIZE;
if (!prog->send_prog_data(FLASH_PROG, &prog->parser->flash[c*FLASH_BLOCK_SIZE], len,
flash_addr)) {
qDebug() << "Error occured";
return;
}
size += len;
ui->progressBar->setValue(size);
flash_addr += FLASH_ADDR_STEP;
}
prog->enter_endprog_mode();
/* start timer for checking SPI */
timer->start(500);
prog->reset();
ui->UploadButton->setStyleSheet("QPushButton {}");
ui->UploadButton->setDisabled(true);
ui->EnterProgButton->setEnabled(true);
}
void MainWindow::on_btn_teminal_spi_send_clicked()
{
QString data = ui->spi_data_tx->displayText();
if (data != "") {
/* if data are ready print to text array in Terminal label */
QString str;
QTime tm;
str.append(tm.currentTime().toString());
str.append(" TxD : \"");
str.append(data);
str.append("\"");
ui->term_text_edit->append(str);
QByteArray arr = data.toAscii();
int i = arr.count();
unsigned char *data = (unsigned char *)arr.data();
this->prog->write_read_spi_data(data, i, true);
ui->spi_data_tx->clear();
}
}
void MainWindow::writeSettings()
{
QSettings settings("Open Nandra", "IQRF IDE");
settings.beginGroup("MainWindow");
settings.setValue("compiler location", this->setup_win->compiler_location);
settings.setValue("compiler options", this->setup_win->compiler_options);
settings.setValue("editor location", this->setup_win->editor_location);
settings.setValue("mcu selection", this->setup_win->select_index);
settings.setValue("directory", this->directory);
settings.setValue("file type", this->file_type);
settings.endGroup();
}
void MainWindow::readSettings()
{
QSettings settings("Open Nandra", "IQRF IDE");
settings.beginGroup("MainWindow");
this->setup_win->compiler_location = settings.value("compiler location").toString();
this->setup_win->compiler_options = settings.value("compiler options").toString();
this->setup_win->editor_location = settings.value("editor location").toString();
this->setup_win->select_index = settings.value("mcu selection").toInt();
this->directory = settings.value("directory").toString();
this->file_type = (file_types) settings.value("file type").toInt();
settings.endGroup();
}
/* send and receive raw spi data entered by user in spi test tab*/
void MainWindow::on_send_spi_data_clicked()
{
unsigned char buff[64];
bool ok;
int i = 0, len, temp;
QString data = ui->line_tx_data_spi->displayText();
if (data != "") {
QString str;
str.append(QTime::currentTime().toString());
str.append(" TxD : \"");
str.append(data);
str.append("\"");
ui->spi_text_edit->append(str);
QStringList bytes = data.split(".");
len = bytes.count();
foreach (QString dta, bytes) {
//qDebug() << dta.toInt(&ok,16);
temp = dta.toInt(&ok,16);
if (ok) {
buff[i] = temp;
} else {
qDebug() << "Cannot convert dta.toInt(&ok,16)";
len --;
}
i++;
}
len = this->prog->write_read_test_spi_data(buff, len);
QString str_rx, t, s;
str_rx.append(QTime::currentTime().toString());
str_rx.append(" RxD : \"");
for (i = 0; i < len; i++) {
qDebug() << buff[i];
t = t.setNum(buff[i], 16);
str_rx.append(t);
s.append(t);
if (i < len - 1) {
s.append(".");
str_rx.append(".");
}
}
ui->line_rx_data_spi->setText(s);
str_rx.append("\"");
ui->spi_text_edit->append(str_rx);
ui->line_tx_data_spi->clear();
}
}
void MainWindow::on_btn_crcm_clicked()
{
unsigned char buff[35];
int i = 0, crc;
bool ok;
QString str;
QString data = ui->line_tx_data_spi->displayText();
QStringList bytes = data.split(".");
foreach (QString dta, bytes) {
buff[i] = dta.toInt(&ok,16);
i++;
}
int len = bytes.count();
crc = prog->crc(buff, len);
ui->line_tx_data_spi->insert("." + str.setNum(crc, 16) + ".");
}
void MainWindow::on_btn_add_00_clicked()
{
ui->line_tx_data_spi->insert("00.");
}
void MainWindow::on_spi_m1_clicked()
{
ui->line_tx_data_spi->insert("00.00.00.00.00.00");
}
void MainWindow::on_spi_m2_clicked()
{
ui->line_tx_data_spi->insert("F0.7F.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.00.D0");
}
void MainWindow::on_spi_data_tx_returnPressed()
{
this->on_btn_teminal_spi_send_clicked();//on_btn_teminal_spi_send_clicked();