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 "customfunctiondata.h"
22 #include "eeprominterface.h"
23 #include "radiodata.h"
24 #include "radiodataconversionstate.h"
26 void CustomFunctionData::clear()
28 memset(reinterpret_cast<void *>(this), 0, sizeof(CustomFunctionData
));
29 if (!getCurrentFirmware()->getCapability(SafetyChannelCustomFunction
)) {
34 bool CustomFunctionData::isEmpty() const
36 return (swtch
.type
== SWITCH_TYPE_NONE
);
39 QString
CustomFunctionData::nameToString(int index
, bool globalContext
) const
41 return RadioData::getElementName((globalContext
? tr("GF") : tr("SF")), index
+1, 0, true);
44 QString
CustomFunctionData::funcToString(const ModelData
* model
) const
46 if (func
>= FuncOverrideCH1
&& func
<= FuncOverrideCH32
)
47 return tr("Override %1").arg(RawSource(SOURCE_TYPE_CH
, func
).toString(model
));
48 else if (func
== FuncTrainer
)
50 else if (func
== FuncTrainerRUD
)
51 return tr("Trainer RUD");
52 else if (func
== FuncTrainerELE
)
53 return tr("Trainer ELE");
54 else if (func
== FuncTrainerTHR
)
55 return tr("Trainer THR");
56 else if (func
== FuncTrainerAIL
)
57 return tr("Trainer AIL");
58 else if (func
== FuncInstantTrim
)
59 return tr("Instant Trim");
60 else if (func
== FuncPlaySound
)
61 return tr("Play Sound");
62 else if (func
== FuncPlayHaptic
)
64 else if (func
== FuncReset
)
66 else if (func
>= FuncSetTimer1
&& func
<= FuncSetTimer3
)
67 return tr("Set Timer %1").arg(func
-FuncSetTimer1
+1);
68 else if (func
== FuncVario
)
70 else if (func
== FuncPlayPrompt
)
71 return tr("Play Track");
72 else if (func
== FuncPlayBoth
)
73 return tr("Play Both");
74 else if (func
== FuncPlayValue
)
75 return tr("Play Value");
76 else if (func
== FuncPlayScript
)
77 return tr("Play Script");
78 else if (func
== FuncLogs
)
80 else if (func
== FuncVolume
)
82 else if (func
== FuncBacklight
)
83 return tr("Backlight");
84 else if (func
== FuncScreenshot
)
85 return tr("Screenshot");
86 else if (func
== FuncBackgroundMusic
)
87 return tr("Background Music");
88 else if (func
== FuncBackgroundMusicPause
)
89 return tr("Background Music Pause");
90 else if (func
>= FuncAdjustGV1
&& func
<= FuncAdjustGVLast
)
91 return tr("Adjust %1").arg(RawSource(SOURCE_TYPE_GVAR
, func
-FuncAdjustGV1
).toString(model
));
92 else if (func
== FuncSetFailsafeInternalModule
)
93 return tr("SetFailsafe Int. Module");
94 else if (func
== FuncSetFailsafeExternalModule
)
95 return tr("SetFailsafe Ext. Module");
96 else if (func
== FuncRangeCheckInternalModule
)
97 return tr("RangeCheck Int. Module");
98 else if (func
== FuncRangeCheckExternalModule
)
99 return tr("RangeCheck Ext. Module");
100 else if (func
== FuncBindInternalModule
)
101 return tr("Bind Int. Module");
102 else if (func
== FuncBindExternalModule
)
103 return tr("Bind Ext. Module");
105 return QString("???"); // Highlight unknown functions with output of question marks.(BTW should not happen that we do not know what a function is)
109 void CustomFunctionData::populateResetParams(const ModelData
* model
, QComboBox
* b
, unsigned int value
= 0)
112 Firmware
* firmware
= Firmware::getCurrentVariant();
113 Board::Type board
= firmware
->getBoard();
115 b
->addItem(tr("Timer1"), val
++);
116 b
->addItem(tr("Timer2"), val
++);
118 b
->addItem( tr("Timer3"), val
++);
120 b
->addItem(tr("Flight"), val
++);
121 b
->addItem(tr("Telemetry"), val
++);
122 int reCount
= firmware
->getCapability(RotaryEncoders
);
124 b
->addItem(tr("Rotary Encoder"), val
++);
126 else if (reCount
== 2) {
127 b
->addItem(tr("REa"), val
++);
128 b
->addItem(tr("REb"), val
++);
130 if ((int)value
< b
->count()) {
131 b
->setCurrentIndex(value
);
133 if (model
&& IS_ARM(board
)) {
134 for (unsigned i
=0; i
<CPN_MAX_SENSORS
; ++i
) {
135 if (model
->sensorData
[i
].isAvailable()) {
136 RawSource item
= RawSource(SOURCE_TYPE_TELEMETRY
, 3*i
);
137 b
->addItem(item
.toString(model
), val
+i
);
138 if (value
== val
+i
) {
139 b
->setCurrentIndex(b
->count()-1);
146 void CustomFunctionData::populatePlaySoundParams(QStringList
& qs
)
148 qs
<<"Beep 1" << "Beep 2" << "Beep 3" << "Warn1" << "Warn2" << "Cheep" << "Ratata" << "Tick" << "Siren" << "Ring" ;
149 qs
<< "SciFi" << "Robot" << "Chirp" << "Tada" << "Crickt" << "AlmClk" ;
152 void CustomFunctionData::populateHapticParams(QStringList
& qs
)
154 qs
<< "0" << "1" << "2" << "3";
157 QString
CustomFunctionData::paramToString(const ModelData
* model
) const
160 if (func
<= FuncInstantTrim
) {
161 return QString("%1").arg(param
);
163 else if (func
==FuncLogs
) {
164 return QString("%1").arg(param
/10.0) + tr("s");
166 else if (func
==FuncPlaySound
) {
167 CustomFunctionData::populatePlaySoundParams(qs
);
168 if (param
>=0 && param
<(int)qs
.count())
171 return tr("<font color=red><b>Inconsistent parameter</b></font>");
173 else if (func
==FuncPlayHaptic
) {
174 CustomFunctionData::populateHapticParams(qs
);
175 if (param
>=0 && param
<(int)qs
.count())
178 return tr("<font color=red><b>Inconsistent parameter</b></font>");
180 else if (func
==FuncReset
) {
182 CustomFunctionData::populateResetParams(model
, &cb
);
183 int pos
= cb
.findData(param
);
185 return cb
.itemText(pos
);
187 return tr("<font color=red><b>Inconsistent parameter</b></font>");
189 else if ((func
==FuncVolume
)|| (func
==FuncPlayValue
)) {
190 RawSource
item(param
);
191 return item
.toString(model
);
193 else if ((func
==FuncPlayPrompt
) || (func
==FuncPlayBoth
)) {
194 if ( getCurrentFirmware()->getCapability(VoicesAsNumbers
)) {
195 return QString("%1").arg(param
);
201 else if ((func
>=FuncAdjustGV1
) && (func
<FuncCount
)) {
202 switch (adjustMode
) {
203 case FUNC_ADJUST_GVAR_CONSTANT
:
204 return tr("Value ")+QString("%1").arg(param
);
205 case FUNC_ADJUST_GVAR_SOURCE
:
206 case FUNC_ADJUST_GVAR_GVAR
:
207 return RawSource(param
).toString();
208 case FUNC_ADJUST_GVAR_INCDEC
:
211 if (IS_ARM(getCurrentBoard())) {
212 val
= param
* model
->gvarData
[func
- FuncAdjustGV1
].multiplierGet();
213 unit
= model
->gvarData
[func
- FuncAdjustGV1
].unitToString();
219 return QString("Increment: %1%2").arg(val
).arg(unit
);
225 QString
CustomFunctionData::repeatToString() const
227 if (repeatParam
== -1) {
228 return tr("played once, not during startup");
230 else if (repeatParam
== 0) {
234 unsigned int step
= IS_ARM(getCurrentBoard()) ? 1 : 10;
235 return tr("repeat(%1s)").arg(step
*repeatParam
);
239 QString
CustomFunctionData::enabledToString() const
241 if ((func
>=FuncOverrideCH1
&& func
<=FuncOverrideCH32
) ||
242 (func
>=FuncAdjustGV1
&& func
<=FuncAdjustGVLast
) ||
244 (func
>=FuncSetTimer1
&& func
<=FuncSetTimer2
) ||
245 (func
==FuncVolume
) ||
246 (func
<= FuncInstantTrim
)) {
248 return tr("DISABLED");
254 void CustomFunctionData::convert(RadioDataConversionState
& cstate
)
256 cstate
.setComponent(tr("CFN"), 8);
257 cstate
.setSubComp(nameToString(cstate
.subCompIdx
, (cstate
.toModel() ? false : true)));
258 swtch
.convert(cstate
);
259 if (func
== FuncVolume
|| func
== FuncPlayValue
|| (func
>= FuncAdjustGV1
&& func
<= FuncAdjustGVLast
&& adjustMode
== 1)) {
260 param
= RawSource(param
).convert(cstate
.withComponentField("PARAM")).toValue();