Browse Source

AP_Button: make singleton

c415-sdk
Peter Hall 5 years ago committed by Randy Mackay
parent
commit
0226d28d4b
  1. 16
      libraries/AP_Button/AP_Button.cpp
  2. 16
      libraries/AP_Button/AP_Button.h

16
libraries/AP_Button/AP_Button.cpp

@ -20,6 +20,8 @@ @@ -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[] = { @@ -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) @@ -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();
}
}

16
libraries/AP_Button/AP_Button.h

@ -28,12 +28,23 @@ public: @@ -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: @@ -65,3 +76,6 @@ private:
void setup_pins();
};
namespace AP {
AP_Button &button();
};

Loading…
Cancel
Save