Class ControllerBindings
You say what each button does once, at the start. Then you call update(com.qualcomm.robotcore.hardware.Gamepad, com.qualcomm.robotcore.hardware.Gamepad) every loop.
The class does the "was it just pressed?" work for you.
Why "was it just pressed?" matters: the loop runs 50-200 times a second, and a quick tap keeps a button down for several loops. If we ran the action every loop the button is down, one tap would open the claw ten times. So for a "press" we only act on the loop where the button changes from up to down. That change is called an "edge" (like stepping off a curb).
ControllerBindings controls = new ControllerBindings()
.bindPress(Pad.G2, Btn.A, () -> claw.open()) // once per press
.bindHold(Pad.G2, Btn.DPAD_UP, () -> lift.up()) // every loop while held
.bindToggle(Pad.G1, Btn.Y, () -> slow = true, () -> slow = false)
.bindTriggerPress(Pad.G2, Trigger.RT, () -> shooter.fire());
waitForStart();
controls.ignoreHeldButtons(gamepad1, gamepad2); // a button held through START does not fire
while (opModeIsActive()) {
controls.update(gamepad1, gamepad2);
...
}
() -> claw.open() is a "lambda": a small piece of code you hand over now, to be run
later (here, when the button is pressed). Runnable is Java's name for such a piece of
code that takes nothing and gives nothing back.
Many actions can share one button. They run in the order you added them. Actions must be quick: never sleep or wait inside one. See docs/guides/non-blocking.md, and Tutorial 03 (docs/tutorials/03-buttons.md) for a walk-through. Docs: docs/modules/gamepad.md
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumGamepad buttons.static enumWhich gamepad: G1 = gamepad1 (usually the driver), G2 = gamepad2 (usually the operator).static enumThe two analog triggers. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doubleAn action slower than this (milliseconds) is reported bygetSlowActionWarning().static final doubleA slow-action note stays on the screen this many seconds after the last slow action. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbindHold(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable whileHeld) RunswhileHeldevery loop while the button is down.bindPress(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onPress) RunsonPressonce each time the button goes down.bindRelease(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onRelease) RunsonReleaseonce each time the button comes back up.bindToggle(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onEnable, Runnable onDisable) Each press flips between ON and OFF.bindTriggerAxis(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, DoubleConsumer use) Gives the trigger's value (0 to 1) touseevery loop.bindTriggerHold(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable whileHeld) RunswhileHeldevery loop while the trigger is past the threshold.bindTriggerPress(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onPress) RunsonPressonce each time the trigger goes past the threshold (default 0.5).bindTriggerRelease(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onRelease) RunsonReleaseonce each time the trigger comes back under the threshold.bindTriggerToggle(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onEnable, Runnable onDisable) Each pull flips between ON and OFF, likebindToggle(com.crossbotics.ftchelper.gamepad.ControllerBindings.Pad, com.crossbotics.ftchelper.gamepad.ControllerBindings.Btn, java.lang.Runnable, java.lang.Runnable)for a button.voidclear()Removes every binding."" or a note about a button action that took too long (more thanSLOW_ACTION_MS), e.g.voidignoreHeldButtons(com.qualcomm.robotcore.hardware.Gamepad gamepad1, com.qualcomm.robotcore.hardware.Gamepad gamepad2) Call once right after START.booleanTrue if the button is down right now (as of the last update).registerExtraButton(ControllerBindings.Pad pad, ControllerBindings.Btn btn, BooleanSupplier isDown) Lets you bind extra paddles (M1, M2) on controllers that have them.voidActs as if every button were let go now: "release" actions run for buttons that are down.setTriggerThreshold(double threshold) How far a trigger must be pulled to count as "pressed".voidupdate(com.qualcomm.robotcore.hardware.Gamepad gamepad1, com.qualcomm.robotcore.hardware.Gamepad gamepad2) Reads both gamepads and runs the actions.
-
Field Details
-
SLOW_ACTION_MS
public static final double SLOW_ACTION_MSAn action slower than this (milliseconds) is reported bygetSlowActionWarning().- See Also:
-
WARNING_SHOW_SEC
public static final double WARNING_SHOW_SECA slow-action note stays on the screen this many seconds after the last slow action.- See Also:
-
-
Constructor Details
-
ControllerBindings
public ControllerBindings()
-
-
Method Details
-
bindPress
public ControllerBindings bindPress(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onPress) RunsonPressonce each time the button goes down. Holding does not repeat it.- Parameters:
pad- which gamepadbtn- which buttononPress- the code to run, for example() -> claw.open()- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindRelease
public ControllerBindings bindRelease(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onRelease) RunsonReleaseonce each time the button comes back up.- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindHold
public ControllerBindings bindHold(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable whileHeld) RunswhileHeldevery loop while the button is down. Pair it with abindRelease(com.crossbotics.ftchelper.gamepad.ControllerBindings.Pad, com.crossbotics.ftchelper.gamepad.ControllerBindings.Btn, java.lang.Runnable)that stops the mechanism when the button is let go.- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindToggle
public ControllerBindings bindToggle(ControllerBindings.Pad pad, ControllerBindings.Btn btn, Runnable onEnable, Runnable onDisable) Each press flips between ON and OFF. Starts OFF.- Parameters:
onEnable- runs when it turns ON (may be null)onDisable- runs when it turns OFF (may be null)- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindTriggerPress
public ControllerBindings bindTriggerPress(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onPress) RunsonPressonce each time the trigger goes past the threshold (default 0.5).- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindTriggerHold
public ControllerBindings bindTriggerHold(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable whileHeld) RunswhileHeldevery loop while the trigger is past the threshold.- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindTriggerRelease
public ControllerBindings bindTriggerRelease(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onRelease) RunsonReleaseonce each time the trigger comes back under the threshold. Pair it withbindTriggerHold(com.crossbotics.ftchelper.gamepad.ControllerBindings.Pad, com.crossbotics.ftchelper.gamepad.ControllerBindings.Trigger, java.lang.Runnable)to stop a mechanism when the trigger is let go.- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindTriggerToggle
public ControllerBindings bindTriggerToggle(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, Runnable onEnable, Runnable onDisable) Each pull flips between ON and OFF, likebindToggle(com.crossbotics.ftchelper.gamepad.ControllerBindings.Pad, com.crossbotics.ftchelper.gamepad.ControllerBindings.Btn, java.lang.Runnable, java.lang.Runnable)for a button. Starts OFF.- Parameters:
onEnable- runs when it turns ON (may be null)onDisable- runs when it turns OFF (may be null)- Returns:
- this ControllerBindings, so you can chain the next binding
-
bindTriggerAxis
public ControllerBindings bindTriggerAxis(ControllerBindings.Pad pad, ControllerBindings.Trigger trigger, DoubleConsumer use) Gives the trigger's value (0 to 1) touseevery loop. Example:bindTriggerAxis(Pad.G2, Trigger.RT, v -> intake.setPower(v)).- Parameters:
use- code that takes the trigger value;v -> ...is a lambda that gets the value as v- Returns:
- this ControllerBindings, so you can chain the next binding
-
registerExtraButton
public ControllerBindings registerExtraButton(ControllerBindings.Pad pad, ControllerBindings.Btn btn, BooleanSupplier isDown) Lets you bind extra paddles (M1, M2) on controllers that have them. You tell the class how to read the paddle.- Parameters:
isDown- code that answers true while the paddle is pressed- Returns:
- this ControllerBindings, so you can chain the next binding
- Throws:
IllegalArgumentException- ifbtnis not M1 or M2
-
setTriggerThreshold
How far a trigger must be pulled to count as "pressed". Default 0.5.- Parameters:
threshold- 0 to 1- Returns:
- this ControllerBindings, so you can chain the next binding
-
update
public void update(com.qualcomm.robotcore.hardware.Gamepad gamepad1, com.qualcomm.robotcore.hardware.Gamepad gamepad2) Reads both gamepads and runs the actions. Call once per loop.- Parameters:
gamepad1- the OpMode'sgamepad1gamepad2- the OpMode'sgamepad2
-
getSlowActionWarning
"" or a note about a button action that took too long (more thanSLOW_ACTION_MS), e.g. "G2:A action took 250 ms: actions must not wait or sleep". Show it witht.warn(...). A slow action freezes the WHOLE robot for that time.The note stays
WARNING_SHOW_SECseconds after the last slow action, so the drivers have time to read it, then goes away. -
ignoreHeldButtons
public void ignoreHeldButtons(com.qualcomm.robotcore.hardware.Gamepad gamepad1, com.qualcomm.robotcore.hardware.Gamepad gamepad2) Call once right after START. For any button or trigger already held, NOTHING bound to it runs (press, release, hold or toggle) until it is let go once. This stops surprise actions when a driver holds a button through START. Trigger axis bindings are not affected.- Parameters:
gamepad1- the OpMode'sgamepad1gamepad2- the OpMode'sgamepad2
-
releaseAll
public void releaseAll()Acts as if every button were let go now: "release" actions run for buttons that are down. Call it before you stop callingupdate(com.qualcomm.robotcore.hardware.Gamepad, com.qualcomm.robotcore.hardware.Gamepad)for a while (for example while a menu uses the gamepad), so a "start on press, stop on release" action can never be left running. Afterwards, callignoreHeldButtons(com.qualcomm.robotcore.hardware.Gamepad, com.qualcomm.robotcore.hardware.Gamepad)when you start calling update() again. -
clear
public void clear()Removes every binding. -
isDown
True if the button is down right now (as of the last update).- Parameters:
pad- which gamepadbtn- which button
-