[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / generaledit / generaledit.cpp
blob9e9273eb6783d526a6f75566ba65f4439e369cdf
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 "generaledit.h"
22 #include "ui_generaledit.h"
23 #include "helpers.h"
24 #include "appdata.h"
25 #include "generalsetup.h"
26 #include "trainer.h"
27 #include "calibration.h"
28 #include "hardware.h"
29 #include "../modeledit/customfunctions.h"
30 #include "verticalscrollarea.h"
32 GeneralEdit::GeneralEdit(QWidget * parent, RadioData & radioData, Firmware * firmware) :
33 QDialog(parent),
34 ui(new Ui::GeneralEdit),
35 generalSettings(radioData.generalSettings),
36 firmware(firmware)
38 ui->setupUi(this);
39 this->setWindowIcon(CompanionIcon("open.png"));
41 QString firmware_id = g.profile[g.id()].fwType();
42 QString name=g.profile[g.id()].name();
43 if (name.isEmpty()) {
44 ui->calstore_PB->setDisabled(true);
47 ui->profile_CB->clear();
48 for (int i=0; i<MAX_PROFILES; ++i) {
49 QString name=g.profile[i].name();
50 if (!name.isEmpty()) {
51 ui->profile_CB->addItem(name, i);
52 if (i==g.id()) {
53 ui->profile_CB->setCurrentIndex(ui->profile_CB->count()-1);
58 addTab(new GeneralSetupPanel(this, generalSettings, firmware), tr("Setup"));
59 if (IS_ARM(firmware->getBoard())) {
60 addTab(new CustomFunctionsPanel(this, NULL, generalSettings, firmware), tr("Global Functions"));
62 addTab(new TrainerPanel(this, generalSettings, firmware), tr("Trainer"));
63 addTab(new HardwarePanel(this, generalSettings, firmware), tr("Hardware"));
64 addTab(new CalibrationPanel(this, generalSettings, firmware), tr("Calibration"));
66 ui->tabWidget->setCurrentIndex( g.generalEditTab() );
69 GeneralEdit::~GeneralEdit()
71 delete ui;
74 void GeneralEdit::closeEvent(QCloseEvent *event)
76 g.generalEditTab(ui->tabWidget->currentIndex());
79 void GeneralEdit::addTab(GenericPanel *panel, QString text)
81 panels << panel;
82 QWidget * widget = new QWidget(ui->tabWidget);
83 QVBoxLayout *baseLayout = new QVBoxLayout(widget);
84 VerticalScrollArea * area = new VerticalScrollArea(widget, panel);
85 baseLayout->addWidget(area);
86 ui->tabWidget->addTab(widget, text);
87 connect(panel, SIGNAL(modified()), this, SLOT(onTabModified()));
90 void GeneralEdit::onTabModified()
92 emit modified();
95 void GeneralEdit::on_tabWidget_currentChanged(int index)
97 panels[index]->update();
100 void GeneralEdit::on_calretrieve_PB_clicked()
102 Board::Type board = getCurrentBoard();
103 int profile_id=ui->profile_CB->itemData(ui->profile_CB->currentIndex()).toInt();
104 QString calib=g.profile[profile_id].stickPotCalib();
105 int potsnum=getBoardCapability(board, Board::Pots)+getBoardCapability(board, Board::Sliders);
106 int numSwPots=getBoardCapability(board, Board::Switches)+getBoardCapability(board, Board::Pots)+getBoardCapability(board, Board::Sliders);
107 if (calib.isEmpty()) {
108 return;
110 else {
111 QString trainercalib = g.profile[profile_id].trainerCalib();
112 QString hwtypes = g.profile[profile_id].controlTypes();
113 QString controlNames=g.profile[profile_id].controlNames();
114 int8_t txVoltageCalibration = (int8_t)g.profile[profile_id].txVoltageCalibration();
115 int8_t txCurrentCalibration = (int8_t)g.profile[profile_id].txCurrentCalibration();
116 int8_t PPM_Multiplier = (int8_t)g.profile[profile_id].ppmMultiplier();
117 uint8_t GSStickMode = (uint8_t)g.profile[profile_id].gsStickMode();
118 uint8_t vBatWarn = (uint8_t)g.profile[profile_id].vBatWarn();
119 QString DisplaySet = g.profile[profile_id].display();
120 QString BeeperSet = g.profile[profile_id].beeper();
121 QString HapticSet = g.profile[profile_id].haptic();
122 QString SpeakerSet = g.profile[profile_id].speaker();
123 QString CountrySet = g.profile[profile_id].countryCode();
125 if ((calib.length()==(CPN_MAX_STICKS+potsnum)*12) && (trainercalib.length()==16)) {
126 QString Byte;
127 int16_t byte16;
128 bool ok;
129 for (int i=0; i<(CPN_MAX_STICKS+potsnum); i++) {
130 Byte=calib.mid(i*12,4);
131 byte16=(int16_t)Byte.toInt(&ok,16);
132 if (ok) generalSettings.calibMid[i]=byte16;
133 Byte=calib.mid(4+i*12,4);
134 byte16=(int16_t)Byte.toInt(&ok,16);
135 if (ok) generalSettings.calibSpanNeg[i]=byte16;
136 Byte=calib.mid(8+i*12,4);
137 byte16=(int16_t)Byte.toInt(&ok,16);
138 if (ok) generalSettings.calibSpanPos[i]=byte16;
140 for (int i=0; i<4; i++) {
141 Byte=trainercalib.mid(i*4,4);
142 byte16=(int16_t)Byte.toInt(&ok,16);
143 if (ok) generalSettings.trainer.calib[i]=byte16;
145 generalSettings.txCurrentCalibration=txCurrentCalibration;
146 generalSettings.txVoltageCalibration=txVoltageCalibration;
147 generalSettings.vBatWarn=vBatWarn;
148 if (getCurrentFirmware()->getCapability(HasBatMeterRange)) {
149 generalSettings.vBatMin = (int8_t) g.profile[profile_id].vBatMin();
150 generalSettings.vBatMax = (int8_t) g.profile[profile_id].vBatMax();
152 generalSettings.PPM_Multiplier=PPM_Multiplier;
153 } else {
154 QMessageBox::critical(this, tr("Warning"), tr("Wrong data in profile, radio calibration was not retrieved"));
156 if (hwtypes.length()==numSwPots) {
157 QString Byte;
158 int16_t byte16;
159 QByteArray qba;
160 int16_t offset;
161 bool ok;
162 for (int i=0; i<CPN_MAX_STICKS; i++) {
163 qba = controlNames.mid(3*i,3).toLatin1();
164 strcpy(generalSettings.stickName[i], qba.data());
166 for (int i=0; i<getBoardCapability(board, Board::Switches); i++) {
167 Byte = hwtypes.mid(i, 1);
168 byte16=(int16_t)Byte.toInt(&ok,16);
169 qba=controlNames.mid(3*(i+CPN_MAX_STICKS),3).toLatin1();
170 if (ok) {
171 generalSettings.switchConfig[i]=byte16;
172 strcpy(generalSettings.switchName[i], qba.data());
175 offset = getBoardCapability(board, Board::Switches);
176 for (int i=0; i<getBoardCapability(board, Board::Pots); i++) {
177 Byte=hwtypes.mid(i+offset,1);
178 byte16=(int16_t)Byte.toInt(&ok,16);
179 qba=controlNames.mid(3*(i+CPN_MAX_STICKS+offset),3).toLatin1();
180 if (ok) {
181 generalSettings.potConfig[i]=byte16;
182 strcpy(generalSettings.potName[i], qba.data());
185 offset += getBoardCapability(board, Board::Pots);
186 for (int i=0; i<getBoardCapability(board, Board::Sliders); i++) {
187 Byte=hwtypes.mid(i+offset,1);
188 byte16=(int16_t)Byte.toInt(&ok,16);
189 qba=controlNames.mid(3*(i+CPN_MAX_STICKS+offset),3).toLatin1();
190 if (ok) {
191 generalSettings.sliderConfig[i]=byte16;
192 strcpy(generalSettings.sliderName[i], qba.data());
195 } else {
196 QMessageBox::critical(this, tr("Warning"), tr("Wrong data in profile, Switch/pot config not retrieved"));
198 if ((DisplaySet.length()==6) && (BeeperSet.length()==4) && (HapticSet.length()==6) && (SpeakerSet.length()==6)) {
199 generalSettings.stickMode=GSStickMode;
200 uint8_t byte8u;
201 int8_t byte8;
202 QString chars;
203 bool ok;
204 byte8=(int8_t)DisplaySet.mid(0,2).toInt(&ok,16);
205 if (ok) generalSettings.optrexDisplay=(byte8==1 ? true : false);
206 byte8u=(uint8_t)DisplaySet.mid(2,2).toUInt(&ok,16);
207 if (ok) generalSettings.contrast=byte8u;
208 byte8u=(uint8_t)DisplaySet.mid(4,2).toUInt(&ok,16);
209 if (ok) generalSettings.backlightBright=byte8u;
210 byte8=(int8_t)BeeperSet.mid(0,2).toUInt(&ok,16);
211 if (ok) generalSettings.beeperMode = (GeneralSettings::BeeperMode)byte8;
212 byte8=(int8_t)BeeperSet.mid(2,2).toInt(&ok,16);
213 if (ok) generalSettings.beeperLength=byte8;
214 byte8=(int8_t)HapticSet.mid(0,2).toUInt(&ok,16);
215 if (ok) generalSettings.hapticMode=(GeneralSettings::BeeperMode)byte8;
216 byte8=(int8_t)HapticSet.mid(2,2).toInt(&ok,16);
217 if (ok) generalSettings.hapticStrength=byte8;
218 byte8=(int8_t)HapticSet.mid(4,2).toInt(&ok,16);
219 if (ok) generalSettings.hapticLength=byte8;
220 byte8u=(uint8_t)SpeakerSet.mid(0,2).toUInt(&ok,16);
221 if (ok) generalSettings.speakerMode=byte8u;
222 byte8u=(uint8_t)SpeakerSet.mid(2,2).toUInt(&ok,16);
223 if (ok) generalSettings.speakerPitch=byte8u;
224 byte8u=(uint8_t)SpeakerSet.mid(4,2).toUInt(&ok,16);
225 if (ok) generalSettings.speakerVolume=byte8u;
226 if (CountrySet.length()==6) {
227 byte8u=(uint8_t)CountrySet.mid(0,2).toUInt(&ok,16);
228 if (ok) generalSettings.countryCode=byte8u;
229 byte8u=(uint8_t)CountrySet.mid(2,2).toUInt(&ok,16);
230 if (ok) generalSettings.imperial=byte8u;
231 chars=CountrySet.mid(4,2);
232 generalSettings.ttsLanguage[0]=chars[0].toLatin1();
233 generalSettings.ttsLanguage[1]=chars[1].toLatin1();
236 else {
237 QMessageBox::critical(this, tr("Warning"), tr("Wrong data in profile, hw related parameters were not retrieved"));
241 emit modified();
244 void GeneralEdit::on_calstore_PB_clicked()
246 int profile_id=ui->profile_CB->itemData(ui->profile_CB->currentIndex()).toInt();
248 QString name=g.profile[profile_id].name();
249 int potsnum=getBoardCapability(getCurrentBoard(), Board::Pots)+getBoardCapability(getCurrentBoard(), Board::Sliders);
250 if (name.isEmpty()) {
251 ui->calstore_PB->setDisabled(true);
252 return;
254 else {
255 QString calib=g.profile[profile_id].stickPotCalib();
256 QString hwtypes=g.profile[profile_id].controlTypes();
257 QString controlNames=g.profile[profile_id].controlNames();
258 if (!(calib.isEmpty())) {
259 int ret = QMessageBox::question(this, "Companion",
260 tr("Do you want to store calibration in %1 profile<br>overwriting existing calibration?").arg(name) ,
261 QMessageBox::Yes | QMessageBox::No);
262 if (ret == QMessageBox::No) {
263 return;
266 calib.clear();
267 for (int i=0; i< (CPN_MAX_STICKS+potsnum); i++) {
268 calib.append(QString("%1").arg((uint16_t)generalSettings.calibMid[i], 4, 16, QChar('0')));
269 calib.append(QString("%1").arg((uint16_t)generalSettings.calibSpanNeg[i], 4, 16, QChar('0')));
270 calib.append(QString("%1").arg((uint16_t)generalSettings.calibSpanPos[i], 4, 16, QChar('0')));
272 g.profile[profile_id].stickPotCalib( calib );
273 calib.clear();
274 for (int i=0; i< 4; i++) {
275 calib.append(QString("%1").arg((uint16_t)generalSettings.trainer.calib[i], 4, 16, QChar('0')));
277 g.profile[profile_id].trainerCalib( calib );
278 hwtypes.clear();
279 controlNames.clear();
280 for (int i=0; i<CPN_MAX_STICKS; i++) {
281 controlNames.append(QString("%1").arg(generalSettings.stickName[i], -3));
283 for (int i=0; i<getBoardCapability(getCurrentBoard(), Board::Switches); i++) {
284 hwtypes.append(QString("%1").arg((uint16_t)generalSettings.switchConfig[i], 1));
285 controlNames.append(QString("%1").arg(generalSettings.switchName[i], -3));
287 for (int i=0; i<getBoardCapability(getCurrentBoard(), Board::Pots); i++) {
288 hwtypes.append(QString("%1").arg((uint16_t)generalSettings.potConfig[i], 1));
289 controlNames.append(QString("%1").arg(generalSettings.potName[i], -3));
291 for (int i=0; i<getBoardCapability(getCurrentBoard(), Board::Sliders); i++) {
292 hwtypes.append(QString("%1").arg((uint16_t)generalSettings.sliderConfig[i], 1));
293 controlNames.append(QString("%1").arg(generalSettings.sliderName[i], -3));
295 g.profile[profile_id].controlTypes( hwtypes );
296 g.profile[profile_id].controlNames( controlNames );
297 g.profile[profile_id].txVoltageCalibration( generalSettings.txVoltageCalibration );
298 g.profile[profile_id].txCurrentCalibration( generalSettings.txCurrentCalibration );
299 g.profile[profile_id].vBatWarn( generalSettings.vBatWarn );
300 if (getCurrentFirmware()->getCapability(HasBatMeterRange)) {
301 g.profile[profile_id].vBatMin( generalSettings.vBatMin );
302 g.profile[profile_id].vBatMax( generalSettings.vBatMax );
304 g.profile[profile_id].ppmMultiplier( generalSettings.PPM_Multiplier );
305 g.profile[profile_id].gsStickMode( generalSettings.stickMode );
306 g.profile[profile_id].display( QString("%1%2%3").arg((generalSettings.optrexDisplay ? 1:0), 2, 16, QChar('0')).arg((uint8_t)generalSettings.contrast, 2, 16, QChar('0')).arg((uint8_t)generalSettings.backlightBright, 2, 16, QChar('0')) );
307 g.profile[profile_id].beeper( QString("%1%2").arg(((uint8_t)generalSettings.beeperMode), 2, 16, QChar('0')).arg((uint8_t)generalSettings.beeperLength, 2, 16, QChar('0')));
308 g.profile[profile_id].haptic( QString("%1%2%3").arg(((uint8_t)generalSettings.hapticMode), 2, 16, QChar('0')).arg((int8_t)generalSettings.hapticStrength, 2, 16, QChar('0')).arg((uint8_t)generalSettings.hapticLength, 2, 16, QChar('0')));
309 g.profile[profile_id].speaker( QString("%1%2%3").arg((uint8_t)generalSettings.speakerMode, 2, 16, QChar('0')).arg((uint8_t)generalSettings.speakerPitch, 2, 16, QChar('0')).arg((uint8_t)generalSettings.speakerVolume, 2, 16, QChar('0')));
310 g.profile[profile_id].countryCode( QString("%1%2%3").arg((uint8_t)generalSettings.countryCode, 2, 16, QChar('0')).arg((uint8_t)generalSettings.imperial, 2, 16, QChar('0')).arg(generalSettings.ttsLanguage));
312 QDateTime dateTime = QDateTime::currentDateTime();
313 g.profile[profile_id].timeStamp(dateTime.toString("yyyy-MM-dd hh:mm"));
314 QMessageBox::information(this, "Companion", tr("Calibration and HW parameters saved."));