Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / tests / switches.cpp
blob4eae3ba5caf1d4b1a3cb3abb0240cebd4405e21b
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 "gtests.h"
23 void setLogicalSwitch(int index, uint16_t _func, int16_t _v1, int16_t _v2, int16_t _v3 = 0, uint8_t _delay = 0, uint8_t _duration = 0, int8_t _andsw = 0)
25 g_model.logicalSw[index].func = _func;
26 g_model.logicalSw[index].v1 = _v1;
27 g_model.logicalSw[index].v2 = _v2;
28 g_model.logicalSw[index].v3 = _v3;
29 g_model.logicalSw[index].delay = _delay;
30 g_model.logicalSw[index].duration = _duration;
31 g_model.logicalSw[index].andsw = _andsw;
34 #if defined(PCBTARANIS)
35 TEST(getSwitch, OldTypeStickyCSW)
37 RADIO_RESET();
38 MODEL_RESET();
39 MIXER_RESET();
41 setLogicalSwitch(0, LS_FUNC_AND, SWSRC_SA0, SWSRC_NONE);
42 setLogicalSwitch(1, LS_FUNC_OR, SWSRC_SW1, SWSRC_SW2);
44 simuSetSwitch(0, 0);
45 evalLogicalSwitches();
46 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
47 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
49 // now trigger SA0, both switches should become true
50 simuSetSwitch(0, -1);
51 evalLogicalSwitches();
52 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
53 EXPECT_EQ(getSwitch(SWSRC_SW2), true);
55 // now release SA0 and SW2 should stay true
56 simuSetSwitch(0, 0);
57 evalLogicalSwitches();
58 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
59 EXPECT_EQ(getSwitch(SWSRC_SW2), true);
61 // now reset logical switches
62 logicalSwitchesReset();
63 evalLogicalSwitches();
64 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
65 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
67 #endif
69 TEST(getSwitch, nullSW)
71 MODEL_RESET();
72 EXPECT_EQ(getSwitch(0), true);
76 #if defined(PCBTARANIS)
77 TEST(getSwitch, inputWithTrim)
79 MODEL_RESET();
80 modelDefault(0);
81 MIXER_RESET();
83 // g_model.logicalSw[0] = { LS_FUNC_VPOS, MIXSRC_FIRST_INPUT, 0, 0 };
84 setLogicalSwitch(0, LS_FUNC_VPOS, MIXSRC_FIRST_INPUT, 0, 0);
86 evalMixes(1);
87 evalLogicalSwitches();
88 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
90 setTrimValue(0, 0, 32);
91 evalMixes(1);
92 evalLogicalSwitches();
93 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
95 #endif
97 #if defined(PCBTARANIS) || defined(PCBHORUS)
98 TEST(evalLogicalSwitches, playFile)
100 SYSTEM_RESET();
101 MODEL_RESET();
102 modelDefault(0);
103 MIXER_RESET();
105 extern BitField<(MAX_LOGICAL_SWITCHES * 2/*on, off*/)> sdAvailableLogicalSwitchAudioFiles;
106 char filename[AUDIO_FILENAME_MAXLEN+1];
108 #if defined(EEPROM)
109 #define MODELNAME "MODEL01"
110 #else
111 #define MODELNAME "Model00"
112 #endif
114 sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(0,AUDIO_EVENT_OFF));
115 sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(0,AUDIO_EVENT_ON));
116 sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(31,AUDIO_EVENT_OFF));
117 sdAvailableLogicalSwitchAudioFiles.setBit(INDEX_LOGICAL_SWITCH_AUDIO_FILE(31,AUDIO_EVENT_ON));
119 isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (0 << 16) + AUDIO_EVENT_OFF, filename);
120 EXPECT_EQ(strcmp(filename, "/SOUNDS/en/" MODELNAME "/L1-off.wav"), 0);
121 isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (0 << 16) + AUDIO_EVENT_ON, filename);
122 EXPECT_EQ(strcmp(filename, "/SOUNDS/en/" MODELNAME "/L1-on.wav"), 0);
123 isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (31 << 16) + AUDIO_EVENT_OFF, filename);
124 EXPECT_EQ(strcmp(filename, "/SOUNDS/en/" MODELNAME "/L32-off.wav"), 0);
125 isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (31 << 16) + AUDIO_EVENT_ON, filename);
126 EXPECT_EQ(strcmp(filename, "/SOUNDS/en/" MODELNAME "/L32-on.wav"), 0);
128 EXPECT_EQ(isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (31 << 16) + AUDIO_EVENT_ON, filename), true);
129 EXPECT_EQ(isAudioFileReferenced((LOGICAL_SWITCH_AUDIO_CATEGORY << 24) + (32 << 16) + AUDIO_EVENT_ON, filename), false);
131 #undef MODELNAME
133 #endif
135 #if defined(PCBTARANIS) && NUM_SWITCHES >= 8 && !defined(PCBX7)
136 TEST(getSwitch, edgeInstant)
138 MODEL_RESET();
139 MIXER_RESET();
140 // LS1 setup: EDGE SFup (0:instant)
141 // LS2 setup: (EDGE SFup (0:instant)) AND SAup
142 setLogicalSwitch(0, LS_FUNC_EDGE, SWSRC_SF2, -129, -1);
143 setLogicalSwitch(1, LS_FUNC_EDGE, SWSRC_SF2, -129, -1, 0, 0, SWSRC_SA2);
145 simuSetSwitch(0, -1); //SA down
146 simuSetSwitch(5, 0); //SF down
147 // EXPECT_EQ(getSwitch(SWSRC_SF2), false);
148 logicalSwitchesTimerTick();
149 evalLogicalSwitches();
150 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
151 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
153 // now trigger SFup, LS1 should become true
154 simuSetSwitch(5, 1); //SF up
155 // EXPECT_EQ(getSwitch(SWSRC_SF2), true);
156 logicalSwitchesTimerTick();
157 evalLogicalSwitches();
158 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
159 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
161 // now release SA0 and SW2 should stay true
162 simuSetSwitch(5, 0); //SF down
163 logicalSwitchesTimerTick();
164 evalLogicalSwitches();
165 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
166 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
168 // now reset logical switches
169 logicalSwitchesReset();
170 logicalSwitchesTimerTick();
171 evalLogicalSwitches();
172 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
173 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
176 // second part with SAup
178 simuSetSwitch(0, 1); //SA up
179 simuSetSwitch(5, 0); //SF down
180 // EXPECT_EQ(getSwitch(SWSRC_SF2), false);
181 logicalSwitchesTimerTick();
182 evalLogicalSwitches();
183 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
184 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
186 // now trigger SFup, LS1 should become true
187 simuSetSwitch(5, 1); //SF up
188 // EXPECT_EQ(getSwitch(SWSRC_SF2), true);
189 logicalSwitchesTimerTick();
190 evalLogicalSwitches();
191 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
192 EXPECT_EQ(getSwitch(SWSRC_SW2), true);
194 // now release SA0 and SW2 should stay true
195 simuSetSwitch(5, 0); //SF down
196 logicalSwitchesTimerTick();
197 evalLogicalSwitches();
198 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
199 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
201 // now reset logical switches
202 logicalSwitchesReset();
203 logicalSwitchesTimerTick();
204 evalLogicalSwitches();
205 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
206 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
208 // now bug #2939
209 // SF is kept up and SA is toggled
210 simuSetSwitch(0, -1); //SA down
211 simuSetSwitch(5, 1); //SF up
212 logicalSwitchesTimerTick();
213 evalLogicalSwitches();
214 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
215 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
217 simuSetSwitch(0, 1); //SA up
218 simuSetSwitch(5, 1); //SF up
219 logicalSwitchesTimerTick();
220 evalLogicalSwitches();
221 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
222 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
224 simuSetSwitch(0, -1); //SA down
225 simuSetSwitch(5, 1); //SF up
226 logicalSwitchesTimerTick();
227 evalLogicalSwitches();
228 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
229 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
231 //test what happens when EDGE condition is true and
232 //logical switches are reset - the switch should fire again
234 simuSetSwitch(0, 1); //SA up
235 simuSetSwitch(5, 1); //SF up
236 logicalSwitchesTimerTick();
237 evalLogicalSwitches();
238 EXPECT_EQ(getSwitch(SWSRC_SW1), false); //switch will not trigger, because SF was already up
239 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
241 logicalSwitchesReset();
242 logicalSwitchesTimerTick();
243 evalLogicalSwitches();
244 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
245 EXPECT_EQ(getSwitch(SWSRC_SW2), true);
247 logicalSwitchesTimerTick();
248 evalLogicalSwitches();
249 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
250 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
253 TEST(getSwitch, edgeRelease)
255 MODEL_RESET();
256 MIXER_RESET();
257 // test for issue #2728
258 // LS1 setup: EDGE SFup (0:release)
259 // LS2 setup: (EDGE SFup (0:release)) AND SAup
260 setLogicalSwitch(0, LS_FUNC_EDGE, SWSRC_SF2, -129, 0);
261 setLogicalSwitch(1, LS_FUNC_EDGE, SWSRC_SF2, -129, 0, 0, 0, SWSRC_SA2 );
263 simuSetSwitch(0, -1); //SA down
264 simuSetSwitch(5, 0); //SF down
265 logicalSwitchesTimerTick();
266 evalLogicalSwitches();
267 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
268 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
270 simuSetSwitch(5, 1); //SF up
271 logicalSwitchesTimerTick();
272 evalLogicalSwitches();
273 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
274 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
276 simuSetSwitch(5, 0); //SF down
277 logicalSwitchesTimerTick();
278 evalLogicalSwitches();
279 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
280 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
283 // second part with SAup
284 simuSetSwitch(0, 1); //SA up
285 simuSetSwitch(5, 0); //SF down
286 logicalSwitchesTimerTick();
287 evalLogicalSwitches();
288 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
289 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
291 simuSetSwitch(5, 1); //SF up
292 logicalSwitchesTimerTick();
293 evalLogicalSwitches();
294 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
295 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
297 simuSetSwitch(5, 0); //SF down
298 logicalSwitchesTimerTick();
299 evalLogicalSwitches();
300 EXPECT_EQ(getSwitch(SWSRC_SW1), true);
301 EXPECT_EQ(getSwitch(SWSRC_SW2), true);
303 // with switches reset both should remain false
304 logicalSwitchesReset();
305 logicalSwitchesTimerTick();
306 evalLogicalSwitches();
307 EXPECT_EQ(getSwitch(SWSRC_SW1), false);
308 EXPECT_EQ(getSwitch(SWSRC_SW2), false);
311 #endif // defined(PCBTARANIS)