diff --git a/libraries/AP_Button/AP_Button.cpp b/libraries/AP_Button/AP_Button.cpp index 95545c1082..040a7f6de9 100644 --- a/libraries/AP_Button/AP_Button.cpp +++ b/libraries/AP_Button/AP_Button.cpp @@ -20,6 +20,8 @@ extern const AP_HAL::HAL& hal; +AP_Button *AP_Button::_singleton; + const AP_Param::GroupInfo AP_Button::var_info[] = { // @Param: ENABLE @@ -72,6 +74,11 @@ const AP_Param::GroupInfo AP_Button::var_info[] = { AP_Button::AP_Button(void) { AP_Param::setup_object_defaults(this, var_info); + + if (_singleton != nullptr) { + AP_HAL::panic("AP_Button must be singleton"); + } + _singleton = this; } /* @@ -166,3 +173,12 @@ void AP_Button::setup_pins(void) hal.gpio->write(pin[i], 1); } } + +namespace AP { + +AP_Button &button() +{ + return *AP_Button::get_singleton(); +} + +} diff --git a/libraries/AP_Button/AP_Button.h b/libraries/AP_Button/AP_Button.h index a0c2903cd2..ef69ff6efd 100644 --- a/libraries/AP_Button/AP_Button.h +++ b/libraries/AP_Button/AP_Button.h @@ -28,12 +28,23 @@ public: // constructor AP_Button(void); + /* Do not allow copies */ + AP_Button(const AP_Button &other) = delete; + AP_Button &operator=(const AP_Button&) = delete; + static const struct AP_Param::GroupInfo var_info[]; // update button state and send messages, called periodically by main loop void update(void); - + + static AP_Button *get_singleton(void) { + return _singleton; + } + private: + + static AP_Button *_singleton; + AP_Int8 enable; AP_Int8 pin[AP_BUTTON_NUM_PINS]; @@ -65,3 +76,6 @@ private: void setup_pins(); }; +namespace AP { + AP_Button &button(); +};