mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-20 00:51:42 +00:00
Move generated files to more consistent locations (#6906)
Java templates always go under src/generate/main/java. JSON files go to src/generate.
This commit is contained in:
306
wpilibj/src/generate/main/java/hid.java.jinja
Normal file
306
wpilibj/src/generate/main/java/hid.java.jinja
Normal file
@@ -0,0 +1,306 @@
|
||||
// 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 ./wpilibj/generate_hids.py. DO NOT MODIFY
|
||||
{% macro capitalize_first(string) -%}
|
||||
{{ string[0]|capitalize + string[1:] }}
|
||||
{%- endmacro %}
|
||||
package edu.wpi.first.wpilibj;
|
||||
|
||||
{{ "// " if SkipReporting }}import edu.wpi.first.hal.FRCNetComm.tResourceType;
|
||||
{{ "// " if SkipReporting }}import edu.wpi.first.hal.HAL;
|
||||
import edu.wpi.first.util.sendable.Sendable;
|
||||
import edu.wpi.first.util.sendable.SendableBuilder;
|
||||
import edu.wpi.first.wpilibj.event.BooleanEvent;
|
||||
import edu.wpi.first.wpilibj.event.EventLoop;
|
||||
|
||||
/**
|
||||
* Handle input from {{ ConsoleName }} controllers connected to the Driver Station.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*/
|
||||
public class {{ ConsoleName }}Controller extends GenericHID implements Sendable {
|
||||
/** Represents a digital button on a {{ ConsoleName }}Controller. */
|
||||
public enum Button {
|
||||
{%- for button in buttons %}
|
||||
/** {{ capitalize_first(button.DocName|default(button.name)) }} button. */
|
||||
k{{ capitalize_first(button.name) }}({{ button.value }}){{ ";" if loop.last else ","}}
|
||||
{%- endfor %}
|
||||
|
||||
/** Button value. */
|
||||
public final int value;
|
||||
|
||||
Button(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the human-friendly name of the button, matching the relevant methods. This is done by
|
||||
* stripping the leading `k`, and appending `Button`.
|
||||
*
|
||||
* <p>Primarily used for automated unit tests.
|
||||
*
|
||||
* @return the human-friendly name of the button.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
// Remove leading `k`
|
||||
return this.name().substring(1) + "Button";
|
||||
}
|
||||
}
|
||||
|
||||
/** Represents an axis on an {{ ConsoleName }}Controller. */
|
||||
public enum Axis {
|
||||
{%- for stick in sticks %}
|
||||
/** {{ stick.NameParts|map("capitalize")|join(" ") }} axis. */
|
||||
k{{ stick.NameParts|map("capitalize")|join }}({{ stick.value }}){{ "," if triggers|length > 0 or not loop.last else ";"}}
|
||||
{%- endfor %}
|
||||
{%- for trigger in triggers %}
|
||||
/** {{ trigger.DocName|capitalize }}. */
|
||||
k{{ capitalize_first(trigger.name) }}({{ trigger.value }}){{ ";" if loop.last else ","}}
|
||||
{%- endfor %}
|
||||
|
||||
/** Axis value. */
|
||||
public final int value;
|
||||
|
||||
Axis(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the human-friendly name of the axis, matching the relevant methods. This is done by
|
||||
* stripping the leading `k`, and appending `Axis` if the name ends with `{{ AxisNameSuffix }}`.
|
||||
*
|
||||
* <p>Primarily used for automated unit tests.
|
||||
*
|
||||
* @return the human-friendly name of the axis.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
var name = this.name().substring(1); // Remove leading `k`
|
||||
if (name.endsWith("{{ AxisNameSuffix }}")) {
|
||||
return name + "Axis";
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct an instance of a controller.
|
||||
*
|
||||
* @param port The port index on the Driver Station that the controller is plugged into (0-5).
|
||||
*/
|
||||
public {{ ConsoleName }}Controller(final int port) {
|
||||
super(port);
|
||||
{{ "// " if SkipReporting }}HAL.report(tResourceType.kResourceType_{{ ConsoleName }}Controller, port + 1);
|
||||
}
|
||||
{% for stick in sticks %}
|
||||
/**
|
||||
* Get the {{ stick.NameParts[1] }} axis value of {{ stick.NameParts[0] }} side of the controller.
|
||||
*
|
||||
* @return The axis value.
|
||||
*/
|
||||
public double get{{ stick.NameParts|map("capitalize")|join }}() {
|
||||
return getRawAxis(Axis.k{{ stick.NameParts|map("capitalize")|join }}.value);
|
||||
}
|
||||
{% 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.
|
||||
*/
|
||||
public double get{{ capitalize_first(trigger.name) }}Axis() {
|
||||
return getRawAxis(Axis.k{{ capitalize_first(trigger.name) }}.value);
|
||||
}
|
||||
{% 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 {@link BooleanEvent} 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
|
||||
*/
|
||||
public BooleanEvent {{ trigger.name }}(double threshold, EventLoop loop) {
|
||||
return axisGreaterThan(Axis.k{{ capitalize_first(trigger.name) }}.value, threshold, loop);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the provided
|
||||
* threshold, attached to the given event loop
|
||||
*/
|
||||
public BooleanEvent {{ trigger.name }}(EventLoop loop) {
|
||||
return {{ trigger.name }}(0.5, loop);
|
||||
}
|
||||
{% 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.
|
||||
*/
|
||||
public boolean get{{ capitalize_first(button.name) }}Button() {
|
||||
return getRawButton(Button.k{{ capitalize_first(button.name) }}.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the {{ button.DocName|default(button.name) }} button was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
*/
|
||||
public boolean get{{ capitalize_first(button.name) }}ButtonPressed() {
|
||||
return getRawButtonPressed(Button.k{{ capitalize_first(button.name) }}.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the {{ button.DocName|default(button.name) }} button was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
*/
|
||||
public boolean get{{ capitalize_first(button.name) }}ButtonReleased() {
|
||||
return getRawButtonReleased(Button.k{{ capitalize_first(button.name) }}.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
public BooleanEvent {{ button.name }}(EventLoop loop) {
|
||||
return button(Button.k{{ capitalize_first(button.name) }}.value, loop);
|
||||
}
|
||||
{% 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 {@link getLeftBumperButton} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getLeftBumper() {
|
||||
return getRawButton(Button.kLeftBumper.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the value of the right bumper (RB) button on the controller.
|
||||
*
|
||||
* @return The state of the button.
|
||||
* @deprecated Use {@link getRightBumperButton} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getRightBumper() {
|
||||
return getRawButton(Button.kRightBumper.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the left bumper (LB) was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
* @deprecated Use {@link getLeftBumperButtonPressed} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getLeftBumperPressed() {
|
||||
return getRawButtonPressed(Button.kLeftBumper.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the right bumper (RB) was pressed since the last check.
|
||||
*
|
||||
* @return Whether the button was pressed since the last check.
|
||||
* @deprecated Use {@link getRightBumperButtonPressed} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getRightBumperPressed() {
|
||||
return getRawButtonPressed(Button.kRightBumper.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the left bumper (LB) was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
* @deprecated Use {@link getLeftBumperButtonReleased} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getLeftBumperReleased() {
|
||||
return getRawButtonReleased(Button.kLeftBumper.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the right bumper (RB) was released since the last check.
|
||||
*
|
||||
* @return Whether the button was released since the last check.
|
||||
* @deprecated Use {@link getRightBumperButtonReleased} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getRightBumperReleased() {
|
||||
return getRawButtonReleased(Button.kRightBumper.value);
|
||||
}
|
||||
{%- elif ConsoleName == "PS4" or ConsoleName == "PS5" %}
|
||||
/**
|
||||
* Read the value of the touchpad on the controller.
|
||||
*
|
||||
* @return The state of the touchpad.
|
||||
* @deprecated Use {@link getTouchpadButton} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getTouchpad() {
|
||||
return getRawButton(Button.kTouchpad.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the touchpad was pressed since the last check.
|
||||
*
|
||||
* @return Whether the touchpad was pressed since the last check.
|
||||
* @deprecated Use {@link getTouchpadButtonPressed} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getTouchpadPressed() {
|
||||
return getRawButtonPressed(Button.kTouchpad.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the touchpad was released since the last check.
|
||||
*
|
||||
* @return Whether the touchpad was released since the last check.
|
||||
* @deprecated Use {@link getTouchpadButtonReleased} instead
|
||||
*/
|
||||
@Deprecated(since = "2025", forRemoval = true)
|
||||
public boolean getTouchpadReleased() {
|
||||
return getRawButtonReleased(Button.kTouchpad.value);
|
||||
}
|
||||
{%- endif %}
|
||||
|
||||
@Override
|
||||
public void initSendable(SendableBuilder builder) {
|
||||
builder.setSmartDashboardType("HID");
|
||||
builder.publishConstString("ControllerType", "{{ ConsoleName }}");
|
||||
{%- for trigger in triggers %}
|
||||
builder.addDoubleProperty("{{ capitalize_first(trigger.name) }}", this::get{{ capitalize_first(trigger.name) }}Axis, null);
|
||||
{%- endfor -%}
|
||||
{% for stick in sticks %}
|
||||
builder.addDoubleProperty("{{ stick.NameParts|map("capitalize")|join }}", this::get{{ stick.NameParts|map("capitalize")|join }}, null);
|
||||
{%- endfor -%}
|
||||
{% for button in buttons %}
|
||||
builder.addBooleanProperty("{{ capitalize_first(button.name) }}", this::get{{ capitalize_first(button.name) }}Button, null);
|
||||
{%- endfor %}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user