-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
146 lines (122 loc) · 4.08 KB
/
mainwindow.cpp
File metadata and controls
146 lines (122 loc) · 4.08 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
void MainWindow::updateUartDevices()
{
auto uartDevices = QSerialPortInfo::availablePorts();
ui->comboBoxUartDeviceSelect->clear();
for(const auto &portInfo : uartDevices)
ui->comboBoxUartDeviceSelect->addItem(portInfo.portName());
}
void MainWindow::connects()
{
connect(ui->actionStepper, SIGNAL(triggered(bool)), stepperSettingsDialog.get(), SLOT(show()));
connect(serial, SIGNAL(connectionClosed()), this, SLOT(enableSerialWidgets()));
connect(serial, SIGNAL(readLine(QByteArray)), this, SLOT(readSerialLine(QByteArray)));
connect(dcMotorController, SIGNAL(dcMotorJsonUpdate(QJsonObject)),
this, SLOT(setSerialMsgToSend(QJsonObject)));
connect(stepperController, SIGNAL(stepperJsonUpdate(QJsonObject)),
this, SLOT(setSerialMsgToSend(QJsonObject)));
}
void MainWindow::openSerialConnection()
{
auto portName = ui->comboBoxUartDeviceSelect->currentText();
if(!portName.isEmpty())
{
serial->setPortName(portName);
if(serial->open())
{
disableSerialWidgets();
setStatusBarMsg(5000, tr("Connection established..."));
}
}
}
void MainWindow::setStatusBarMsg(int time, const QString &str)
{
ui->statusBar->showMessage(str, time);
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
dcMotorController = new DCMotorWidgetController(
ui->tabWidgetDCMotorSelect, this);
dcMotorController->startInternalEnumerationAt(1);
dcMotorController->addTab(Consts::DCMOTOR1_LABEL);
dcMotorController->addTab(Consts::DCMOTOR2_LABEL);
stepperController = new StepperWidgetController(
ui->tabWidgetStepperSelect, this);
stepperController->startInternalEnumerationAt(1);
stepperController->addTab(Consts::STEPPER1_LABEL);
stepperController->addTab(Consts::STEPPER2_LABEL);
stepperSettingsDialog = stepperController->createSettingsDialog();
serial = new SerialPort(this);
connects();
updateUartDevices();
}
MainWindow::~MainWindow()
{
serial->close();
delete serial;
delete stepperController;
delete dcMotorController;
delete ui;
}
void MainWindow::enableSerialWidgets()
{
auto uartDevices = QSerialPortInfo::availablePorts();
ui->comboBoxUartDeviceSelect->clear();
setStatusBarMsg(5000, tr("Connection closed..."));
dcMotorController->reset();
stepperController->reset();
dcMotorController->setSlidersEnabled(false);
stepperController->setSlidersEnabled(false);
ui->comboBoxUartDeviceSelect->setEnabled(true);
ui->pushButtonUartReload->setEnabled(true);
ui->pushButtonUartConnect->setText(tr("&Connect"));
}
void MainWindow::disableSerialWidgets()
{
dcMotorController->setSlidersEnabled(true);
stepperController->setSlidersEnabled(true);
ui->comboBoxUartDeviceSelect->setEnabled(false);
ui->pushButtonUartReload->setEnabled(false);
ui->pushButtonUartConnect->setText(tr("Dis&connect"));
}
void MainWindow::setSerialMsgToSend(const QJsonObject &json)
{
serial->setSerialMsgToSend(json);
}
void MainWindow::readSerialLine(const QByteArray &line)
{
dcMotorController->dcMotorUpdateFeedback(line);
stepperController->stepperUpdateFeedback(line);
}
void MainWindow::on_pushButtonUartReload_clicked()
{
updateUartDevices();
}
void MainWindow::on_pushButtonUartConnect_clicked()
{
if(!serial->isOpen())
openSerialConnection();
else
serial->close();
}
void MainWindow::on_action_Exit_triggered()
{
QApplication::quit();
}
void MainWindow::on_action_Author_triggered()
{
QString msg = "<p><h3>Stepper and DC Motor Controller</h3></p>"
"<p>Control stepper motors and dc motors via serial port.</p>"
"<p>© <a href=\"mailto:krzysztof.pawel.kruk@gmail.com\">"
"Krzysztof Kruk</a> 2016</p>";
QMessageBox::about(this, tr("About"), msg);
}
void MainWindow::on_action_About_Qt_triggered()
{
QMessageBox::aboutQt(this, tr("About Qt"));
}