[wpilib,commands] Use Jinja to generate HID classes (#6274)

This commit is contained in:
Gold856
2024-06-08 12:59:07 -04:00
committed by GitHub
parent a0efc9ca31
commit 65c6306047
75 changed files with 7622 additions and 4546 deletions

View File

@@ -0,0 +1,92 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
// THIS FILE WAS AUTO-GENERATED BY ./wpilibc/generate_hids.py. DO NOT MODIFY
{% macro capitalize_first(string) -%}
{{ string[0]|capitalize + string[1:] }}
{%- endmacro %}
#include "frc/{{ ConsoleName }}Controller.h"
#include <hal/FRCUsageReporting.h>
#include "frc/event/BooleanEvent.h"
using namespace frc;
{{ ConsoleName }}Controller::{{ ConsoleName }}Controller(int port) : GenericHID(port) {
{{ "// " if SkipReporting }}HAL_Report(HALUsageReporting::kResourceType_{{ ConsoleName }}Controller, port + 1);
}
{% for stick in sticks %}
double {{ ConsoleName }}Controller::Get{{ stick.NameParts|map("capitalize")|join }}() const {
return GetRawAxis(Axis::k{{ stick.NameParts|map("capitalize")|join }});
}
{% endfor -%}
{% for trigger in triggers %}
double {{ ConsoleName }}Controller::Get{{ capitalize_first(trigger.name) }}Axis() const {
return GetRawAxis(Axis::k{{ capitalize_first(trigger.name) }});
}
{% if trigger.UseThresholdMethods %}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(double threshold, EventLoop* loop) const {
return BooleanEvent(loop, [this, threshold] { return this->Get{{ capitalize_first(trigger.name) }}Axis() > threshold; });
}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(trigger.name) }}(EventLoop* loop) const {
return this->{{ capitalize_first(trigger.name) }}(0.5, loop);
}
{% endif -%}
{% endfor -%}
{% for button in buttons %}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}Button() const {
return GetRawButton(Button::k{{ capitalize_first(button.name) }});
}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}ButtonPressed() {
return GetRawButtonPressed(Button::k{{ capitalize_first(button.name) }});
}
bool {{ ConsoleName }}Controller::Get{{ capitalize_first(button.name) }}ButtonReleased() {
return GetRawButtonReleased(Button::k{{ capitalize_first(button.name) }});
}
BooleanEvent {{ ConsoleName }}Controller::{{ capitalize_first(button.name) }}(EventLoop* loop) const {
return BooleanEvent(loop, [this]() { return this->Get{{ capitalize_first(button.name) }}Button(); });
}
{% endfor -%}
{% if ConsoleName == "Xbox" or ConsoleName == "Stadia"%}
bool {{ ConsoleName }}Controller::GetLeftBumper() const {
return GetRawButton(Button::kLeftBumper);
}
bool {{ ConsoleName }}Controller::GetRightBumper() const {
return GetRawButton(Button::kRightBumper);
}
bool {{ ConsoleName }}Controller::GetLeftBumperPressed() {
return GetRawButtonPressed(Button::kLeftBumper);
}
bool {{ ConsoleName }}Controller::GetRightBumperPressed() {
return GetRawButtonPressed(Button::kRightBumper);
}
bool {{ ConsoleName }}Controller::GetLeftBumperReleased() {
return GetRawButtonReleased(Button::kLeftBumper);
}
bool {{ ConsoleName }}Controller::GetRightBumperReleased() {
return GetRawButtonReleased(Button::kRightBumper);
}
{% elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
bool {{ ConsoleName }}Controller::GetTouchpad() const {
return GetRawButton(Button::kTouchpad);
}
bool {{ ConsoleName }}Controller::GetTouchpadPressed() {
return GetRawButtonPressed(Button::kTouchpad);
}
bool {{ ConsoleName }}Controller::GetTouchpadReleased() {
return GetRawButtonReleased(Button::kTouchpad);
}
{% endif %}

View File

@@ -0,0 +1,42 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
// THIS FILE WAS AUTO-GENERATED BY ./wpilibc/generate_hids.py. DO NOT MODIFY
{% macro capitalize_first(string) -%}
{{ string[0]|capitalize + string[1:] }}
{%- endmacro %}
#include "frc/simulation/{{ ConsoleName }}ControllerSim.h"
#include "frc/{{ ConsoleName }}Controller.h"
using namespace frc;
using namespace frc::sim;
{{ ConsoleName }}ControllerSim::{{ ConsoleName }}ControllerSim(const {{ ConsoleName }}Controller& joystick)
: GenericHIDSim{joystick} {
SetAxisCount({{ sticks|length + triggers|length }});
SetButtonCount({{ buttons|length }});
SetPOVCount(1);
}
{{ ConsoleName }}ControllerSim::{{ ConsoleName }}ControllerSim(int port) : GenericHIDSim{port} {
SetAxisCount({{ sticks|length + triggers|length }});
SetButtonCount({{ buttons|length }});
SetPOVCount(1);
}
{% for stick in sticks %}
void {{ ConsoleName }}ControllerSim::Set{{ stick.NameParts|map("capitalize")|join }}(double value) {
SetRawAxis({{ ConsoleName }}Controller::Axis::k{{ stick.NameParts|map("capitalize")|join }}, value);
}
{% endfor -%}
{% for trigger in triggers %}
void {{ ConsoleName }}ControllerSim::Set{{ capitalize_first(trigger.name) }}Axis(double value) {
SetRawAxis({{ ConsoleName }}Controller::Axis::k{{ capitalize_first(trigger.name) }}, value);
}
{% endfor -%}
{% for button in buttons %}
void {{ ConsoleName }}ControllerSim::Set{{ capitalize_first(button.name) }}Button(bool value) {
SetRawButton({{ ConsoleName }}Controller::Button::k{{ capitalize_first(button.name) }}, value);
}
{% endfor %}

View File

@@ -0,0 +1,208 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
// THIS FILE WAS AUTO-GENERATED BY ./wpilibc/generate_hids.py. DO NOT MODIFY
{% macro capitalize_first(string) -%}
{{ string[0]|capitalize + string[1:] }}
{%- endmacro %}
#pragma once
#include "frc/GenericHID.h"
namespace frc {
/**
* Handle input from {{ ConsoleName }} controllers connected to the Driver Station.
*
* This class handles {{ ConsoleName }} input that comes from the Driver Station. Each
* time a value is requested the most recent value is returned. There is a
* single class instance for each controller and the mapping of ports to
* hardware buttons depends on the code in the Driver Station.
*
* Only first party controllers from {{ Manufacturer }} are guaranteed to have the
* correct mapping, and only through the official NI DS. Sim is not guaranteed
* to have the same mapping, as well as any 3rd party controllers.
*/
class {{ ConsoleName }}Controller : public GenericHID {
public:
/**
* Construct an instance of a controller.
*
* The controller index is the USB port on the Driver Station.
*
* @param port The port on the Driver Station that the controller is plugged
* into (0-5).
*/
explicit {{ ConsoleName }}Controller(int port);
~{{ ConsoleName }}Controller() override = default;
{{ ConsoleName }}Controller({{ ConsoleName }}Controller&&) = default;
{{ ConsoleName }}Controller& operator=({{ ConsoleName }}Controller&&) = default;
{% for stick in sticks %}
/**
* Get the {{ stick.NameParts[1] }} axis value of {{ stick.NameParts[0] }} side of the controller.
*
* @return the axis value.
*/
double Get{{ stick.NameParts|map("capitalize")|join }}() const;
{% endfor -%}
{% for trigger in triggers %}
/**
* Get the {{ trigger.DocName }} axis value of the controller. Note that this axis
* is bound to the range of [0, 1] as opposed to the usual [-1, 1].
*
* @return the axis value.
*/
double Get{{ capitalize_first(trigger.name) }}Axis() const;
{% if trigger.UseThresholdMethods %}
/**
* Constructs an event instance around the axis value of the {{ trigger.DocName }}.
* The returned trigger will be true when the axis value is greater than
* {@code threshold}.
* @param threshold the minimum axis value for the returned event to be true.
* This value should be in the range [0, 1] where 0 is the unpressed state of
* the axis.
* @param loop the event loop instance to attach the event to.
* @return an event instance that is true when the {{ trigger.DocName }}'s axis
* exceeds the provided threshold, attached to the given event loop
*/
BooleanEvent {{ capitalize_first(trigger.name) }}(double threshold, EventLoop* loop) const;
/**
* Constructs an event instance around the axis value of the {{ trigger.DocName }}.
* The returned trigger will be true when the axis value is greater than 0.5.
* @param loop the event loop instance to attach the event to.
* @return an event instance that is true when the {{ trigger.DocName }}'s axis
* exceeds 0.5, attached to the given event loop
*/
BooleanEvent {{ capitalize_first(trigger.name) }}(EventLoop* loop) const;
{% endif -%}
{% endfor -%}
{% for button in buttons %}
/**
* Read the value of the {{ button.DocName|default(button.name) }} button on the controller.
*
* @return The state of the button.
*/
bool Get{{ capitalize_first(button.name) }}Button() const;
/**
* Whether the {{ button.DocName|default(button.name) }} button was pressed since the last check.
*
* @return Whether the button was pressed since the last check.
*/
bool Get{{ capitalize_first(button.name) }}ButtonPressed();
/**
* Whether the {{ button.DocName|default(button.name) }} button was released since the last check.
*
* @return Whether the button was released since the last check.
*/
bool Get{{ capitalize_first(button.name) }}ButtonReleased();
/**
* Constructs an event instance around the {{ button.DocName|default(button.name) }} button's
* digital signal.
*
* @param loop the event loop instance to attach the event to.
* @return an event instance representing the {{ button.DocName|default(button.name) }} button's
* digital signal attached to the given loop.
*/
BooleanEvent {{ capitalize_first(button.name) }}(EventLoop* loop) const;
{% endfor -%}
{% if ConsoleName == "Xbox" or ConsoleName == "Stadia" %}
/**
* Read the value of the left bumper (LB) button on the controller.
*
* @return the state of the button
*/
[[deprecated("Use GetLeftBumperButton instead")]]
bool GetLeftBumper() const;
/**
* Read the value of the right bumper (RB) button on the controller.
*
* @return the state of the button
*/
[[deprecated("Use GetRightBumperButton instead")]]
bool GetRightBumper() const;
/**
* Whether the left bumper (LB) was pressed since the last check.
*
* @return Whether the button was pressed since the last check
*/
[[deprecated("Use GetLeftBumperButtonPressed instead")]]
bool GetLeftBumperPressed();
/**
* Whether the right bumper (RB) was pressed since the last check.
*
* @return Whether the button was pressed since the last check
*/
[[deprecated("Use GetRightBumperButtonPressed instead")]]
bool GetRightBumperPressed();
/**
* Whether the left bumper (LB) was released since the last check.
*
* @return Whether the button was released since the last check.
*/
[[deprecated("Use GetLeftBumperButtonReleased instead")]]
bool GetLeftBumperReleased();
/**
* Whether the right bumper (RB) was released since the last check.
*
* @return Whether the button was released since the last check.
*/
[[deprecated("Use GetRightBumperButtonReleased instead")]]
bool GetRightBumperReleased();
{% elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
/**
* Read the value of the touchpad button on the controller.
*
* @return The state of the button.
*/
[[deprecated("Use GetTouchpadButton instead")]]
bool GetTouchpad() const;
/**
* Whether the touchpad was pressed since the last check.
*
* @return Whether the touchpad was pressed since the last check.
*/
[[deprecated("Use GetTouchpadButtonPressed instead")]]
bool GetTouchpadPressed();
/**
* Whether the touchpad was released since the last check.
*
* @return Whether the touchpad was released since the last check.
*/
[[deprecated("Use GetTouchpadButtonReleased instead")]]
bool GetTouchpadReleased();
{% endif %}
/** Represents a digital button on an {{ ConsoleName }}Controller. */
struct Button {
{%- for button in buttons %}
/// {{ capitalize_first(button.DocName|default(button.name)) }} button.
static constexpr int k{{ capitalize_first(button.name) }} = {{ button.value }};
{%- endfor %}
};
/** Represents an axis on an {{ ConsoleName }}Controller. */
struct Axis {
{%- for stick in sticks %}
/// {{ stick.NameParts|map("capitalize")|join(" ") }} axis.
static constexpr int k{{ stick.NameParts|map("capitalize")|join }} = {{ stick.value }};
{%- endfor %}
{%- for trigger in triggers %}
/// {{ trigger.DocName|capitalize }}.
static constexpr int k{{ capitalize_first(trigger.name) }} = {{ trigger.value }};
{%- endfor %}
};
};
} // namespace frc

View File

@@ -0,0 +1,89 @@
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
// THIS FILE WAS AUTO-GENERATED BY ./wpilibc/generate_hids.py. DO NOT MODIFY
{% macro capitalize_first(string) -%}
{{ string[0]|capitalize + string[1:] }}
{%- endmacro %}
#pragma once
#include "frc/simulation/GenericHIDSim.h"
namespace frc {
class {{ ConsoleName }}Controller;
namespace sim {
/**
* Class to control a simulated {{ ConsoleName }} controller.
*/
class {{ ConsoleName }}ControllerSim : public GenericHIDSim {
public:
/**
* Constructs from a {{ ConsoleName }}Controller object.
*
* @param joystick controller to simulate
*/
explicit {{ ConsoleName }}ControllerSim(const {{ ConsoleName }}Controller& joystick);
/**
* Constructs from a joystick port number.
*
* @param port port number
*/
explicit {{ ConsoleName }}ControllerSim(int port);
{% for stick in sticks %}
/**
* Change the {{ stick.NameParts|join(" ") }} value of the controller's joystick.
*
* @param value the new value
*/
void Set{{ stick.NameParts|map("capitalize")|join }}(double value);
{% endfor -%}
{% for trigger in triggers %}
/**
* Change the value of the {{ trigger.DocName }} axis on the controller.
*
* @param value the new value
*/
void Set{{ capitalize_first(trigger.name) }}Axis(double value);
{% endfor -%}
{% for button in buttons %}
/**
* Change the value of the {{ button.DocName|default(button.name) }} button on the controller.
*
* @param value the new value
*/
void Set{{ capitalize_first(button.name) }}Button(bool value);
{% endfor -%}
{% if ConsoleName == "Xbox" %}
/**
* Change the left bumper value of the joystick.
*
* @param value the new value
*/
[[deprecated("Use SetLeftBumperButton instead")]]
void SetLeftBumper(bool value);
/**
* Change the right bumper value of the joystick.
*
* @param value the new value
*/
[[deprecated("Use SetRightBumperButton instead")]]
void SetRightBumper(bool value);
{% elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
/**
* Change the value of the touchpad button on the controller.
*
* @param value the new value
*/
[[deprecated("Use SetTouchpadButton instead")]]
void SetTouchpad(bool value);
{% endif %}
};
} // namespace sim
} // namespace frc