|
| 1 | +--- |
| 2 | +title: Buttons |
| 3 | +description: Reference documentation for CoreControl Buttons. |
| 4 | +--- |
| 5 | + |
| 6 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 7 | + |
| 8 | +CoreControl provides a powerful button system that wraps standard gamepad inputs to provide advanced features like toggle states, double-click detection, and hold duration tracking. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +The `Button` and `AnalogButton` classes allow you to treat gamepad inputs as objects with state. Instead of manually checking `if (gamepad1.a && !previousA)`, you can simply check `buttonA.pressed`. |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +Buttons should be instantiated inside your OpMode. They automatically register themselves to be updated every loop. |
| 17 | + |
| 18 | +<Tabs> |
| 19 | + <TabItem label="Kotlin"> |
| 20 | + ```kotlin |
| 21 | + class MyOpMode : CoreTeleOp() { |
| 22 | + // Define buttons |
| 23 | + val grabButton = Button { gamepad1.a } |
| 24 | + val shootButton = Button { gamepad1.b } |
| 25 | + val triggerButton = AnalogButton { gamepad1.right_trigger } |
| 26 | + |
| 27 | + override fun onMainLoop() { |
| 28 | + // Use button states |
| 29 | + if (grabButton.pressed) { |
| 30 | + claw.toggle() |
| 31 | + } |
| 32 | + |
| 33 | + if (shootButton.held) { |
| 34 | + shooter.spin(1.0) |
| 35 | + } else { |
| 36 | + shooter.spin(0.0) |
| 37 | + } |
| 38 | + |
| 39 | + if (triggerButton.pressed) { |
| 40 | + // Trigger crossed the threshold |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + ``` |
| 45 | + </TabItem> |
| 46 | + <TabItem label="Java"> |
| 47 | + ```java |
| 48 | + public class MyOpMode extends CoreTeleOp { |
| 49 | + // Define buttons |
| 50 | + Button grabButton = new Button(() -> gamepad1.a); |
| 51 | + Button shootButton = new Button(() -> gamepad1.b); |
| 52 | + AnalogButton triggerButton = new AnalogButton(() -> gamepad1.right_trigger); |
| 53 | + |
| 54 | + @Override |
| 55 | + public void onMainLoop() { |
| 56 | + // Use button states |
| 57 | + if (grabButton.getPressed()) { |
| 58 | + claw.toggle(); |
| 59 | + } |
| 60 | + |
| 61 | + if (shootButton.getHeld()) { |
| 62 | + shooter.spin(1.0); |
| 63 | + } else { |
| 64 | + shooter.spin(0.0); |
| 65 | + } |
| 66 | + |
| 67 | + if (triggerButton.getPressed()) { |
| 68 | + // Trigger crossed the threshold |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + ``` |
| 73 | + </TabItem> |
| 74 | +</Tabs> |
| 75 | + |
| 76 | +## API Reference |
| 77 | + |
| 78 | +### Button |
| 79 | + |
| 80 | +Wraps a digital input (boolean). |
| 81 | + |
| 82 | +**Properties:** |
| 83 | +* `pressed`: True only on the frame the button was pressed. |
| 84 | +* `released`: True only on the frame the button was released. |
| 85 | +* `held`: True as long as the button is held down. |
| 86 | +* `toggled`: Flips state (true/false) each time the button is pressed. |
| 87 | +* `doublePressed`: True if the button is pressed twice within the double-click interval (default 300ms). |
| 88 | + |
| 89 | +<Tabs> |
| 90 | + <TabItem label="Kotlin"> |
| 91 | + ```kotlin |
| 92 | + val myButton = Button { gamepad1.x } |
| 93 | + if (myButton.doublePressed) { |
| 94 | + // Handle double click |
| 95 | + } |
| 96 | + ``` |
| 97 | + </TabItem> |
| 98 | + <TabItem label="Java"> |
| 99 | + ```java |
| 100 | + Button myButton = new Button(() -> gamepad1.x); |
| 101 | + if (myButton.getDoublePressed()) { |
| 102 | + // Handle double click |
| 103 | + } |
| 104 | + ``` |
| 105 | + </TabItem> |
| 106 | +</Tabs> |
| 107 | + |
| 108 | +### AnalogButton |
| 109 | + |
| 110 | +Wraps an analog input (float) and treats it as a button based on a threshold. |
| 111 | + |
| 112 | +**Properties:** |
| 113 | +* Inherits all properties from `Button` (`pressed`, `held`, etc.). |
| 114 | +* `value`: The raw float value of the input (0.0 to 1.0). |
| 115 | + |
| 116 | +<Tabs> |
| 117 | + <TabItem label="Kotlin"> |
| 118 | + ```kotlin |
| 119 | + // Threshold defaults to 0.2, but can be customized |
| 120 | + val trigger = AnalogButton({ gamepad1.right_trigger }, threshold = 0.5) |
| 121 | + |
| 122 | + if (trigger.pressed) { |
| 123 | + // Trigger passed 0.5 |
| 124 | + } |
| 125 | + ``` |
| 126 | + </TabItem> |
| 127 | + <TabItem label="Java"> |
| 128 | + ```java |
| 129 | + // Threshold defaults to 0.2, but can be customized |
| 130 | + AnalogButton trigger = new AnalogButton(() -> gamepad1.right_trigger, 0.5); |
| 131 | + |
| 132 | + if (trigger.getPressed()) { |
| 133 | + // Trigger passed 0.5 |
| 134 | + } |
| 135 | + ``` |
| 136 | + </TabItem> |
| 137 | +</Tabs> |
| 138 | + |
| 139 | +### ActionButton |
| 140 | + |
| 141 | +A specialized button that automatically registers a command when triggered. |
| 142 | + |
| 143 | +<Tabs> |
| 144 | + <TabItem label="Kotlin"> |
| 145 | + ```kotlin |
| 146 | + val launchButton = ActionButton( |
| 147 | + button = { gamepad1.y }, |
| 148 | + commandToRun = LaunchDroneCommand(), |
| 149 | + whenToTrigger = ButtonState.WHEN_DOUBLE_PRESSED |
| 150 | + ) |
| 151 | + ``` |
| 152 | + </TabItem> |
| 153 | + <TabItem label="Java"> |
| 154 | + ```java |
| 155 | + ActionButton launchButton = new ActionButton( |
| 156 | + () -> gamepad1.y, |
| 157 | + new LaunchDroneCommand(), |
| 158 | + ButtonState.WHEN_DOUBLE_PRESSED |
| 159 | + ); |
| 160 | + ``` |
| 161 | + </TabItem> |
| 162 | +</Tabs> |
0 commit comments