Skip to content

Commit c6d7395

Browse files
Only run cyclic calculation once. Add several mixer tests
1 parent 4c1aa10 commit c6d7395

2 files changed

Lines changed: 164 additions & 12 deletions

File tree

src/mixer.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static buttonAction_t button_action;
5757
static unsigned switch_is_on(unsigned sw, volatile s32 *raw);
5858
static s32 get_trim(unsigned src);
5959

60-
static s32 MIXER_CreateCyclicOutput(volatile s32 *raw, unsigned cycnum);
60+
static void MIXER_CreateCyclicOutput(volatile s32 *raw, s32 *cyc);
6161

6262
struct Mixer *MIXER_GetAllMixers()
6363
{
@@ -178,13 +178,14 @@ void MIXER_CalcChannels()
178178
MIXER_EvalMixers(raw);
179179

180180
//4th step: apply auto-templates
181+
s32 cyc[3];
182+
MIXER_CreateCyclicOutput(raw, cyc);
181183
for (i = 0; i < NUM_OUT_CHANNELS; i++) {
182184
switch(Model.templates[i]) {
183185
case MIXERTEMPLATE_CYC1:
184186
case MIXERTEMPLATE_CYC2:
185187
case MIXERTEMPLATE_CYC3:
186-
raw[NUM_INPUTS+i+1] = MIXER_CreateCyclicOutput(raw,
187-
Model.templates[i] - MIXERTEMPLATE_CYC1 + 1);
188+
raw[NUM_INPUTS+i+1] = cyc[Model.templates[i] - MIXERTEMPLATE_CYC1];
188189
break;
189190
}
190191
}
@@ -231,11 +232,13 @@ char* MIXER_GetChannelDisplayFormat(unsigned channel)
231232

232233
#define REZ_SWASH_X(x) ((x) - (x)/8 - (x)/128 - (x)/512) // 1024*sin(60) ~= 886
233234
#define REZ_SWASH_Y(x) (1*(x)) // 1024 => 1024
234-
s32 MIXER_CreateCyclicOutput(volatile s32 *raw, unsigned cycnum)
235+
void MIXER_CreateCyclicOutput(volatile s32 *raw, s32 *cyc)
235236
{
236-
s32 cyc[3];
237237
if (! Model.swash_type) {
238-
return raw[NUM_INPUTS + NUM_OUT_CHANNELS + cycnum];
238+
cyc[0] = raw[NUM_INPUTS + NUM_OUT_CHANNELS + 1];
239+
cyc[1] = raw[NUM_INPUTS + NUM_OUT_CHANNELS + 2];
240+
cyc[2] = raw[NUM_INPUTS + NUM_OUT_CHANNELS + 3];
241+
return;
239242
}
240243
s32 aileron = raw[NUM_INPUTS + NUM_OUT_CHANNELS + 1];
241244
s32 elevator = raw[NUM_INPUTS + NUM_OUT_CHANNELS + 2];
@@ -286,8 +289,6 @@ s32 MIXER_CreateCyclicOutput(volatile s32 *raw, unsigned cycnum)
286289
cyc[2] = collective - aileron;
287290
break;
288291
}
289-
290-
return cyc[cycnum-1];
291292
}
292293

293294
void MIXER_ApplyMixer(struct Mixer *mixer, volatile s32 *raw, s32 *orig_value)

src/tests/test_mixer.c

Lines changed: 155 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ void TestUpdateRawInputs(CuTest *t)
6161
memset((s32 *)raw, 0, sizeof(raw));
6262
for (int i = 1; i <= NUM_TX_INPUTS; i++) {
6363
TEST_CHAN_SetChannelValue(i, (i + 1) * 200);
64-
Model.mixers[i].src = i;
65-
Model.mixers[i].dest = i;
66-
Model.mixers[i].scalar = 100;
67-
Model.mixers[i].flags = MUX_REPLACE;
6864
}
6965
MIXER_UpdateRawInputs();
7066
s32 expected[] = {
@@ -105,6 +101,161 @@ void TestUpdateRawInputs(CuTest *t)
105101
}
106102
}
107103

