Skip to content

Commit 3e6007c

Browse files
committed
Defaults can be shorter than the list
1 parent 4aa2664 commit 3e6007c

3 files changed

Lines changed: 40 additions & 34 deletions

File tree

src/config/config.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
#include <stdlib.h>
2424
#include <string.h>
2525

26-
#define DEFAULTS_ZERO (const u16*)0x01
27-
2826
#define MATCH_KEY(s) strcasecmp(name, s) == 0
2927
#define MATCH_VALUE(s) strcasecmp(value, s) == 0
3028

@@ -158,7 +156,8 @@ int assign_int(void* ptr, const struct struct_map *map, int map_size, const char
158156
return 0;
159157
}
160158

161-
int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int map_size, FILE* fh) {
159+
int write_int2(void* ptr, const struct struct_map *map, int map_size,
160+
const u16* defaults, int defaults_size, FILE* fh) {
162161
for (int i = 0; i < map_size; i++) {
163162
int size = map[i].offset >> 12;
164163
int offset = map[i].offset & 0xFFF;
@@ -190,26 +189,28 @@ int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int
190189

191190
if (defaults == DEFAULTS_ZERO) {
192191
if (value == 0) continue;
192+
} else if (defaults == DEFAULTS_ALWAYS) {
193+
// always write out
193194
} else if (defaults != NULL) {
194-
if (value == defaults[i]) continue;
195+
if (i < defaults_size && value == defaults[i]) continue;
195196
}
196197

197198
switch (size)
198199
{
199-
case TYPE_STR_LIST: {
200-
i++; // next entry is additional info for string list
201-
const char* const *list = (const char* const *)map[i].str;
202-
fprintf(fh, "%s=%s\n", map[i - 1].str, list[value]);
203-
break;
204-
}
205-
case TYPE_BUTTON:
206-
fprintf(fh, "%s=%s\n", map[i].str, INPUT_ButtonName(value));
207-
break;
208-
case TYPE_SOURCE: {
209-
char tmpstr[20];
210-
fprintf(fh, "%s=%s\n", map[i].str, INPUT_SourceNameReal(tmpstr, value));
211-
break;
212-
}
200+
case TYPE_STR_LIST: {
201+
i++; // next entry is additional info for string list
202+
const char* const *list = (const char* const *)map[i].str;
203+
fprintf(fh, "%s=%s\n", map[i - 1].str, list[value]);
204+
break;
205+
}
206+
case TYPE_BUTTON:
207+
fprintf(fh, "%s=%s\n", map[i].str, INPUT_ButtonName(value));
208+
break;
209+
case TYPE_SOURCE: {
210+
char tmpstr[20];
211+
fprintf(fh, "%s=%s\n", map[i].str, INPUT_SourceNameReal(tmpstr, value));
212+
break;
213+
}
213214
default:
214215
fprintf(fh, "%s=%d\n", map[i].str, value);
215216
}
@@ -219,7 +220,7 @@ int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int
219220
}
220221

221222
int write_int(void* ptr, const struct struct_map *map, int map_size, FILE* fh) {
222-
return write_int2(ptr, map, DEFAULTS_ZERO, map_size, fh);
223+
return write_int2(ptr, map, map_size, DEFAULTS_ALWAYS, 0, fh);
223224
}
224225

225226
#define TESTNAME config

src/config/config.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ enum {
2828
TYPE_S32 = 7,
2929

3030
TYPE_STR_LIST = 8,
31-
TYPE_STR_LIST_OFF_ONE = 9,
31+
TYPE_STR_CALL = 9,
3232

3333
TYPE_COLOR = 10,
3434
TYPE_FONT = 11,
@@ -44,13 +44,18 @@ struct struct_map {const char *str; u16 offset;};
4444
#define OFFSET_FON(s, v) (offsetof(s, v) | (TYPE_FONT << 12))
4545
#define OFFSET_STRLIST(s, v, StrList, StrListSize) \
4646
(offsetof(s, v) | (TYPE_STR_LIST << 12)) }, { (const char*)StrList, StrListSize
47+
#define OFFSET_STRCALL(s, v, StrCallback, N) \
48+
(offsetof(s, v) | (TYPE_STR_LIST << 12)) }, { (const char*)StrCallback, N
4749
#define OFFSET_SRC(s, v) (offsetof(s, v) | (TYPE_SOURCE << 12))
4850
#define OFFSET_BUT(s, v) (offsetof(s, v) | (TYPE_BUTTON << 12))
4951

5052
int assign_int(void* ptr, const struct struct_map *map, int map_size,
5153
const char* name, const char* value);
5254
int write_int(void* ptr, const struct struct_map *map, int map_size, FILE* fh);
5355

54-
int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int map_size, FILE* fh);
56+
#define DEFAULTS_ZERO (const u16*)0x01
57+
#define DEFAULTS_ALWAYS (const u16*)0x02
58+
int write_int2(void* ptr, const struct struct_map *map, int map_size,
59+
const u16* defaults, int defaults_size, FILE* fh);
5560

5661
#endif

src/config/model.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,12 @@ static const struct struct_map _secradio[] = {
528528
#endif
529529
};
530530
static const struct struct_map _secmixer[] = {
531-
{MIXER_SWITCH, OFFSET_SRC(struct Mixer, sw)},
532531
{MIXER_SCALAR, OFFSETS(struct Mixer, scalar)},
532+
{MIXER_SWITCH, OFFSET_SRC(struct Mixer, sw)},
533533
{MIXER_OFFSET, OFFSETS(struct Mixer, offset)},
534534
};
535535
static const u16 _secmixer_defaults[] = {
536-
0, 100, 0
536+
100
537537
};
538538
static const struct struct_map _seclimit[] = {
539539
{CHAN_LIMIT_SAFETYSW, OFFSET_SRC(struct Limit, safetysw)},
@@ -549,12 +549,12 @@ static const u16 _seclimit_defaults[] = {
549549
0, 0, DEFAULT_SERVO_LIMIT, 0, 100, 0, 0, DEFAULT_DISPLAY_SCALE
550550
};
551551
static const struct struct_map _sectrim[] = {
552+
{TRIM_STEP, OFFSET(struct Trim, step)},
552553
{TRIM_POS, OFFSET_BUT(struct Trim, pos)},
553554
{TRIM_NEG, OFFSET_BUT(struct Trim, neg)},
554-
{TRIM_STEP, OFFSET(struct Trim, step)},
555555
};
556556
static const u16 _sectrim_defaults[] = {
557-
0, 0, 1
557+
1
558558
};
559559
static const struct struct_map _sectrim_rdonly[] = {
560560
{TRIM_SOURCE, OFFSET_SRC(struct Trim, src)},
@@ -568,6 +568,7 @@ static const struct struct_map _secswash[] = {
568568
static const struct struct_map _sectimer[] = {
569569
{TIMER_SOURCE, OFFSET_SRC(struct Timer, src)},
570570
{TIMER_RESETSRC, OFFSET_SRC(struct Timer, resetsrc)},
571+
{TIMER_TYPE, OFFSET_STRLIST(struct Timer, type, TIMER_TYPE_VAL, ARRAYSIZE(TIMER_TYPE_VAL))}
571572
};
572573
static const struct struct_map _sectimer_rdonly[] = {
573574
{TIMER_TIME, OFFSET(struct Timer, timer)},
@@ -1087,7 +1088,8 @@ static u8 write_mixer(FILE *fh, struct Model *m, u8 channel)
10871088
fprintf(fh, "[%s]\n", SECTION_MIXER);
10881089
fprintf(fh, "%s=%s\n", MIXER_SOURCE, INPUT_SourceNameReal(tmpstr, m->mixers[idx].src));
10891090
fprintf(fh, "%s=%s\n", MIXER_DEST, INPUT_SourceNameReal(tmpstr, m->mixers[idx].dest + NUM_INPUTS + 1));
1090-
write_int2(&m->mixers[idx], _secmixer, _secmixer_defaults, ARRAYSIZE(_secmixer), fh);
1091+
write_int2(&m->mixers[idx], _secmixer, ARRAYSIZE(_secmixer),
1092+
_secmixer_defaults, ARRAYSIZE(_secmixer_defaults), fh);
10911093
if(WRITE_FULL_MODEL || ! MIXER_APPLY_TRIM(&m->mixers[idx]))
10921094
fprintf(fh, "%s=%d\n", MIXER_USETRIM, MIXER_APPLY_TRIM(&m->mixers[idx]) ? 1 : 0);
10931095
if(WRITE_FULL_MODEL || MIXER_MUX(&m->mixers[idx]))
@@ -1153,7 +1155,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
11531155
CONFIG_EnableLanguage(0);
11541156
fprintf(fh, "%s=%s\n", MODEL_NAME, m->name);
11551157
#if HAS_PERMANENT_TIMER
1156-
write_int(m, _secnone, ARRAYSIZE(_secnone), fh);
1158+
write_int2(m, _secnone, ARRAYSIZE(_secnone), DEFAULTS_ZERO, 0, fh);
11571159
#endif
11581160
fprintf(fh, "%s=%s\n", MODEL_MIXERMODE, STDMIXER_ModeName(m->mixer_mode));
11591161
if(m->icon[0] != 0)
@@ -1162,7 +1164,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
11621164
fprintf(fh, "%s=%s\n", MODEL_TYPE, MODEL_TYPE_VAL[m->type]);
11631165
fprintf(fh, "[%s]\n", SECTION_RADIO);
11641166
fprintf(fh, "%s=%s\n", RADIO_PROTOCOL, PROTOCOL_GetName(m->protocol));
1165-
write_int(m, _secradio, ARRAYSIZE(_secradio), fh);
1167+
write_int2(m, _secradio, ARRAYSIZE(_secradio), DEFAULTS_ZERO, 0, fh);
11661168
fprintf(fh, "%s=%s\n", RADIO_TX_POWER, radio_tx_power_val(m->radio, m->tx_power));
11671169
fprintf(fh, "\n");
11681170
write_proto_opts(fh, m);
@@ -1181,7 +1183,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
11811183
fprintf(fh, "[%s%d]\n", SECTION_CHANNEL, idx+1);
11821184
if(WRITE_FULL_MODEL || (m->limits[idx].flags & CH_REVERSE))
11831185
fprintf(fh, "%s=%d\n", CHAN_LIMIT_REVERSE, (m->limits[idx].flags & CH_REVERSE) ? 1 : 0);
1184-
write_int2(&m->limits[idx], _seclimit, _seclimit_defaults, ARRAYSIZE(_seclimit), fh);
1186+
write_int2(&m->limits[idx], _seclimit, ARRAYSIZE(_seclimit), _seclimit_defaults, ARRAYSIZE(_seclimit_defaults),fh);
11851187
if(WRITE_FULL_MODEL || (m->limits[idx].flags & CH_FAILSAFE_EN)) {
11861188
if(m->limits[idx].flags & CH_FAILSAFE_EN) {
11871189
fprintf(fh, "%s=%d\n", CHAN_LIMIT_FAILSAFE, m->limits[idx].failsafe);
@@ -1216,7 +1218,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
12161218
if (PPMin_Mode() != PPM_IN_SOURCE) {
12171219
fprintf(fh, "%s=%s\n", PPMIN_SWITCH, INPUT_SourceNameReal(file, m->train_sw));
12181220
}
1219-
write_int(m, _secppm, ARRAYSIZE(_secppm), fh);
1221+
write_int2(m, _secppm, ARRAYSIZE(_secppm), DEFAULTS_ZERO, 0, fh);
12201222
//fprintf(fh, "%s=%d\n", PPMIN_CENTERPW, m->ppmin_centerpw);
12211223
//fprintf(fh, "%s=%d\n", PPMIN_DELTAPW, m->ppmin_deltapw);
12221224
if (PPMin_Mode() != PPM_IN_SOURCE) {
@@ -1237,7 +1239,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
12371239
m->trims[idx].src >= 1 && m->trims[idx].src <= 4
12381240
? tx_stick_names[m->trims[idx].src-1]
12391241
: INPUT_SourceNameReal(file, m->trims[idx].src));
1240-
write_int2(&m->trims[idx], _sectrim, _sectrim_defaults, ARRAYSIZE(_sectrim), fh);
1242+
write_int2(&m->trims[idx], _sectrim, ARRAYSIZE(_sectrim), _sectrim_defaults, ARRAYSIZE(_sectrim_defaults), fh);
12411243
if(WRITE_FULL_MODEL || m->trims[idx].sw)
12421244
fprintf(fh, "%s=%s\n", TRIM_SWITCH, INPUT_SourceNameAbbrevSwitchReal(file, m->trims[idx].sw));
12431245
if(WRITE_FULL_MODEL || m->trims[idx].value[0] || m->trims[idx].value[1] || m->trims[idx].value[2]
@@ -1255,14 +1257,12 @@ u8 CONFIG_WriteModel(u8 model_num) {
12551257
fprintf(fh, "%s=1\n", SWASH_AIL_INV);
12561258
if (WRITE_FULL_MODEL || m->swash_invert & 0x04)
12571259
fprintf(fh, "%s=1\n", SWASH_COL_INV);
1258-
write_int(m, _secswash, ARRAYSIZE(_secswash), fh);
1260+
write_int2(m, _secswash, ARRAYSIZE(_secswash), DEFAULTS_ZERO, 0, fh);
12591261
}
12601262
for(idx = 0; idx < NUM_TIMERS; idx++) {
12611263
if (! WRITE_FULL_MODEL && m->timer[idx].src == 0 && m->timer[idx].type == TIMER_STOPWATCH)
12621264
continue;
12631265
fprintf(fh, "[%s%d]\n", SECTION_TIMER, idx+1);
1264-
if (WRITE_FULL_MODEL || m->timer[idx].type != TIMER_STOPWATCH)
1265-
fprintf(fh, "%s=%s\n", TIMER_TYPE, TIMER_TYPE_VAL[m->timer[idx].type]);
12661266
write_int(&m->timer[idx], _sectimer, ARRAYSIZE(_sectimer), fh);
12671267
if (WRITE_FULL_MODEL || ((m->timer[idx].type == TIMER_COUNTDOWN || m->timer[idx].type == TIMER_COUNTDOWN_PROP) && m->timer[idx].timer))
12681268
fprintf(fh, "%s=%d\n", TIMER_TIME, m->timer[idx].timer);

0 commit comments

Comments
 (0)