forked from CieNTi/serial_port_plotter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
1996 lines (1696 loc) · 72.5 KB
/
mainwindow.cpp
File metadata and controls
1996 lines (1696 loc) · 72.5 KB
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
/***************************************************************************
** This file is part of Serial Port Plotter Ver 2022 **
** **
** **
** Serial Port Plotter is a program for plotting integer data from **
** serial port using Qt and QCustomPlot **
** **
** 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 3 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, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Borislav **
** Contact: b.kereziev@gmail.com **
** Date: 29.12.14 **
****************************************************************************/
#include "mainwindow.hpp"
#include "ui_mainwindow.h"
#include <x86intrin.h>
#include "dialog.hpp"
#include "ui_mainwindow.h"
#include <QDialog>
#include <QMessageBox>
#include <QPixmap>
// #include <QLineEdit>
// import QtQuick;.Extras 1.4;
/**
* @brief Constructor
* @param parent
*/
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow),
// int spinPoints=600;
/* Populate colors */
line_colors{
/* For channel data (gruvbox palette) */
/* Light */
QColor("#ED1A1A"), // Power TOP
QColor("#6030F0"), // Power BOT
QColor("#EDE255"), // Power PCB
QColor("#F08922"), // Temp TOP
QColor("#C837F0"), // Temp BOT
QColor("#54F06C"), // Temp PCB
QColor("#F76E1B"), // Diff TOP
QColor("#B231F7"), // Diff BOT
QColor("#75F759"), // Diff PCB
QColor("#C74A20"), // Profile TOP
QColor("#7F32C7"), // Profile BOT
QColor("#699a32"), // Profile PCB
QColor("#689d6a"),
QColor("#d65d0e"),
},
gui_colors{
/* Monochromatic for axes and ui */
QColor(48, 47, 47, 255), /**< 0: qdark ui dark/background color */
QColor(80, 80, 80, 255), /**< 1: qdark ui medium/grid color */
QColor(170, 170, 170, 255), /**< 2: qdark ui light/text color */
QColor(48, 47, 47,
200) /**< 3: qdark ui dark/background color w/transparency */
},
/* Main vars */
connected(false), plotting(false), dataPointNumber(0), channels(0),
serialPort(nullptr), STATE(WAIT_START), NUMBER_OF_POINTS(600) {
// Dialog *dialog=new Dialog();
ui->setupUi(this);
dialog = new Dialog(this);
dialog->setWindowTitle("Settings");
dialog->setModal(false);
/* Init UI and populate UI controls */
createUI();
/* Setup plot area and connect controls slots */
setupPlot();
/* Wheel over plot when plotting */
connect(ui->plot, SIGNAL(mouseWheel(QWheelEvent *)), this,
SLOT(on_mouse_wheel_in_plot(QWheelEvent *)));
connect (ui->plot->xAxis, SIGNAL(rangeChanged(QCPRange)), this, SLOT(slotRangeChanged(QCPRange)));
/* Slot for printing coordinates */
connect(ui->plot, SIGNAL(mouseMove(QMouseEvent *)), this,
SLOT(onMouseMoveInPlot(QMouseEvent *)));
/* Channel selection */
connect(ui->plot, SIGNAL(selectionChangedByUser()), this,
SLOT(channel_selection()));
connect(ui->plot,
SIGNAL(legendDoubleClick(QCPLegend *, QCPAbstractLegendItem *,
QMouseEvent *)),
this,
SLOT(legend_double_click(QCPLegend *, QCPAbstractLegendItem *,
QMouseEvent *)));
/* Connect update timer to replot slot */
connect(&updateTimer, SIGNAL(timeout()), this, SLOT(replot()));
connect(dialog, SIGNAL(buttonPressed()), this, SLOT(profilread()));
connect(dialog, SIGNAL(buttonPressed1()), this,
SLOT(on_pushButton_RIGHT_clicked()));
connect(dialog, SIGNAL(buttonPressed2()), this,
SLOT(on_pushButton_LEFT_clicked()));
connect(this, SIGNAL(sendData(QString)), dialog, SLOT(recieveData(QString)));
connect(dialog, SIGNAL(SendParameter(QString)), this,
SLOT(send_data_to_com(QString)));
connect(dialog, SIGNAL(buttonPressed3(QString)), this,
SLOT(unblock(QString)));
connect(dialog, SIGNAL(buttonPressed4()), this, SLOT(unblock2()));
connect(dialog, SIGNAL(toggle_EDIT_MODE(bool)), this, SLOT(switch_EDIT_MODE(bool)));
/* Slots for editing graphs values*/
// connect(ui->plot->graph(10), SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), this, SLOT(plottableClicked(QCPAbstractPlottable*,int,QMouseEvent)));
connect(ui->plot, SIGNAL(plottableDoubleClick(QCPAbstractPlottable *,int,QMouseEvent *)), this, SLOT(plottableDoubleClicked(QCPAbstractPlottable *,int,QMouseEvent *)));
connect(ui->plot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(handleMousePress(QMouseEvent*)));
connect(ui->plot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(handleMouseRelease(QMouseEvent*)));
m_csvFile = nullptr;
QSound *dong = new QSound("qrc:/serial_port_plotter/dong.wav");
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Destructor
*/
MainWindow::~MainWindow() {
closeCsvFile();
if (serialPort != nullptr) {
delete serialPort;
}
delete ui;
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Create remaining elements and populate the controls
*/
void MainWindow::createUI() {
// ui->pushButton_CANCEL->setEnabled(false);
ui->pushButton_2->setVisible(false);
ui->ready->setVisible(false);
ui->manulabutton->setVisible(false);
ui->manualTemp->setVisible(false);
ui->HotStart->setEnabled(false);
ui->HotStart->setVisible(false);
ui->pushButton_UP->setEnabled(false);
// ui->pushButton_UP->setStyleSheet("QPushButton { background-color: grey; }\n");
ui->pushButton_DOWN->setEnabled(false);
//ui->pushButton_DOWN->setStyleSheet("QPushButton { background-color: grey; }\n");
// ui->pushButton_UP->setStyleSheet("QPushButton:enabled { background-color:
// rgb(150,200,250); }\n"
// "QPushButton:enabled { color: rgb(0,0,0);
// }\n");
// ui->pushButton_CANCEL->setStyleSheet("QPushButton { background-color: grey;
// }\n"); ui->pushButton_CANCEL->setEnabled(false);
ui->PlotControlsBox->setVisible(false); // Hide uPlotControlsBox
ui->HeaterControlsBox->setVisible(false); // Hide Heater controls
/* Check if there are any ports at all; if not, disable controls and return */
tmr = new QTimer(this);
ui->actionSave_Settings->setEnabled(false);
if (QSerialPortInfo::availablePorts().size() == 0) {
enable_com_controls(false);
ui->statusBar->showMessage("No ports detected.");
ui->actionRecord_PNG->setEnabled(false);
connect(tmr, SIGNAL(timeout()), this, SLOT(on_pushButton_clicked()));
tmr->start(3000);
return;
}
/* List all available serial ports and populate ports combo box */
for (QSerialPortInfo port : QSerialPortInfo::availablePorts()) {
ui->comboPort->addItem(port.portName());
UpdatePortControls();
enable_com_controls(true);
ui->actionConnect->trigger(); // autoconnect if arduino on same port
dialog->show();
// dialog->buttonPressed();
dialog->close();
}
}
void MainWindow::UpdatePortControls() { /* Populate baud rate combo box with
standard rates */
// ui->comboBaud->addItem("1200");
// ui->comboBaud->addItem("2400");
// ui->comboBaud->addItem("4800");
ui->comboBaud->addItem("9600");
// ui->comboBaud->addItem("19200");
// ui->comboBaud->addItem("38400");
ui->comboBaud->addItem("57600");
ui->comboBaud->addItem("115200");
/* And some not-so-standard */
// ui->comboBaud->addItem("128000");
// ui->comboBaud->addItem("153600");
// ui->comboBaud->addItem("230400");
// ui->comboBaud->addItem("256000");
// ui->comboBaud->addItem("460800");
// ui->comboBaud->addItem("921600");
// ui->comboMod->addItem ("921600");
/* Select 115200 bits by default */
// ui->comboBaud->setCurrentIndex (3);
/* Populate data bits combo box */
// ui->comboData->addItem ("8 bits");
// ui->comboData->addItem ("7 bits");
/* Populate parity combo box */
// ui->comboParity->addItem ("none");
// ui->comboParity->addItem ("odd");
// ui->comboParity->addItem ("even");
/* Populate stop bits combo box */
// ui->comboStop->addItem ("1 bit");
// ui->comboStop->addItem ("2 bits");
/* Initialize the listwidget */
ui->listWidget_Channels->clear();
// try to load settings, or populate with default value
ui->actionLoad_Settings->trigger();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Setup the plot area
*/
void MainWindow::setupPlot() {
/* Remove everything from the plot */
ui->plot->clearItems();
/* Background for the plot area */
ui->plot->setBackground(gui_colors[0]);
/* Used for higher performance (see QCustomPlot real time example) */
ui->plot->setNotAntialiasedElements(QCP::aeAll);
QFont font;
font.setStyleStrategy(QFont::NoAntialias);
ui->plot->legend->setFont(font);
/** See QCustomPlot examples / styled demo **/
/* X Axis: Style */
ui->plot->xAxis->grid()->setPen(QPen(gui_colors[2], 1, Qt::DotLine));
ui->plot->xAxis->grid()->setSubGridPen(QPen(gui_colors[1], 1, Qt::DotLine));
ui->plot->xAxis->grid()->setSubGridVisible(true);
ui->plot->xAxis->setBasePen(QPen(gui_colors[2]));
ui->plot->xAxis->setTickPen(QPen(gui_colors[2]));
ui->plot->xAxis->setSubTickPen(QPen(gui_colors[2]));
ui->plot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->plot->xAxis->setTickLabelColor(gui_colors[2]);
ui->plot->xAxis->setTickLabelFont(font);
// ui->plot->xAxis->setTickLabelType(QCPAxis::ltDateTime); // Подпись
// координат по Оси X в качестве Даты и Времени
// ui->plot->xAxis->setDateTimeFormat("hh:mm"); // Устанавливаем формат даты
// и времени
/* Range */
// ui->plot->xAxis->setRange (dataPointNumber - ui->spinPoints->value(), dataPointNumber);
ui->plot->xAxis->setRange (ui->spinPoints->value(), dataPointNumber); // 0 on left
// setColor(palette().WindowText, Qt::blue);
ui->lcdChannelTemp_2->setStyleSheet("QLCDNumber{color:#B231F7}"); // Bottom
ui->lcdChannelTemp_3->setStyleSheet("QLCDNumber{color:#E05123}"); // top
ui->lcdChannelTemp->setStyleSheet("QLCDNumber{color:#A3E05C}"); // PCB
//ui->plot->xAxis->setRange (dataPointNumber - spinPoints1, dataPointNumber);
// //Reverse
//ui->plot->xAxis->setRange(spinPoints1, dataPointNumber); // 0 on left
/* Y Axis */
ui->plot->yAxis->grid()->setPen(QPen(gui_colors[2], 1, Qt::DotLine));
ui->plot->yAxis->grid()->setSubGridPen(QPen(gui_colors[1], 1, Qt::DotLine));
ui->plot->yAxis->grid()->setSubGridVisible(true);
ui->plot->yAxis->setBasePen(QPen(gui_colors[2]));
ui->plot->yAxis->setTickPen(QPen(gui_colors[2]));
ui->plot->yAxis->setSubTickPen(QPen(gui_colors[2]));
ui->plot->yAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);
ui->plot->yAxis->setTickLabelColor(gui_colors[2]);
ui->plot->yAxis->setTickLabelFont(font);
/* Range */
ui->plot->yAxis->setRange (ui->spinAxesMin->value(), ui->spinAxesMax->value());
/* User can change Y axis tick step with a spin box */
//ui->plot->yAxis->setAutoTickStep (false);
ui->plot->yAxis->ticker()->setTickCount(ui->spinYStep->value());
/* User interactions Drag and Zoom are allowed only on X axis, Y is fixed manually by UI control */
ui->plot->setInteraction (QCP::iRangeDrag, true);
// ui->plot->setInteraction (QCP::iMultiSelect , true);
ui->plot->setInteraction (QCP::iSelectPlottables, false);
// ui->plot->graph(3)->selectable(QCP::SelectionType(QCP::stSingleData));
ui->plot->setInteraction (QCP::iSelectLegend, true);
ui->plot->axisRect()->setRangeDrag (Qt::Horizontal);
ui->plot->axisRect()->setRangeZoom (Qt::Horizontal);
/* Legend */
QFont legendFont;
legendFont.setPointSize(9);
// ui->plot->legend->setVisible (true); // показать легенду
ui->plot->legend->setVisible(false); // скрыть легенду
ui->plot->legend->setFont(legendFont);
ui->plot->legend->setBrush(gui_colors[3]);
ui->plot->legend->setBorderPen(gui_colors[2]);
/* By default, the legend is in the inset layout of the main axis rect. So
* this is how we access it to change legend placement */
ui->plot->axisRect()->insetLayout()->setInsetAlignment( 0, Qt::AlignTop | Qt::AlignLeft); // вставить квадрат с именами и выровнять
// if(clkb==1){ui->plot->axisRect()->insetLayout()->setInsetAlignment (0,
// Qt::AlignTop|Qt::AlignLeft);}
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Enable/disable COM controls
* @param enable true enable, false disable
*/
void MainWindow::enable_com_controls(bool enable) {
/* Com port properties */
ui->comboBaud->setEnabled(enable);
// ui->comboData->setEnabled (enable);
// ui->comboParity->setEnabled (enable);
ui->comboPort->setEnabled(enable);
// ui->comboStop->setEnabled (enable);
/* Toolbar elements */
ui->actionConnect->setEnabled(enable);
ui->actionPause_Plot->setEnabled(!enable);
ui->actionDisconnect->setEnabled(!enable);
enable_heater_controls(!enable);
loadSettings();
}
void MainWindow::enable_heater_controls(bool enable) {
ui->pushButton_CANCEL->setEnabled(enable);
ui->pushButton_OK->setEnabled(enable);
ui->pushButton_UP->setEnabled(enable);
ui->pushButton_LEFT->setEnabled(enable);
ui->pushButton_RIGHT->setEnabled(enable);
ui->pushButton_DOWN->setEnabled(enable);
// ui->pushButton_2->setEnabled (enable);
}
void MainWindow::enable_advanced_controls(bool enable) {
ui->PlotControlsBox->setVisible(enable);
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Open the inside serial port; connect its signals
* @param portInfo
* @param baudRate
* @param dataBits
* @param parity
* @param stopBits
*/
void MainWindow::openPort(QSerialPortInfo portInfo, int baudRate,
QSerialPort::DataBits, QSerialPort::Parity,
QSerialPort::StopBits) {
serialPort = new QSerialPort(portInfo, nullptr); // Create a new serial port
connect(this, SIGNAL(portOpenOK()), this,
SLOT(portOpenedSuccess())); // Connect port signals to GUI slots
connect(this, SIGNAL(portOpenFail()), this, SLOT(portOpenedFail()));
connect(this, SIGNAL(portClosed()), this, SLOT(onPortClosed()));
connect(this, SIGNAL(newData(QStringList)), this,
SLOT(onNewDataArrived(QStringList)));
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
// connect (serialPort, SIGNAL(bytesWritten()), this, SLOT(writeData()));
connect(this, SIGNAL(newData(QStringList)), this,
SLOT(saveStream(QStringList)));
if (serialPort->open(QIODevice::ReadWrite)) {
QSerialPort::DataBits dataBits;
dataBits = QSerialPort::Data8;
QSerialPort::Parity parity;
parity = QSerialPort::NoParity;
QSerialPort::StopBits stopBits;
stopBits = QSerialPort::OneStop;
serialPort->setBaudRate(baudRate);
serialPort->setParity(parity);
serialPort->setDataBits(dataBits);
serialPort->setStopBits(stopBits);
// serialPort->setDataBits(QSerialPort::Data8);
serialPort->setFlowControl(QSerialPort::NoFlowControl);
// serialPort->setParity(QSerialPort::NoParity);
// serialPort->setStopBits(QSerialPort::OneStop);
emit portOpenOK();
} else {
emit portOpenedFail();
qDebug() << serialPort->errorString();
}
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for closing the port
*/
void MainWindow::onPortClosed() {
// qDebug() << "Port closed signal received!";
updateTimer.stop();
connected = false;
plotting = false;
//--
closeCsvFile();
disconnect(serialPort, SIGNAL(readyRead()), this, SLOT(readData()));
// disconnect (serialPort, SIGNAL(readySend()), this, SLOT(writeData()));
disconnect(this, SIGNAL(portOpenOK()), this,
SLOT(portOpenedSuccess())); // Disconnect port signals to GUI slots
disconnect(this, SIGNAL(portOpenFail()), this, SLOT(portOpenedFail()));
disconnect(this, SIGNAL(portClosed()), this, SLOT(onPortClosed()));
disconnect(this, SIGNAL(newData(QStringList)), this,
SLOT(onNewDataArrived(QStringList)));
disconnect(this, SIGNAL(newData(QStringList)), this,
SLOT(saveStream(QStringList)));
ui->PortControlsBox->setVisible(true);
ui->HeaterControlsBox->setVisible(false);
ui->PlotControlsBox->setVisible(false);
ui->gridGroupBox->setVisible(false);
ui->actionSave_Settings->setEnabled(false);
ui->pushButton_2->setVisible(false);
ui->ready->setVisible(false);
ui->manualTemp->setVisible(false);
ui->manulabutton->setVisible(false);
ui->SimpleExpert->setVisible(false);
// ui->spinmanual->setVisible(false);
setupPlot();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Port Combo Box index changed slot; displays info for selected port
* when combo box is changed
* @param arg1
*/
void MainWindow::on_comboPort_currentIndexChanged(const QString &arg1) {
QSerialPortInfo selectedPort(arg1); // Dislplay info for selected port
ui->statusBar->showMessage(selectedPort.description());
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for port opened successfully
*/
void MainWindow::portOpenedSuccess() {
// qDebug() << "Port opened signal received!";
setupPlot(); // Create the QCustomPlot area
ui->statusBar->showMessage("Connected!");
enable_com_controls(false); // Disable controls if port is open
if (ui->actionRecord_stream->isChecked()) {
//--> Create new CSV file with current date/timestamp
openCsvFile();
}
/* Lock the save option while recording */
ui->actionRecord_stream->setEnabled(false);
ui->PortControlsBox->setVisible(false); // Hide Port settings
ui->PlotControlsBox->setVisible(false);
ui->HeaterControlsBox->setVisible(true); // Show Heater Controls
ui->actionSave_Settings->setEnabled(true); // Enable Save Setings
ui->pushButton_2->setVisible(true);
//ui->spinmanual->setVisible(true);
ui->manualTemp->setVisible(false);
ui->manulabutton->setVisible(true);
ui->HotStart->setEnabled(false);
ui->HotStart->setVisible(true);
ui->SimpleExpert->setVisible(true);
/*
ui->pushButton_OK->setStyleSheet(
"QPushButton:enabled { background-color: rgb(0,250,0); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
ui->pushButton_CANCEL->setStyleSheet(
"QPushButton { background-color: grey; }\n");
ui->pushButton_LEFT->setStyleSheet(
"QPushButton:enabled { background-color: rgb(250,250,0); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
ui->pushButton_RIGHT->setStyleSheet(
"QPushButton:enabled { background-color: rgb(250,250,0); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
// ui->pushButton_UP
// ui->pushButton_DOWN
ui->HotStart->setStyleSheet("QPushButton { background-color: grey; }\n");
ui->manulabutton->setStyleSheet(
"QPushButton:enabled { background-color: rgb(0,250,0); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
*/
updateTimer.start(20); // Slot is refreshed 20 times per second
connected = true; // Set flags
plotting = true;
// setupPlot();
profilread();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for fail to open the port
*/
void MainWindow::portOpenedFail() {
// qDebug() << "Port cannot be open signal received!";
ui->statusBar->showMessage("Cannot open port!");
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Replot
*/
void MainWindow::replot() {
//ui->plot->xAxis->setRange (dataPointNumber - ui->spinPoints->value(), dataPointNumber);
ui->plot->replot();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for new data from serial port . Data is comming in QStringList
* and needs to be parsed
* @param newData
*/
void MainWindow::onNewDataArrived(QStringList newData) {
static int data_members = 0;
static int channel = 0;
static int i = 0;
volatile bool you_shall_NOT_PASS = false;
/* When a fast baud rate is set (921kbps was the first to starts to bug),
this method is called multiple times (2x in the 921k tests), so a flag
is used to throttle
TO-DO: Separate processes, buffer data (1) and process data (2) */
while (you_shall_NOT_PASS) {
}
you_shall_NOT_PASS = true;
if (plotting) {
/* Get size of received list */
data_members = newData.size();
if (data_members > 9) {data_members = 9;} // limit number of plotted channels to 9
/* Parse data */
for (i = 0; i < data_members; i++) {
/* Update number of axes if needed */
while (ui->plot->plottableCount() <= channel) {
/* Add new channel data */
ui->plot->addGraph(); // добавить график
ui->plot->graph()->setPen(line_colors[channels % CUSTOM_LINE_COLORS]);
QString channelscount =
QString("Count %1").arg(m_prefs.channelnames.size());
if (i < (m_prefs.channelnames.size())) // /3 because have x3 elements
{
ui->plot->graph()->setName(m_prefs.channelnames.value(i).channelName);
ui->plot->graph()->setVisible(
m_prefs.channelnames.value(i).channelVisibie);
// ui->statusBar->showMessage("channelscount");
} else {
ui->plot->graph()->setName(QString("Channel %1").arg(channels + 1));
}
ui->plot->graph()->setVisible(
m_prefs.channelnames.value(i).channelVisibie);
if (ui->plot->legend->item(channels)) {
ui->plot->legend->item(channels)->setTextColor(
line_colors[channels % CUSTOM_LINE_COLORS]);
}
ui->listWidget_Channels->addItem(ui->plot->graph()->name());
ui->listWidget_Channels->item(channel)->setForeground(
QBrush(line_colors[channels % CUSTOM_LINE_COLORS]));
channels++;
ui->statusBar->showMessage(channelscount);
}
/* [TODO] Method selection and plotting */
/* X-Y */
if (ui->plot->plottableCount() == data_members) { //если у нас графиков столько, сколько приходит данных то добавляем статичные графики
for (i = 9; i < 12; i++) {
ui->plot->addGraph();
ui->plot->graph()->setName(m_prefs.channelnames.value(i).channelName);
}
}
/* Rolling (v1.0.0 compatible) */
else {
/* Add data to Graph 0 */
ui->plot->graph(channel)->addData (dataPointNumber, newData[channel].toDouble()); // add piont to correspoding channel
ui->lcdChannelTemp->display(newData[5]); //PCB
ui->lcdChannelTemp_2->display(newData[4]); // Bottom
ui->lcdChannelTemp_3->display(newData[3]); // Top
/* Increment data number and channel */
channel++;
}
}
/* Post-parsing */
/* X-Y */
if (0) {
}
/* Rolling (v1.0.0 compatible) */
else {
dataPointNumber++;
channel = 0;
}
}
you_shall_NOT_PASS = false;
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for spin box for plot minimum value on y axis
* @param arg1
*/
void MainWindow::on_spinAxesMin_valueChanged(int arg1)
{
//arg1=0;
ui->plot->yAxis->setRangeLower (arg1);
ui->plot->replot();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Slot for spin box for plot maximum value on y axis
* @param arg1
*/
void MainWindow::on_spinAxesMax_valueChanged(int arg1)
{
//arg1=600;
ui->plot->yAxis->setRangeUpper (arg1);
ui->plot->replot();
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Read data for inside serial port
*/
void MainWindow::readData() {
if (serialPort->bytesAvailable()) { // If any bytes are available
QByteArray data = serialPort->readAll(); // Read all data in QByteArray
if (!data.isEmpty() && data.length()>4) { // If the byte array is not empty
// char *temp = data.data(); // Get a '\0'-terminated char* to the data
// unsigned char *temp = (unsigned char*)data.data();
unsigned char *temp = reinterpret_cast<unsigned char *>(data.data());
// unsigned char *tempal = reinterpret_cast<unsigned char *>(data.data());
// unsigned char *tempst = reinterpret_cast<unsigned char *>(data.data());
if (!filterDisplayedData) { // Merge recieved data if it come in parts,
// print when first \r found
receivedDataRaw.append(data);
for (int i = 0; receivedDataRaw[i] != '\0'; i++) {
if (receivedDataRaw[i] == '\r') { // clean
if (!receivedDataRaw.startsWith('\n') &&
!receivedDataRaw.startsWith('\r')) {
ui->textEdit_UartWindow->append(receivedDataRaw.left(i));
// emit sendData(ui->textEdit_UartWindow->toPlainText()); //
// вызываем сигнал, в котором передаём данные
receivedDataRaw.remove(0, i); // print in textEdit from 0 to i
} else {
receivedDataRaw.remove(
0, 1); // if found more than 1 \n or \r in row - delete them
// receivedDataRaw=receivedDataRaw.trimmed();
}
if (receivedDataRaw[i + 1] == '\0')
i = 0;
}
if (i >= receivedDataRaw.size()) {
break;
}
}
}
for (int i = 0; temp[i] != '\0'; i++) { // Iterate over the char*
switch (STATE) { // Switch the current state of the message
case WAIT_START: // If waiting for start [$], examine each char
if (temp[i] ==
START_MSG) { // If the char is $, change STATE to IN_MESSAGE
receivedData
.clear(); // Clear temporary QString that holds the message
STATE = IN_MESSAGE;
break; // Break out of the switch
}
if (temp[i] ==
COMMAND_MSG) { // If the char is !, change STATE to IN_COMMAND
receivedData
.clear(); // Clear temporary QString that holds the message
STATE = IN_COMMAND;
break; // Break out of the switch
}
if (temp[i] ==
DONG_MSG) { // If the char is @, change STATE to IN_STATUS
statusData.clear();
receivedData
.clear(); // Clear temporary QString that holds the message
STATE = IN_STATUS;
break; // Break out of the switch
}
break; // If waiting for start [$], examine each char
case IN_MESSAGE: // If state is IN_MESSAGE
if (temp[i] == CLEAR_MSG) { // If recieve # symbol IN-MESSAGE
clear_plottables_graph();
receivedData.clear();
STATE = WAIT_START;
}
if (temp[i] == DONG_MSG) { // @ symbol IN-MESSAGE
// dong->play();
ui->statusBar->showMessage("SOUND!");
receivedData.clear();
STATE = WAIT_START;
}
if (temp[i] ==
END_MSG) { // If char examined is ;, switch state to END_MSG
STATE = WAIT_START;
QStringList incomingData = receivedData.split(
' '); // Split string received from port and put it into list
if (filterDisplayedData) {
ui->textEdit_UartWindow->append(receivedData);
receivedData.clear();
}
emit newData(
incomingData); // Emit signal for data received with the list
receivedData.clear();
if ((PREV_STATE == IN_COMMAND || PREV_STATE == IN_STATUS)){STATE = PREV_STATE;}
break;
}
if (isdigit(temp[i]) || isspace(temp[i]) || temp[i] == '-' ||
temp[i] == '.') {
/* If examined char is a digit, and not '$' or ';', append it to
* temporary string */
receivedData.append(temp[i]);
}
break;
case IN_COMMAND: // If state is IN_COMMAND after recieve "!"
if (temp[i] ==
START_MSG) { // If char examined is $, - we have interrupt with temperatures data.
PREV_STATE = STATE;
STATE = IN_MESSAGE;
}
if (temp[i] ==
END_MSG) { // If char examined is ;, switch state to END_MSG
STATE = WAIT_START;
if (receivedData.contains(':')) {
emit sendData(receivedData); // emit signal
QStringList data = receivedData.split(
':'); // Split string received from port and put it into list
profiledata.addValue(data[0],data[1]);
if (data[0] == "Set Temp") {
ui->manualTemp->display(data[1]);
}
if (data[0] == "profile") {
ui->profile->setText(data[1]);
}
if (data[0] == "aliasprofile") {
ui->aliasprofile->setText(data[1]);
}
if (data[0] == "profile_transfer_finished"){
if(ui->plot->plottableCount() >= 9) {
plot_All_Profile();
}
}
if (data[0] == "second"){
dataPointNumber = (data[1]).toInt();
}
data.clear();
}
receivedData.clear();
PREV_STATE = WAIT_START;
break;
}
if (isalpha(temp[i]) || isdigit(temp[i]) || temp[i] == ':' ||
temp[i] == ',' || temp[i] == '_' || temp[i] == '-' ||
temp[i] == '.' || isspace(temp[i])) {
receivedData.append(temp[i]);
}
break;
case IN_STATUS:
if (temp[i] ==
START_MSG) { // If char examined is $, - we have interrupt with temperatures data.
PREV_STATE = STATE;
STATE = IN_MESSAGE;
}
if (temp[i] == END_MSG) { // switch state to END_MSG
STATE = WAIT_START;
if (statusData == '0') {
ui->ready->setVisible(false); // IDLE
} else if (statusData == '1') {
ui->ready->setVisible(true);
ui->ready->setText("WARMUP...");
dataPointNumber = 0; // Lock graph redraw
for (int j = 0; j <= 8; j++) { // Draw round point at Y Axis during Warmup
ui->plot->graph(j)->setLineStyle(QCPGraph::lsNone); // Change 0-8 plot to fat point
ui->plot->graph(j)->setPen(line_colors[j % CUSTOM_LINE_COLORS]);
ui->plot->graph(j)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 12));
double lastValue = ui->plot->graph(j)->data()->at(1)->value;
ui->plot->graph(j)->data()->clear();
ui->plot->graph(j)->setData({0,2}, {lastValue});
// ui->plot->graph(j)->setData(dataPointNumber, lastValue);
}
} else if (statusData == '2') {
ui->ready->setVisible(true);
ui->ready->setText("RUN");
ui->pushButton_UP->setEnabled(true);
ui->pushButton_UP->setStyleSheet(
"QPushButton:enabled { background-color: rgb(50,150,250); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
ui->pushButton_DOWN->setEnabled(true);
ui->pushButton_DOWN->setStyleSheet(
"QPushButton:enabled { background-color: rgb(50,150,250); }\n"
"QPushButton:enabled { color: rgb(0,0,0); }\n");
// plot_All_Profile(); // Вывести график термопрофиля
for (int j=0; j<=8; j++){ // Change all plott to point
ui->plot->graph(j)->setLineStyle(QCPGraph::lsLine);
ui->plot->graph(j)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssNone , 1));
}
} else if (statusData == '3') {
ui->ready->setVisible(true);
ui->ready->setText("AUTO PAUSE");
} else if (statusData == '4') {
ui->ready->setVisible(true);
ui->ready->setText("MANUAL");
ui->manualTemp->setVisible(true);
} else if (statusData == '5') {
ui->ready->setVisible(true);
ui->ready->setText("FINISH !");
for (int j=0; j<=8; j++){ // Change all plott to point
ui->plot->graph(j)->setLineStyle(QCPGraph::lsLine);
ui->plot->graph(j)->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssNone , 1));
}
} else if (statusData == '6') {
ui->ready->setVisible(false);
// } else if (statusData == '7') {
// ui->error->setVisible(true);
}
statusData.clear();
PREV_STATE = WAIT_START;
}
if (isdigit(temp[i])) {
statusData.append(temp[i]);
}
break;
default:
break;
}
}
}
}
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Number of axes combo; when changed, display axes colors in status bar
* @param index
*/
// void MainWindow::on_comboAxes_currentIndexChanged(int index)
//{
// if(index == 0) {
// ui->statusBar->showMessage("Axis 1: Red");
// } else if(index == 1) {
// ui->statusBar->showMessage("Axis 1: Red; Axis 2: Yellow");
// } else {
// ui->statusBar->showMessage("Axis 1: Red; Axis 2: Yellow; Axis 3:
// Green");
// }
// }
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Spin box for changing the Y Tick step
* @param arg1
*/
void MainWindow::on_spinYStep_valueChanged(int arg1)
{
ui->plot->yAxis->ticker()->setTickCount(arg1);
ui->plot->replot();
ui->spinYStep->setValue(ui->plot->yAxis->ticker()->tickCount());
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Save a PNG image of the plot to current EXE directory
*/
void MainWindow::on_actionRecord_PNG_triggered() {
ui->plot->savePng(QString::number(dataPointNumber) + ".png", 1920, 1080, 2,
50);
ui->statusBar->showMessage("PNG Saved.");
}
/** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/**
* @brief Send plot wheelmouse to spinbox
* @param event
*/