Skip to content

Commit

Permalink
Defaults can be shorter than the list
Browse files Browse the repository at this point in the history
  • Loading branch information
howard0su committed Feb 12, 2019
1 parent 4aa2664 commit dd682ef
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
39 changes: 20 additions & 19 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#include <stdlib.h>
#include <string.h>

#define DEFAULTS_ZERO (const u16*)0x01

#define MATCH_KEY(s) strcasecmp(name, s) == 0
#define MATCH_VALUE(s) strcasecmp(value, s) == 0

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

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

if (defaults == DEFAULTS_ZERO) {
if (value == 0) continue;
} else if (defaults == DEFAULTS_ALWAYS) {
// always write out
} else if (defaults != NULL) {
if (value == defaults[i]) continue;
if (i < defaults_size && value == defaults[i]) continue;
}

switch (size)
{
case TYPE_STR_LIST: {
i++; // next entry is additional info for string list
const char* const *list = (const char* const *)map[i].str;
fprintf(fh, "%s=%s\n", map[i - 1].str, list[value]);
break;
}
case TYPE_BUTTON:
fprintf(fh, "%s=%s\n", map[i].str, INPUT_ButtonName(value));
break;
case TYPE_SOURCE: {
char tmpstr[20];
fprintf(fh, "%s=%s\n", map[i].str, INPUT_SourceNameReal(tmpstr, value));
break;
}
case TYPE_STR_LIST: {
i++; // next entry is additional info for string list
const char* const *list = (const char* const *)map[i].str;
fprintf(fh, "%s=%s\n", map[i - 1].str, list[value]);
break;
}
case TYPE_BUTTON:
fprintf(fh, "%s=%s\n", map[i].str, INPUT_ButtonName(value));
break;
case TYPE_SOURCE: {
char tmpstr[20];
fprintf(fh, "%s=%s\n", map[i].str, INPUT_SourceNameReal(tmpstr, value));
break;
}
default:
fprintf(fh, "%s=%d\n", map[i].str, value);
}
Expand All @@ -219,7 +220,7 @@ int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int
}

int write_int(void* ptr, const struct struct_map *map, int map_size, FILE* fh) {
return write_int2(ptr, map, DEFAULTS_ZERO, map_size, fh);
return write_int2(ptr, map, map_size, DEFAULTS_ALWAYS, 0, fh);
}

