diff --git a/libraries/AP_Scripting/AP_Scripting_helpers.cpp b/libraries/AP_Scripting/AP_Scripting_helpers.cpp new file mode 100644 index 0000000000..b22be853b8 --- /dev/null +++ b/libraries/AP_Scripting/AP_Scripting_helpers.cpp @@ -0,0 +1,95 @@ +#include "AP_Scripting_helpers.h" + +/// Fast param access via pointer helper class + +// init by name +bool Parameter::init(const char *name) +{ + vp = AP_Param::find(name, &vtype); + if (vp == nullptr) { + return false; + } + return true; +} + +// set a value +bool Parameter::set(float value) +{ + if (vp == nullptr) { + return false; + } + switch (vtype) { + case AP_PARAM_INT8: + ((AP_Int8 *)vp)->set(value); + return true; + case AP_PARAM_INT16: + ((AP_Int16 *)vp)->set(value); + return true; + case AP_PARAM_INT32: + ((AP_Int32 *)vp)->set(value); + return true; + case AP_PARAM_FLOAT: + ((AP_Float *)vp)->set(value); + return true; + default: + break; + } + // not a supported type + return false; +} + +// get a value by name +bool Parameter::get(float &value) +{ + 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 +bool Parameter::set_and_save(float value) +{ + if (vp == nullptr) { + return false; + } + switch (vtype) { + case AP_PARAM_INT8: + ((AP_Int8 *)vp)->set_and_save(value); + return true; + case AP_PARAM_INT16: + ((AP_Int16 *)vp)->set_and_save(value); + return true; + case AP_PARAM_INT32: + ((AP_Int32 *)vp)->set_and_save(value); + return true; + case AP_PARAM_FLOAT: + ((AP_Float *)vp)->set_and_save(value); + return true; + default: + break; + } + // not a supported type + return false; +} + diff --git a/libraries/AP_Scripting/AP_Scripting_helpers.h b/libraries/AP_Scripting/AP_Scripting_helpers.h new file mode 100644 index 0000000000..2a74199b73 --- /dev/null +++ b/libraries/AP_Scripting/AP_Scripting_helpers.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +/// Fast param access via pointer helper +class Parameter +{ +public: + + // init to param by name + bool init(const char *name); + + // setters and getters + bool set(float value); + bool set_and_save(float value); + bool get(float &value); + +private: + enum ap_var_type vtype; + AP_Param *vp; +}; +