-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AP_RCProtocol: move support for SFML joysticks down into AP_RCProtocol
- Loading branch information
1 parent
0ee21c1
commit 638778f
Showing
5 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "AP_RCProtocol_config.h" | ||
|
||
#if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED | ||
|
||
#include "AP_RCProtocol_Joystick_SFML.h" | ||
|
||
#include <SITL/SITL.h> | ||
|
||
#ifdef HAVE_SFML_GRAPHICS_HPP | ||
#include <SFML/Window/Joystick.hpp> | ||
#elif HAVE_SFML_GRAPHIC_H | ||
#include <SFML/Window/Joystick.h> | ||
#endif | ||
|
||
void AP_RCProtocol_Joystick_SFML::update() | ||
{ | ||
auto *_sitl = AP::sitl(); | ||
if (_sitl == nullptr) { | ||
return; | ||
} | ||
|
||
sf::Joystick::update(); | ||
|
||
const unsigned int stick_id = _sitl->sfml_joystick_id; | ||
if (!sf::Joystick::isConnected(stick_id)) { | ||
return; | ||
} | ||
|
||
uint16_t pwm_values[ARRAY_SIZE(_sitl->sfml_joystick_axis)]{}; | ||
for (uint8_t ch=0; ch<ARRAY_SIZE(_sitl->sfml_joystick_axis); ch++) { | ||
const sf::Joystick::Axis axis = sf::Joystick::Axis(_sitl->sfml_joystick_axis[ch].get()); | ||
if (!sf::Joystick::hasAxis(stick_id, axis)) { | ||
continue; | ||
} | ||
|
||
// pos is a value between -100 and 100: | ||
const auto pos = sf::Joystick::getAxisPosition(stick_id, axis); | ||
|
||
// convert to a "pwm" value between 1000 and 2000: | ||
const uint16_t pwm = (constrain_float(pos + 100, 0, 200) * 5) + 1000; | ||
pwm_values[ch] = pwm; | ||
} | ||
|
||
// never in failsafe: | ||
add_input(ARRAY_SIZE(pwm_values), pwm_values, false); | ||
} | ||
|
||
#endif // AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "AP_RCProtocol_config.h" | ||
|
||
#if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED | ||
|
||
#include "AP_RCProtocol_Backend.h" | ||
|
||
class AP_RCProtocol_Joystick_SFML : public AP_RCProtocol_Backend { | ||
public: | ||
|
||
AP_RCProtocol_Joystick_SFML(AP_RCProtocol &_frontend) : | ||
AP_RCProtocol_Backend(_frontend) { | ||
} | ||
|
||
void update() override; | ||
|
||
private: | ||
|
||
uint32_t last_receive_ms; | ||
}; | ||
|
||
|
||
#endif // AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters