Make TX volatge for simu more flexible (#7124)
[opentx.git] / companion / src / firmwares / radiodata.cpp
blob0710910a090839cef1b20feaa4e7c9dd6e3f687f
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 "radiodataconversionstate.h"
23 #include "eeprominterface.h"
25 // static
26 QString RadioData::getElementName(const QString & prefix, unsigned int index, const char * name, bool padding)
28 QString result = prefix;
29 if (padding)
30 result += QString("%1").arg(index, 2, 10, QChar('0'));
31 else
32 result += QString("%1").arg(index);
33 if (name) {
34 QString trimmed = QString(name).trimmed();
35 if (trimmed.length() > 0) {
36 result += ":" + QString(name).trimmed();
39 return result;
42 RadioData::RadioData()
44 models.resize(getCurrentFirmware()->getCapability(Models));
47 void RadioData::setCurrentModel(unsigned int index)
49 generalSettings.currModelIndex = index;
50 if (index < models.size()) {
51 strcpy(generalSettings.currModelFilename, models[index].filename);
55 void RadioData::fixModelFilename(unsigned int index)
57 ModelData & model = models[index];
58 QString filename(model.filename);
59 bool ok = filename.endsWith(".bin");
60 if (ok) {
61 if (filename.startsWith("model") && filename.mid(5, filename.length()-9).toInt() > 0) {
62 ok = false;
65 if (ok) {
66 for (unsigned i=0; i<index; i++) {
67 if (strcmp(models[i].filename, model.filename) == 0) {
68 ok = false;
69 break;
73 if (!ok) {
74 sprintf(model.filename, "model%d.bin", index+1);
78 void RadioData::fixModelFilenames()
80 for (unsigned int i=0; i<models.size(); i++) {
81 fixModelFilename(i);
83 setCurrentModel(generalSettings.currModelIndex);
86 QString RadioData::getNextModelFilename()
88 char filename[sizeof(ModelData::filename)];
89 int index = 0;
90 bool found = true;
91 while (found) {
92 sprintf(filename, "model%d.bin", ++index);
93 found = false;
94 for (unsigned int i=0; i<models.size(); i++) {
95 if (strcmp(filename, models[i].filename) == 0) {
96 found = true;
97 break;
101 return filename;
104 void RadioData::convert(RadioDataConversionState & cstate)
106 generalSettings.convert(cstate.withModelIndex(-1));
108 for (unsigned i=0; i<models.size(); i++) {
109 models[i].convert(cstate.withModelIndex(i));
112 if (categories.size() == 0) {
113 categories.push_back(CategoryData(qPrintable(tr("Models"))));
114 for (unsigned i=0; i<models.size(); i++) {
115 models[i].category = 0;
119 if (IS_HORUS(cstate.toType)) {
120 fixModelFilenames();
123 // ensure proper number of model slots
124 if (getCurrentFirmware()->getCapability(Models) && getCurrentFirmware()->getCapability(Models) != (int)models.size()) {
125 models.resize(getCurrentFirmware()->getCapability(Models));