[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / radiodata.cpp
blobe770bb8beefa7a1affead6f48f15f89ed4e333e8
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 "radiodata.h"
22 #include "eeprominterface.h"
24 // TODO here we will move a lot of functions from eeprominterface.cpp when no merge risk
26 RawSource RawSource::convert(Board::Type before, Board::Type after)
28 if (type == SOURCE_TYPE_STICK && index >= 4 + getBoardCapability(before, Board::Pots)) {
29 // 1st slider alignment
30 index += getBoardCapability(after, Board::Pots) - getBoardCapability(before, Board::Pots);
33 if (IS_HORUS(after)) {
34 if (IS_TARANIS_X9D(before)) {
35 if (type == SOURCE_TYPE_STICK && index >= 7) {
36 // LS and RS on Horus are after sliders L1 and L2
37 index += 2;
42 // SWI to SWR don't exist on !X9E board
43 if (!IS_TARANIS_X9E(after) && IS_TARANIS_X9E(before)) {
44 if (type == SOURCE_TYPE_SWITCH && index >= 8) {
45 index = index % 8;
49 if (IS_TARANIS_X7(after)) {
50 // No S3, LS and RS on X7 board
51 if (type == SOURCE_TYPE_STICK && index >= 6) {
52 index = 5;
55 // No SE and SG on X7 board
56 if ((IS_TARANIS_X9(before) || IS_HORUS(before)) && type == SOURCE_TYPE_SWITCH) {
57 if (index == 4 || index == 6) {
58 index = 3; // SG and SE to SD
60 else if (index == 5) {
61 index = 4; // SF to SF
63 else if (index == 7) {
64 index = 5; // SH to SH
69 // Compensate for SE and SG on X9/Horus board if converting from X7
70 if ((IS_TARANIS_X9(after) || IS_HORUS(after)) && IS_TARANIS_X7(before) && type == SOURCE_TYPE_SWITCH) {
71 if (index == 4) {
72 index = 5; // SF to SF
74 else if (index == 5) {
75 index = 7; // SH to SH
79 return *this;
82 RawSwitch RawSwitch::convert(Board::Type before, Board::Type after)
84 if (!index || type != SWITCH_TYPE_SWITCH) {
85 return *this; // no changes
88 // SWI to SWR don't exist on !X9E board
89 if (!IS_TARANIS_X9E(after) && IS_TARANIS_X9E(before)) {
90 if (abs(index) > 24) {
91 index = index % 24;
95 int srcIdx = div(abs(index)-1, 3).quot; // raw source index
96 int delta = 0;
98 // No SE and SG on X7 board
99 if (IS_TARANIS_X7(after) && (IS_TARANIS_X9(before) || IS_HORUS(before))) {
100 if (srcIdx == 4 || srcIdx == 5) {
101 delta = 3; // SE to SD & SF to SF
103 else if (srcIdx == 6) {
104 delta = 9; // SG to SD
106 else if (srcIdx == 7) {
107 delta = 6; // SH to SH
111 // Compensate for SE and SG on X9/Horus board if converting from X7
112 if ((IS_TARANIS_X9(after) || IS_HORUS(after)) && IS_TARANIS_X7(before)) {
113 if (srcIdx == 4) {
114 delta = -3; // SF to SF
116 else if (srcIdx == 5) {
117 delta = -6; // SH to SH
121 if (index < 0) {
122 delta = -delta; // invert for !switch
125 index -= delta;
127 return *this;
130 void ExpoData::convert(Board::Type before, Board::Type after)
132 srcRaw.convert(before, after);
133 swtch.convert(before, after);
136 void MixData::convert(Board::Type before, Board::Type after)
138 srcRaw.convert(before, after);
139 swtch.convert(before, after);
142 void LogicalSwitchData::convert(Board::Type before, Board::Type after)
144 CSFunctionFamily family = getFunctionFamily();
145 switch(family) {
146 case LS_FAMILY_VOFS:
147 val1 = RawSource(val1).convert(before, after).toValue();
148 break;
149 case LS_FAMILY_STICKY:
150 case LS_FAMILY_VBOOL:
151 val1 = RawSwitch(val1).convert(before, after).toValue();
152 val2 = RawSwitch(val2).convert(before, after).toValue();
153 break;
154 case LS_FAMILY_EDGE:
155 val1 = RawSwitch(val1).convert(before, after).toValue();
156 break;
157 case LS_FAMILY_VCOMP:
158 val1 = RawSource(val1).convert(before, after).toValue();
159 val2 = RawSource(val2).convert(before, after).toValue();
160 break;
161 default:
162 break;
165 andsw = RawSwitch(andsw).convert(before, after).toValue();
168 void CustomFunctionData::convert(Board::Type before, Board::Type after)
170 swtch.convert(before, after);
173 void FlightModeData::convert(Board::Type before, Board::Type after)
175 swtch.convert(before, after);
178 void ModelData::convert(Board::Type before, Board::Type after)
180 // Here we can add explicit conversions when moving from one board to another
181 for (int i=0; i<CPN_MAX_MIXERS; i++) {
182 mixData[i].convert(before, after);
185 for (int i=0; i<CPN_MAX_EXPOS; i++) {
186 expoData[i].convert(before, after);
189 for (int i=0; i<CPN_MAX_LOGICAL_SWITCHES; i++) {
190 logicalSw[i].convert(before, after);
193 for (int i=0; i<CPN_MAX_SPECIAL_FUNCTIONS; i++) {
194 customFn[i].convert(before, after);
197 for (int i=0; i<CPN_MAX_FLIGHT_MODES; i++) {
198 flightModeData[i].convert(before, after);
202 void GeneralSettings::convert(Board::Type before, Board::Type after)
204 // Here we can add explicit conversions when moving from one board to another
206 // No SE and SG on X7 board
207 if (IS_TARANIS_X7(after)) {
208 if (IS_TARANIS_X9(before) || IS_HORUS(before)) {
209 switchConfig[4] = switchConfig[5];
210 memcpy(switchName[4], switchName[5], sizeof(switchName[4]));
211 switchConfig[5] = switchConfig[7];
212 memcpy(switchName[5], switchName[7], sizeof(switchName[5]));
219 * RadioData
222 RadioData::RadioData()
224 models.resize(getCurrentFirmware()->getCapability(Models));
227 void RadioData::setCurrentModel(unsigned int index)
229 generalSettings.currModelIndex = index;
230 if (index < models.size()) {
231 strcpy(generalSettings.currModelFilename, models[index].filename);
235 void RadioData::fixModelFilename(unsigned int index)
237 ModelData & model = models[index];
238 QString filename(model.filename);
239 bool ok = filename.endsWith(".bin");
240 if (ok) {
241 if (filename.startsWith("model") && filename.mid(5, filename.length()-9).toInt() > 0) {
242 ok = false;
245 if (ok) {
246 for (unsigned i=0; i<index; i++) {
247 if (strcmp(models[i].filename, model.filename) == 0) {
248 ok = false;
249 break;
253 if (!ok) {
254 sprintf(model.filename, "model%d.bin", index+1);
258 void RadioData::fixModelFilenames()
260 for (unsigned int i=0; i<models.size(); i++) {
261 fixModelFilename(i);
263 setCurrentModel(generalSettings.currModelIndex);
266 QString RadioData::getNextModelFilename()
268 char filename[sizeof(ModelData::filename)];
269 int index = 0;
270 bool found = true;
271 while (found) {
272 sprintf(filename, "model%d.bin", ++index);
273 found = false;
274 for (unsigned int i=0; i<models.size(); i++) {
275 if (strcmp(filename, models[i].filename) == 0) {
276 found = true;
277 break;
281 return filename;
284 void RadioData::convert(Board::Type before, Board::Type after)
286 generalSettings.convert(before, after);
287 for (unsigned i=0; i<models.size(); i++) {
288 models[i].convert(before, after);
290 if (categories.size() == 0) {
291 categories.push_back(CategoryData(qPrintable(QObject::tr("Models"))));
292 for (unsigned i=0; i<models.size(); i++) {
293 models[i].category = 0;
297 if (IS_HORUS(after)) {
298 fixModelFilenames();