6 #include "common/string_light.h"
7 #include "common/utils.h"
9 #include "settings_generated.h"
11 #include "fc/settings.h"
13 #include "config/general_settings.h"
14 #include "flight/rpm_filter.h"
15 #include "settings_generated.c"
17 static bool settingGetWord(char *buf
, int idx
)
22 const uint8_t *ptr
= settingNamesWords
;
27 int shift
= 8 - SETTINGS_WORDS_BITS_PER_CHAR
- used_bits
;
30 chr
= (*ptr
>> shift
) & (0xff >> (8 - SETTINGS_WORDS_BITS_PER_CHAR
));
32 chr
= (*ptr
& (0xff >> (8 - (SETTINGS_WORDS_BITS_PER_CHAR
+ shift
)))) << -shift
;
34 chr
|= (*ptr
) >> (8 + shift
);
38 // Finished copying the word
46 c
= wordSymbols
[chr
- 27];
55 used_bits
= (used_bits
+ SETTINGS_WORDS_BITS_PER_CHAR
) % 8;
60 void settingGetName(const setting_t
*val
, char *buf
)
64 char word
[SETTING_MAX_WORD_LENGTH
];
65 #ifndef SETTING_ENCODED_NAME_USES_BYTE_INDEXING
68 for (uint8_t ii
= 0; ii
< SETTING_ENCODED_NAME_MAX_BYTES
; ii
++) {
69 #ifdef SETTING_ENCODED_NAME_USES_BYTE_INDEXING
70 n
= val
->encoded_name
[ii
];
72 // Decode a variable size uint
73 uint16_t b
= val
->encoded_name
[ii
];
76 n
|= (b
&0x7f) << shift
;
83 if (!settingGetWord(word
, n
)) {
91 strcpy(&buf
[bpos
], word
);
93 #ifndef SETTING_ENCODED_NAME_USES_BYTE_INDEXING
102 bool settingNameContains(const setting_t
*val
, char *buf
, const char *cmdline
)
104 settingGetName(val
, buf
);
105 return strstr(buf
, cmdline
) != NULL
;
108 bool settingNameIsExactMatch(const setting_t
*val
, char *buf
, const char *cmdline
, uint8_t var_name_length
)
110 settingGetName(val
, buf
);
111 return sl_strncasecmp(cmdline
, buf
, strlen(buf
)) == 0 && var_name_length
== strlen(buf
);
114 const setting_t
*settingFind(const char *name
)
116 char buf
[SETTING_MAX_NAME_LENGTH
];
117 for (int ii
= 0; ii
< SETTINGS_TABLE_COUNT
; ii
++) {
118 const setting_t
*setting
= &settingsTable
[ii
];
119 settingGetName(setting
, buf
);
120 if (strcmp(buf
, name
) == 0) {
127 const setting_t
*settingGet(unsigned index
)
129 return index
< SETTINGS_TABLE_COUNT
? &settingsTable
[index
] : NULL
;
132 unsigned settingGetIndex(const setting_t
*val
)
134 return val
- settingsTable
;
137 bool settingsValidate(unsigned *invalidIndex
)
139 for (unsigned ii
= 0; ii
< SETTINGS_TABLE_COUNT
; ii
++) {
140 const setting_t
*setting
= settingGet(ii
);
141 setting_min_t min
= settingGetMin(setting
);
142 setting_max_t max
= settingGetMax(setting
);
143 void *ptr
= settingGetValuePointer(setting
);
144 bool isValid
= false;
145 switch (SETTING_TYPE(setting
)) {
148 uint8_t *value
= ptr
;
149 isValid
= *value
>= min
&& *value
<= max
;
155 isValid
= *value
>= min
&& *value
<= (int8_t)max
;
160 uint16_t *value
= ptr
;
161 isValid
= *value
>= min
&& *value
<= max
;
166 int16_t *value
= ptr
;
167 isValid
= *value
>= min
&& *value
<= (int16_t)max
;
172 uint32_t *value
= ptr
;
173 isValid
= *value
>= (uint32_t)min
&& *value
<= max
;
179 isValid
= *value
>= min
&& *value
<= max
;
183 // We assume all strings are valid
197 size_t settingGetValueSize(const setting_t
*val
)
199 switch (SETTING_TYPE(val
)) {
213 return settingGetMax(val
);
215 return 0; // Unreachable
218 pgn_t
settingGetPgn(const setting_t
*val
)
220 uint16_t pos
= val
- (const setting_t
*)settingsTable
;
222 for (uint8_t ii
= 0; ii
< SETTINGS_PGN_COUNT
; ii
++) {
223 acc
+= settingsPgnCounts
[ii
];
225 return settingsPgn
[ii
];
231 static uint16_t getValueOffset(const setting_t
*value
)
233 switch (SETTING_SECTION(value
)) {
235 return value
->offset
;
237 return value
->offset
+ sizeof(pidProfile_t
) * getConfigProfile();
238 case CONTROL_RATE_VALUE
:
239 return value
->offset
+ sizeof(controlRateConfig_t
) * getConfigProfile();
241 return value
->offset
+ sizeof(ezTuneSettings_t
) * getConfigProfile();
242 case BATTERY_CONFIG_VALUE
:
243 return value
->offset
+ sizeof(batteryProfile_t
) * getConfigBatteryProfile();
244 case MIXER_CONFIG_VALUE
:
245 return value
->offset
+ sizeof(mixerProfile_t
) * getConfigMixerProfile();
250 void *settingGetValuePointer(const setting_t
*val
)
252 const pgRegistry_t
*pg
= pgFind(settingGetPgn(val
));
253 return pg
->address
+ getValueOffset(val
);
256 const void * settingGetCopyValuePointer(const setting_t
*val
)
258 const pgRegistry_t
*pg
= pgFind(settingGetPgn(val
));
259 return pg
->copy
+ getValueOffset(val
);
262 setting_min_t
settingGetMin(const setting_t
*val
)
264 if (SETTING_MODE(val
) == MODE_LOOKUP
) {
267 return settingMinMaxTable
[SETTING_INDEXES_GET_MIN(val
)];
270 setting_max_t
settingGetMax(const setting_t
*val
)
272 if (SETTING_MODE(val
) == MODE_LOOKUP
) {
273 return settingLookupTables
[val
->config
.lookup
.tableIndex
].valueCount
- 1;
275 return settingMinMaxTable
[SETTING_INDEXES_GET_MAX(val
)];
278 const lookupTableEntry_t
* settingLookupTable(const setting_t
*val
)
280 if (SETTING_MODE(val
) == MODE_LOOKUP
&& val
->config
.lookup
.tableIndex
< LOOKUP_TABLE_COUNT
) {
281 return &settingLookupTables
[val
->config
.lookup
.tableIndex
];
286 const char * settingLookupValueName(const setting_t
*val
, unsigned v
)
288 const lookupTableEntry_t
*table
= settingLookupTable(val
);
289 if (table
&& v
< table
->valueCount
) {
290 return table
->values
[v
];
295 size_t settingGetValueNameMaxSize(const setting_t
*val
)
298 const lookupTableEntry_t
*table
= settingLookupTable(val
);
300 for (unsigned ii
= 0; ii
< table
->valueCount
; ii
++) {
301 maxSize
= MAX(maxSize
, strlen(table
->values
[ii
]));
307 const char * settingGetString(const setting_t
*val
)
309 if (SETTING_TYPE(val
) == VAR_STRING
) {
310 return settingGetValuePointer(val
);
315 void settingSetString(const setting_t
*val
, const char *s
, size_t size
)
317 if (SETTING_TYPE(val
) == VAR_STRING
) {
318 char *p
= settingGetValuePointer(val
);
319 size_t copySize
= MIN(size
, settingGetMax(val
));
320 memcpy(p
, s
, copySize
);
325 setting_max_t
settingGetStringMaxLength(const setting_t
*val
)
327 if (SETTING_TYPE(val
) == VAR_STRING
) {
328 // Max string length is stored as its max
329 return settingGetMax(val
);
334 bool settingsGetParameterGroupIndexes(pgn_t pg
, uint16_t *start
, uint16_t *end
)
337 for (int ii
= 0; ii
< SETTINGS_PGN_COUNT
; ii
++) {
338 if (settingsPgn
[ii
] == pg
) {
343 *end
= acc
+ settingsPgnCounts
[ii
] - 1;
347 acc
+= settingsPgnCounts
[ii
];