[commands] Add functions to HID classes to allow use of axes as BooleanEvents/Triggers (#4762)

This commit is contained in:
Ryan Blue
2022-12-26 14:29:14 -05:00
committed by GitHub
parent 87a34af367
commit 176fddeb4c
12 changed files with 413 additions and 0 deletions

View File

@@ -297,6 +297,33 @@ public class GenericHID {
return pov(-1, loop);
}
/**
* Constructs an event instance that is true when the axis value is less than {@code threshold},
* attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param threshold The value below which this event should return true.
* @param loop the event loop instance to attach the event to.
* @return an event instance that is true when the axis value is less than the provided threshold.
*/
public BooleanEvent axisLessThan(int axis, double threshold, EventLoop loop) {
return new BooleanEvent(loop, () -> getRawAxis(axis) < threshold);
}
/**
* Constructs an event instance that is true when the axis value is greater than {@code
* threshold}, attached to the given loop.
*
* @param axis The axis to read, starting at 0
* @param threshold The value above which this event should return true.
* @param loop the event loop instance to attach the event to.
* @return an event instance that is true when the axis value is greater than the provided
* threshold.
*/
public BooleanEvent axisGreaterThan(int axis, double threshold, EventLoop loop) {
return new BooleanEvent(loop, () -> getRawAxis(axis) > threshold);
}
/**
* Get the number of axes for the HID.
*

View File

@@ -537,4 +537,56 @@ public class XboxController extends GenericHID {
public BooleanEvent start(EventLoop loop) {
return new BooleanEvent(loop, this::getStartButton);
}
/**
* Constructs an event instance around the axis value of the right trigger. 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 right trigger's axis exceeds the provided
* threshold, attached to the given event loop
*/
public BooleanEvent leftTrigger(double threshold, EventLoop loop) {
return new BooleanEvent(loop, () -> getLeftTriggerAxis() > threshold);
}
/**
* Constructs an event instance around the axis value of the right trigger. 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 right trigger's axis exceeds the provided
* threshold, attached to the given event loop
*/
public BooleanEvent leftTrigger(EventLoop loop) {
return leftTrigger(0.5, loop);
}
/**
* Constructs an event instance around the axis value of the right trigger. 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 right trigger's axis exceeds the provided
* threshold, attached to the given event loop
*/
public BooleanEvent rightTrigger(double threshold, EventLoop loop) {
return new BooleanEvent(loop, () -> getRightTriggerAxis() > threshold);
}
/**
* Constructs an event instance around the axis value of the right trigger. 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 right trigger's axis exceeds the provided
* threshold, attached to the given event loop
*/
public BooleanEvent rightTrigger(EventLoop loop) {
return rightTrigger(0.5, loop);
}
}