1+ package frc .robot .util ;
2+
3+
4+ import edu .wpi .first .networktables .GenericEntry ;
5+ import edu .wpi .first .wpilibj .shuffleboard .Shuffleboard ;
6+ import edu .wpi .first .wpilibj .shuffleboard .ShuffleboardTab ;
7+ import edu .wpi .first .wpilibj2 .command .button .Trigger ;
8+
9+
10+ /**
11+ * AutoResetDashboardTrigger
12+ * -------------------------
13+ * A dashboard-backed one-shot trigger designed to behave like
14+ * a gamepad "onTrue" button.
15+ *
16+ * Behavior:
17+ * - Exposes a Shuffleboard Toggle Button
18+ * - Detects a FALSE -> TRUE transition (rising edge)
19+ * - Fires TRUE for exactly one scheduler loop
20+ * - Automatically resets the dashboard value back to FALSE
21+ *
22+ * Intended use cases:
23+ * - Single-action commands (reset, zeroing, shoot-once, test actions)
24+ * - Debug and pit utilities where holding a button is unsafe
25+ *
26+ * Not intended for:
27+ * - whileTrue / continuous commands
28+ * - toggle or latch-style behaviors
29+ */
30+ public final class AutoResetDashboardTrigger {
31+
32+
33+ /** NetworkTables-backed boolean entry shown on Shuffleboard */
34+ private final GenericEntry buttonEntry ;
35+
36+
37+ /** Cached previous value for edge detection */
38+ private boolean previousValue = false ;
39+
40+
41+ /**
42+ * Creates an auto-resetting dashboard trigger.
43+ *
44+ * @param tabName Shuffleboard tab name
45+ * @param buttonName Label displayed on the dashboard
46+ */
47+ public AutoResetDashboardTrigger (String tabName , String buttonName ) {
48+ ShuffleboardTab tab = Shuffleboard .getTab (tabName );
49+
50+
51+ this .buttonEntry = tab .add (buttonName , false )
52+ .withWidget ("Toggle Button" )
53+ .getEntry ();
54+ }
55+
56+
57+ /**
58+ * Returns a WPILib Trigger that fires exactly once
59+ * when the dashboard button is pressed.
60+ *
61+ * @return one-shot Trigger (rising-edge based)
62+ */
63+ public Trigger trigger () {
64+ return new Trigger (() -> {
65+ boolean currentValue = buttonEntry .getBoolean (false );
66+
67+
68+ // Rising edge detection
69+ if (currentValue && !previousValue ) {
70+ buttonEntry .setBoolean (false ); // auto-reset immediately
71+ previousValue = true ;
72+ return true ;
73+ }
74+
75+
76+ previousValue = currentValue ;
77+ return false ;
78+ });
79+ }
80+
81+
82+ /**
83+ * Programmatically fires the trigger once.
84+ * Useful for tests or internal control logic.
85+ */
86+ public void fireOnce () {
87+ buttonEntry .setBoolean (true );
88+ }
89+ }
90+
91+
92+ /*
93+ ========================
94+ Example usage (RobotContainer)
95+ ========================
96+
97+
98+ AutoResetDashboardTrigger shootOnce =
99+ new AutoResetDashboardTrigger("Driver", "Shoot Once");
100+
101+
102+ shootOnce.trigger()
103+ .onTrue(new ShootCommand());
104+
105+
106+ // Designed for onTrue only
107+ // whileTrue / toggle usage is intentionally unsupported
108+ */
0 commit comments