WPILibC++ 2024.3.2
GenericHID.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <stdint.h>
8
9#include <string>
10
11namespace frc {
12
13class BooleanEvent;
14class EventLoop;
15
16/**
17 * Handle input from standard HID devices connected to the Driver Station.
18 *
19 * <p>This class handles standard input that comes from the Driver Station. Each
20 * time a value is requested the most recent value is returned. There is a
21 * single class instance for each device and the mapping of ports to hardware
22 * buttons depends on the code in the Driver Station.
23 */
25 public:
26 /**
27 * Represents a rumble output on the Joystick.
28 */
30 /// Left rumble motor.
32 /// Right rumble motor.
34 /// Both left and right rumble motors.
36 };
37
38 /**
39 * USB HID interface type.
40 */
41 enum HIDType {
42 /// Unknown.
44 /// XInputUnknown.
46 /// XInputGamepad.
48 /// XInputWheel.
50 /// XInputArcadeStick.
52 /// XInputFlightStick.
54 /// XInputDancePad.
56 /// XInputGuitar.
58 /// XInputGuitar2.
60 /// XInputDrumKit.
62 /// XInputGuitar3.
64 /// XInputArcadePad.
66 /// HIDJoystick.
68 /// HIDGamepad.
70 /// HIDDriving.
72 /// HIDFlight.
74 /// HID1stPerson.
75 kHID1stPerson = 24
76 };
77
78 explicit GenericHID(int port);
79 virtual ~GenericHID() = default;
80
81 GenericHID(GenericHID&&) = default;
83
84 /**
85 * Get the button value (starting at button 1).
86 *
87 * The buttons are returned in a single 16 bit value with one bit representing
88 * the state of each button. The appropriate button is returned as a boolean
89 * value.
90 *
91 * This method returns true if the button is being held down at the time
92 * that this method is being called.
93 *
94 * @param button The button number to be read (starting at 1)
95 * @return The state of the button.
96 */
97 bool GetRawButton(int button) const;
98
99 /**
100 * Whether the button was pressed since the last check. %Button indexes begin
101 * at 1.
102 *
103 * This method returns true if the button went from not pressed to held down
104 * since the last time this method was called. This is useful if you only
105 * want to call a function once when you press the button.
106 *
107 * @param button The button index, beginning at 1.
108 * @return Whether the button was pressed since the last check.
109 */
110 bool GetRawButtonPressed(int button);
111
112 /**
113 * Whether the button was released since the last check. %Button indexes begin
114 * at 1.
115 *
116 * This method returns true if the button went from held down to not pressed
117 * since the last time this method was called. This is useful if you only
118 * want to call a function once when you release the button.
119 *
120 * @param button The button index, beginning at 1.
121 * @return Whether the button was released since the last check.
122 */
123 bool GetRawButtonReleased(int button);
124
125 /**
126 * Constructs an event instance around this button's digital signal.
127 *
128 * @param button the button index
129 * @param loop the event loop instance to attach the event to.
130 * @return an event instance representing the button's digital signal attached
131 * to the given loop.
132 */
133 BooleanEvent Button(int button, EventLoop* loop) const;
134
135 /**
136 * Get the value of the axis.
137 *
138 * @param axis The axis to read, starting at 0.
139 * @return The value of the axis.
140 */
141 double GetRawAxis(int axis) const;
142
143 /**
144 * Get the angle in degrees of a POV on the HID.
145 *
146 * The POV angles start at 0 in the up direction, and increase clockwise
147 * (e.g. right is 90, upper-left is 315).
148 *
149 * @param pov The index of the POV to read (starting at 0)
150 * @return the angle of the POV in degrees, or -1 if the POV is not pressed.
151 */
152 int GetPOV(int pov = 0) const;
153
154 /**
155 * Constructs a BooleanEvent instance based around this angle of a POV on the
156 * HID.
157 *
158 * <p>The POV angles start at 0 in the up direction, and increase clockwise
159 * (eg right is 90, upper-left is 315).
160 *
161 * @param loop the event loop instance to attach the event to.
162 * @param angle POV angle in degrees, or -1 for the center / not pressed.
163 * @return a BooleanEvent instance based around this angle of a POV on the
164 * HID.
165 */
166 BooleanEvent POV(int angle, EventLoop* loop) const;
167
168 /**
169 * Constructs a BooleanEvent instance based around this angle of a POV on the
170 * HID.
171 *
172 * <p>The POV angles start at 0 in the up direction, and increase clockwise
173 * (eg right is 90, upper-left is 315).
174 *
175 * @param loop the event loop instance to attach the event to.
176 * @param pov index of the POV to read (starting at 0). Defaults to 0.
177 * @param angle POV angle in degrees, or -1 for the center / not pressed.
178 * @return a BooleanEvent instance based around this angle of a POV on the
179 * HID.
180 */
181 BooleanEvent POV(int pov, int angle, EventLoop* loop) const;
182
183 /**
184 * Constructs a BooleanEvent instance based around the 0 degree angle (up) of
185 * the default (index 0) POV on the HID.
186 *
187 * @return a BooleanEvent instance based around the 0 degree angle of a POV on
188 * the HID.
189 */
191
192 /**
193 * Constructs a BooleanEvent instance based around the 45 degree angle (right
194 * up) of the default (index 0) POV on the HID.
195 *
196 * @return a BooleanEvent instance based around the 45 degree angle of a POV
197 * on the HID.
198 */
200
201 /**
202 * Constructs a BooleanEvent instance based around the 90 degree angle (right)
203 * of the default (index 0) POV on the HID.
204 *
205 * @return a BooleanEvent instance based around the 90 degree angle of a POV
206 * on the HID.
207 */
209
210 /**
211 * Constructs a BooleanEvent instance based around the 135 degree angle (right
212 * down) of the default (index 0) POV on the HID.
213 *
214 * @return a BooleanEvent instance based around the 135 degree angle of a POV
215 * on the HID.
216 */
218
219 /**
220 * Constructs a BooleanEvent instance based around the 180 degree angle (down)
221 * of the default (index 0) POV on the HID.
222 *
223 * @return a BooleanEvent instance based around the 180 degree angle of a POV
224 * on the HID.
225 */
227
228 /**
229 * Constructs a BooleanEvent instance based around the 225 degree angle (down
230 * left) of the default (index 0) POV on the HID.
231 *
232 * @return a BooleanEvent instance based around the 225 degree angle of a POV
233 * on the HID.
234 */
236
237 /**
238 * Constructs a BooleanEvent instance based around the 270 degree angle (left)
239 * of the default (index 0) POV on the HID.
240 *
241 * @return a BooleanEvent instance based around the 270 degree angle of a POV
242 * on the HID.
243 */
245
246 /**
247 * Constructs a BooleanEvent instance based around the 315 degree angle (left
248 * up) of the default (index 0) POV on the HID.
249 *
250 * @return a BooleanEvent instance based around the 315 degree angle of a POV
251 * on the HID.
252 */
254
255 /**
256 * Constructs a BooleanEvent instance based around the center (not pressed) of
257 * the default (index 0) POV on the HID.
258 *
259 * @return a BooleanEvent instance based around the center of a POV on the
260 * HID.
261 */
263
264 /**
265 * Constructs an event instance that is true when the axis value is less than
266 * threshold
267 *
268 * @param axis The axis to read, starting at 0.
269 * @param threshold The value below which this trigger should return true.
270 * @param loop the event loop instance to attach the event to.
271 * @return an event instance that is true when the axis value is less than the
272 * provided threshold.
273 */
274 BooleanEvent AxisLessThan(int axis, double threshold, EventLoop* loop) const;
275
276 /**
277 * Constructs an event instance that is true when the axis value is greater
278 * than threshold
279 *
280 * @param axis The axis to read, starting at 0.
281 * @param threshold The value above which this trigger should return true.
282 * @param loop the event loop instance to attach the event to.
283 * @return an event instance that is true when the axis value is greater than
284 * the provided threshold.
285 */
286 BooleanEvent AxisGreaterThan(int axis, double threshold,
287 EventLoop* loop) const;
288
289 /**
290 * Get the number of axes for the HID.
291 *
292 * @return the number of axis for the current HID
293 */
294 int GetAxisCount() const;
295
296 /**
297 * Get the number of POVs for the HID.
298 *
299 * @return the number of POVs for the current HID
300 */
301 int GetPOVCount() const;
302
303 /**
304 * Get the number of buttons for the HID.
305 *
306 * @return the number of buttons on the current HID
307 */
308 int GetButtonCount() const;
309
310 /**
311 * Get if the HID is connected.
312 *
313 * @return true if the HID is connected
314 */
315 bool IsConnected() const;
316
317 /**
318 * Get the type of the HID.
319 *
320 * @return the type of the HID.
321 */
323
324 /**
325 * Get the name of the HID.
326 *
327 * @return the name of the HID.
328 */
329 std::string GetName() const;
330
331 /**
332 * Get the axis type of a joystick axis.
333 *
334 * @return the axis type of a joystick axis.
335 */
336 int GetAxisType(int axis) const;
337
338 /**
339 * Get the port number of the HID.
340 *
341 * @return The port number of the HID.
342 */
343 int GetPort() const;
344
345 /**
346 * Set a single HID output value for the HID.
347 *
348 * @param outputNumber The index of the output to set (1-32)
349 * @param value The value to set the output to
350 */
351 void SetOutput(int outputNumber, bool value);
352
353 /**
354 * Set all output values for the HID.
355 *
356 * @param value The 32 bit output value (1 bit for each output)
357 */
358 void SetOutputs(int value);
359
360 /**
361 * Set the rumble output for the HID.
362 *
363 * The DS currently supports 2 rumble values, left rumble and right rumble.
364 *
365 * @param type Which rumble value to set
366 * @param value The normalized value (0 to 1) to set the rumble to
367 */
368 void SetRumble(RumbleType type, double value);
369
370 private:
371 int m_port;
372 int m_outputs = 0;
373 uint16_t m_leftRumble = 0;
374 uint16_t m_rightRumble = 0;
375};
376
377} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition: EventLoop.h:15
Handle input from standard HID devices connected to the Driver Station.
Definition: GenericHID.h:24
double GetRawAxis(int axis) const
Get the value of the axis.
BooleanEvent POVUpLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 315 degree angle (left up) of the default (index ...
GenericHID::HIDType GetType() const
Get the type of the HID.
BooleanEvent POVCenter(EventLoop *loop) const
Constructs a BooleanEvent instance based around the center (not pressed) of the default (index 0) POV...
BooleanEvent POVDown(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 180 degree angle (down) of the default (index 0) ...
virtual ~GenericHID()=default
BooleanEvent POVUp(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 0 degree angle (up) of the default (index 0) POV ...
BooleanEvent POV(int pov, int angle, EventLoop *loop) const
Constructs a BooleanEvent instance based around this angle of a POV on the HID.
BooleanEvent POVRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 90 degree angle (right) of the default (index 0) ...
int GetAxisType(int axis) const
Get the axis type of a joystick axis.
bool GetRawButton(int button) const
Get the button value (starting at button 1).
BooleanEvent POVUpRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 45 degree angle (right up) of the default (index ...
BooleanEvent AxisGreaterThan(int axis, double threshold, EventLoop *loop) const
Constructs an event instance that is true when the axis value is greater than threshold.
BooleanEvent AxisLessThan(int axis, double threshold, EventLoop *loop) const
Constructs an event instance that is true when the axis value is less than threshold.
RumbleType
Represents a rumble output on the Joystick.
Definition: GenericHID.h:29
@ kBothRumble
Both left and right rumble motors.
Definition: GenericHID.h:35
@ kLeftRumble
Left rumble motor.
Definition: GenericHID.h:31
@ kRightRumble
Right rumble motor.
Definition: GenericHID.h:33
int GetPOVCount() const
Get the number of POVs for the HID.
int GetPort() const
Get the port number of the HID.
void SetOutputs(int value)
Set all output values for the HID.
BooleanEvent Button(int button, EventLoop *loop) const
Constructs an event instance around this button's digital signal.
bool IsConnected() const
Get if the HID is connected.
GenericHID(GenericHID &&)=default
GenericHID & operator=(GenericHID &&)=default
void SetOutput(int outputNumber, bool value)
Set a single HID output value for the HID.
BooleanEvent POVLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 270 degree angle (left) of the default (index 0) ...
HIDType
USB HID interface type.
Definition: GenericHID.h:41
@ kXInputArcadeStick
XInputArcadeStick.
Definition: GenericHID.h:51
@ kXInputDrumKit
XInputDrumKit.
Definition: GenericHID.h:61
@ kHIDJoystick
HIDJoystick.
Definition: GenericHID.h:67
@ kHIDGamepad
HIDGamepad.
Definition: GenericHID.h:69
@ kXInputWheel
XInputWheel.
Definition: GenericHID.h:49
@ kHID1stPerson
HID1stPerson.
Definition: GenericHID.h:75
@ kXInputGuitar
XInputGuitar.
Definition: GenericHID.h:57
@ kXInputFlightStick
XInputFlightStick.
Definition: GenericHID.h:53
@ kXInputArcadePad
XInputArcadePad.
Definition: GenericHID.h:65
@ kUnknown
Unknown.
Definition: GenericHID.h:43
@ kHIDFlight
HIDFlight.
Definition: GenericHID.h:73
@ kXInputGuitar3
XInputGuitar3.
Definition: GenericHID.h:63
@ kXInputGuitar2
XInputGuitar2.
Definition: GenericHID.h:59
@ kXInputDancePad
XInputDancePad.
Definition: GenericHID.h:55
@ kHIDDriving
HIDDriving.
Definition: GenericHID.h:71
@ kXInputGamepad
XInputGamepad.
Definition: GenericHID.h:47
@ kXInputUnknown
XInputUnknown.
Definition: GenericHID.h:45
int GetButtonCount() const
Get the number of buttons for the HID.
int GetPOV(int pov=0) const
Get the angle in degrees of a POV on the HID.
GenericHID(int port)
bool GetRawButtonPressed(int button)
Whether the button was pressed since the last check.
std::string GetName() const
Get the name of the HID.
BooleanEvent POV(int angle, EventLoop *loop) const
Constructs a BooleanEvent instance based around this angle of a POV on the HID.
BooleanEvent POVDownRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 135 degree angle (right down) of the default (ind...
void SetRumble(RumbleType type, double value)
Set the rumble output for the HID.
int GetAxisCount() const
Get the number of axes for the HID.
bool GetRawButtonReleased(int button)
Whether the button was released since the last check.
BooleanEvent POVDownLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the 225 degree angle (down left) of the default (inde...
type
Definition: core.h:556
Definition: AprilTagPoseEstimator.h:15