diff --git a/libraries/AP_Common/AP_Param.cpp b/libraries/AP_Common/AP_Param.cpp index 17c2c28ada..7d07f227da 100644 --- a/libraries/AP_Common/AP_Param.cpp +++ b/libraries/AP_Common/AP_Param.cpp @@ -575,9 +575,10 @@ bool AP_Param::load_all(void) // return the first variable in _var_info -AP_Param *AP_Param::first(uint16_t *token, enum ap_var_type *ptype) +AP_Param *AP_Param::first(ParamToken *token, enum ap_var_type *ptype) { - *token = 0; + token->key = 0; + token->group_element = 0; if (_num_vars == 0) { return NULL; } @@ -593,7 +594,7 @@ AP_Param *AP_Param::next_group(uint8_t vindex, const struct GroupInfo *group_inf bool *found_current, uint8_t group_base, uint8_t group_shift, - uint16_t *token, + ParamToken *token, enum ap_var_type *ptype) { uint8_t type; @@ -612,13 +613,14 @@ AP_Param *AP_Param::next_group(uint8_t vindex, const struct GroupInfo *group_inf } else { if (*found_current) { // got a new one - (*token) = ((uint16_t)GROUP_ID(group_info, group_base, i, group_shift)<<8) | vindex; + token->key = vindex; + token->group_element = GROUP_ID(group_info, group_base, i, group_shift); if (ptype != NULL) { *ptype = (enum ap_var_type)type; } return (AP_Param*)(PGM_POINTER(&_var_info[vindex].ptr) + PGM_UINT16(&group_info[i].offset)); } - if (GROUP_ID(group_info, group_base, i, group_shift) == (uint8_t)((*token)>>8)) { + if (GROUP_ID(group_info, group_base, i, group_shift) == token->group_element) { *found_current = true; } } @@ -628,9 +630,9 @@ AP_Param *AP_Param::next_group(uint8_t vindex, const struct GroupInfo *group_inf /// Returns the next variable in _var_info, recursing into groups /// as needed -AP_Param *AP_Param::next(uint16_t *token, enum ap_var_type *ptype) +AP_Param *AP_Param::next(ParamToken *token, enum ap_var_type *ptype) { - uint8_t i = (*token)&0xFF; + uint8_t i = token->key; bool found_current = false; if (i >= _num_vars) { // illegal token @@ -651,7 +653,8 @@ AP_Param *AP_Param::next(uint16_t *token, enum ap_var_type *ptype) } } else { // found the next one - (*token) = i; + token->key = i; + token->group_element = 0; if (ptype != NULL) { *ptype = (enum ap_var_type)type; } @@ -663,7 +666,7 @@ AP_Param *AP_Param::next(uint16_t *token, enum ap_var_type *ptype) /// Returns the next scalar in _var_info, recursing into groups /// as needed -AP_Param *AP_Param::next_scalar(uint16_t *token, enum ap_var_type *ptype) +AP_Param *AP_Param::next_scalar(ParamToken *token, enum ap_var_type *ptype) { AP_Param *ap; enum ap_var_type type; @@ -696,7 +699,7 @@ float AP_Param::cast_to_float(enum ap_var_type type) // print the value of all variables void AP_Param::show_all(void) { - uint16_t token; + ParamToken token; AP_Param *ap; enum ap_var_type type; diff --git a/libraries/AP_Common/AP_Param.h b/libraries/AP_Common/AP_Param.h index b898a8a297..82e234a42f 100644 --- a/libraries/AP_Common/AP_Param.h +++ b/libraries/AP_Common/AP_Param.h @@ -74,6 +74,12 @@ public: const struct GroupInfo *group_info; }; + // a token used for first()/next() state + typedef struct { + uint8_t key; + uint8_t group_element; + } ParamToken; + // called once at startup to setup the _var_info[] table. This // will also check the EEPROM header and re-initialise it if the // wrong version is found @@ -138,15 +144,15 @@ public: /// @return The first variable in _var_info, or NULL if /// there are none. /// - static AP_Param *first(uint16_t *token, enum ap_var_type *ptype); + static AP_Param *first(ParamToken *token, enum ap_var_type *ptype); /// Returns the next variable in _var_info, recursing into groups /// as needed - static AP_Param *next(uint16_t *token, enum ap_var_type *ptype); + static AP_Param *next(ParamToken *token, enum ap_var_type *ptype); /// Returns the next scalar variable in _var_info, recursing into groups /// as needed - static AP_Param *next_scalar(uint16_t *token, enum ap_var_type *ptype); + static AP_Param *next_scalar(ParamToken *token, enum ap_var_type *ptype); /// cast a variable to a float given its type float cast_to_float(enum ap_var_type type); @@ -203,7 +209,7 @@ private: bool *found_current, uint8_t group_base, uint8_t group_shift, - uint16_t *token, + ParamToken *token, enum ap_var_type *ptype); static uint16_t _eeprom_size;