mirror of
https://github.com/ArduPilot/ardupilot
synced 2025-01-30 12:38:33 -04:00
AP_Param: change method of loading defaults
this avoids a dependency on the constructor ordering by loading defaults for each object separately
This commit is contained in:
parent
562b6a20c9
commit
95d4cc2ce9
@ -753,17 +753,17 @@ void AP_Param::set_value(enum ap_var_type type, void *ptr, float def_value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// load default values for scalars in a group
|
// load default values for scalars in a group. This does not recurse
|
||||||
void AP_Param::load_defaults_group(const struct GroupInfo *group_info, uintptr_t base)
|
// into other objects. This is a static function that should be called
|
||||||
|
// in the objects constructor
|
||||||
|
void AP_Param::setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info)
|
||||||
{
|
{
|
||||||
|
uintptr_t base = (uintptr_t)object_pointer;
|
||||||
uint8_t type;
|
uint8_t type;
|
||||||
for (uint8_t i=0;
|
for (uint8_t i=0;
|
||||||
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
(type=PGM_UINT8(&group_info[i].type)) != AP_PARAM_NONE;
|
||||||
i++) {
|
i++) {
|
||||||
if (type == AP_PARAM_GROUP) {
|
if (type <= AP_PARAM_FLOAT) {
|
||||||
const struct GroupInfo *ginfo = (const struct GroupInfo *)PGM_POINTER(&group_info[i].group_info);
|
|
||||||
load_defaults_group(ginfo, base);
|
|
||||||
} else if (type <= AP_PARAM_FLOAT) {
|
|
||||||
void *ptr = (void *)(base + PGM_UINT16(&group_info[i].offset));
|
void *ptr = (void *)(base + PGM_UINT16(&group_info[i].offset));
|
||||||
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&group_info[i].def_value));
|
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&group_info[i].def_value));
|
||||||
}
|
}
|
||||||
@ -771,16 +771,14 @@ void AP_Param::load_defaults_group(const struct GroupInfo *group_info, uintptr_t
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// load default values for all scalars
|
// load default values for all scalars in a sketch. This does not
|
||||||
void AP_Param::load_defaults(void)
|
// recurse into sub-objects
|
||||||
|
void AP_Param::setup_sketch_defaults(const struct Info *info, uint16_t eeprom_size)
|
||||||
{
|
{
|
||||||
|
setup(info, eeprom_size);
|
||||||
for (uint8_t i=0; i<_num_vars; i++) {
|
for (uint8_t i=0; i<_num_vars; i++) {
|
||||||
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
uint8_t type = PGM_UINT8(&_var_info[i].type);
|
||||||
if (type == AP_PARAM_GROUP) {
|
if (type <= AP_PARAM_FLOAT) {
|
||||||
const struct GroupInfo *group_info = (const struct GroupInfo *)PGM_POINTER(&_var_info[i].group_info);
|
|
||||||
uintptr_t base = PGM_POINTER(&_var_info[i].ptr);
|
|
||||||
load_defaults_group(group_info, base);
|
|
||||||
} else if (type <= AP_PARAM_FLOAT) {
|
|
||||||
void *ptr = (void*)PGM_POINTER(&_var_info[i].ptr);
|
void *ptr = (void*)PGM_POINTER(&_var_info[i].ptr);
|
||||||
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&_var_info[i].def_value));
|
set_value((enum ap_var_type)type, ptr, PGM_FLOAT(&_var_info[i].def_value));
|
||||||
}
|
}
|
||||||
|
@ -16,8 +16,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <avr/pgmspace.h>
|
#include <AP_Progmem.h>
|
||||||
#include <avr/eeprom.h>
|
|
||||||
|
|
||||||
#define AP_MAX_NAME_SIZE 16
|
#define AP_MAX_NAME_SIZE 16
|
||||||
#define AP_NESTED_GROUPS_ENABLED
|
#define AP_NESTED_GROUPS_ENABLED
|
||||||
@ -89,15 +88,8 @@ public:
|
|||||||
// wrong version is found
|
// wrong version is found
|
||||||
static bool setup(const struct Info *info, uint16_t eeprom_size);
|
static bool setup(const struct Info *info, uint16_t eeprom_size);
|
||||||
|
|
||||||
// constructor to load default values and setup var_info table
|
// empty constructor
|
||||||
AP_Param(const struct Info *info, uint16_t eeprom_size) {
|
AP_Param() {}
|
||||||
setup(info, eeprom_size);
|
|
||||||
load_defaults();
|
|
||||||
}
|
|
||||||
|
|
||||||
// empty constructor for child classes
|
|
||||||
AP_Param() {
|
|
||||||
}
|
|
||||||
|
|
||||||
// a token used for first()/next() state
|
// a token used for first()/next() state
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -167,10 +159,11 @@ public:
|
|||||||
static void set_value(enum ap_var_type type, void *ptr, float def_value);
|
static void set_value(enum ap_var_type type, void *ptr, float def_value);
|
||||||
|
|
||||||
// load default values for scalars in a group
|
// load default values for scalars in a group
|
||||||
static void load_defaults_group(const struct GroupInfo *group_info, uintptr_t base);
|
static void setup_object_defaults(const void *object_pointer, const struct GroupInfo *group_info);
|
||||||
|
|
||||||
// load default values for all scalars
|
// load default values for all scalars in the main sketch. This
|
||||||
static void load_defaults(void);
|
// does not recurse into the sub-objects
|
||||||
|
static void setup_sketch_defaults(const struct Info *info, uint16_t eeprom_size);
|
||||||
|
|
||||||
/// Erase all variables in EEPROM.
|
/// Erase all variables in EEPROM.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user