-
Notifications
You must be signed in to change notification settings - Fork 6
/
mainwindow.cpp
170 lines (144 loc) · 4.33 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
#ifdef EFM32_LOADER_GUI
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "geckoloader.h"
#include "helpdialog.h"
#include <QDebug>
#include <QSerialPortInfo>
#include <QFileDialog>
#include <QSettings>
#include <QSerialPort>
#include <QMessageBox>
#include <QTextStream>
#include <QRegularExpression>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineASCII->hide();
ui->textLog->setFont(QFont("Courier New", 9));
loader = new GeckoLoader(this);
serialPort = loader->serialPort();
m_connected = false;
readSettings();
slotReloadSerialPorts();
connect(loader, SIGNAL(output(QString)), this, SLOT(log(QString)));
connect(serialPort, SIGNAL(readyRead()), this, SLOT(slotDataReady()));
connect(ui->buttonHelp, SIGNAL(clicked()), this, SLOT(slotHelp()));
connect(ui->buttonReload, SIGNAL(clicked()), this, SLOT(slotReloadSerialPorts()));
connect(ui->buttonBrowse, SIGNAL(clicked()), this, SLOT(slotBrowse()));
connect(ui->buttonUpload, SIGNAL(clicked()), this, SLOT(slotUpload()));
connect(ui->buttonConnect, SIGNAL(clicked()), this, SLOT(slotConnect()));
connect(ui->comboTransport, SIGNAL(currentIndexChanged(int)), this, SLOT(slotTransport()));
connect(ui->lineASCII, SIGNAL(returnPressed()), this, SLOT(slotSendASCII()));
updateInterface();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::readSettings()
{
QSettings settings;
ui->lineFile->setText(settings.value("lastBinaryFile").toString());
}
void MainWindow::log(const QString &text)
{
QString output = text;
if(output.contains(QRegularExpression("\\d+\\s+\\/\\s+\\d+")))
{
output = output.remove('[');
output = output.remove(']');
output = output.remove(' ');
QStringList sl = output.split('/');
int progress = (int)(sl[0].toFloat() / sl[1].toFloat() * 100.0);
ui->progressBar->setValue(progress);
}
else
{
ui->textLog->appendPlainText(text);
}
}
void MainWindow::slotHelp()
{
HelpDialog dialog;
dialog.exec();
}
void MainWindow::slotReloadSerialPorts()
{
QStringList list;
foreach(QSerialPortInfo info, QSerialPortInfo::availablePorts())
{
QString portName = info.portName();
list.append(portName);
}
ui->comboPort->clear();
ui->comboPort->addItems(list);
updateInterface();
}
void MainWindow::slotBrowse()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Select a binary file"), QString(), "*.bin");
ui->lineFile->setText(filePath);
QSettings settings;
settings.setValue("lastBinaryFile", QVariant(filePath));
}
void MainWindow::slotConnect()
{
if(!m_connected)
{
m_connected = loader->open(ui->comboPort->currentText());
}
else
{
loader->close();
m_connected = false;
}
updateInterface();
}
void MainWindow::slotTransport()
{
bool transportIsUart = ui->comboTransport->currentText() == "UART";
if(transportIsUart)
loader->setTransport(GeckoLoader::TransportUART);
else
loader->setTransport(GeckoLoader::TransportUSB);
updateInterface();
}
void MainWindow::slotUpload()
{
disconnect(serialPort, SIGNAL(readyRead()), this, SLOT(slotDataReady()));
if(ui->comboBootEnPol->currentText() == "HIGH")
loader->setBootEnablePolarity(true);
else
loader->setBootEnablePolarity(false);
loader->upload(ui->lineFile->text());
connect(serialPort, SIGNAL(readyRead()), this, SLOT(slotDataReady()));
}
void MainWindow::slotSendASCII()
{
QString text = ui->lineASCII->text();
serialPort->write(text.toLatin1());
}
void MainWindow::slotDataReady()
{
while(serialPort->bytesAvailable() > 0)
{
ui->textLog->appendPlainText(serialPort->readAll());
}
}
void MainWindow::updateInterface()
{
ui->buttonConnect->setDisabled(ui->comboPort->count() == 0);
ui->buttonUpload->setEnabled(m_connected);
if(m_connected)
ui->buttonConnect->setText(tr("Disconnect"));
else
ui->buttonConnect->setText(tr("Connect"));
ui->comboPort->setEnabled(!m_connected);
bool transportIsUart = ui->comboTransport->currentText() == "UART";
ui->labelBootEn->setVisible(transportIsUart);
ui->comboBootEnPol->setVisible(transportIsUart);
}
#endif //EFM32_LOADER_GUI