104+
void TestGetCachedInputs(CuTest *t)
105+
{
106+
s32 cache[NUM_SOURCES + 1];
107+
for (int i = 0; i < NUM_SOURCES + 1; i++) {
108+
cache[i] = i + 1;
109+
raw[i] = i;
110+
}
111+
CuAssertIntEquals(t, 0, MIXER_GetCachedInputs(cache, 1));
112+
for (int i = 0; i < NUM_SOURCES + 1; i++) {
113+
CuAssertIntEquals(t, i + 1, cache[i]);
114+
}
115+
CuAssertIntEquals(t, 1, MIXER_GetCachedInputs(cache, 0));
116+
for (int i = 0; i < NUM_SOURCES + 1; i++) {
117+
//values from 1 - NUM_TX_INPUTS should match raw
118+
CuAssertIntEquals(t, i ? (i <= NUM_TX_INPUTS ? i : i + 1) : 1, cache[i]);
119+
}
120+
}
121+
122+
void TestCalcChannels(CuTest *t)
123+
{
124+
memset(&Model, 0, sizeof(Model));
125+
memset((s32 *)raw, 0, sizeof(raw));
126+
for (int i = 0; i < 4; i++) {
127+
TEST_CHAN_SetChannelValue(i, (i + 1) * 200);
128+
Model.mixers[i].src = i + 1;
129+
Model.mixers[i].dest = NUM_OUT_CHANNELS + i;
130+
Model.mixers[i].scalar = 100;
131+
Model.mixers[i].flags = MUX_REPLACE;
132+
}
133+
for (int i = 0; i < NUM_OUT_CHANNELS; i++) {
134+
Model.limits[i].servoscale = 100;
135+
Model.limits[i].max = 200;
136+
Model.limits[i].min = 200;
137+
}
138+
Model.templates[5] = MIXERTEMPLATE_CYC1;
139+
Model.templates[6] = MIXERTEMPLATE_CYC2;
140+
Model.templates[7] = MIXERTEMPLATE_CYC3;
141+
MIXER_CalcChannels();
142+
s32 expected[NUM_OUT_CHANNELS] = {0, 0, 0, 0, 0, 1000, 800, 600, 0, 0, 0, 0, 0, 0, 0, 0};
143+
for (int i = 0; i < NUM_OUT_CHANNELS; i++) {
144+
CuAssertIntEquals(t, expected[i], Channels[i]);
145+
}
146+
}
147+
148+
void TestGetInputs(CuTest *t)
149+
{
150+
CuAssertPtrEquals(t, (void *)raw, (void *)MIXER_GetInputs());
151+
}
152+
153+
void TestGetChannel(CuTest *t)
154+
{
155+
memset(&Model, 0, sizeof(Model));
156+
memset((s32 *)raw, 0, sizeof(raw));
157+
raw[NUM_INPUTS + 1] = 1000;
158+
CuAssertIntEquals(t, 1000, MIXER_GetChannel(0, 0));
159+
160+
Model.num_ppmin = PPM_IN_TRAIN1 << 6;
161+
Model.train_sw = INP_GEAR1;
162+
raw[Model.train_sw] = 100;
163+
ppmSync = 1;
164+
ppmChannels[0] = 2000;
165+
CuAssertIntEquals(t, 2000, MIXER_GetChannel(0, 0));
166+
167+
ppmSync = 0;
168+
Channels[0] = 3000;
169+
CuAssertIntEquals(t, 3000, MIXER_GetChannel(0, 0));
170+
}
171+
172+
void TestGetChannelDisplayScale(CuTest *t)
173+
{
174+
memset(&Model, 0, sizeof(Model));
175+
Model.limits[0].displayscale = 100;
176+
CuAssertIntEquals(t, 100, MIXER_GetChannelDisplayScale(0));
177+
CuAssertIntEquals(t, DEFAULT_DISPLAY_SCALE, MIXER_GetChannelDisplayScale(NUM_OUT_CHANNELS));
178+
}
179+
180+
void TestGetChannelDisplayFormat(CuTest *t)
181+
{
182+
memset(&Model, 0, sizeof(Model));
183+
strcpy(Model.limits[0].displayformat, "%");
184+
CuAssertStrEquals(t, "%", MIXER_GetChannelDisplayFormat(0));
185+
CuAssertStrEquals(t, DEFAULT_DISPLAY_FORMAT, MIXER_GetChannelDisplayFormat(NUM_OUT_CHANNELS));
186+
}
187+
188+
void TestCreateCyclicOutput(CuTest *t)
189+
{
190+
s32 rawdata[NUM_SOURCES + 1] = {0};
191+
memset(&Model, 0, sizeof(Model));
192+
s32 cyc[3] = {0};
193+
rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 1] = 1000;
194+
rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 2] = 2000;
195+
rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 3] = 3000;
196+
MIXER_CreateCyclicOutput(rawdata, cyc);
197+
for (int i = 0; i < 3; i ++) {
198+
CuAssertIntEquals(t, rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 1 + i], cyc[i]);
199+
}
200+
Model.swash_type = SWASH_TYPE_LAST;
201+
memset(cyc, 0, sizeof(cyc));
202+
MIXER_CreateCyclicOutput(rawdata, cyc);
203+
for (int i = 0; i < 3; i ++) {
204+
CuAssertIntEquals(t, rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 1 + i], cyc[i]);
205+
}
206+
207+
Model.swash_invert = SWASH_INV_ELEVATOR_MASK | SWASH_INV_AILERON_MASK | SWASH_INV_COLLECTIVE_MASK;
208+
memset(cyc, 0, sizeof(cyc));
209+
MIXER_CreateCyclicOutput(rawdata, cyc);
210+
for (int i = 0; i < 3; i ++) {
211+
CuAssertIntEquals(t, -rawdata[NUM_INPUTS + NUM_OUT_CHANNELS + 1 + i], cyc[i]);
212+
}
213+
Model.swash_invert = 0;
214+
215+
Model.swash_type = SWASH_TYPE_120;
216+
{
217+
memset(cyc, 0, sizeof(cyc));
218+
Model.swashmix[0] = 25;
219+
Model.swashmix[1] = 50;
220+
Model.swashmix[2] = 75;
221+
MIXER_CreateCyclicOutput(rawdata, cyc);
222+
s32 expected[] = {1250, 3000, 2500};
223+
for (int i = 0; i < 3; i ++) {
224+
CuAssertIntEquals(t, expected[i], cyc[i]);
225+
}
226+
}
227+
228+
Model.swash_type = SWASH_TYPE_120X;
229+
{
230+
memset(cyc, 0, sizeof(cyc));
231+
MIXER_CreateCyclicOutput(rawdata, cyc);
232+
s32 expected[] = {2000, 3375, 1375};
233+
for (int i = 0; i < 3; i ++) {
234+
CuAssertIntEquals(t, expected[i], cyc[i]);
235+
}
236+
}
237+
238+
Model.swash_type = SWASH_TYPE_140;
239+
{
240+
memset(cyc, 0, sizeof(cyc));
241+
MIXER_CreateCyclicOutput(rawdata, cyc);
242+
s32 expected[] = {1250, 3500, 3000};
243+
for (int i = 0; i < 3; i ++) {
244+
CuAssertIntEquals(t, expected[i], cyc[i]);
245+
}
246+
}
247+
248+
Model.swash_type = SWASH_TYPE_90;
249+
{
250+
memset(cyc, 0, sizeof(cyc));
251+
MIXER_CreateCyclicOutput(rawdata, cyc);
252+
s32 expected[] = {1250, 2500, 2000};
253+
for (int i = 0; i < 3; i ++) {
254+
CuAssertIntEquals(t, expected[i], cyc[i]);
255+
}
256+
}
257+
}
258+
108259
void TestApplyMixerSimple(CuTest *t)
109260
{
110261
struct Mixer initmixer = {

0 commit comments

Comments
 (0)