mirror of https://github.com/ArduPilot/ardupilot
Sub: provide script access to joystick buttons
This commit is contained in:
parent
924a391d55
commit
5e8fed9d43
|
@ -43,7 +43,13 @@ Sub::Sub()
|
|||
#if CONFIG_HAL_BOARD != HAL_BOARD_SITL
|
||||
failsafe.pilot_input = true;
|
||||
#endif
|
||||
if (_singleton != nullptr) {
|
||||
AP_HAL::panic("Can only be one Sub");
|
||||
}
|
||||
_singleton = this;
|
||||
}
|
||||
|
||||
Sub *Sub::_singleton = nullptr;
|
||||
|
||||
Sub sub;
|
||||
AP_Vehicle& vehicle = sub;
|
||||
|
|
|
@ -72,6 +72,7 @@
|
|||
#include "AP_Arming_Sub.h"
|
||||
#include "GCS_Sub.h"
|
||||
#include "mode.h"
|
||||
#include "script_button.h"
|
||||
|
||||
#include <AP_OpticalFlow/AP_OpticalFlow.h> // Optical Flow library
|
||||
|
||||
|
@ -580,8 +581,26 @@ private:
|
|||
AutoSubMode auto_mode; // controls which auto controller is run
|
||||
GuidedSubMode guided_mode;
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
ScriptButton script_buttons[4];
|
||||
#endif // AP_SCRIPTING_ENABLED
|
||||
|
||||
public:
|
||||
void mainloop_failsafe_check();
|
||||
|
||||
static Sub *_singleton;
|
||||
|
||||
static Sub *get_singleton() {
|
||||
return _singleton;
|
||||
}
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
// For Lua scripting, so index is 1..4, not 0..3
|
||||
bool is_button_pressed(uint8_t index);
|
||||
|
||||
// For Lua scripting, so index is 1..4, not 0..3
|
||||
uint8_t get_and_clear_button_count(uint8_t index);
|
||||
#endif // AP_SCRIPTING_ENABLED
|
||||
};
|
||||
|
||||
extern const AP_HAL::HAL& hal;
|
||||
|
|
|
@ -593,6 +593,21 @@ void Sub::handle_jsbutton_press(uint8_t _button, bool shift, bool held)
|
|||
case JSButton::button_function_t::k_custom_6:
|
||||
// Not implemented
|
||||
break;
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
case JSButton::button_function_t::k_script_1:
|
||||
sub.script_buttons[0].press();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_2:
|
||||
sub.script_buttons[1].press();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_3:
|
||||
sub.script_buttons[2].press();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_4:
|
||||
sub.script_buttons[3].press();
|
||||
break;
|
||||
#endif // AP_SCRIPTING_ENABLED
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -637,6 +652,21 @@ void Sub::handle_jsbutton_release(uint8_t _button, bool shift) {
|
|||
}
|
||||
break;
|
||||
#endif
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
case JSButton::button_function_t::k_script_1:
|
||||
sub.script_buttons[0].release();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_2:
|
||||
sub.script_buttons[1].release();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_3:
|
||||
sub.script_buttons[2].release();
|
||||
break;
|
||||
case JSButton::button_function_t::k_script_4:
|
||||
sub.script_buttons[3].release();
|
||||
break;
|
||||
#endif // AP_SCRIPTING_ENABLED
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -730,3 +760,15 @@ void Sub::clear_input_hold()
|
|||
zTrim = 0;
|
||||
input_hold_engaged = false;
|
||||
}
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
bool Sub::is_button_pressed(uint8_t index)
|
||||
{
|
||||
return script_buttons[index - 1].is_pressed();
|
||||
}
|
||||
|
||||
uint8_t Sub::get_and_clear_button_count(uint8_t index)
|
||||
{
|
||||
return script_buttons[index - 1].get_and_clear_count();
|
||||
}
|
||||
#endif // AP_SCRIPTING_ENABLED
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
#include "AP_Scripting/AP_Scripting_config.h"
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
|
||||
#include <limits>
|
||||
#include "script_button.h"
|
||||
|
||||
void ScriptButton::press()
|
||||
{
|
||||
if (!pressed) {
|
||||
pressed = true;
|
||||
|
||||
// The count will max out at 255, but it won't roll over to 0.
|
||||
if (count < std::numeric_limits<uint8_t>::max()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ScriptButton::release()
|
||||
{
|
||||
pressed = false;
|
||||
}
|
||||
|
||||
bool ScriptButton::is_pressed() const
|
||||
{
|
||||
return pressed;
|
||||
}
|
||||
|
||||
uint8_t ScriptButton::get_count() const
|
||||
{
|
||||
return count;
|
||||
}
|
||||
|
||||
void ScriptButton::clear_count()
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
|
||||
uint8_t ScriptButton::get_and_clear_count()
|
||||
{
|
||||
auto result = get_count();
|
||||
clear_count();
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif // AP_SCRIPTING_ENABLED
|
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#if AP_SCRIPTING_ENABLED
|
||||
|
||||
#include <AP_Common/AP_Common.h>
|
||||
|
||||
// Joystick button object for use in Lua scripts.
|
||||
//
|
||||
// Provide 2 ways to use a joystick button:
|
||||
// is_pressed() returns true if the button is currently (as of the most recent MANUAL_CONTROL msg) pressed
|
||||
// get_and_clear_count() returns the number of times the button was pressed since the last call
|
||||
//
|
||||
class ScriptButton {
|
||||
public:
|
||||
ScriptButton(): pressed(false), count(0) {}
|
||||
|
||||
void press();
|
||||
|
||||
void release();
|
||||
|
||||
bool is_pressed() const WARN_IF_UNUSED;
|
||||
|
||||
uint8_t get_count() const WARN_IF_UNUSED;
|
||||
|
||||
void clear_count();
|
||||
|
||||
uint8_t get_and_clear_count();
|
||||
|
||||
private:
|
||||
bool pressed;
|
||||
uint8_t count;
|
||||
};
|
||||
|
||||
#endif // AP_SCRIPTING_ENABLED
|
Loading…
Reference in New Issue