From 3984cdd823ba19e9dd2dfe10b29492937d4934b2 Mon Sep 17 00:00:00 2001 From: Randy Mackay Date: Fri, 27 Nov 2020 17:17:01 +0900 Subject: [PATCH] AP_WheelEncoder: add singleton --- libraries/AP_WheelEncoder/AP_WheelEncoder.cpp | 14 ++++++++++++++ libraries/AP_WheelEncoder/AP_WheelEncoder.h | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/libraries/AP_WheelEncoder/AP_WheelEncoder.cpp b/libraries/AP_WheelEncoder/AP_WheelEncoder.cpp index 545a7dcf9d..1d439126e9 100644 --- a/libraries/AP_WheelEncoder/AP_WheelEncoder.cpp +++ b/libraries/AP_WheelEncoder/AP_WheelEncoder.cpp @@ -151,6 +151,8 @@ const AP_Param::GroupInfo AP_WheelEncoder::var_info[] = { AP_WheelEncoder::AP_WheelEncoder(void) { + _singleton = this; + AP_Param::setup_object_defaults(this, var_info); } @@ -347,3 +349,15 @@ uint32_t AP_WheelEncoder::get_last_reading_ms(uint8_t instance) const } return state[instance].last_reading_ms; } + +// singleton instance +AP_WheelEncoder *AP_WheelEncoder::_singleton; + +namespace AP { + +AP_WheelEncoder *wheelencoder() +{ + return AP_WheelEncoder::get_singleton(); +} + +} diff --git a/libraries/AP_WheelEncoder/AP_WheelEncoder.h b/libraries/AP_WheelEncoder/AP_WheelEncoder.h index fb64a57808..2ec4ac2c1a 100644 --- a/libraries/AP_WheelEncoder/AP_WheelEncoder.h +++ b/libraries/AP_WheelEncoder/AP_WheelEncoder.h @@ -39,6 +39,11 @@ public: AP_WheelEncoder(const AP_WheelEncoder &other) = delete; AP_WheelEncoder &operator=(const AP_WheelEncoder&) = delete; + // get singleton instance + static AP_WheelEncoder *get_singleton() { + return _singleton; + } + // WheelEncoder driver types enum WheelEncoder_Type : uint8_t { WheelEncoder_TYPE_NONE = 0, @@ -121,4 +126,12 @@ protected: AP_WheelEncoder_Backend *drivers[WHEELENCODER_MAX_INSTANCES]; uint8_t num_instances; Vector3f pos_offset_zero; // allows returning position offsets of zero for invalid requests + +private: + + static AP_WheelEncoder *_singleton; }; + +namespace AP { + AP_WheelEncoder *wheelencoder(); +}