From 3538fe360b02cd5135b38f67132ccd401d2c162c Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Fri, 1 Nov 2019 17:11:59 +1100 Subject: [PATCH] AP_RangeFinder: add intermediate serial backend class to share common code --- libraries/AP_RangeFinder/RangeFinder.cpp | 1 - .../RangeFinder_Backend_Serial.cpp | 49 +++++++++++++++++++ .../RangeFinder_Backend_Serial.h | 19 +++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 libraries/AP_RangeFinder/RangeFinder_Backend_Serial.cpp create mode 100644 libraries/AP_RangeFinder/RangeFinder_Backend_Serial.h diff --git a/libraries/AP_RangeFinder/RangeFinder.cpp b/libraries/AP_RangeFinder/RangeFinder.cpp index aa1c726422..b9478a41ec 100644 --- a/libraries/AP_RangeFinder/RangeFinder.cpp +++ b/libraries/AP_RangeFinder/RangeFinder.cpp @@ -43,7 +43,6 @@ #include #include -#include #include extern const AP_HAL::HAL &hal; diff --git a/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.cpp b/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.cpp new file mode 100644 index 0000000000..8e0c184b3e --- /dev/null +++ b/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.cpp @@ -0,0 +1,49 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include +#include "RangeFinder_Backend_Serial.h" +#include + +#include + +extern const AP_HAL::HAL& hal; + +/* + The constructor also initialises the rangefinder. Note that this + constructor is not called until detect() returns true, so we + already know that we should setup the rangefinder +*/ +AP_RangeFinder_Backend_Serial::AP_RangeFinder_Backend_Serial( + RangeFinder::RangeFinder_State &_state, + AP_RangeFinder_Params &_params, + uint8_t serial_instance) : + AP_RangeFinder_Backend(_state, _params) +{ + const AP_SerialManager &serial_manager = AP::serialmanager(); + uart = serial_manager.find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance); + if (uart != nullptr) { + uart->begin(serial_manager.find_baudrate(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance)); + } +} + +/* + detect if a Serial rangefinder is connected. We'll detect by simply + checking for SerialManager configuration +*/ +bool AP_RangeFinder_Backend_Serial::detect(uint8_t serial_instance) +{ + return AP::serialmanager().find_serial(AP_SerialManager::SerialProtocol_Rangefinder, serial_instance) != nullptr; +} diff --git a/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.h b/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.h new file mode 100644 index 0000000000..12a216ff09 --- /dev/null +++ b/libraries/AP_RangeFinder/RangeFinder_Backend_Serial.h @@ -0,0 +1,19 @@ +#pragma once + +#include "RangeFinder_Backend.h" + +class AP_RangeFinder_Backend_Serial : public AP_RangeFinder_Backend +{ +public: + // constructor + AP_RangeFinder_Backend_Serial(RangeFinder::RangeFinder_State &_state, + AP_RangeFinder_Params &_params, + uint8_t serial_instance); + + // static detection function + static bool detect(uint8_t serial_instance); + +protected: + + AP_HAL::UARTDriver *uart = nullptr; +};