-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmainwindow.cpp
138 lines (115 loc) · 4.08 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
/*
* This file is part of the Paparazzi UAV project.
*
* Copyright (C) 2012 Piotr Esden-Tempski <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QErrorMessage>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEdit_Binary->setText(getenv("HOME"));
ui->progressBar->hide();
device_valid = false;
connect(ui->pushButton_Flash, SIGNAL(clicked()), this, SLOT(flash()));
dfu_manager.moveToThread(&dfu_thread);
connect(ui->pushButton_Open, SIGNAL(clicked()), this, SLOT(openBinary()));
connect(&dfu_manager, SIGNAL(foundDevice(QString*)), this, SLOT(foundDevice(QString*)));
connect(&dfu_manager, SIGNAL(lostDevice()), this, SLOT(lostDevice()));
connect(&dfu_thread, SIGNAL(started()), &dfu_manager, SLOT(start()));
connect(&dfu_manager, SIGNAL(finishedFlash()), this, SLOT(finishedFlash()));
connect(&dfu_manager, SIGNAL(flashProgressUpdate(int, int)), this, SLOT(onFlashProgressUpdate(int, int)));
connect(this, SIGNAL(doFlash(QString*)), &dfu_manager, SLOT(flash(QString*)));
dfu_thread.start();
}
MainWindow::~MainWindow()
{
dfu_thread.quit();
dfu_thread.wait();
delete ui;
}
void MainWindow::openBinary()
{
ui->lineEdit_Binary->setText(QFileDialog::getOpenFileName(this, tr("Open Binary"), tr("Binary File (*.bin)")));
}
void MainWindow::foundDevice(QString *string)
{
QPalette plt;
QColor color(0, 142, 10);
plt.setColor(QPalette::WindowText, color);
ui->label_Iface->setPalette(plt);
ui->label_Iface->setText(*string);
ui->pushButton_Flash->setEnabled(true);
delete(string);
device_valid = true;
}
void MainWindow::lostDevice()
{
QPalette plt;
QColor color(142, 0, 10);
plt.setColor(QPalette::WindowText, color);
ui->label_Iface->setPalette(plt);
ui->label_Iface->setText("No device connected.");
ui->pushButton_Flash->setEnabled(false);
device_valid = false;
}
void MainWindow::flash()
{
QFileInfo binary_info(ui->lineEdit_Binary->text());
QErrorMessage error_message;
if (!binary_info.exists()) {
qDebug() << "File does not exist!";
error_message.showMessage("The file you selected does not exist!");
error_message.exec();
return;
}
if (!binary_info.isFile()) {
qDebug() << "\"File\" is not a file!";
error_message.showMessage("The \"file\" you selected is not a file!");
error_message.exec();
return;
}
if (binary_info.size() == 0) {
qDebug() << "File is empty!";
error_message.showMessage("The file is empty!");
error_message.exec();
return;
}
if (binary_info.size() > dfu_manager.get_flash_size()) {
qDebug() << "File is too big!";
error_message.showMessage(QString("The file size is too big! Max size is %1kb!").arg(dfu_manager.get_flash_size()/1024));
error_message.exec();
return;
}
ui->progressBar->setValue(0);
ui->progressBar->show();
emit doFlash(new QString(ui->lineEdit_Binary->text()));
}
void MainWindow::finishedFlash()
{
ui->progressBar->hide();
ui->label_Iface->setText("Done Flashing!");
}
void MainWindow::onFlashProgressUpdate(int address, int percent)
{
ui->label_Iface->setText(QString("Flashing address 0x%1 %2%").arg(QString::number(address, 16)).arg(percent));
ui->progressBar->setValue(percent);
}