From 5164eacc4a8f1c6cc922635488729c25df8ade22 Mon Sep 17 00:00:00 2001 From: Peter Hall <33176108+IamPete1@users.noreply.github.com> Date: Mon, 25 Nov 2019 16:21:57 +0000 Subject: [PATCH] AP_Param: add get and set functions --- libraries/AP_Param/AP_Param.cpp | 33 +++++++++++++++++++++++++++++++++ libraries/AP_Param/AP_Param.h | 11 +++++++++++ 2 files changed, 44 insertions(+) diff --git a/libraries/AP_Param/AP_Param.cpp b/libraries/AP_Param/AP_Param.cpp index f2abcc13ec..4be8a1b116 100644 --- a/libraries/AP_Param/AP_Param.cpp +++ b/libraries/AP_Param/AP_Param.cpp @@ -2399,6 +2399,39 @@ bool AP_Param::set_by_name(const char *name, float value) return false; } +/* + get a value by name + */ +bool AP_Param::get(const char *name, float &value) +{ + enum ap_var_type vtype; + AP_Param *vp = find(name, &vtype); + if (vp == nullptr) { + return false; + } + switch (vtype) { + case AP_PARAM_INT8: + value = ((AP_Int8 *)vp)->get(); + break; + case AP_PARAM_INT16: + value = ((AP_Int16 *)vp)->get(); + break; + + case AP_PARAM_INT32: + value = ((AP_Int32 *)vp)->get(); + break; + + case AP_PARAM_FLOAT: + value = ((AP_Float *)vp)->get(); + break; + + default: + // not a supported type + return false; + } + return true; +} + /* set and save a value by name */ diff --git a/libraries/AP_Param/AP_Param.h b/libraries/AP_Param/AP_Param.h index 9c892cc912..1fb5386288 100644 --- a/libraries/AP_Param/AP_Param.h +++ b/libraries/AP_Param/AP_Param.h @@ -286,6 +286,15 @@ public: /// @param value The new value /// @return true if the variable is found static bool set_by_name(const char *name, float value); + // name helper for scripting + static bool set(const char *name, float value) { return set_by_name(name, value); }; + + /// gat a value by name, used by scripting + /// + /// @param name The full name of the variable to be found. + /// @param value A refernce to the variable + /// @return true if the variable is found + static bool get(const char *name, float &value); /// set and save a value by name /// @@ -293,6 +302,8 @@ public: /// @param value The new value /// @return true if the variable is found static bool set_and_save_by_name(const char *name, float value); + // name helper for scripting + static bool set_and_save(const char *name, float value) { return set_and_save_by_name(name, value); }; /// Find a variable by index. ///