AP_Compass: add AP::compass() singleton getter

This commit is contained in:
Peter Barker 2018-03-29 15:13:55 +11:00 committed by Peter Barker
parent 631e967df3
commit a3a1967e05
2 changed files with 29 additions and 0 deletions

View File

@ -462,6 +462,13 @@ Compass::Compass(void) :
_null_init_done(false),
_hil_mode(false)
{
if (_singleton != nullptr) {
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
AP_HAL::panic("Compass must be singleton");
#endif
return;
}
_singleton = this;
AP_Param::setup_object_defaults(this, var_info);
for (uint8_t i=0; i<COMPASS_MAX_BACKEND; i++) {
_backends[i] = nullptr;
@ -1324,3 +1331,15 @@ bool Compass::consistent() const
return true;
}
// singleton instance
Compass *Compass::_singleton;
namespace AP {
Compass &compass()
{
return *Compass::get_singleton();
}
}

View File

@ -56,6 +56,11 @@ public:
Compass(const Compass &other) = delete;
Compass &operator=(const Compass&) = delete;
// get singleton instance
static Compass *get_singleton() {
return _singleton;
}
friend class CompassLearn;
/// Initialize the compass device.
@ -326,6 +331,7 @@ public:
uint8_t get_filter_range() const { return uint8_t(_filter_range.get()); }
private:
static Compass *_singleton;
/// Register a new compas driver, allocating an instance number
///
/// @return number of compass instances
@ -466,3 +472,7 @@ private:
AP_Int8 _filter_range;
};
namespace AP {
Compass &compass();
};