missing declare currentPath
[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 = new Programmer(this);
51     theMcu = new Mcu(this);
52
53     connect(theMcu, SIGNAL(currentWidgetChanged(QWidget*)), this, SLOT(changeWidget(QWidget*)));
54     windowBaseSize = sizeHint();
55     connect(theProgrammer, SIGNAL(programmerChanged(QString)), this, SLOT(cBoxProgChanged(QString)));
56     ui->programmerButton->setEnabled(false);
57     connect(ui->programmerButton, SIGNAL(clicked()), this, SLOT(programmerDialog()));
58
59     connect(ui->actionOpenHexProgram, SIGNAL(triggered()), this, SLOT(loadHex()));
60     connect(ui->tbBrowseFile, SIGNAL(clicked()), this, SLOT(loadHex()));
61     connect(ui->flashFile, SIGNAL(textChanged(QString)), this, SLOT(noEdit()));
62 }
63
64 AvrDudeQui::~AvrDudeQui()
65 {
66     delete ui;
67 }
68
69 void AvrDudeQui::noEdit()
70 {
71    ui->flashFile->setText(flashFileName);
72 }
73
74 void AvrDudeQui::operateModeChanged()
75 {
76     updateChange();
77 }
78
79 void AvrDudeQui::clearFlashEdit()
80 {
81     flashFileName.clear();
82     noEdit();
83     ui->ckBoxOnFlash->setEnabled(false);
84     ui->ckBoxOnFlash->setChecked(false);
85         ui->flashFile->setToolTip(flashFileName);
86 }
87
88 void AvrDudeQui::loadHex()
89 {
90     flashFileName = QFileDialog::getOpenFileName(this,
91         tr("Open ihex file"), currentPath, tr("ihex files (*.hex)"));
92     if (flashFileName.isEmpty()) {
93         if (!ui->flashFile->text().isEmpty())
94             flashFileName = ui->flashFile->text();
95         return;
96     }
97     ui->flashFile->setToolTip(flashFileName);
98     currentPath = QDir(flashFileName).absolutePath();
99     ui->flashFile->blockSignals(true);
100     ui->flashFile->setText(flashFileName);
101     ui->flashFile->blockSignals(false);
102     ui->ckBoxOnFlash->setEnabled(true);
103     ui->ckBoxOnFlash->setChecked(true);
104 }
105
106
107
108 void AvrDudeQui::populate()
109 {
110     theProgrammer->fillProgrammer(ui->cBoxProgrammer);
111     theMcu->fillMcu(ui->cBoxMcu);
112 }
113
114 void AvrDudeQui::cBoxProgChanged(const QString &name)
115 {
116     if (!name.isEmpty()) {
117         progData = theProgrammer->map_data();
118         ui->info_prg0->setText("port: " + progData->value("port.device_file"));
119         ui->info_prg1->setText("baurate: " + progData->value("port.baudrate"));
120         //ui->prog_info->setText("Programmer type: " + name);
121         ui->programmerButton->setEnabled(true);
122     } else {
123         ui->programmerButton->setEnabled(false);
124         ui->info_prg0->clear();
125         ui->info_prg1->clear();
126         //ui->prog_info->setText("Programmer type: No programmer selected");
127     }
128     updateChange();
129 }
130
131 void AvrDudeQui::cBoxMcuChanged(int idx)
132 {
133     ui->mcu_info->setText("Mcu type: " + ui->cBoxMcu->itemText(idx));
134
135 }
136
137 void AvrDudeQui::changeWidget(QWidget *mcuWidget)
138 {
139     if (mcuWidget) {
140         ui->splitter->addWidget(mcuWidget);
141         resize(windowBaseSize + mcuWidget->sizeHint());
142         update();
143     } else {
144         // il widget รจ stato rimosso;
145         resize(windowBaseSize);
146         update();
147     }
148     updateChange();
149 }
150
151 void AvrDudeQui::programmerDialog()
152 {
153     //progData = theProgrammer->map_data();
154     ProgrammerSettings settingDialog(progData, this);
155     settingDialog.exec();
156     ui->info_prg0->setText("port: " + progData->value("port.device_file"));
157     ui->info_prg1->setText("baurate: " + progData->value("port.baudrate"));
158     updateChange();
159 }
160
161 void AvrDudeQui::updateChange()
162 {
163     if (theProgrammer->validate()) {
164
165         if (progData->value("port.device_file").isEmpty() || theMcu->getMcu().isEmpty()) {
166             enableButtonWrite(false);
167             ui->ckBoxOnFuse->setDisabled(true);
168         } else {
169             ui->ckBoxOnFuse->setDisabled(false);
170             if (ui->ckBoxOnFlash->isChecked() || ui->ckBoxOnFuse->isChecked()
171                             || ui->ckBoxOnLockBit->isChecked()) {
172                 enableButtonWrite(true);
173             } else {
174                 enableButtonWrite(false);
175             }
176         }
177     
178     }
179 }
180
181 void AvrDudeQui::writeData()
182 {
183     QString commandLine = "";
184     QString command = theMcu->getBackEnd();
185     QString arg_mcu = " -p " + theMcu->getMcu();
186     QString arg_programmer = " -c " + progData->value("id.dudeid");
187     QString arg_port = " -P " + progData->value("port.device_file");
188     QString arg_baudrate = " -b " + progData->value("port.baudrate");
189
190     commandLine = command
191                 + arg_mcu
192                 + arg_programmer
193                 + arg_port;
194     if (!arg_baudrate.isEmpty())
195         commandLine + arg_baudrate;
196
197     if (ui->ckBoxOnFlash->isChecked())
198         commandLine += " -U flash:w:" + flashFileName;
199     if (ui->ckBoxOnFuse->isChecked())
200         commandLine += " " + theMcu->getAllFuse();
201
202 //    qDebug() << commandLine;
203     RunProcessDialog dialog(commandLine, this);
204     int result = dialog.exec();
205 //    qDebug() << "result" << result;
206 }
207
208 void AvrDudeQui::readData()
209 {
210     QString commandLine = "";
211     QString command = theMcu->getBackEnd();
212     QString arg_mcu = " -p " + theMcu->getMcu();
213     QString arg_programmer = " -c " + progData->value("id.dudeid");
214     QString arg_port = " -P " + progData->value("port.device_file");
215     QString arg_baudrate = " -b " + progData->value("port.baudrate");
216 //    qDebug() << arg_baudrate;
217     commandLine = command
218                 + arg_mcu
219                 + arg_programmer
220                 + arg_port;
221     if (arg_baudrate != " -b ")
222         commandLine += arg_baudrate;
223
224     if (ui->ckBoxOnFuse->isChecked()) {
225         commandLine += " " + theMcu->readAllFuse();
226     }
227
228         //commandLine += " " + theMcu->getAllFuse();
229
230
231 //    qDebug() << commandLine;
232     //commandLine += " -v";
233     RunProcessDialog dialog(commandLine, this);
234     int result = dialog.exec();
235     if (!result && ui->ckBoxOnFuse->isChecked()) {
236         ParserOutput parserOutput(dialog.getOutput());
237         QMap<QString, QString> fuses = parserOutput.getAllValue();
238         theMcu->setFuses(fuses);
239         //QString output = dialog.getOutput().simplified();
240         //qDebug() << output;
241     }
242 }
243
244 void AvrDudeQui::enableButtonRead(bool tf = false)
245 {
246     ui->readButton->setEnabled(tf);
247 }
248
249 void AvrDudeQui::enableButtonWrite(bool tf = false)
250 {
251     ui->writeButton->setEnabled(tf);
252 }
253