|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from wpilib.event import EventLoop |
| 4 | +from wpilib.interfaces import GenericHID |
| 5 | + |
| 6 | +from ..commandscheduler import CommandScheduler |
| 7 | +from .trigger import Trigger |
| 8 | + |
| 9 | + |
| 10 | +class CommandGenericHID: |
| 11 | + """ |
| 12 | + A version of GenericHID with Trigger factories for command-based. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, port: int): |
| 16 | + """ |
| 17 | + Construct an instance of a device. |
| 18 | +
|
| 19 | + :param port: The port on the Driver Station that the device is plugged into. |
| 20 | + """ |
| 21 | + self._hid = GenericHID(port) |
| 22 | + |
| 23 | + def getHID(self) -> GenericHID: |
| 24 | + """ |
| 25 | + Get the underlying GenericHID object. |
| 26 | + """ |
| 27 | + return self._hid |
| 28 | + |
| 29 | + def button(self, button: int, loop: Optional[EventLoop] = None) -> Trigger: |
| 30 | + """ |
| 31 | + Constructs an event instance around this button's digital signal. |
| 32 | +
|
| 33 | + :param button: The button index |
| 34 | + :param loop: the event loop instance to attache the event to. |
| 35 | + """ |
| 36 | + if loop is None: |
| 37 | + loop = CommandScheduler.getInstance().getDefaultButtonLoop() |
| 38 | + return Trigger(loop, lambda: self._hid.getRawButtonPressed(button)) |
| 39 | + |
| 40 | + def pov( |
| 41 | + self, angle: int, *, pov: int = 0, loop: Optional[EventLoop] = None |
| 42 | + ) -> Trigger: |
| 43 | + """ |
| 44 | + Constructs a Trigger instance based around this angle of a POV on the HID. |
| 45 | +
|
| 46 | + The POV angles start at 0 in the up direction, and increase clockwise (e.g. right is 90, |
| 47 | + upper-left is 315). |
| 48 | +
|
| 49 | + :param angle: POV angle in degrees, or -1 for the center / not pressed. |
| 50 | + :param pov: index of the POV to read (starting at 0). Defaults to 0. |
| 51 | + :param loop: the event loop instance to attach the event to. Defaults to {@link |
| 52 | + CommandScheduler#getDefaultButtonLoop() the default command scheduler button loop}. |
| 53 | + :returns: a Trigger instance based around this angle of a POV on the HID. |
| 54 | + """ |
| 55 | + if loop is None: |
| 56 | + loop = CommandScheduler.getInstance().getDefaultButtonLoop() |
| 57 | + return Trigger(loop, lambda: self._hid.getPOV(pov) == angle) |
| 58 | + |
| 59 | + def povUp(self) -> Trigger: |
| 60 | + """ |
| 61 | + Constructs a Trigger instance based around the 0 degree angle (up) of the default (index 0) POV |
| 62 | + on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command |
| 63 | + scheduler button loop}. |
| 64 | +
|
| 65 | + :returns: a Trigger instance based around the 0 degree angle of a POV on the HID. |
| 66 | + """ |
| 67 | + return self.pov(0) |
| 68 | + |
| 69 | + def povUpRight(self) -> Trigger: |
| 70 | + """ |
| 71 | + Constructs a Trigger instance based around the 45 degree angle (right up) of the default (index |
| 72 | + 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default |
| 73 | + command scheduler button loop}. |
| 74 | +
|
| 75 | + :returns: a Trigger instance based around the 45 degree angle of a POV on the HID. |
| 76 | + """ |
| 77 | + return self.pov(45) |
| 78 | + |
| 79 | + def povRight(self) -> Trigger: |
| 80 | + """ |
| 81 | + Constructs a Trigger instance based around the 90 degree angle (right) of the default (index 0) |
| 82 | + POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command |
| 83 | + scheduler button loop}. |
| 84 | +
|
| 85 | + :returns: a Trigger instance based around the 90 degree angle of a POV on the HID. |
| 86 | + """ |
| 87 | + return self.pov(90) |
| 88 | + |
| 89 | + def povDownRight(self) -> Trigger: |
| 90 | + """ |
| 91 | + Constructs a Trigger instance based around the 135 degree angle (right down) of the default |
| 92 | + (index 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the |
| 93 | + default command scheduler button loop}. |
| 94 | +
|
| 95 | + :returns: a Trigger instance based around the 135 degree angle of a POV on the HID. |
| 96 | + """ |
| 97 | + return self.pov(135) |
| 98 | + |
| 99 | + def povDown(self) -> Trigger: |
| 100 | + """ |
| 101 | + Constructs a Trigger instance based around the 180 degree angle (down) of the default (index 0) |
| 102 | + POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command |
| 103 | + scheduler button loop}. |
| 104 | +
|
| 105 | + :returns: a Trigger instance based around the 180 degree angle of a POV on the HID. |
| 106 | + """ |
| 107 | + return self.pov(180) |
| 108 | + |
| 109 | + def povDownLeft(self) -> Trigger: |
| 110 | + """ |
| 111 | + Constructs a Trigger instance based around the 225 degree angle (down left) of the default |
| 112 | + (index 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the |
| 113 | + default command scheduler button loop}. |
| 114 | +
|
| 115 | + :returns: a Trigger instance based around the 225 degree angle of a POV on the HID. |
| 116 | + """ |
| 117 | + return self.pov(225) |
| 118 | + |
| 119 | + def povLeft(self) -> Trigger: |
| 120 | + """ |
| 121 | + Constructs a Trigger instance based around the 270 degree angle (left) of the default (index 0) |
| 122 | + POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default command |
| 123 | + scheduler button loop}. |
| 124 | +
|
| 125 | + :returns: a Trigger instance based around the 270 degree angle of a POV on the HID. |
| 126 | + """ |
| 127 | + return self.pov(270) |
| 128 | + |
| 129 | + def povUpLeft(self) -> Trigger: |
| 130 | + """ |
| 131 | + Constructs a Trigger instance based around the 315 degree angle (left up) of the default (index |
| 132 | + 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the default |
| 133 | + command scheduler button loop}. |
| 134 | +
|
| 135 | + :returns: a Trigger instance based around the 315 degree angle of a POV on the HID. |
| 136 | + """ |
| 137 | + return self.pov(315) |
| 138 | + |
| 139 | + def povCenter(self) -> Trigger: |
| 140 | + """ |
| 141 | + Constructs a Trigger instance based around the center (not pressed) position of the default |
| 142 | + (index 0) POV on the HID, attached to {@link CommandScheduler#getDefaultButtonLoop() the |
| 143 | + default command scheduler button loop}. |
| 144 | +
|
| 145 | + :returns: a Trigger instance based around the center position of a POV on the HID. |
| 146 | + """ |
| 147 | + return self.pov(-1) |
| 148 | + |
| 149 | + def axisLessThan( |
| 150 | + self, axis: int, threshold: float, loop: Optional[EventLoop] = None |
| 151 | + ) -> Trigger: |
| 152 | + """ |
| 153 | + Constructs a Trigger instance that is true when the axis value is less than {@code threshold}, |
| 154 | + attached to the given loop. |
| 155 | +
|
| 156 | + :param axis: The axis to read, starting at 0 |
| 157 | + :param threshold: The value below which this trigger should return true. |
| 158 | + :param loop: the event loop instance to attach the trigger to |
| 159 | + :returns: a Trigger instance that is true when the axis value is less than the provided |
| 160 | + threshold. |
| 161 | + """ |
| 162 | + if loop is None: |
| 163 | + loop = CommandScheduler.getInstance().getDefaultButtonLoop() |
| 164 | + return Trigger(loop, lambda: self._hid.getRawAxis(axis) < threshold) |
| 165 | + |
| 166 | + def axisGreaterThan( |
| 167 | + self, axis: int, threshold: float, loop: Optional[EventLoop] = None |
| 168 | + ) -> Trigger: |
| 169 | + """ |
| 170 | + Constructs a Trigger instance that is true when the axis value is greater than {@code |
| 171 | + threshold}, attached to the given loop. |
| 172 | +
|
| 173 | + :param axis: The axis to read, starting at 0 |
| 174 | + :param threshold: The value above which this trigger should return true. |
| 175 | + :param loop: the event loop instance to attach the trigger to. |
| 176 | + :returns: a Trigger instance that is true when the axis value is greater than the provided |
| 177 | + threshold. |
| 178 | + """ |
| 179 | + if loop is None: |
| 180 | + loop = CommandScheduler.getInstance().getDefaultButtonLoop() |
| 181 | + return Trigger(loop, lambda: self._hid.getRawAxis(axis) > threshold) |
| 182 | + |
| 183 | + def getRawAxis(self, axis: int) -> float: |
| 184 | + """ |
| 185 | + Get the value of the axis. |
| 186 | +
|
| 187 | + :param axis: The axis to read, starting at 0. |
| 188 | + :returns: The value of the axis. |
| 189 | + """ |
| 190 | + return self._hid.getRawAxis(axis) |
0 commit comments