From 6045612011e8f789f9e698065ceb0441282a40c3 Mon Sep 17 00:00:00 2001 From: murata Date: Sat, 10 Dec 2016 02:33:25 +0900 Subject: [PATCH] AP_Beacon: combine duplicate checks into device_ready method --- libraries/AP_Beacon/AP_Beacon.cpp | 20 +++++++++++++------- libraries/AP_Beacon/AP_Beacon.h | 4 ++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/libraries/AP_Beacon/AP_Beacon.cpp b/libraries/AP_Beacon/AP_Beacon.cpp index 43237df859..889bf6ddaf 100644 --- a/libraries/AP_Beacon/AP_Beacon.cpp +++ b/libraries/AP_Beacon/AP_Beacon.cpp @@ -103,7 +103,7 @@ bool AP_Beacon::enabled(void) // return true if sensor is basically healthy (we are receiving data) bool AP_Beacon::healthy(void) { - if (_driver == nullptr || _type == AP_BeaconType_None) { + if (!device_ready()) { return false; } return _driver->healthy(); @@ -112,7 +112,7 @@ bool AP_Beacon::healthy(void) // update state. This should be called often from the main loop void AP_Beacon::update(void) { - if (_driver == nullptr || _type == AP_BeaconType_None) { + if (!device_ready()) { return; } _driver->update(); @@ -121,7 +121,7 @@ void AP_Beacon::update(void) // return origin of position estimate system bool AP_Beacon::get_origin(Location &origin_loc) const { - if (_driver == nullptr || _type == AP_BeaconType_None) { + if (!device_ready()) { return false; } @@ -142,7 +142,7 @@ bool AP_Beacon::get_origin(Location &origin_loc) const // return position in NED from position estimate system's origin bool AP_Beacon::get_vehicle_position_ned(Vector3f &position, float& accuracy_estimate) const { - if (_driver == nullptr || _type == AP_BeaconType_None) { + if (!device_ready()) { return false; } @@ -160,7 +160,7 @@ bool AP_Beacon::get_vehicle_position_ned(Vector3f &position, float& accuracy_est // return the number of beacons uint8_t AP_Beacon::count() const { - if (_driver == nullptr || _type == AP_BeaconType_None) { + if (!device_ready()) { return 0; } return num_beacons; @@ -169,7 +169,7 @@ uint8_t AP_Beacon::count() const // return all beacon data bool AP_Beacon::get_beacon_data(uint8_t beacon_instance, struct BeaconState& state) const { - if (_driver == nullptr || _type == AP_BeaconType_None || beacon_instance >= num_beacons) { + if (!device_ready() || beacon_instance >= num_beacons) { return false; } state = beacon_state[beacon_instance]; @@ -206,7 +206,7 @@ float AP_Beacon::beacon_distance(uint8_t beacon_instance) const // return beacon position Vector3f AP_Beacon::beacon_position(uint8_t beacon_instance) const { - if (_driver == nullptr || _type == AP_BeaconType_None || beacon_instance >= num_beacons) { + if (!device_ready() || beacon_instance >= num_beacons) { Vector3f temp = {}; return temp; } @@ -221,3 +221,9 @@ uint32_t AP_Beacon::beacon_last_update_ms(uint8_t beacon_instance) const } return beacon_state[beacon_instance].distance_update_ms; } + +// check if the device is ready +bool AP_Beacon::device_ready(void) const +{ + return ((_driver != nullptr) && (_type != AP_BeaconType_None)); +} diff --git a/libraries/AP_Beacon/AP_Beacon.h b/libraries/AP_Beacon/AP_Beacon.h index eab3751fdf..862aa9f5ea 100644 --- a/libraries/AP_Beacon/AP_Beacon.h +++ b/libraries/AP_Beacon/AP_Beacon.h @@ -92,6 +92,10 @@ public: static const struct AP_Param::GroupInfo var_info[]; private: + + // check if device is ready + bool device_ready(void) const; + // parameters AP_Int8 _type; AP_Float origin_lat;