Improve settings migration and management (#5107)
[opentx.git] / companion / src / wizarddata.cpp
blob9e9d2c2ecf789f2a0d7e1b4a8122841aae519bbd
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 <string.h>
22 #include "eeprominterface.h"
23 #include "wizarddata.h"
25 Channel::Channel()
27 clear();
30 void Channel::clear()
32 page = Page_None;
33 prebooked = false;
34 input1 = NO_INPUT;
35 input2 = NO_INPUT;
36 weight1 = 0;
37 weight2 = 0;
40 WizMix::WizMix(const GeneralSettings & settings, unsigned int modelId, const ModelData & modelData):
41 complete(false),
42 modelId(modelId),
43 settings(settings),
44 originalModelData(modelData),
45 vehicle(NOVEHICLE)
47 strncpy(name, originalModelData.name, WIZ_MODEL_NAME_LENGTH);
51 void WizMix::maxMixSwitch(char *name, MixData &mix, int channel, int sw, int weight)
53 strncpy(mix.name, name, MIXDATA_NAME_LEN);
54 mix.name[MIXDATA_NAME_LEN] = '\0';
55 mix.destCh = channel;
56 mix.srcRaw = RawSource(SOURCE_TYPE_MAX);
57 mix.swtch = RawSwitch(SWITCH_TYPE_SWITCH, sw);
58 mix.weight = weight;
61 void WizMix::addMix(ModelData &model, Input input, int weight, int channel, int & mixIndex)
63 if (input != NO_INPUT) {
64 bool isHorusOrTaranis = IS_HORUS_OR_TARANIS(getCurrentBoard());
65 if (input >= RUDDER_INPUT && input <= AILERONS_INPUT) {
66 MixData & mix = model.mixData[mixIndex++];
67 mix.destCh = channel+1;
68 if (isHorusOrTaranis){
69 int channel = settings.getDefaultChannel(input-1);
70 mix.srcRaw = RawSource(SOURCE_TYPE_VIRTUAL_INPUT, channel);
72 else
73 mix.srcRaw = RawSource(SOURCE_TYPE_STICK, input-1);
74 mix.weight = weight;
76 else if (input==FLAPS_INPUT){
77 // There ought to be some kind of constants for switches somewhere...
78 maxMixSwitch((char *)"Flaps Up", model.mixData[mixIndex++], channel+1, isHorusOrTaranis ? SWITCH_SA0 :-SWITCH_ELE , weight); //Taranis-Horus SA-UP, 9X ELE-UP
79 maxMixSwitch((char *)"Flaps Dn", model.mixData[mixIndex++], channel+1, isHorusOrTaranis ? SWITCH_SA2 : SWITCH_ELE , -weight); //Taranis-Horus SA-DOWN, 9X ELE-DOWN
82 else if (input==AIRBRAKES_INPUT){
83 maxMixSwitch((char *)"AirbkOff", model.mixData[mixIndex++], channel+1, isHorusOrTaranis ? SWITCH_SE0 :-SWITCH_RUD , -weight); //Taranis-Horus SE-UP, 9X RUD-UP
84 maxMixSwitch((char *)"AirbkOn", model.mixData[mixIndex++], channel+1, isHorusOrTaranis ? SWITCH_SE2 : SWITCH_RUD , weight); //Tatanis-Horus SE-DOWN, 9X RUD-DOWN
89 WizMix::operator ModelData()
91 int throttleChannel = -1;
93 ModelData model;
94 //ModelData model(originalModelData);
95 model.category = originalModelData.category;
96 model.used = true;
97 model.moduleData[0].modelId = modelId;
98 model.setDefaultInputs(settings);
100 int mixIndex = 0;
101 int switchIndex = 0;
102 int timerIndex = 0;
104 // Safe copy model name
105 strncpy(model.name, name, WIZ_MODEL_NAME_LENGTH);
106 model.name[WIZ_MODEL_NAME_LENGTH] = 0;
108 // Add the channel mixes
109 for (int i=0; i<WIZ_MAX_CHANNELS; i++ )
111 Channel ch = channel[i];
112 if (ch.input1 == THROTTLE_INPUT || ch.input2 == THROTTLE_INPUT)
113 throttleChannel = i;
115 addMix(model, ch.input1, ch.weight1, i, mixIndex);
116 addMix(model, ch.input2, ch.weight2, i, mixIndex);
119 // Add the Throttle Cut option
120 if( options[THROTTLE_CUT_OPTION] && throttleChannel >=0 ){
121 model.customFn[switchIndex].swtch.type = SWITCH_TYPE_SWITCH;
122 model.customFn[switchIndex].swtch.index = IS_HORUS_OR_TARANIS(getCurrentBoard()) ? SWITCH_SF0 : SWITCH_THR;
123 model.customFn[switchIndex].enabled = 1;
124 model.customFn[switchIndex].func = (AssignFunc)throttleChannel;
125 model.customFn[switchIndex].param = -100;
128 // Add the Flight Timer option
129 if (options[FLIGHT_TIMER_OPTION] ){
130 model.timers[timerIndex].mode.type = SWITCH_TYPE_TIMER_MODE;
131 model.timers[timerIndex].mode.index = TMRMODE_THR_TRG;
132 timerIndex++;
135 // Add the Throttle Timer option
136 if (options[THROTTLE_TIMER_OPTION] && throttleChannel >=0){
137 model.timers[timerIndex].mode.type = SWITCH_TYPE_TIMER_MODE;
138 model.timers[timerIndex].mode.index = TMRMODE_THR;
139 timerIndex++;
142 return model;