[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / flasheepromdialog.cpp
blobadd1a697ca974292600ef5c53e1ee0cc8c86726a
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include "flasheepromdialog.h"
22 #include "ui_flasheepromdialog.h"
23 #include "eeprominterface.h"
24 #include "helpers.h"
25 #include "storage.h"
26 #include "appdata.h"
27 #include "progressdialog.h"
28 #include "radiointerface.h"
29 #include "splashlibrarydialog.h"
31 FlashEEpromDialog::FlashEEpromDialog(QWidget *parent, const QString &filename):
32 QDialog(parent),
33 ui(new Ui::FlashEEpromDialog),
34 eepromFilename(filename),
35 radioData(new RadioData())
37 ui->setupUi(this);
38 ui->profileLabel->setText(tr("Current profile: %1").arg(g.profile[g.id()].name()));
39 if (!filename.isEmpty()) {
40 ui->eepromFilename->hide();
41 ui->eepromLoad->hide();
43 QString backupPath = g.profile[g.id()].pBackupDir();
44 if (backupPath.isEmpty()) {
45 backupPath=g.backupDir();
46 ui->backupBeforeWrite->setChecked(g.enableBackup() || g.profile[g.id()].penableBackup());
48 if (backupPath.isEmpty() || !QDir(backupPath).exists()) {
49 ui->backupBeforeWrite->setEnabled(false);
53 FlashEEpromDialog::~FlashEEpromDialog()
55 delete ui;
58 void FlashEEpromDialog::showEvent(QShowEvent *)
60 updateUI();
63 void FlashEEpromDialog::updateUI()
65 // ui->burnButton->setEnabled(true);
66 ui->eepromFilename->setText(eepromFilename);
67 if (!eepromFilename.isEmpty() && getEEpromVersion(eepromFilename) >= 0) {
68 ui->profileLabel->show();
69 ui->patchCalibration->show();
70 ui->patchHardwareSettings->show();
71 QString name = g.profile[g.id()].name();
72 QString calib = g.profile[g.id()].stickPotCalib();
73 QString trainercalib = g.profile[g.id()].trainerCalib();
74 QString DisplaySet = g.profile[g.id()].display();
75 QString BeeperSet = g.profile[g.id()].beeper();
76 QString HapticSet = g.profile[g.id()].haptic();
77 QString SpeakerSet = g.profile[g.id()].speaker();
78 if (!name.isEmpty()) {
79 ui->profileLabel->show();
80 ui->patchCalibration->show();
81 ui->patchHardwareSettings->show();
82 // TODO I hardcode the number of pots here, should be dependant on the board?
83 if (!((calib.length()==(CPN_MAX_STICKS+3)*12) && (trainercalib.length()==16))) {
84 ui->patchCalibration->setDisabled(true);
86 if (!((DisplaySet.length()==6) && (BeeperSet.length()==4) && (HapticSet.length()==6) && (SpeakerSet.length()==6))) {
87 ui->patchHardwareSettings->setDisabled(true);
90 else {
91 ui->profileLabel->hide();
93 ui->burnButton->setEnabled(true);
95 else {
96 ui->profileLabel->hide();
97 ui->patchCalibration->hide();
98 ui->patchHardwareSettings->hide();
100 QTimer::singleShot(0, this, SLOT(shrink()));
103 void FlashEEpromDialog::on_eepromLoad_clicked()
105 QString filename = QFileDialog::getOpenFileName(this, tr("Choose Radio Backup file"), g.eepromDir(), EXTERNAL_EEPROM_FILES_FILTER);
106 if (!filename.isEmpty()) {
107 eepromFilename = filename;
108 updateUI();
112 int FlashEEpromDialog::getEEpromVersion(const QString & filename)
114 int result = -1;
115 QSharedPointer<RadioData> radioData = QSharedPointer<RadioData>(new RadioData());
116 Storage storage(filename);
117 if (storage.load(*radioData)) {
118 result = radioData->generalSettings.version;
120 else {
121 QMessageBox::warning(this, tr("Error"), storage.error());
123 return result;
126 bool FlashEEpromDialog::patchCalibration()
128 QString calib = g.profile[g.id()].stickPotCalib();
129 QString trainercalib = g.profile[g.id()].trainerCalib();
130 int potsnum = getBoardCapability(getCurrentBoard(), Board::Pots);
131 int8_t txVoltageCalibration = (int8_t) g.profile[g.id()].txVoltageCalibration();
132 int8_t txCurrentCalibration = (int8_t) g.profile[g.id()].txCurrentCalibration();
133 int8_t PPM_Multiplier = (int8_t) g.profile[g.id()].ppmMultiplier();
135 if ((calib.length()==(CPN_MAX_STICKS+potsnum)*12) && (trainercalib.length()==16)) {
136 QString Byte;
137 int16_t byte16;
138 bool ok;
139 for (int i=0; i<CPN_MAX_STICKS+potsnum; i++) {
140 Byte=calib.mid(i*12,4);
141 byte16=(int16_t)Byte.toInt(&ok,16);
142 if (ok)
143 radioData->generalSettings.calibMid[i]=byte16;
144 Byte=calib.mid(4+i*12,4);
145 byte16=(int16_t)Byte.toInt(&ok,16);
146 if (ok)
147 radioData->generalSettings.calibSpanNeg[i]=byte16;
148 Byte=calib.mid(8+i*12,4);
149 byte16=(int16_t)Byte.toInt(&ok,16);
150 if (ok)
151 radioData->generalSettings.calibSpanPos[i]=byte16;
153 for (int i=0; i<4; i++) {
154 Byte = trainercalib.mid(i*4,4);
155 byte16 = (int16_t)Byte.toInt(&ok,16);
156 if (ok) {
157 radioData->generalSettings.trainer.calib[i] = byte16;
160 radioData->generalSettings.txCurrentCalibration = txCurrentCalibration;
161 radioData->generalSettings.txVoltageCalibration = txVoltageCalibration;
162 radioData->generalSettings.PPM_Multiplier = PPM_Multiplier;
163 return true;
165 else {
166 QMessageBox::critical(this, tr("Warning"), tr("Wrong radio calibration data in profile, Settings not patched"));
167 return false;
171 bool FlashEEpromDialog::patchHardwareSettings()
173 QString DisplaySet = g.profile[g.id()].display();
174 QString BeeperSet = g.profile[g.id()].beeper();
175 QString HapticSet = g.profile[g.id()].haptic();
176 QString SpeakerSet = g.profile[g.id()].speaker();
177 uint8_t GSStickMode=(uint8_t) g.profile[g.id()].gsStickMode();
178 uint8_t vBatWarn=(uint8_t) g.profile[g.id()].vBatWarn();
180 if ((DisplaySet.length()==6) && (BeeperSet.length()==4) && (HapticSet.length()==6) && (SpeakerSet.length()==6)) {
181 radioData->generalSettings.vBatWarn = vBatWarn;
182 radioData->generalSettings.stickMode = GSStickMode;
183 uint8_t byte8u;
184 int8_t byte8;
185 bool ok;
186 byte8=(int8_t)DisplaySet.mid(0,2).toInt(&ok,16);
187 if (ok) radioData->generalSettings.optrexDisplay=(byte8==1 ? true : false);
188 byte8u=(uint8_t)DisplaySet.mid(2,2).toUInt(&ok,16);
189 if (ok) radioData->generalSettings.contrast=byte8u;
190 byte8u=(uint8_t)DisplaySet.mid(4,2).toUInt(&ok,16);
191 if (ok) radioData->generalSettings.backlightBright=byte8u;
192 byte8u=(uint8_t)BeeperSet.mid(0,2).toUInt(&ok,16);
193 if (ok) radioData->generalSettings.beeperMode=(GeneralSettings::BeeperMode)byte8u;
194 byte8=(int8_t)BeeperSet.mid(2,2).toInt(&ok,16);
195 if (ok) radioData->generalSettings.beeperLength=byte8;
196 byte8u=(uint8_t)HapticSet.mid(0,2).toUInt(&ok,16);
197 if (ok) radioData->generalSettings.hapticMode=(GeneralSettings::BeeperMode)byte8u;
198 byte8u=(uint8_t)HapticSet.mid(2,2).toUInt(&ok,16);
199 if (ok) radioData->generalSettings.hapticStrength=byte8u;
200 byte8=(int8_t)HapticSet.mid(4,2).toInt(&ok,16);
201 if (ok) radioData->generalSettings.hapticLength=byte8;
202 byte8u=(uint8_t)SpeakerSet.mid(0,2).toUInt(&ok,16);
203 if (ok) radioData->generalSettings.speakerMode=byte8u;
204 byte8u=(uint8_t)SpeakerSet.mid(2,2).toUInt(&ok,16);
205 if (ok) radioData->generalSettings.speakerPitch=byte8u;
206 byte8u=(uint8_t)SpeakerSet.mid(4,2).toUInt(&ok,16);
207 if (ok) radioData->generalSettings.speakerVolume=byte8u;
208 return true;
210 else {
211 QMessageBox::critical(this, tr("Warning"), tr("Wrong radio setting data in profile, Settings not patched"));
212 return false;
216 void FlashEEpromDialog::on_burnButton_clicked()
218 // patch the eeprom if needed
219 bool patch = false;
220 if (ui->patchCalibration->isChecked()) {
221 patch |= patchCalibration();
223 if (ui->patchHardwareSettings->isChecked()) {
224 patch |= patchHardwareSettings();
226 QString filename = eepromFilename;
227 if (patch) {
228 QString filename = generateProcessUniqueTempFileName("temp.bin");
229 QFile file(filename);
230 uint8_t *eeprom = (uint8_t*)malloc(Boards::getEEpromSize(getCurrentBoard()));
231 int eeprom_size = getCurrentEEpromInterface()->save(eeprom, *radioData, 0, getCurrentFirmware()->getVariantNumber());
232 if (eeprom_size == 0) {
233 return;
235 if (!file.open(QIODevice::WriteOnly)) {
236 QMessageBox::warning(this, tr("Error"), tr("Cannot write file %1:\n%2.").arg(filename).arg(file.errorString()));
237 return;
239 QTextStream outputStream(&file);
240 long result = file.write((char*)eeprom, eeprom_size);
241 if (result != eeprom_size) {
242 QMessageBox::warning(this, tr("Error"), tr("Error writing file %1:\n%2.").arg(filename).arg(file.errorString()));
243 return;
247 close();
249 ProgressDialog progressDialog(this, tr("Write Models and Settings to Radio"), CompanionIcon("write_eeprom.png"));
251 // backup previous EEPROM if requested
252 QString backupFilename;
253 QString backupPath;
254 if (ui->backupBeforeWrite->isChecked()) {
255 backupPath = g.profile[g.id()].pBackupDir();
256 if (backupPath.isEmpty()) {
257 backupPath=g.backupDir();
259 backupFilename = backupPath + "/backup-" + QDateTime().currentDateTime().toString("yyyy-MM-dd-HHmmss") + ".bin";
261 else if (ui->checkFirmwareCompatibility->isChecked()) {
262 backupFilename = generateProcessUniqueTempFileName("eeprom.bin");
264 if (!backupFilename.isEmpty()) {
265 if (!readEeprom(backupFilename, progressDialog.progress())) {
266 return;
270 // check EEPROM compatibility if requested
271 if (ui->checkFirmwareCompatibility->isChecked()) {
272 int eepromVersion = getEEpromVersion(filename);
273 QString firmwareFilename = generateProcessUniqueTempFileName("flash.bin");
274 if (!readFirmware(firmwareFilename, progressDialog.progress()))
275 return;
276 QString compatEEprom = generateProcessUniqueTempFileName("compat.bin");
277 if (convertEEprom(filename, compatEEprom, firmwareFilename)) {
278 int compatVersion = getEEpromVersion(compatEEprom);
279 if ((compatVersion / 100) != (eepromVersion / 100)) {
280 QMessageBox::warning(this, tr("Warning"), tr("The radio firmware belongs to another product family, check file and preferences!"));
281 return;
283 else if (compatVersion < eepromVersion) {
284 QMessageBox::warning(this, tr("Warning"), tr("The radio firmware is outdated, please upgrade!"));
285 return;
287 filename = compatEEprom;
289 else if (QMessageBox::question(this, "Error", tr("Cannot check Models and Settings compatibility! Continue anyway?"), QMessageBox::Yes|QMessageBox::No) == QMessageBox::No) {
290 return;
292 qunlink(firmwareFilename);
295 // and write...
296 bool result = writeEeprom(filename, progressDialog.progress());
297 if (!result && !progressDialog.isEmpty()) {
298 progressDialog.exec();
302 void FlashEEpromDialog::on_cancelButton_clicked()
304 close();
307 void FlashEEpromDialog::shrink()
309 resize(0, 0);