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