sistemata la gestione dei devices
[avrdudequi:avrdudequi.git] / avrdudequi.cpp
1 /***************************************************************************
2  *   Copyright (C) 2011 by Maurilio                                        *
3  *   mau_ri_tec@yahoo.it                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "avrdudequi.h"
22 #include "ui_avrdudequi.h"
23 #include <QDebug>
24 #include <QVariant>
25 #include <QFileDialog>
26 #include <QDir>
27
28
29 AvrDudeQui::AvrDudeQui(QWidget *parent) :
30     QMainWindow(parent),
31     ui(new Ui::AvrDudeQui),
32     minimumStep(0), currentPath(QDir::homePath())
33 {
34     ui->setupUi(this);
35     connect(ui->tbClearLE, SIGNAL(clicked()), this, SLOT(clearFlashEdit()));
36     connect(ui->writeButton, SIGNAL(clicked()), this, SLOT(writeData()));
37     connect(ui->readButton, SIGNAL(clicked()), this, SLOT(readData()));
38     enableButtonWrite(false);
39     enableButtonRead(true);
40     ui->ckBoxOnFlash->setEnabled(false);
41     ui->ckBoxOnFuse->setEnabled(false);
42     ui->ckBoxOnLockBit->setEnabled(false);
43     connect(ui->ckBoxOnFlash, SIGNAL(toggled(bool)), this, SLOT(operateModeChanged()));
44     connect(ui->ckBoxOnFuse, SIGNAL(toggled(bool)), this, SLOT(operateModeChanged()));
45     connect(ui->ckBoxOnLockBit, SIGNAL(toggled(bool)), this, SLOT(operateModeChanged()));
46
47     connect(ui->cBoxMcu, SIGNAL(currentIndexChanged(int)), this, SLOT(cBoxMcuChanged(int)));
48     cBoxMcuChanged(ui->cBoxMcu->currentIndex());
49
50     theProgrammer = Programmer::instance();
51     theProgrammer->setParent(this);
52     theMcu = Mcu::instance();
53     theMcu->setParent(this);
54
55
56
57     connect(theMcu, SIGNAL(currentWidgetChanged(QWidget*)), this, SLOT(changeWidget(QWidget*)));
58     windowBaseSize = sizeHint();
59     connect(theProgrammer, SIGNAL(programmerChanged(QString)), this, SLOT(cBoxProgChanged(QString)));
60     ui->programmerButton->setEnabled(false);
61     connect(ui->programmerButton, SIGNAL(clicked()), this, SLOT(programmerDialog()));
62
63     connect(ui->actionOpenHexProgram, SIGNAL(triggered()), this, SLOT(loadHex()));
64     connect(ui->tbBrowseFile, SIGNAL(clicked()), this, SLOT(loadHex()));
65     connect(ui->flashFile, SIGNAL(textChanged(QString)), this, SLOT(noEdit()));
66
67     //SysDevice *sysDevice = SysDevice::instance();
68     //connect(sysDevice, SIGNAL(deviceRemoved(UdevQt::Device)), this, SLOT(deviceRemoved(UdevQt::Device)));
69 }
70
71 AvrDudeQui::~AvrDudeQui()
72 {
73     delete ui;
74 }
75
76 void AvrDudeQui::noEdit()
77 {
78    ui->flashFile->setText(flashFileName);
79 }
80
81 void AvrDudeQui::operateModeChanged()
82 {
83     updateChange();
84 }
85
86 void AvrDudeQui::clearFlashEdit()
87 {
88     flashFileName.clear();
89     noEdit();
90     ui->ckBoxOnFlash->setEnabled(false);
91     ui->ckBoxOnFlash->setChecked(false);
92         ui->flashFile->setToolTip(flashFileName);
93 }
94
95 void AvrDudeQui::loadHex()
96 {
97     flashFileName = QFileDialog::getOpenFileName(this,
98         tr("Open ihex file"), currentPath, tr("ihex files (*.hex)"));
99     if (flashFileName.isEmpty()) {
100         if (!ui->flashFile->text().isEmpty())
101             flashFileName = ui->flashFile->text();
102         return;
103     }
104     ui->flashFile->setToolTip(flashFileName);
105     currentPath = QDir(flashFileName).absolutePath();
106     ui->flashFile->blockSignals(true);
107     ui->flashFile->setText(flashFileName);
108     ui->flashFile->blockSignals(false);
109     ui->ckBoxOnFlash->setEnabled(true);
110     ui->ckBoxOnFlash->setChecked(true);
111 }
112
113
114
115 void AvrDudeQui::populate()
116 {
117     theProgrammer->fillProgrammer(ui->cBoxProgrammer);
118     theMcu->fillMcu(ui->cBoxMcu);
119 }
120
121 void AvrDudeQui::cBoxProgChanged(const QString &name)
122 {
123     if (theProgrammer->isValid()) {
124         progData = theProgrammer->dataMap();
125
126         ui->info_prg0->setText("port: " + progData->value("device.device_file"));
127         if (progData->value("device.baudrate").isEmpty())
128             ui->info_prg1->setText("bitclock: " + progData->value("id.bitclock"));
129         else
130             ui->info_prg1->setText("baurate: " + progData->value("device.baudrate"));
131         //ui->prog_info->setText("Programmer type: " + name);
132         ui->programmerButton->setEnabled(true);
133     } else {
134         ui->programmerButton->setEnabled(false);
135         ui->info_prg0->clear();
136         ui->info_prg1->clear();
137         //ui->prog_info->setText("Programmer type: No programmer selected");
138     }
139     updateChange();
140 }
141
142 void AvrDudeQui::cBoxMcuChanged(int idx)
143 {
144     ui->mcu_info->setText("Mcu type: " + ui->cBoxMcu->itemText(idx));
145
146 }
147
148 void AvrDudeQui::changeWidget(QWidget *mcuWidget)
149 {
150     if (mcuWidget) {
151         ui->splitter->addWidget(mcuWidget);
152         resize(windowBaseSize + mcuWidget->sizeHint());
153         update();
154     } else {
155         // il widget è stato rimosso;
156         resize(windowBaseSize);
157         update();
158     }
159     updateChange();
160 }
161
162 void AvrDudeQui::programmerDialog()
163 {
164     progData = theProgrammer->dataMap();
165     ProgrammerSettings settingDialog(this);
166     connect(theProgrammer, SIGNAL(programmerChanged(QString)), &settingDialog, SLOT(abort(QString)));
167     int result = settingDialog.exec();
168
169     if (result && theProgrammer->isValid()) { // pressed ok
170         theProgrammer->setCurrentDevice(settingDialog.currentDevice());
171         updateChange();
172         // id.bitclock ora è opt.bitclock !*!*!*!sistemare!*!*!*!*!
173         ui->info_prg0->setText("port: " + progData->value("device.device_file"));
174         if (progData->value("device.baudrate").isEmpty())
175             ui->info_prg1->setText("bitclock: " + progData->value("opt.bitclock"));
176         else
177             ui->info_prg1->setText("baurate: " + progData->value("device.baudrate"));
178     } else {    // pressed cancel
179         ui->info_prg0->clear();
180         ui->info_prg1->clear();
181
182     }
183 }
184
185 void AvrDudeQui::updateChange()
186 {
187
188     if (!theProgrammer->isValid() || theMcu->getMcu().isEmpty()) {
189         enableButtonWrite(false);
190         enableButtonRead(false);
191         ui->ckBoxOnFuse->setDisabled(true);
192     } else {
193         enableButtonRead(true);
194         ui->ckBoxOnFuse->setDisabled(false);
195         if (ui->ckBoxOnFlash->isChecked() || ui->ckBoxOnFuse->isChecked()
196                 || ui->ckBoxOnLockBit->isChecked()) {
197             enableButtonWrite(true);
198         } else {
199             enableButtonWrite(false);
200         }
201     }
202 }
203
204 void AvrDudeQui::writeData()
205 {
206     QString commandLine = "";
207     QString command = theMcu->getBackEnd();
208     QString arg_mcu = " -p " + theMcu->getMcu();
209     QString arg_programmer = " -c " + progData->value("id.dudeid");
210     QString arg_port = " -P " + progData->value("device.device_file");
211     QString arg_baudrate = " -b " + progData->value("device.baudrate");
212
213     commandLine = command
214                 + arg_mcu
215                 + arg_programmer
216                 + arg_port;
217     if (!arg_baudrate.isEmpty())
218         commandLine + arg_baudrate;
219
220     if (ui->ckBoxOnFlash->isChecked())
221         commandLine += " -U flash:w:" + flashFileName;
222     if (ui->ckBoxOnFuse->isChecked())
223         commandLine += " " + theMcu->getAllFuse();
224
225 //    qDebug() << commandLine;
226     RunProcessDialog dialog(commandLine, this);
227     int result = dialog.exec();
228 //    qDebug() << "result" << result;
229 }
230
231 void AvrDudeQui::readData()
232 {
233
234
235     QString commandLine = "";
236     QString command = theMcu->getBackEnd();
237     QString arg_mcu = " -p " + theMcu->getMcu();
238     QString arg_programmer = " -c " + progData->value("id.dudeid");
239     QString arg_port = " -P " + progData->value("device.device_file");
240     QString arg_baudrate = " -b " + progData->value("device.baudrate");
241 //    qDebug() << arg_baudrate;
242     commandLine = command
243                 + arg_mcu
244                 + arg_programmer
245                 + arg_port;
246     if (arg_baudrate != " -b ")
247         commandLine += arg_baudrate;
248
249     if (ui->ckBoxOnFuse->isChecked()) {
250         commandLine += " " + theMcu->readAllFuse();
251     }
252
253         //commandLine += " " + theMcu->getAllFuse();
254
255
256 //    qDebug() << commandLine;
257     //commandLine += " -v";
258     RunProcessDialog dialog(commandLine, this);
259     int result = dialog.exec();
260     if (!result && ui->ckBoxOnFuse->isChecked()) {
261         ParserOutput parserOutput(dialog.getOutput());
262         QMap<QString, QString> fuses = parserOutput.getAllValue();
263         theMcu->setFuses(fuses);
264         //QString output = dialog.getOutput().simplified();
265         //qDebug() << output;
266     }
267 }
268
269 void AvrDudeQui::enableButtonRead(bool tf = false)
270 {
271     ui->readButton->setEnabled(tf);
272 }
273
274 void AvrDudeQui::enableButtonWrite(bool tf = false)
275 {
276     ui->writeButton->setEnabled(tf);
277 }
278