WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
GenericHID.hpp
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
14
15namespace wpi {
16
17class BooleanEvent;
18class EventLoop;
19
20/**
21 * Handle input from standard HID devices connected to the Driver Station.
22 *
23 * <p>This class handles standard input that comes from the Driver Station. Each
24 * time a value is requested the most recent value is returned. There is a
25 * single class instance for each device and the mapping of ports to hardware
26 * buttons depends on the code in the Driver Station.
27 */
29 public:
30 /**
31 * Represents a rumble output on the Joystick.
32 */
33 enum class RumbleType {
34 /// Left rumble motor.
36 /// Right rumble motor.
38 /// Left trigger rumble motor.
40 /// Right trigger rumble motor.
42 };
43
44 /**
45 * Represents the various outputs that a HID may support.
46 */
47 enum class SupportedOutputs : int {
48 /// No outputs supported.
49 NONE = 0x0,
50 /// Mono LED support.
51 MONO_LED = 0x1,
52 /// RGB LED support.
53 RGB_LED = 0x2,
54 /// Player LED support.
56 /// Rumble support.
57 RUMBLE = 0x8,
58 /// Trigger rumble support.
60 };
61
62 /**
63 * USB HID interface type.
64 */
65 enum class HIDType {
66 /// Unknown.
68 /// Standard HID device.
70 /// Xbox 360 controller.
72 /// Xbox One controller.
74 /// PS3 controller.
76 /// PS4 controller.
78 /// PS5 controller.
80 /// Nintendo Switch Pro controller.
82 /// Nintendo Switch Joycon Left controller.
84 /// Nintendo Switch Joycon Right controller.
86 /// Nintendo Switch Joycon controller pair.
88 };
89
90 explicit GenericHID(int port);
91 virtual ~GenericHID() = default;
92
93 GenericHID(GenericHID&&) = default;
95
96 /**
97 * Get the button value (starting at button 1).
98 *
99 * The buttons are returned in a single 16 bit value with one bit representing
100 * the state of each button. The appropriate button is returned as a boolean
101 * value.
102 *
103 * This method returns true if the button is being held down at the time
104 * that this method is being called.
105 *
106 * @param button The button number to be read (starting at 1)
107 * @return The state of the button.
108 */
109 bool GetRawButton(int button) const;
110
111 /**
112 * Whether the button was pressed since the last check. %Button indexes begin
113 * at 1.
114 *
115 * This method returns true if the button went from not pressed to held down
116 * since the last time this method was called. This is useful if you only
117 * want to call a function once when you press the button.
118 *
119 * @param button The button index, beginning at 0.
120 * @return Whether the button was pressed since the last check.
121 */
122 bool GetRawButtonPressed(int button);
123
124 /**
125 * Whether the button was released since the last check. %Button indexes begin
126 * at 1.
127 *
128 * This method returns true if the button went from held down to not pressed
129 * since the last time this method was called. This is useful if you only
130 * want to call a function once when you release the button.
131 *
132 * @param button The button index, beginning at 0.
133 * @return Whether the button was released since the last check.
134 */
135 bool GetRawButtonReleased(int button);
136
137 /**
138 * Constructs an event instance around this button's digital signal.
139 *
140 * @param button the button index
141 * @param loop the event loop instance to attach the event to.
142 * @return an event instance representing the button's digital signal attached
143 * to the given loop.
144 */
145 BooleanEvent Button(int button, EventLoop* loop) const;
146
147 /**
148 * Get the value of the axis.
149 *
150 * @param axis The axis to read, starting at 0.
151 * @return The value of the axis.
152 */
153 double GetRawAxis(int axis) const;
154
155 /**
156 * Get the angle of a POV on the HID.
157 *
158 * @param pov The index of the POV to read (starting at 0)
159 * @return the angle of the POV.
160 */
161 POVDirection GetPOV(int pov = 0) const;
162
163 /**
164 * Constructs a BooleanEvent instance based around this angle of a POV on the
165 * HID.
166 *
167 * @param loop the event loop instance to attach the event to.
168 * @param angle POV angle.
169 * @return a BooleanEvent instance based around this angle of a POV on the
170 * HID.
171 */
173
174 /**
175 * Constructs a BooleanEvent instance based around this angle of a POV on the
176 * HID.
177 *
178 * @param loop the event loop instance to attach the event to.
179 * @param pov index of the POV to read (starting at 0). Defaults to 0.
180 * @param angle POV angle.
181 * @return a BooleanEvent instance based around this angle of a POV on the
182 * HID.
183 */
184 BooleanEvent POV(int pov, POVDirection angle, EventLoop* loop) const;
185
186 /**
187 * Constructs a BooleanEvent instance based around the up direction of
188 * the default (index 0) POV on the HID.
189 *
190 * @return a BooleanEvent instance based around the up direction of a POV on
191 * the HID.
192 */
194
195 /**
196 * Constructs a BooleanEvent instance based around the up right direction
197 * of the default (index 0) POV on the HID.
198 *
199 * @return a BooleanEvent instance based around the up right direction of a
200 * POV on the HID.
201 */
203
204 /**
205 * Constructs a BooleanEvent instance based around the right direction
206 * of the default (index 0) POV on the HID.
207 *
208 * @return a BooleanEvent instance based around the right direction of a POV
209 * on the HID.
210 */
212
213 /**
214 * Constructs a BooleanEvent instance based around the down right direction
215 * of the default (index 0) POV on the HID.
216 *
217 * @return a BooleanEvent instance based around the down right direction of a
218 * POV on the HID.
219 */
221
222 /**
223 * Constructs a BooleanEvent instance based around the down direction
224 * of the default (index 0) POV on the HID.
225 *
226 * @return a BooleanEvent instance based around the down direction of a POV
227 * on the HID.
228 */
230
231 /**
232 * Constructs a BooleanEvent instance based around the down left direction
233 * of the default (index 0) POV on the HID.
234 *
235 * @return a BooleanEvent instance based around the down left direction of a
236 * POV on the HID.
237 */
239
240 /**
241 * Constructs a BooleanEvent instance based around the left direction
242 * of the default (index 0) POV on the HID.
243 *
244 * @return a BooleanEvent instance based around the left direction of a POV
245 * on the HID.
246 */
248
249 /**
250 * Constructs a BooleanEvent instance based around the up left direction
251 * of the default (index 0) POV on the HID.
252 *
253 * @return a BooleanEvent instance based around the up left direction of a POV
254 * on the HID.
255 */
257
258 /**
259 * Constructs a BooleanEvent instance based around the center (not pressed) of
260 * the default (index 0) POV on the HID.
261 *
262 * @return a BooleanEvent instance based around the center of a POV on the
263 * HID.
264 */
266
267 /**
268 * Constructs an event instance that is true when the axis value is less than
269 * threshold
270 *
271 * @param axis The axis to read, starting at 0.
272 * @param threshold The value below which this trigger should return true.
273 * @param loop the event loop instance to attach the event to.
274 * @return an event instance that is true when the axis value is less than the
275 * provided threshold.
276 */
277 BooleanEvent AxisLessThan(int axis, double threshold, EventLoop* loop) const;
278
279 /**
280 * Constructs an event instance that is true when the axis value is greater
281 * than threshold
282 *
283 * @param axis The axis to read, starting at 0.
284 * @param threshold The value above which this trigger should return true.
285 * @param loop the event loop instance to attach the event to.
286 * @return an event instance that is true when the axis value is greater than
287 * the provided threshold.
288 */
289 BooleanEvent AxisGreaterThan(int axis, double threshold,
290 EventLoop* loop) const;
291
293
294 /**
295 * Get the number of axes for the HID.
296 *
297 * @return the number of axis for the current HID
298 */
299 int GetAxesAvailable() const;
300
302
303 /**
304 * Get the number of POVs for the HID.
305 *
306 * @return the number of POVs for the current HID
307 */
308 int GetPOVsAvailable() const;
309
311
312 /**
313 * Get the number of buttons for the HID.
314 *
315 * @return the number of buttons on the current HID
316 */
317 uint64_t GetButtonsAvailable() const;
318
319 /**
320 * Get if the HID is connected.
321 *
322 * @return true if the HID is connected
323 */
324 bool IsConnected() const;
325
326 /**
327 * Get the type of the HID.
328 *
329 * @return the type of the HID.
330 */
332
333 /**
334 * Get the supported outputs of the HID.
335 *
336 * @return the supported outputs of the HID.
337 */
339
340 /**
341 * Get the name of the HID.
342 *
343 * @return the name of the HID.
344 */
345 std::string GetName() const;
346
347 /**
348 * Get the port number of the HID.
349 *
350 * @return The port number of the HID.
351 */
352 int GetPort() const;
353
354 /**
355 * Set leds on the controller. If only mono is supported, the system will use
356 * the highest value passed in.
357 *
358 * @param r Red value from 0-255
359 * @param g Green value from 0-255
360 * @param b Blue value from 0-255
361 */
362 void SetLeds(int r, int g, int b);
363
364 /**
365 * Set the rumble output for the HID.
366 *
367 * The DS currently supports 4 rumble values: left rumble, right rumble, left
368 * trigger rumble, and right trigger rumble.
369 *
370 * @param type Which rumble value to set
371 * @param value The normalized value (0 to 1) to set the rumble to
372 */
373 void SetRumble(RumbleType type, double value);
374
375 /**
376 * Check if a touchpad finger is available.
377 * @param touchpad The touchpad to check.
378 * @param finger The finger to check.
379 * @return true if the touchpad finger is available.
380 */
381 bool GetTouchpadFingerAvailable(int touchpad, int finger) const;
382
383 /**
384 * Get the touchpad finger data.
385 * @param touchpad The touchpad to read.
386 * @param finger The finger to read.
387 * @return The touchpad finger data.
388 */
389 TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const;
390
391 private:
392 int m_port;
393 uint16_t m_leftRumble = 0;
394 uint16_t m_rightRumble = 0;
395 uint16_t m_leftTriggerRumble = 0;
396 uint16_t m_rightTriggerRumble = 0;
397};
398
401 return static_cast<GenericHID::SupportedOutputs>(static_cast<int>(A) |
402 static_cast<int>(B));
403}
404
410
411} // namespace wpi
This class provides an easy way to link actions to active high logic signals.
Definition BooleanEvent.hpp:28
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.hpp:15
BooleanEvent POVRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the right direction of the default (index 0) POV on t...
BooleanEvent POVDownLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the down left direction of the default (index 0) POV ...
BooleanEvent POV(int pov, POVDirection angle, EventLoop *loop) const
Constructs a BooleanEvent instance based around this angle of a POV on the HID.
RumbleType
Represents a rumble output on the Joystick.
Definition GenericHID.hpp:33
@ RIGHT_TRIGGER_RUMBLE
Right trigger rumble motor.
Definition GenericHID.hpp:41
@ RIGHT_RUMBLE
Right rumble motor.
Definition GenericHID.hpp:37
@ LEFT_RUMBLE
Left rumble motor.
Definition GenericHID.hpp:35
@ LEFT_TRIGGER_RUMBLE
Left trigger rumble motor.
Definition GenericHID.hpp:39
GenericHID::HIDType GetGamepadType() const
Get the type of the HID.
int GetAxesAvailable() const
Get the number of axes for the HID.
int GetPOVsAvailable() const
Get the number of POVs for the HID.
std::string GetName() const
Get the name of the HID.
int GetPort() const
Get the port number of the HID.
BooleanEvent POVDownRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the down right direction of the default (index 0) POV...
int GetButtonsMaximumIndex() const
uint64_t GetButtonsAvailable() const
Get the number of buttons for the HID.
void SetLeds(int r, int g, int b)
Set leds on the controller.
double GetRawAxis(int axis) const
Get the value of the axis.
int GetPOVsMaximumIndex() const
bool GetRawButton(int button) const
Get the button value (starting at button 1).
virtual ~GenericHID()=default
BooleanEvent AxisLessThan(int axis, double threshold, EventLoop *loop) const
Constructs an event instance that is true when the axis value is less than threshold.
bool IsConnected() const
Get if the HID is connected.
GenericHID(int port)
bool GetTouchpadFingerAvailable(int touchpad, int finger) const
Check if a touchpad finger is available.
void SetRumble(RumbleType type, double value)
Set the rumble output for the HID.
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 POVLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the left direction of the default (index 0) POV on th...
SupportedOutputs GetSupportedOutputs() const
Get the supported outputs of the HID.
bool GetRawButtonReleased(int button)
Whether the button was released since the last check.
BooleanEvent POVUpLeft(EventLoop *loop) const
Constructs a BooleanEvent instance based around the up left direction of the default (index 0) POV on...
BooleanEvent POV(POVDirection angle, EventLoop *loop) const
Constructs a BooleanEvent instance based around this angle of a POV on the HID.
BooleanEvent POVDown(EventLoop *loop) const
Constructs a BooleanEvent instance based around the down direction of the default (index 0) POV on th...
POVDirection GetPOV(int pov=0) const
Get the angle of a POV on the HID.
TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const
Get the touchpad finger data.
HIDType
USB HID interface type.
Definition GenericHID.hpp:65
@ SWITCH_PRO
Nintendo Switch Pro controller.
Definition GenericHID.hpp:81
@ PS5
PS5 controller.
Definition GenericHID.hpp:79
@ UNKNOWN
Unknown.
Definition GenericHID.hpp:67
@ SWITCH_JOYCON_LEFT
Nintendo Switch Joycon Left controller.
Definition GenericHID.hpp:83
@ PS4
PS4 controller.
Definition GenericHID.hpp:77
@ STANDARD
Standard HID device.
Definition GenericHID.hpp:69
@ SWITCH_JOYCON_RIGHT
Nintendo Switch Joycon Right controller.
Definition GenericHID.hpp:85
@ XBOX_360
Xbox 360 controller.
Definition GenericHID.hpp:71
@ PS3
PS3 controller.
Definition GenericHID.hpp:75
@ SWITCH_JOYCON_PAIR
Nintendo Switch Joycon controller pair.
Definition GenericHID.hpp:87
@ XBOX_ONE
Xbox One controller.
Definition GenericHID.hpp:73
GenericHID & operator=(GenericHID &&)=default
GenericHID(GenericHID &&)=default
BooleanEvent POVCenter(EventLoop *loop) const
Constructs a BooleanEvent instance based around the center (not pressed) of the default (index 0) POV...
SupportedOutputs
Represents the various outputs that a HID may support.
Definition GenericHID.hpp:47
@ TRIGGER_RUMBLE
Trigger rumble support.
Definition GenericHID.hpp:59
@ RGB_LED
RGB LED support.
Definition GenericHID.hpp:53
@ PLAYER_LED
Player LED support.
Definition GenericHID.hpp:55
@ RUMBLE
Rumble support.
Definition GenericHID.hpp:57
@ MONO_LED
Mono LED support.
Definition GenericHID.hpp:51
@ NONE
No outputs supported.
Definition GenericHID.hpp:49
int GetAxesMaximumIndex() const
BooleanEvent POVUpRight(EventLoop *loop) const
Constructs a BooleanEvent instance based around the up right direction of the default (index 0) POV o...
BooleanEvent Button(int button, EventLoop *loop) const
Constructs an event instance around this button's digital signal.
BooleanEvent POVUp(EventLoop *loop) const
Constructs a BooleanEvent instance based around the up direction of the default (index 0) POV on the ...
bool GetRawButtonPressed(int button)
Whether the button was pressed since the last check.
Definition CvSource.hpp:15
GenericHID::SupportedOutputs operator|(GenericHID::SupportedOutputs A, GenericHID::SupportedOutputs B)
Definition GenericHID.hpp:399
POVDirection
A controller POV direction.
Definition POVDirection.hpp:18
GenericHID::SupportedOutputs & operator|=(GenericHID::SupportedOutputs &A, GenericHID::SupportedOutputs B)
Definition GenericHID.hpp:405
Touchpad finger data from a joystick.
Definition TouchpadFinger.hpp:12