-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmainwindow.cpp
148 lines (117 loc) · 4.45 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <register-model.h>
#include <QRandomGenerator>
#include <qcustomplot.h>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, device(nullptr)
, replotTimer(this)
, connectState(CONNECT)
{
ui->setupUi(this);
connect(ui->horizontalSlider, &QSlider::valueChanged, device, &Device::setThrottle);
connect(ui->horizontalSlider, &QSlider::valueChanged, [&](int value) {
ui->label_3->setText(QString::number(value));
device->setThrottle(value);
});
connect(&portScanner, &PortScanner::scanUpdate, this, &MainWindow::onPortScanFinished);
portScanner.startScanning(500);
for (uint8_t i = 0; i < 20; i++) {
ui->customPlot->addGraph()->setVisible(false);
// random color for each plot
ui->customPlot->graph(i)->setPen(QColor(QRandomGenerator::global()->bounded(50,255), QRandomGenerator::global()->bounded(50, 255), QRandomGenerator::global()->bounded(50,255)));
}
ui->customPlot->yAxis2->setVisible(true);
}
void MainWindow::onPlotEnabledChanged(int index) {
if (device->registerList[index].plotEnabled) {
ui->customPlot->graph(index)->setVisible(true);
} else {
ui->customPlot->graph(index)->setVisible(false);
}
}
MainWindow::~MainWindow()
{
delete ui;
if (device) {
delete device;
}
}
void MainWindow::on_serialConnectButton_clicked()
{
if (connectState == CONNECT) {
for (auto availablePort : portScanner.availablePorts) {
if (availablePort.portName() == ui->serialComboBox->currentText()) {
if (!device) {
portScanner.stopScanning();
device = new Device(availablePort);
connect(device, &Device::newData, this, &MainWindow::handleNewDeviceData);
connect(device, &Device::closed, this, &MainWindow::deviceClosed);
connect(&device->registerModel, &RegisterModel::plotEnabledChanged, this, &MainWindow::onPlotEnabledChanged);
if (device->open()) {
ui->label->setText(availablePort.portName());
ui->serialConnectButton->setText("disconnect");
device->requestDeviceInformation();
replotTimer.start(40);
ui->tableView->setModel(device->getRegisterModel());
// device->readRegisters();
device->readRegisterMulti(0x0, 22);
connectState = DISCONNECT;
}
}
}
}
} else if (connectState == DISCONNECT) {
device->close();
}
//assert_not_reached();
}
void MainWindow::deviceClosed()
{
if (device) {
delete(device);
device = nullptr;
}
portScanner.startScanning();
ui->serialConnectButton->setText("connect");
connectState = CONNECT;
}
void MainWindow::readDeviceRegisters()
{
// ui->tableView->setModel();
}
void MainWindow::handleNewDeviceData()
{
ui->label->setText(QString::number(device->commutationFrequency));
static QTime time(QTime::currentTime());
// calculate two new data points:
double key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
static double lastPointKey = 0;
for (int i = 0; i < device->registerList.size(); i++) {
ui->customPlot->graph(i)->addData(key, device->deviceGlobal.adc_buffer[i]);
}
if (key-lastPointKey > 0.020) { // at most add point every 2 ms
ui->customPlot->xAxis->setRange(key, 5, Qt::AlignRight);
bool first = true;
for (int i = 0;i < device->registerList.size(); i++) {
if (device->registerList[i].plotEnabled) {
if (first) {
ui->customPlot->graph(i)->rescaleValueAxis(false, true);
first = false;
} else {
ui->customPlot->graph(i)->rescaleValueAxis(true, true);
}
}
}
ui->customPlot->replot();
}
}
void MainWindow::onPortScanFinished(QList<QSerialPortInfo> availablePorts)
{
ui->serialComboBox->clear();
for (auto portInfo : availablePorts) {
ui->serialComboBox->addItem(portInfo.portName());
}
}