Class ControllerBindings

java.lang.Object
com.crossbotics.ftchelper.gamepad.ControllerBindings

public class ControllerBindings extends Object
Connects gamepad buttons to robot actions in one place.

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