From 0922b82d4233cf105d0a825a1174e572f9d75798 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 20 Sep 2012 07:42:35 +1000 Subject: [PATCH] AP_Param: added find_by_index() --- libraries/AP_Common/AP_Param.cpp | 16 ++++++++++++++++ libraries/AP_Common/AP_Param.h | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/libraries/AP_Common/AP_Param.cpp b/libraries/AP_Common/AP_Param.cpp index 8eae231eab..7e9a0d0827 100644 --- a/libraries/AP_Common/AP_Param.cpp +++ b/libraries/AP_Common/AP_Param.cpp @@ -579,6 +579,22 @@ AP_Param::find(const char *name, enum ap_var_type *ptype) return NULL; } +// Find a variable by index. Note that this is quite slow. +// +AP_Param * +AP_Param::find_by_index(uint16_t idx, enum ap_var_type *ptype) +{ + ParamToken token; + AP_Param *ap; + uint16_t count=0; + for (ap=AP_Param::first(&token, ptype); + ap && count < idx; + ap=AP_Param::next_scalar(&token, ptype)) { + count++; + } + return ap; +} + // Save the variable to EEPROM, if supported // bool AP_Param::save(void) diff --git a/libraries/AP_Common/AP_Param.h b/libraries/AP_Common/AP_Param.h index c06d9a03dc..9b1379e415 100644 --- a/libraries/AP_Common/AP_Param.h +++ b/libraries/AP_Common/AP_Param.h @@ -132,6 +132,15 @@ public: /// static AP_Param * find(const char *name, enum ap_var_type *ptype); + /// Find a variable by index. + /// + /// + /// @param idx The index of the variable + /// @return A pointer to the variable, or NULL if + /// it does not exist. + /// + static AP_Param * find_by_index(uint16_t idx, enum ap_var_type *ptype); + /// Save the current value of the variable to EEPROM. /// /// @return True if the variable was saved successfully.