2015-08-11 03:28:43 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2023-08-19 19:49:28 -03:00
|
|
|
#include <AP_RCProtocol/AP_RCProtocol_config.h>
|
|
|
|
|
|
|
|
#if CONFIG_HAL_BOARD == HAL_BOARD_SITL && AP_RCPROTOCOL_ENABLED
|
|
|
|
|
2012-12-17 23:56:21 -04:00
|
|
|
|
|
|
|
#include "RCInput.h"
|
2018-07-23 23:46:31 -03:00
|
|
|
#include <SITL/SITL.h>
|
2019-08-28 19:39:39 -03:00
|
|
|
#include <AP_RCProtocol/AP_RCProtocol.h>
|
2012-12-17 23:56:21 -04:00
|
|
|
|
2020-11-12 17:12:56 -04:00
|
|
|
#ifdef SFML_JOYSTICK
|
|
|
|
#ifdef HAVE_SFML_GRAPHICS_HPP
|
|
|
|
#include <SFML/Window/Joystick.hpp>
|
|
|
|
#elif HAVE_SFML_GRAPHIC_H
|
|
|
|
#include <SFML/Window/Joystick.h>
|
|
|
|
#endif
|
|
|
|
#endif // SFML_JOYSTICK
|
|
|
|
|
2015-05-04 03:15:12 -03:00
|
|
|
using namespace HALSITL;
|
2012-12-17 23:56:21 -04:00
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
void RCInput::init()
|
2012-12-17 23:56:21 -04:00
|
|
|
{
|
2019-08-28 19:39:39 -03:00
|
|
|
AP::RC().init();
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
bool RCInput::new_input()
|
2015-02-08 18:56:47 -04:00
|
|
|
{
|
2019-08-28 19:39:39 -03:00
|
|
|
if (!using_rc_protocol) {
|
|
|
|
if (AP::RC().new_input()) {
|
|
|
|
using_rc_protocol = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (using_rc_protocol) {
|
|
|
|
return AP::RC().new_input();
|
|
|
|
}
|
2015-02-08 18:56:47 -04:00
|
|
|
if (_sitlState->new_rc_input) {
|
|
|
|
_sitlState->new_rc_input = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
uint16_t RCInput::read(uint8_t ch)
|
2016-01-03 17:22:02 -04:00
|
|
|
{
|
2019-08-28 19:39:39 -03:00
|
|
|
if (using_rc_protocol) {
|
|
|
|
return AP::RC().read(ch);
|
|
|
|
}
|
2018-07-23 23:46:31 -03:00
|
|
|
if (ch >= num_channels()) {
|
2014-11-11 00:15:28 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2020-11-12 17:12:56 -04:00
|
|
|
#ifdef SFML_JOYSTICK
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *_sitl = AP::sitl();
|
2020-11-12 17:12:56 -04:00
|
|
|
if (_sitl) {
|
|
|
|
const sf::Joystick::Axis axis = sf::Joystick::Axis(_sitl->sfml_joystick_axis[ch].get());
|
|
|
|
const unsigned int stickID = _sitl->sfml_joystick_id;
|
|
|
|
if (sf::Joystick::hasAxis(stickID, axis)) {
|
|
|
|
return (constrain_float(sf::Joystick::getAxisPosition(stickID, axis) + 100, 0, 200) * 5) + 1000;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
2016-05-03 23:50:02 -03:00
|
|
|
return _sitlState->pwm_input[ch];
|
2020-11-12 17:12:56 -04:00
|
|
|
#endif
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2016-01-10 02:23:32 -04:00
|
|
|
uint8_t RCInput::read(uint16_t* periods, uint8_t len)
|
2016-01-03 17:22:02 -04:00
|
|
|
{
|
2018-07-23 23:46:31 -03:00
|
|
|
if (len > num_channels()) {
|
|
|
|
len = num_channels();
|
2016-01-03 17:22:02 -04:00
|
|
|
}
|
2017-01-09 08:28:35 -04:00
|
|
|
for (uint8_t i=0; i < len; i++) {
|
2016-05-03 23:50:02 -03:00
|
|
|
periods[i] = read(i);
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
2017-01-09 08:28:35 -04:00
|
|
|
return len;
|
2012-12-17 23:56:21 -04:00
|
|
|
}
|
|
|
|
|
2018-07-23 23:46:31 -03:00
|
|
|
uint8_t RCInput::num_channels()
|
|
|
|
{
|
2019-08-28 19:39:39 -03:00
|
|
|
if (using_rc_protocol) {
|
|
|
|
return AP::RC().num_channels();
|
|
|
|
}
|
2021-07-30 07:13:59 -03:00
|
|
|
SITL::SIM *_sitl = AP::sitl();
|
2018-07-23 23:46:31 -03:00
|
|
|
if (_sitl) {
|
2020-11-12 17:12:56 -04:00
|
|
|
#ifdef SFML_JOYSTICK
|
|
|
|
return (sf::Joystick::isConnected(_sitl->sfml_joystick_id.get())) ? ARRAY_SIZE(_sitl->sfml_joystick_axis) : 0;
|
|
|
|
#else
|
2018-07-23 23:46:31 -03:00
|
|
|
return MIN(_sitl->rc_chancount.get(), SITL_RC_INPUT_CHANNELS);
|
2020-11-12 17:12:56 -04:00
|
|
|
#endif // SFML_JOYSTICK
|
2018-07-23 23:46:31 -03:00
|
|
|
}
|
|
|
|
return SITL_RC_INPUT_CHANNELS;
|
|
|
|
}
|
|
|
|
|
2012-12-18 05:04:47 -04:00
|
|
|
#endif
|