#define TESTNAME config
Expand Down
9 changes: 7 additions & 2 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ enum {
TYPE_S32 = 7,

TYPE_STR_LIST = 8,
TYPE_STR_LIST_OFF_ONE = 9,
TYPE_STR_CALL = 9,

TYPE_COLOR = 10,
TYPE_FONT = 11,
Expand All @@ -44,13 +44,18 @@ struct struct_map {const char *str; u16 offset;};
#define OFFSET_FON(s, v) (offsetof(s, v) | (TYPE_FONT << 12))
#define OFFSET_STRLIST(s, v, StrList, StrListSize) \
(offsetof(s, v) | (TYPE_STR_LIST << 12)) }, { (const char*)StrList, StrListSize
#define OFFSET_STRCALL(s, v, StrCallback, N) \
(offsetof(s, v) | (TYPE_STR_LIST << 12)) }, { (const char*)StrCallback, N
#define OFFSET_SRC(s, v) (offsetof(s, v) | (TYPE_SOURCE << 12))
#define OFFSET_BUT(s, v) (offsetof(s, v) | (TYPE_BUTTON << 12))

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

int write_int2(void* ptr, const struct struct_map *map, const u16* defaults, int map_size, FILE* fh);
#define DEFAULTS_ZERO (const u16*)0x01
#define DEFAULTS_ALWAYS (const u16*)0x02
int write_int2(void* ptr, const struct struct_map *map, int map_size,
const u16* defaults, int defaults_size, FILE* fh);

#endif
30 changes: 17 additions & 13 deletions src/config/model.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ static const struct struct_map _secradio[] = {
#endif
};
static const struct struct_map _secmixer[] = {
{MIXER_SWITCH, OFFSET_SRC(struct Mixer, sw)},
{MIXER_SCALAR, OFFSETS(struct Mixer, scalar)},
{MIXER_SWITCH, OFFSET_SRC(struct Mixer, sw)},
{MIXER_OFFSET, OFFSETS(struct Mixer, offset)},
};
static const u16 _secmixer_defaults[] = {
0, 100, 0
100
};
static const struct struct_map _seclimit[] = {
{CHAN_LIMIT_SAFETYSW, OFFSET_SRC(struct Limit, safetysw)},
Expand All @@ -549,12 +549,12 @@ static const u16 _seclimit_defaults[] = {
0, 0, DEFAULT_SERVO_LIMIT, 0, 100, 0, 0, DEFAULT_DISPLAY_SCALE
};
static const struct struct_map _sectrim[] = {
{TRIM_STEP, OFFSET(struct Trim, step)},
{TRIM_POS, OFFSET_BUT(struct Trim, pos)},
{TRIM_NEG, OFFSET_BUT(struct Trim, neg)},
{TRIM_STEP, OFFSET(struct Trim, step)},
};
static const u16 _sectrim_defaults[] = {
0, 0, 1
1
};
static const struct struct_map _sectrim_rdonly[] = {
{TRIM_SOURCE, OFFSET_SRC(struct Trim, src)},
Expand All @@ -565,9 +565,14 @@ static const struct struct_map _secswash[] = {
{SWASH_ELEMIX, OFFSET(struct Model, swashmix[1])},
{SWASH_COLMIX, OFFSET(struct Model, swashmix[2])},
};
static const u16 _secswash_defaults[] = {
60, 60, 60
};

static const struct struct_map _sectimer[] = {
{TIMER_SOURCE, OFFSET_SRC(struct Timer, src)},
{TIMER_RESETSRC, OFFSET_SRC(struct Timer, resetsrc)},
{TIMER_TYPE, OFFSET_STRLIST(struct Timer, type, TIMER_TYPE_VAL, ARRAYSIZE(TIMER_TYPE_VAL))}
};
static const struct struct_map _sectimer_rdonly[] = {
{TIMER_TIME, OFFSET(struct Timer, timer)},
Expand Down Expand Up @@ -1087,7 +1092,8 @@ static u8 write_mixer(FILE *fh, struct Model *m, u8 channel)
fprintf(fh, "[%s]\n", SECTION_MIXER);
fprintf(fh, "%s=%s\n", MIXER_SOURCE, INPUT_SourceNameReal(tmpstr, m->mixers[idx].src));
fprintf(fh, "%s=%s\n", MIXER_DEST, INPUT_SourceNameReal(tmpstr, m->mixers[idx].dest + NUM_INPUTS + 1));
write_int2(&m->mixers[idx], _secmixer, _secmixer_defaults, ARRAYSIZE(_secmixer), fh);
write_int2(&m->mixers[idx], _secmixer, ARRAYSIZE(_secmixer),
_secmixer_defaults, ARRAYSIZE(_secmixer_defaults), fh);
if(WRITE_FULL_MODEL || ! MIXER_APPLY_TRIM(&m->mixers[idx]))
fprintf(fh, "%s=%d\n", MIXER_USETRIM, MIXER_APPLY_TRIM(&m->mixers[idx]) ? 1 : 0);
if(WRITE_FULL_MODEL || MIXER_MUX(&m->mixers[idx]))
Expand Down Expand Up @@ -1153,7 +1159,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
CONFIG_EnableLanguage(0);
fprintf(fh, "%s=%s\n", MODEL_NAME, m->name);
#if HAS_PERMANENT_TIMER
write_int(m, _secnone, ARRAYSIZE(_secnone), fh);
write_int2(m, _secnone, ARRAYSIZE(_secnone), DEFAULTS_ZERO, 0, fh);
#endif
fprintf(fh, "%s=%s\n", MODEL_MIXERMODE, STDMIXER_ModeName(m->mixer_mode));
if(m->icon[0] != 0)
Expand All @@ -1162,7 +1168,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
fprintf(fh, "%s=%s\n", MODEL_TYPE, MODEL_TYPE_VAL[m->type]);
fprintf(fh, "[%s]\n", SECTION_RADIO);
fprintf(fh, "%s=%s\n", RADIO_PROTOCOL, PROTOCOL_GetName(m->protocol));
write_int(m, _secradio, ARRAYSIZE(_secradio), fh);
write_int2(m, _secradio, ARRAYSIZE(_secradio), DEFAULTS_ZERO, 0, fh);
fprintf(fh, "%s=%s\n", RADIO_TX_POWER, radio_tx_power_val(m->radio, m->tx_power));
fprintf(fh, "\n");
write_proto_opts(fh, m);
Expand All @@ -1181,7 +1187,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
fprintf(fh, "[%s%d]\n", SECTION_CHANNEL, idx+1);
if(WRITE_FULL_MODEL || (m->limits[idx].flags & CH_REVERSE))
fprintf(fh, "%s=%d\n", CHAN_LIMIT_REVERSE, (m->limits[idx].flags & CH_REVERSE) ? 1 : 0);
write_int2(&m->limits[idx], _seclimit, _seclimit_defaults, ARRAYSIZE(_seclimit), fh);
write_int2(&m->limits[idx], _seclimit, ARRAYSIZE(_seclimit), _seclimit_defaults, ARRAYSIZE(_seclimit_defaults), fh);
if(WRITE_FULL_MODEL || (m->limits[idx].flags & CH_FAILSAFE_EN)) {
if(m->limits[idx].flags & CH_FAILSAFE_EN) {
fprintf(fh, "%s=%d\n", CHAN_LIMIT_FAILSAFE, m->limits[idx].failsafe);
Expand Down Expand Up @@ -1216,7 +1222,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
if (PPMin_Mode() != PPM_IN_SOURCE) {
fprintf(fh, "%s=%s\n", PPMIN_SWITCH, INPUT_SourceNameReal(file, m->train_sw));
}
write_int(m, _secppm, ARRAYSIZE(_secppm), fh);
write_int2(m, _secppm, ARRAYSIZE(_secppm), DEFAULTS_ZERO, 0, fh);
//fprintf(fh, "%s=%d\n", PPMIN_CENTERPW, m->ppmin_centerpw);
//fprintf(fh, "%s=%d\n", PPMIN_DELTAPW, m->ppmin_deltapw);
if (PPMin_Mode() != PPM_IN_SOURCE) {
Expand All @@ -1237,7 +1243,7 @@ u8 CONFIG_WriteModel(u8 model_num) {
m->trims[idx].src >= 1 && m->trims[idx].src <= 4
? tx_stick_names[m->trims[idx].src-1]
: INPUT_SourceNameReal(file, m->trims[idx].src));
write_int2(&m->trims[idx], _sectrim, _sectrim_defaults, ARRAYSIZE(_sectrim), fh);
write_int2(&m->trims[idx], _sectrim, ARRAYSIZE(_sectrim), _sectrim_defaults, ARRAYSIZE(_sectrim_defaults), fh);
if(WRITE_FULL_MODEL || m->trims[idx].sw)
fprintf(fh, "%s=%s\n", TRIM_SWITCH, INPUT_SourceNameAbbrevSwitchReal(file, m->trims[idx].sw));
if(WRITE_FULL_MODEL || m->trims[idx].value[0] || m->trims[idx].value[1] || m->trims[idx].value[2]
Expand All @@ -1255,14 +1261,12 @@ u8 CONFIG_WriteModel(u8 model_num) {
fprintf(fh, "%s=1\n", SWASH_AIL_INV);
if (WRITE_FULL_MODEL || m->swash_invert & 0x04)
fprintf(fh, "%s=1\n", SWASH_COL_INV);
write_int(m, _secswash, ARRAYSIZE(_secswash), fh);
write_int2(m, _secswash, ARRAYSIZE(_secswash), _secswash_defaults, ARRAYSIZE(_secswash_defaults), fh);
}
for(idx = 0; idx < NUM_TIMERS; idx++) {
if (! WRITE_FULL_MODEL && m->timer[idx].src == 0 && m->timer[idx].type == TIMER_STOPWATCH)
continue;
fprintf(fh, "[%s%d]\n", SECTION_TIMER, idx+1);
if (WRITE_FULL_MODEL || m->timer[idx].type != TIMER_STOPWATCH)
fprintf(fh, "%s=%s\n", TIMER_TYPE, TIMER_TYPE_VAL[m->timer[idx].type]);
write_int(&m->timer[idx], _sectimer, ARRAYSIZE(_sectimer), fh);
if (WRITE_FULL_MODEL || ((m->timer[idx].type == TIMER_COUNTDOWN || m->timer[idx].type == TIMER_COUNTDOWN_PROP) && m->timer[idx].timer))
fprintf(fh, "%s=%d\n", TIMER_TIME, m->timer[idx].timer);
Expand Down

0 comments on commit dd682ef

Please sign in to comment.