-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
212 lines (172 loc) · 5.65 KB
/
mainwindow.cpp
File metadata and controls
212 lines (172 loc) · 5.65 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
#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include "OptionsDialog.h"
#include "SettingsManager.h"
#include <QRegularExpression>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
SettingsManager::LoadSettings();
connect(&_port, &SerialPort::DataReceived, this, &MainWindow::ReadData);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::ReadData(QByteArray data) {
// Stop timer if it is started
// qint64 elapsed = _timer.elapsed();
// if (elapsed > 0) {
// _timer.invalidate();
// QString elapsed_str = QString::number(elapsed);
// elapsed_str.append(" ms");
// ui->rxLatencyVal->setText(elapsed_str);
// }
// Write data to RxTextEdit
// Set text color to blue
QTextCharFormat black_format;
black_format.setForeground(Qt::black);
ui->RxTextEdit->setCurrentCharFormat(black_format);
ui->RxTextEdit->Input(data);
// Update rx bytes
//AddRxBytes(data.size());
}
void MainWindow::SendData() {
bool is_written;
QString text_data = ui->TxLineEdit->text();
// Turn text_data into a QByteArray
// If input matches hex pattern (hXX), convert to hex
QByteArray write_data;
static QRegularExpression hex_pattern("h([0-9a-fA-F]{2})");
bool contains_hex = hex_pattern.match(text_data).hasMatch();
int pos = 0;
while (pos < text_data.length()) {
QRegularExpressionMatch match = hex_pattern.match(text_data, pos);
if (match.hasMatch() && match.capturedStart(0) == pos) {
// Convert the hex sequence to a byte
bool ok;
int hexValue = match.captured(1).toInt(&ok, 16);
if (ok) {
write_data.append(static_cast<char>(hexValue));
pos += match.captured(0).length(); // Move past this hex sequence
// If the next character is a space and it's followed by another hex pattern, skip the space
if (pos < text_data.length() && text_data[pos] == ' ' && hex_pattern.match(text_data, pos + 1).hasMatch()) {
pos += 1; // Skip the space
}
} else {
pos += 1; // This should never be reached with a valid hex pattern match
}
} else {
// Append this character as its ASCII representation
write_data.append(text_data.at(pos).toLatin1());
pos += 1;
}
}
// Send enter key if no hex characters
if (!contains_hex) {
switch (SettingsManager::GetEnterKey()) {
case 0:
// Append carriage return and line feed
write_data.append('\r');
write_data.append('\n');
break;
case 1:
// Append carriage return
write_data.append('\r');
break;
case 2:
// Append line feed
write_data.append('\n');
break;
default:
// Do nothing
break;
}
}
is_written = _port.Write(write_data);
// Handle errors
if (!is_written)
QMessageBox::critical(this, "Error", "Cannot write to serial port");
// If data is written successfully
else {
// Start timer
_timer.start();
// If echo is enabled, write to text edit
if (SettingsManager::GetLocalEcho()) {
// Set text color to blue
QTextCharFormat blue_format;
blue_format.setForeground(Qt::blue);
ui->RxTextEdit->setCurrentCharFormat(blue_format);
// Write contents of LineEditTx to RxTextEdit
ui->RxTextEdit->Input(write_data);
}
}
// Clear the line edit
ui->TxLineEdit->clear();
}
void MainWindow::on_actionConnect_triggered()
{
// If not currently connected
if (ui->actionConnect->isChecked()) {
bool is_connected;
// Connect to currently selected serial port
is_connected = _port.Connect(SettingsManager::GetPort(),
SettingsManager::GetBaudrate(),
SettingsManager::GetDataBits(),
SettingsManager::GetParity(),
SettingsManager::GetStopBits(),
SettingsManager::GetFlowControl());
// If connection failed
if (!is_connected) {
// Uncheck connect action
ui->actionConnect->setChecked(false);
QMessageBox::critical(this, "Error", "Cannot connect to serial port");
}
}
// If currently connected
else {
_port.Disconnect();
}
}
void MainWindow::on_actionOptions_triggered()
{
// Open options dialog
OptionsDialog optionsDialog;
optionsDialog.exec();
}
void MainWindow::on_actionView_Hex_triggered()
{
ui->RxTextEdit->SetHexFormat(ui->actionView_Hex->isChecked());
}
void MainWindow::on_actionScroll_Lock_triggered()
{
// Scroll lock on
if (ui->actionScroll_Lock->isChecked()) {
ui->RxTextEdit->SetScrollLock(true);
}
// Scroll lock off
else {
ui->RxTextEdit->SetScrollLock(false);
}
}
void MainWindow::on_actionClear_triggered()
{
// Clear RxTextEdit
ui->RxTextEdit->clear();
// TODO: Reset Tx, Rx bytes and latency
}
void MainWindow::on_sendButton_clicked()
{
// Same as when return pressed
SendData();
ui->TxLineEdit->AddToHistory();
}
void MainWindow::on_TxLineEdit_returnPressed()
{
// Same as when send button pressed
SendData();
ui->TxLineEdit->AddToHistory();
}