-
Notifications
You must be signed in to change notification settings - Fork 40
/
mainwindow.cpp
275 lines (234 loc) · 9.01 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtWidgets>
#include <QtSerialPort/QSerialPortInfo>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
serial = new QSerialPort(this);
// Fill baud box
ui->baudBox->clear();
ui->baudBox->addItem(QStringLiteral("1200"), QSerialPort::Baud1200);
ui->baudBox->addItem(QStringLiteral("2400"), QSerialPort::Baud2400);
ui->baudBox->addItem(QStringLiteral("4800"), QSerialPort::Baud4800);
ui->baudBox->addItem(QStringLiteral("9600"), QSerialPort::Baud9600);
ui->baudBox->addItem(QStringLiteral("19200"), QSerialPort::Baud19200);
ui->baudBox->addItem(QStringLiteral("38400"), QSerialPort::Baud38400);
ui->baudBox->addItem(QStringLiteral("57600"), QSerialPort::Baud57600);
ui->baudBox->addItem(QStringLiteral("115200"), QSerialPort::Baud115200);
ui->baudBox->setCurrentIndex(7); // Default 115200
ui->filePathText->setDisabled(true);
ui->testPathText->setDisabled(true);
ui->closeSerialBtn->setDisabled(true);
ui->outputText->setReadOnly(true);
loadPorts();
filePath = "";
testFilePath = "";
connect(ui->reloadBtn, &QPushButton::clicked,
this, &MainWindow::loadPorts);
connect(ui->browseBtn, &QPushButton::clicked,
this, &MainWindow::browseFile);
connect(ui->browseTestBtn, &QPushButton::clicked,
this, &MainWindow::browseTestFile);
connect(ui->uploadBtn, &QPushButton::clicked,
this, &MainWindow::doUpload);
connect(ui->testBtn, &QPushButton::clicked,
this, &MainWindow::doTest);
// Serial port setup
connect(serial, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
this, &MainWindow::handleSerialError);
connect(serial, &QSerialPort::readyRead, this, &MainWindow::readData);
connect(ui->openSerialBtn, &QPushButton::clicked,
this, &MainWindow::openSerialPort);
connect(ui->closeSerialBtn, &QPushButton::clicked,
this, &MainWindow::closeSerialPort);
// Fetch previous settings
QCoreApplication::setOrganizationName("makerlab.mx");
QCoreApplication::setOrganizationDomain("makerlab.mx");
QCoreApplication::setApplicationName("esptool-gui");
QSettings settings;
if(settings.contains("settings/port"))
{
int portIndex = settings.value("settings/port").toInt();
ui->portBox->setCurrentIndex(portIndex);
}
if(settings.contains("settings/baud"))
{
int baudIndex = settings.value("settings/baud").toInt();
ui->baudBox->setCurrentIndex(baudIndex);
}
if(settings.contains("settings/file"))
{
filePath = settings.value("settings/file").toString();
ui->filePathText->setText(filePath);
}
if(settings.contains("settings/testFile"))
{
testFilePath = settings.value("settings/testFile").toString();
ui->testPathText->setText(testFilePath);
}
connect(ui->portBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[=](int index)
{
QSettings settings;
settings.setValue("settings/port", index);
});
connect(ui->baudBox, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
[=](int index)
{
QSettings settings;
settings.setValue("settings/baud", index);
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::loadPorts()
{
ui->portBox->clear();
foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
{
ui->portBox->addItem(info.portName(), info.systemLocation());
}
}
void MainWindow::browseFile()
{
QSettings settings;
const QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
filePath = fileName;
ui->filePathText->setText(filePath);
settings.setValue("settings/file", filePath);
}
}
void MainWindow::browseTestFile()
{
QSettings settings;
const QString fileName = QFileDialog::getOpenFileName(this);
if (!fileName.isEmpty())
{
testFilePath = fileName;
ui->testPathText->setText(testFilePath);
settings.setValue("settings/testFile", testFilePath);
}
}
void MainWindow::doUpload()
{
closeSerialPort();
ui->outputText->clear();
ui->outputText->appendPlainText("Uploading...\n");
QString appPath = QCoreApplication::applicationDirPath();
QString program = appPath + "/tool-esptool/esptool";
QStringList arguments;
arguments << "-vv" << "-cd" << "ck" << "-cb" << ui->baudBox->currentText() << "-cp" << ui->portBox->currentData().toString() << "-cf" << filePath;
startProcess(program, arguments);
}
// Get ESP8266 MAC only works with esptool.py, not esptool-ck
void MainWindow::doGetMac()
{
closeSerialPort();
ui->outputText->clear();
ui->outputText->appendPlainText("Getting MAC...\n");
QString program = "/usr/local/bin/esptool.py";
QStringList arguments;
arguments << "-b" << ui->baudBox->currentText() << "-p" << ui->portBox->currentData().toString() << "read_mac";
startProcess(program, arguments);
}
void MainWindow::doTest()
{
closeSerialPort();
ui->outputText->clear();
ui->outputText->appendPlainText("Uploading Test Firmware...\n");
QString appPath = QCoreApplication::applicationDirPath();
QString program = appPath + "/tool-esptool/esptool";
QStringList arguments;
arguments << "-vv" << "-cd" << "ck" << "-cb" << ui->baudBox->currentText() << "-cp" << ui->portBox->currentData().toString() << "-cf" << testFilePath;
startProcess(program, arguments, true);
}
void MainWindow::setInputsDisabled(bool disabled)
{
ui->baudBox->setDisabled(disabled);
ui->portBox->setDisabled(disabled);
ui->reloadBtn->setDisabled(disabled);
ui->uploadBtn->setDisabled(disabled);
ui->browseBtn->setDisabled(disabled);
ui->browseTestBtn->setDisabled(disabled);
ui->testBtn->setDisabled(disabled);
}
void MainWindow::startProcess(QString program, QStringList arguments, bool isTest)
{
process = new QProcess(this);
connect(process, &QProcess::started, this, [=]()
{
setInputsDisabled(true);
});
connect(process, &QProcess::readyReadStandardError, this, [=]()
{
ui->outputText->insertPlainText(process->readAllStandardError());
ui->outputText->verticalScrollBar()->setValue(ui->outputText->verticalScrollBar()->maximum());
});
connect(process, &QProcess::readyReadStandardOutput, this, [=]()
{
ui->outputText->insertPlainText(process->readAllStandardOutput());
ui->outputText->verticalScrollBar()->setValue(ui->outputText->verticalScrollBar()->maximum());
});
connect(process, static_cast<void(QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished), this, [=](int exitCode, QProcess::ExitStatus exitStatus)
{
ui->outputText->appendPlainText(QString("Finished with exit code ") + QString::number(exitCode));
setInputsDisabled(false);
if(isTest)
{
ui->outputText->appendPlainText("\nTest Firmware upload finished. Press 'Open Serial' and press the config button on the Device (if any) to start testing.");
}
});
connect(process, static_cast<void(QProcess::*)(QProcess::ProcessError)>(&QProcess::errorOccurred), this, [=](QProcess::ProcessError error)
{
ui->outputText->appendPlainText( QString("An error occurred: ") + error );
setInputsDisabled(false);
});
process->start(program, arguments);
}
void MainWindow::handleSerialError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
closeSerialPort();
}
}
void MainWindow::closeSerialPort()
{
if (serial->isOpen())
{
serial->close();
ui->openSerialBtn->setDisabled(false);
ui->closeSerialBtn->setDisabled(true);
ui->outputText->appendPlainText("__________________________________________________________\nSerial port closed.");
}
}
void MainWindow::openSerialPort()
{
serial->setPortName(ui->portBox->currentText());
serial->setBaudRate(ui->baudBox->currentData().toInt());
//serial->setDataBits(p.dataBits);
//serial->setParity(p.parity);
//serial->setStopBits(p.stopBits);
//serial->setFlowControl(p.flowControl);
if (serial->open(QIODevice::ReadWrite)) {
ui->openSerialBtn->setDisabled(true);
ui->closeSerialBtn->setDisabled(false);
ui->outputText->appendPlainText("\nSerial port open:\n__________________________________________________________\n");
} else {
QMessageBox::critical(this, tr("Error"), serial->errorString());
ui->outputText->appendPlainText( tr("Open error"));
}
}
void MainWindow::readData()
{
QByteArray data = serial->readAll();
ui->outputText->insertPlainText(data);
ui->outputText->verticalScrollBar()->setValue(ui->outputText->verticalScrollBar()->maximum());
}