Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/libraries/AP_Frsky_Telem/AP_Frsky_Telem.cpp
Views: 1798
/*12Inspired by work done here3https://github.com/PX4/Firmware/tree/master/src/drivers/frsky_telemetry from Stefan Rado <[email protected]>4https://github.com/opentx/opentx/tree/2.3/radio/src/telemetry from the OpenTX team56This program is free software: you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation, either version 3 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program. If not, see <http://www.gnu.org/licenses/>.18*/1920/*21FRSKY Telemetry library22*/2324#include "AP_Frsky_config.h"2526#if AP_FRSKY_TELEM_ENABLED2728#include "AP_Frsky_Telem.h"29#include "AP_Frsky_Parameters.h"3031#include <AP_SerialManager/AP_SerialManager.h>3233#include <AP_Vehicle/AP_Vehicle.h>3435#include "AP_Frsky_D.h"36#include "AP_Frsky_SPort.h"37#include "AP_Frsky_SPort_Passthrough.h"3839extern const AP_HAL::HAL& hal;4041AP_Frsky_Telem *AP_Frsky_Telem::singleton;4243AP_Frsky_Telem::AP_Frsky_Telem()44{45singleton = this;46#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL47_frsky_parameters = &AP::vehicle()->frsky_parameters;48#endif //HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL49}505152AP_Frsky_Telem::~AP_Frsky_Telem(void)53{54singleton = nullptr;55}5657/*58* init - perform required initialisation59*/60bool AP_Frsky_Telem::init(bool use_external_data)61{62const AP_SerialManager &serial_manager = AP::serialmanager();6364// check for protocol configured for a serial port - only the first serial port with one of these protocols will then run (cannot have FrSky on multiple serial ports)65AP_HAL::UARTDriver *port;66if ((port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_FrSky_D, 0))) {67#if AP_FRSKY_D_TELEM_ENABLED68_backend = NEW_NOTHROW AP_Frsky_D(port);69#endif70} else if ((port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_FrSky_SPort, 0))) {71#if AP_FRSKY_SPORT_TELEM_ENABLED72_backend = NEW_NOTHROW AP_Frsky_SPort(port);73#endif74} else if (use_external_data || (port = serial_manager.find_serial(AP_SerialManager::SerialProtocol_FrSky_SPort_Passthrough, 0))) {75#if AP_FRSKY_SPORT_PASSTHROUGH_ENABLED76_backend = NEW_NOTHROW AP_Frsky_SPort_Passthrough(port, use_external_data, _frsky_parameters);77#endif78}7980if (_backend == nullptr) {81return false;82}8384if (!_backend->init()) {85delete _backend;86_backend = nullptr;87return false;88}8990return true;91}9293bool AP_Frsky_Telem::_get_telem_data(AP_Frsky_Backend::sport_packet_t* packet_array, uint8_t &packet_count, const uint8_t max_size)94{95if (_backend == nullptr) {96return false;97}98if (packet_array == nullptr) {99return false;100}101return _backend->get_telem_data(packet_array, packet_count, max_size);102}103104#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL105bool AP_Frsky_Telem::_set_telem_data(uint8_t frame, uint16_t appid, uint32_t data)106{107if (_backend == nullptr) {108return false;109}110return _backend->set_telem_data(frame, appid, data);111}112#endif113114void AP_Frsky_Telem::try_create_singleton_for_external_data()115{116// try to allocate an AP_Frsky_Telem object only if we are disarmed117if (!singleton && !hal.util->get_soft_armed()) {118NEW_NOTHROW AP_Frsky_Telem();119// initialize the passthrough scheduler120if (singleton) {121singleton->init(true);122}123}124}125126/*127fetch Sport data for an external transport, such as FPort128*/129bool AP_Frsky_Telem::get_telem_data(AP_Frsky_Backend::sport_packet_t* packet_array, uint8_t &packet_count, const uint8_t max_size)130{131try_create_singleton_for_external_data();132if (singleton == nullptr) {133return false;134}135return singleton->_get_telem_data(packet_array, packet_count, max_size);136}137138#if HAL_WITH_FRSKY_TELEM_BIDIRECTIONAL139/*140allow external transports (e.g. FPort), to supply telemetry data141*/142bool AP_Frsky_Telem::set_telem_data(const uint8_t frame, const uint16_t appid, const uint32_t data)143{144try_create_singleton_for_external_data();145if (singleton == nullptr) {146return false;147}148return singleton->_set_telem_data(frame, appid, data);149}150#endif151152namespace AP153{154AP_Frsky_Telem *frsky_telem()155{156return AP_Frsky_Telem::get_singleton();157}158};159160#endif // AP_FRSKY_TELEM_ENABLED161162163