Skip to content

Commit

Permalink
[commands] Cache button and POV triggers
Browse files Browse the repository at this point in the history
This is a common footgun for teams.
  • Loading branch information
PeterJohnson committed Mar 21, 2024
1 parent b2113d3 commit ff929d4
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import edu.wpi.first.wpilibj.GenericHID;
import edu.wpi.first.wpilibj.event.EventLoop;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import java.util.HashMap;
import java.util.Map;

/**
* A version of {@link GenericHID} with {@link Trigger} factories for command-based.
Expand All @@ -15,6 +17,8 @@
*/
public class CommandGenericHID {
private final GenericHID m_hid;
private final Map<EventLoop, Map<Integer, Trigger>> m_buttonCache = new HashMap<>();
private final Map<EventLoop, Map<Integer, Trigger>> m_povCache = new HashMap<>();

/**
* Construct an instance of a device.
Expand Down Expand Up @@ -54,7 +58,9 @@ public Trigger button(int button) {
* @return an event instance representing the button's digital signal attached to the given loop.
*/
public Trigger button(int button, EventLoop loop) {
return new Trigger(loop, () -> m_hid.getRawButton(button));
var cache = m_buttonCache.computeIfAbsent(loop, k -> new HashMap<>());
return cache.computeIfAbsent(button, k ->
new Trigger(loop, () -> m_hid.getRawButton(k)));
}

/**
Expand Down Expand Up @@ -85,7 +91,10 @@ public Trigger pov(int angle) {
* @return a Trigger instance based around this angle of a POV on the HID.
*/
public Trigger pov(int pov, int angle, EventLoop loop) {
return new Trigger(loop, () -> m_hid.getPOV(pov) == angle);
var cache = m_povCache.computeIfAbsent(loop, k -> new HashMap<>());
// angle can be -1, so use 3600 instead of 360
return cache.computeIfAbsent(pov * 3600 + angle, k ->
new Trigger(loop, () -> m_hid.getPOV(pov) == angle));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Trigger trigger() {
* given loop.
*/
public Trigger trigger(EventLoop loop) {
return m_hid.trigger(loop).castTo(Trigger::new);
return button(Joystick.ButtonType.kTrigger.value, loop);
}

/**
Expand All @@ -77,7 +77,7 @@ public Trigger top() {
* loop.
*/
public Trigger top(EventLoop loop) {
return m_hid.top(loop).castTo(Trigger::new);
return button(Joystick.ButtonType.kTop.value, loop);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Trigger L2() {
* loop.
*/
public Trigger L2(EventLoop loop) {
return m_hid.L2(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kL2.value, loop);
}

/**
Expand All @@ -76,7 +76,7 @@ public Trigger R2() {
* loop.
*/
public Trigger R2(EventLoop loop) {
return m_hid.R2(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kR2.value, loop);
}

/**
Expand All @@ -97,7 +97,7 @@ public Trigger L1() {
* loop.
*/
public Trigger L1(EventLoop loop) {
return m_hid.L1(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kL1.value, loop);
}

/**
Expand All @@ -118,7 +118,7 @@ public Trigger R1() {
* loop.
*/
public Trigger R1(EventLoop loop) {
return m_hid.R1(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kR1.value, loop);
}

/**
Expand All @@ -139,7 +139,7 @@ public Trigger L3() {
* loop.
*/
public Trigger L3(EventLoop loop) {
return m_hid.L3(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kL3.value, loop);
}

/**
Expand All @@ -160,7 +160,7 @@ public Trigger R3() {
* loop.
*/
public Trigger R3(EventLoop loop) {
return m_hid.R3(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kR3.value, loop);
}

/**
Expand All @@ -181,7 +181,7 @@ public Trigger square() {
* loop.
*/
public Trigger square(EventLoop loop) {
return m_hid.square(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kSquare.value, loop);
}

/**
Expand All @@ -202,7 +202,7 @@ public Trigger cross() {
* loop.
*/
public Trigger cross(EventLoop loop) {
return m_hid.cross(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kCross.value, loop);
}

/**
Expand All @@ -223,7 +223,7 @@ public Trigger triangle() {
* given loop.
*/
public Trigger triangle(EventLoop loop) {
return m_hid.triangle(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kTriangle.value, loop);
}

/**
Expand All @@ -244,7 +244,7 @@ public Trigger circle() {
* loop.
*/
public Trigger circle(EventLoop loop) {
return m_hid.circle(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kCircle.value, loop);
}

/**
Expand All @@ -265,7 +265,7 @@ public Trigger share() {
* loop.
*/
public Trigger share(EventLoop loop) {
return m_hid.share(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kShare.value, loop);
}

/**
Expand All @@ -286,7 +286,7 @@ public Trigger PS() {
* loop.
*/
public Trigger PS(EventLoop loop) {
return m_hid.PS(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kPS.value, loop);
}

/**
Expand All @@ -307,7 +307,7 @@ public Trigger options() {
* given loop.
*/
public Trigger options(EventLoop loop) {
return m_hid.options(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kOptions.value, loop);
}

/**
Expand All @@ -328,7 +328,7 @@ public Trigger touchpad() {
* loop.
*/
public Trigger touchpad(EventLoop loop) {
return m_hid.touchpad(loop).castTo(Trigger::new);
return button(PS4Controller.Button.kTouchpad.value, loop);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Trigger L2() {
* loop.
*/
public Trigger L2(EventLoop loop) {
return m_hid.L2(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kL2.value, loop);
}

/**
Expand All @@ -76,7 +76,7 @@ public Trigger R2() {
* loop.
*/
public Trigger R2(EventLoop loop) {
return m_hid.R2(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kR2.value, loop);
}

/**
Expand All @@ -97,7 +97,7 @@ public Trigger L1() {
* loop.
*/
public Trigger L1(EventLoop loop) {
return m_hid.L1(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kL1.value, loop);
}

/**
Expand All @@ -118,7 +118,7 @@ public Trigger R1() {
* loop.
*/
public Trigger R1(EventLoop loop) {
return m_hid.R1(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kR1.value, loop);
}

/**
Expand All @@ -139,7 +139,7 @@ public Trigger L3() {
* loop.
*/
public Trigger L3(EventLoop loop) {
return m_hid.L3(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kL3.value, loop);
}

/**
Expand All @@ -160,7 +160,7 @@ public Trigger R3() {
* loop.
*/
public Trigger R3(EventLoop loop) {
return m_hid.R3(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kR3.value, loop);
}

/**
Expand All @@ -181,7 +181,7 @@ public Trigger square() {
* loop.
*/
public Trigger square(EventLoop loop) {
return m_hid.square(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kSquare.value, loop);
}

/**
Expand All @@ -202,7 +202,7 @@ public Trigger cross() {
* loop.
*/
public Trigger cross(EventLoop loop) {
return m_hid.cross(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kCross.value, loop);
}

/**
Expand All @@ -223,7 +223,7 @@ public Trigger triangle() {
* given loop.
*/
public Trigger triangle(EventLoop loop) {
return m_hid.triangle(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kTriangle.value, loop);
}

/**
Expand All @@ -244,7 +244,7 @@ public Trigger circle() {
* loop.
*/
public Trigger circle(EventLoop loop) {
return m_hid.circle(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kCircle.value, loop);
}

/**
Expand All @@ -265,7 +265,7 @@ public Trigger create() {
* loop.
*/
public Trigger create(EventLoop loop) {
return m_hid.create(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kCreate.value, loop);
}

/**
Expand All @@ -286,7 +286,7 @@ public Trigger PS() {
* loop.
*/
public Trigger PS(EventLoop loop) {
return m_hid.PS(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kPS.value, loop);
}

/**
Expand All @@ -307,7 +307,7 @@ public Trigger options() {
* given loop.
*/
public Trigger options(EventLoop loop) {
return m_hid.options(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kOptions.value, loop);
}

/**
Expand All @@ -328,7 +328,7 @@ public Trigger touchpad() {
* loop.
*/
public Trigger touchpad(EventLoop loop) {
return m_hid.touchpad(loop).castTo(Trigger::new);
return button(PS5Controller.Button.kTouchpad.value, loop);
}

/**
Expand Down
Loading

0 comments on commit ff929d4

Please sign in to comment.