From ac2fe5f042f756b154f476d1b997e24cf2279836 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Wed, 2 May 2018 20:16:35 +1000 Subject: [PATCH] AP_RSSI: add singleton --- libraries/AP_RSSI/AP_RSSI.cpp | 23 +++++++++++++++++++++++ libraries/AP_RSSI/AP_RSSI.h | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/libraries/AP_RSSI/AP_RSSI.cpp b/libraries/AP_RSSI/AP_RSSI.cpp index 8532de8c93..3a98998c1a 100644 --- a/libraries/AP_RSSI/AP_RSSI.cpp +++ b/libraries/AP_RSSI/AP_RSSI.cpp @@ -97,6 +97,10 @@ const AP_Param::GroupInfo AP_RSSI::var_info[] = { AP_RSSI::AP_RSSI() { AP_Param::setup_object_defaults(this, var_info); + if (_s_instance) { + AP_HAL::panic("Too many RSSI sensors"); + } + _s_instance = this; } // destructor @@ -104,6 +108,14 @@ AP_RSSI::~AP_RSSI(void) { } +/* + * Get the AP_RSSI singleton + */ +AP_RSSI *AP_RSSI::get_instance() +{ + return _s_instance; +} + // Initialize the rssi object and prepare it for use void AP_RSSI::init() { @@ -200,3 +212,14 @@ float AP_RSSI::scale_and_constrain_float_rssi(float current_rssi_value, float lo // value retrieved falls outside the user-supplied range. return constrain_float(rssi_value_scaled, 0.0f, 1.0f); } + +AP_RSSI *AP_RSSI::_s_instance = nullptr; + +namespace AP { + +AP_RSSI *rssi() +{ + return AP_RSSI::get_instance(); +} + +}; diff --git a/libraries/AP_RSSI/AP_RSSI.h b/libraries/AP_RSSI/AP_RSSI.h index dc39250eac..f766c9d0cf 100644 --- a/libraries/AP_RSSI/AP_RSSI.h +++ b/libraries/AP_RSSI/AP_RSSI.h @@ -37,6 +37,8 @@ public: // destructor ~AP_RSSI(void); + static AP_RSSI *get_instance(); + // Initialize the rssi object and prepare it for use void init(); @@ -55,6 +57,9 @@ public: static const struct AP_Param::GroupInfo var_info[]; private: + + static AP_RSSI *_s_instance; + // RSSI parameters AP_Int8 rssi_type; // Type of RSSI being used AP_Int8 rssi_analog_pin; // Analog pin RSSI value found on @@ -77,3 +82,7 @@ private: // Scale and constrain a float rssi value to 0.0 to 1.0 range float scale_and_constrain_float_rssi(float current_rssi_value, float low_rssi_range, float high_rssi_range); }; + +namespace AP { + AP_RSSI *rssi(); +};