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();
240 case BATTERY_CONFIG_VALUE
:
241 return value
->offset
+ sizeof(batteryProfile_t
) * getConfigBatteryProfile();
246 void *settingGetValuePointer(const setting_t
*val
)
248 const pgRegistry_t
*pg
= pgFind(settingGetPgn(val
));
249 return pg
->address
+ getValueOffset(val
);
252 const void * settingGetCopyValuePointer(const setting_t
*val
)
254 const pgRegistry_t
*pg
= pgFind(settingGetPgn(val
));
255 return pg
->copy
+ getValueOffset(val
);
258 setting_min_t
settingGetMin(const setting_t
*val
)
260 if (SETTING_MODE(val
) == MODE_LOOKUP
) {
263 return settingMinMaxTable
[SETTING_INDEXES_GET_MIN(val
)];
266 setting_max_t
settingGetMax(const setting_t
*val
)
268 if (SETTING_MODE(val
) == MODE_LOOKUP
) {
269 return settingLookupTables
[val
->config
.lookup
.tableIndex
].valueCount
- 1;
271 return settingMinMaxTable
[SETTING_INDEXES_GET_MAX(val
)];
274 const lookupTableEntry_t
* settingLookupTable(const setting_t
*val
)
276 if (SETTING_MODE(val
) == MODE_LOOKUP
&& val
->config
.lookup
.tableIndex
< LOOKUP_TABLE_COUNT
) {
277 return &settingLookupTables
[val
->config
.lookup
.tableIndex
];
282 const char * settingLookupValueName(const setting_t
*val
, unsigned v
)
284 const lookupTableEntry_t
*table
= settingLookupTable(val
);
285 if (table
&& v
< table
->valueCount
) {
286 return table
->values
[v
];
291 size_t settingGetValueNameMaxSize(const setting_t
*val
)
294 const lookupTableEntry_t
*table
= settingLookupTable(val
);
296 for (unsigned ii
= 0; ii
< table
->valueCount
; ii
++) {
297 maxSize
= MAX(maxSize
, strlen(table
->values
[ii
]));
303 const char * settingGetString(const setting_t
*val
)
305 if (SETTING_TYPE(val
) == VAR_STRING
) {
306 return settingGetValuePointer(val
);
311 void settingSetString(const setting_t
*val
, const char *s
, size_t size
)
313 if (SETTING_TYPE(val
) == VAR_STRING
) {
314 char *p
= settingGetValuePointer(val
);
315 size_t copySize
= MIN(size
, settingGetMax(val
));
316 memcpy(p
, s
, copySize
);
321 setting_max_t
settingGetStringMaxLength(const setting_t
*val
)
323 if (SETTING_TYPE(val
) == VAR_STRING
) {
324 // Max string length is stored as its max
325 return settingGetMax(val
);
330 bool settingsGetParameterGroupIndexes(pgn_t pg
, uint16_t *start
, uint16_t *end
)
333 for (int ii
= 0; ii
< SETTINGS_PGN_COUNT
; ii
++) {
334 if (settingsPgn
[ii
] == pg
) {
339 *end
= acc
+ settingsPgnCounts
[ii
] - 1;
343 acc
+= settingsPgnCounts
[ii
];