-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cpp
228 lines (196 loc) · 5.68 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
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Bluetooth Terminal");
/*
* Bluetooth instance
*
* This is an instance of the bluetooth class for
* communication between the UART friend and this program
*/
this->bluetooth = new Bluetooth(this);
connect(bluetooth, SIGNAL(deviceListAvailable()), this, SLOT(refreshDeviceList()));
connect(bluetooth, SIGNAL(deviceConnected()), this, SLOT(handleBluetoothConnect()));
connect(bluetooth, SIGNAL(deviceDisconnected()), this, SLOT(handleBluetoothDisconnect()));
connect(bluetooth, SIGNAL(deviceTransmitReady()), this, SLOT(handleTransmitReady()));
connect(bluetooth, SIGNAL(dataAvailable()), this, SLOT(collectData()));
this->bluetooth->refreshDeviceList();
/*
* Timeout Timer
*
* This timer is used to timeout device connectinos that take too long
*/
timeoutTimer = new QTimer(this);
connect(timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout()));
/*
* Terminal
*
* The terminal is the widget responsible for displaying text and handling
* data enterred by the user.
*/
connect(ui->terminal, SIGNAL(textEnterred(char)), this, SLOT(sendUserInput(char)));
/*
*
* The logger class handles logging data to a file.
*
* This class allows logging data as text, hex, splitting log files (TX and RX files), and more.
*/
logger = new Logger(this);
connect(ui->terminal, SIGNAL(textAdded(QString)), logger, SLOT(log(QString)));
//Apply default settings here
ui->LogPathInput->setText(logger->getLogFilePath());
ui->OvevrwritePromptCheck->setChecked(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::startConnectTimeoutTimer()
{
if(!timeoutTimer){
timeoutTimer = new QTimer(this);
connect(timeoutTimer, SIGNAL(timeout()), this, SLOT(connectionTimeout()));
}
timeoutTimer->start(5000);
}
void MainWindow::refreshDeviceList()
{
if(bluetooth){
QStringList devices = this->bluetooth->getDeviceList();
ui->BluetoothDevicesBox->clear();
ui->BluetoothDevicesBox->addItems(devices);
}
}
void MainWindow::handleBluetoothConnect()
{
if(bluetooth){
this->timeoutTimer->stop();
QString txt = "Connected to " + bluetooth->getDeviceName();
ui->StatusLabel->setText(txt);
ui->ConnectButton->setText("Disconnect");
this->state = CONNECTED;
}
}
void MainWindow::handleBluetoothDisconnect()
{
if(bluetooth){
QString txt = "Disconnected from " + bluetooth->getDeviceName();
ui->StatusLabel->setText(txt);
ui->ConnectButton->setText("Connect");
this->state = DISCONNECTED;
}
}
void MainWindow::handleTransmitReady()
{
if(bluetooth){
qDebug() << "ready!";
this->state = READY;
bluetooth->write("Death and despair!");
}
}
void MainWindow::collectData()
{
if(bluetooth){
ui->terminal->addText(bluetooth->readAll(), true);
}
}
void MainWindow::sendUserInput(char c)
{
if(bluetooth){
bluetooth->write(c);
}
}
void MainWindow::connectionTimeout()
{
qDebug() << "Conn timeout";
this->state = DISCONNECTED;
ui->StatusLabel->setText("Failed to connect to device");
}
void MainWindow::on_RefreshDevicesButton_released()
{
if(bluetooth){
bluetooth->refreshDeviceList();
}
}
void MainWindow::on_ConnectButton_released()
{
if(bluetooth){
if(this->state == DISCONNECTED){
QString deviceName = ui->BluetoothDevicesBox->currentText();
if(!deviceName.isEmpty()){
ui->StatusLabel->setText("Connecting...");
this->startConnectTimeoutTimer();
bluetooth->connectToDevice(deviceName);
}
}
else{
bluetooth->disconnectFromDevice();
}
}
}
void MainWindow::on_EchoTerminalCheck_toggled(bool checked)
{
ui->terminal->enableEcho(checked);
}
void MainWindow::on_DisplayInHexCheck_toggled(bool checked)
{
if(checked){
ui->terminal->setDisplayMode(Terminal::HEX);
}
else{
ui->terminal->setDisplayMode(Terminal::ASCII);
}
}
void MainWindow::on_BrowseButton_released()
{
QString path = QFileDialog::getSaveFileName(this, "Log File",
logger->getLogFilePath(),
"Text Files (*.txt);;CSV Files (*.csv);;All Files (*.*)");
logger->setLogFile(path);
}
void MainWindow::on_StartStopLoggingButton_released()
{
if(logger->isLogging()){
logger->stopLogging();
ui->StartStopLoggingButton->setText("Start Logging");
}
else{
logger->startLogging();
if(logger->isLogging()){
ui->StartStopLoggingButton->setText("Stop Logging");
}
}
}
void MainWindow::on_LogRawDataCheck_toggled(bool checked)
{
logger->logInHex(checked);
}
void MainWindow::on_actionCapture_Terminal_triggered()
{
//Capture the terminal by writing all of the terminal's contents to the logger
if(!logger->isLogging()){
logger->startLogging();
if(logger->isLogging()){
logger->log(ui->terminal->getText());
logger->stopLogging();
}
}
}
void MainWindow::on_actionStart_Logging_triggered()
{
logger->startLogging();
}
void MainWindow::on_actionStop_Logging_triggered()
{
logger->stopLogging();
}
void MainWindow::on_OvevrwritePromptCheck_toggled(bool checked)
{
logger->promptWhenOverwriting(checked);
}