WPILibC++ 2027.0.0-alpha-4
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
12
13namespace wpi {
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 /// Left trigger rumble motor.
38 /// Right trigger rumble motor.
40 };
41
42 /**
43 * Represents the various outputs that a HID may support.
44 */
46 /// No outputs supported.
47 kNone = 0x0,
48 /// Mono LED support.
49 kMonoLed = 0x1,
50 /// RGB LED support.
51 kRgbLed = 0x2,
52 /// Player LED support.
54 /// Rumble support.
55 kRumble = 0x8,
56 /// Trigger rumble support.
58 };
59
60 /**
61 * USB HID interface type.
62 */
63 enum HIDType {
64 /// Unknown.
66 /// Standard HID device.
68 /// Xbox 360 controller.
70 /// Xbox One controller.
72 /// PS3 controller.
74 /// PS4 controller.
76 /// PS5 controller.
78 /// Nintendo Switch Pro controller.
80 /// Nintendo Switch Joycon Left controller.
82 /// Nintendo Switch Joycon Right controller.
84 /// Nintendo Switch Joycon controller pair.
86 };
87
88 explicit GenericHID(int port);
89 virtual ~GenericHID() = default;
90
91 GenericHID(GenericHID&&) = default;
93
94 /**
95 * Get the button value (starting at button 1).
96 *
97 * The buttons are returned in a single 16 bit value with one bit representing
98 * the state of each button. The appropriate button is returned as a boolean
99 * value.
100 *
101 * This method returns true if the button is being held down at the time
102 * that this method is being called.
103 *
104 * @param button The button number to be read (starting at 1)
105 * @return The state of the button.
106 */
107 bool GetRawButton(int button) const;
108
109 /**
110 * Whether the button was pressed since the last check. %Button indexes begin
111 * at 1.
112 *
113 * This method returns true if the button went from not pressed to held down
114 * since the last time this method was called. This is useful if you only
115 * want to call a function once when you press the button.
116 *
117 * @param button The button index, beginning at 0.
118 * @return Whether the button was pressed since the last check.
119 */
120 bool GetRawButtonPressed(int button);
121
122 /**
123 * Whether the button was released since the last check. %Button indexes begin
124 * at 1.
125 *
126 * This method returns true if the button went from held down to not pressed
127 * since the last time this method was called. This is useful if you only
128 * want to call a function once when you release the button.
129 *
130 * @param button The button index, beginning at 0.
131 * @return Whether the button was released since the last check.
132 */
133 bool GetRawButtonReleased(int button);
134
135 /**
136 * Constructs an event instance around this button's digital signal.
137 *
138 * @param button the button index
139 * @param loop the event loop instance to attach the event to.
140 * @return an event instance representing the button's digital signal attached
141 * to the given loop.
142 */
143 BooleanEvent Button(int button, EventLoop* loop) const;
144
145 /**
146 * Get the value of the axis.
147 *
148 * @param axis The axis to read, starting at 0.
149 * @return The value of the axis.
150 */
151 double GetRawAxis(int axis) const;
152
153 /**
154 * Get the angle of a POV on the HID.
155 *
156 * @param pov The index of the POV to read (starting at 0)
157 * @return the angle of the POV.
158 */
160
161 /**
162 * Constructs a BooleanEvent instance based around this angle of a POV on the
163 * HID.
164 *
165 * @param loop the event loop instance to attach the event to.
166 * @param angle POV angle.
167 * @return a BooleanEvent instance based around this angle of a POV on the
168 * HID.
169 */
171
172 /**
173 * Constructs a BooleanEvent instance based around this angle of a POV on the
174 * HID.
175 *
176 * @param loop the event loop instance to attach the event to.
177 * @param pov index of the POV to read (starting at 0). Defaults to 0.
178 * @param angle POV angle.
179 * @return a BooleanEvent instance based around this angle of a POV on the
180 * HID.
181 */
183 EventLoop* loop) const;
184
185 /**
186 * Constructs a BooleanEvent instance based around the up direction of
187 * the default (index 0) POV on the HID.
188 *
189 * @return a BooleanEvent instance based around the up direction of a POV on
190 * the HID.
191 */
193
194 /**
195 * Constructs a BooleanEvent instance based around the up right direction
196 * of the default (index 0) POV on the HID.
197 *
198 * @return a BooleanEvent instance based around the up right direction of a
199 * POV on the HID.
200 */
202
203 /**
204 * Constructs a BooleanEvent instance based around the right direction
205 * of the default (index 0) POV on the HID.
206 *
207 * @return a BooleanEvent instance based around the right direction of a POV
208 * on the HID.
209 */
211
212 /**
213 * Constructs a BooleanEvent instance based around the down right direction
214 * of the default (index 0) POV on the HID.
215 *
216 * @return a BooleanEvent instance based around the down right direction of a
217 * POV on the HID.
218 */
220
221 /**
222 * Constructs a BooleanEvent instance based around the down direction
223 * of the default (index 0) POV on the HID.
224 *
225 * @return a BooleanEvent instance based around the down direction of a POV
226 * on the HID.
227 */
229
230 /**
231 * Constructs a BooleanEvent instance based around the down left direction
232 * of the default (index 0) POV on the HID.
233 *
234 * @return a BooleanEvent instance based around the down left direction of a
235 * POV on the HID.
236 */
238
239 /**
240 * Constructs a BooleanEvent instance based around the left direction
241 * of the default (index 0) POV on the HID.
242 *
243 * @return a BooleanEvent instance based around the left direction of a POV
244 * on the HID.
245 */
247
248 /**
249 * Constructs a BooleanEvent instance based around the up left direction
250 * of the default (index 0) POV on the HID.
251 *
252 * @return a BooleanEvent instance based around the up left direction of a POV
253 * on the HID.
254 */
256
257 /**
258 * Constructs a BooleanEvent instance based around the center (not pressed) of
259 * the default (index 0) POV on the HID.
260 *
261 * @return a BooleanEvent instance based around the center of a POV on the
262 * HID.
263 */
265
266 /**
267 * Constructs an event instance that is true when the axis value is less than
268 * threshold
269 *
270 * @param axis The axis to read, starting at 0.
271 * @param threshold The value below which this trigger should return true.
272 * @param loop the event loop instance to attach the event to.
273 * @return an event instance that is true when the axis value is less than the
274 * provided threshold.
275 */
276 BooleanEvent AxisLessThan(int axis, double threshold, EventLoop* loop) const;
277
278 /**
279 * Constructs an event instance that is true when the axis value is greater
280 * than threshold
281 *
282 * @param axis The axis to read, starting at 0.
283 * @param threshold The value above which this trigger should return true.
284 * @param loop the event loop instance to attach the event to.
285 * @return an event instance that is true when the axis value is greater than
286 * the provided threshold.
287 */
288 BooleanEvent AxisGreaterThan(int axis, double threshold,
289 EventLoop* loop) const;
290
292
293 /**
294 * Get the number of axes for the HID.
295 *
296 * @return the number of axis for the current HID
297 */
298 int GetAxesAvailable() const;
299
301
302 /**
303 * Get the number of POVs for the HID.
304 *
305 * @return the number of POVs for the current HID
306 */
307 int GetPOVsAvailable() const;
308
310
311 /**
312 * Get the number of buttons for the HID.
313 *
314 * @return the number of buttons on the current HID
315 */
316 uint64_t GetButtonsAvailable() const;
317
318 /**
319 * Get if the HID is connected.
320 *
321 * @return true if the HID is connected
322 */
323 bool IsConnected() const;
324
325 /**
326 * Get the type of the HID.
327 *
328 * @return the type of the HID.
329 */
331
332 /**
333 * Get the supported outputs of the HID.
334 *
335 * @return the supported outputs of the HID.
336 */
338
339 /**
340 * Get the name of the HID.
341 *
342 * @return the name of the HID.
343 */
344 std::string GetName() const;
345
346 /**
347 * Get the port number of the HID.
348 *
349 * @return The port number of the HID.
350 */
351 int GetPort() const;
352
353 /**
354 * Set leds on the controller. If only mono is supported, the system will use
355 * the highest value passed in.
356 *
357 * @param r Red value from 0-255
358 * @param g Green value from 0-255
359 * @param b Blue value from 0-255
360 */
361 void SetLeds(int r, int g, int b);
362
363 /**
364 * Set the rumble output for the HID.
365 *
366 * The DS currently supports 4 rumble values: left rumble, right rumble, left
367 * trigger rumble, and right trigger rumble.
368 *
369 * @param type Which rumble value to set
370 * @param value The normalized value (0 to 1) to set the rumble to
371 */
372 void SetRumble(RumbleType type, double value);
373
374 /**
375 * Check if a touchpad finger is available.
376 * @param touchpad The touchpad to check.
377 * @param finger The finger to check.
378 * @return true if the touchpad finger is available.
379 */
380 bool GetTouchpadFingerAvailable(int touchpad, int finger) const;
381
382 /**
383 * Get the touchpad finger data.
384 * @param touchpad The touchpad to read.
385 * @param finger The finger to read.
386 * @return The touchpad finger data.
387 */
389 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
399} // namespace wpi
This class provides an easy way to link actions to active high logic signals.
Definition BooleanEvent.hpp:28
POVDirection
A controller POV direction.
Definition DriverStation.hpp:62
A declarative way to bind a set of actions to a loop and execute them when the loop is polled.
Definition EventLoop.hpp:15
DriverStation::TouchpadFinger GetTouchpadFinger(int touchpad, int finger) const
Get the touchpad finger data.
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 ...
RumbleType
Represents a rumble output on the Joystick.
Definition GenericHID.hpp:31
@ kLeftRumble
Left rumble motor.
Definition GenericHID.hpp:33
@ kRightTriggerRumble
Right trigger rumble motor.
Definition GenericHID.hpp:39
@ kLeftTriggerRumble
Left trigger rumble motor.
Definition GenericHID.hpp:37
@ kRightRumble
Right rumble motor.
Definition GenericHID.hpp:35
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
BooleanEvent POV(DriverStation::POVDirection angle, EventLoop *loop) const
Constructs a BooleanEvent instance based around this angle of a POV on 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.
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...
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 POVDown(EventLoop *loop) const
Constructs a BooleanEvent instance based around the down direction of the default (index 0) POV on th...
DriverStation::POVDirection GetPOV(int pov=0) const
Get the angle of a POV on the HID.
HIDType
USB HID interface type.
Definition GenericHID.hpp:63
@ kUnknown
Unknown.
Definition GenericHID.hpp:65
@ kStandard
Standard HID device.
Definition GenericHID.hpp:67
@ kXboxOne
Xbox One controller.
Definition GenericHID.hpp:71
@ kPS3
PS3 controller.
Definition GenericHID.hpp:73
@ kSwitchJoyconPair
Nintendo Switch Joycon controller pair.
Definition GenericHID.hpp:85
@ kPS5
PS5 controller.
Definition GenericHID.hpp:77
@ kSwitchPro
Nintendo Switch Pro controller.
Definition GenericHID.hpp:79
@ kPS4
PS4 controller.
Definition GenericHID.hpp:75
@ kSwitchJoyconLeft
Nintendo Switch Joycon Left controller.
Definition GenericHID.hpp:81
@ kXbox360
Xbox 360 controller.
Definition GenericHID.hpp:69
@ kSwitchJoyconRight
Nintendo Switch Joycon Right controller.
Definition GenericHID.hpp:83
SupportedOutputs
Represents the various outputs that a HID may support.
Definition GenericHID.hpp:45
@ kPlayerLed
Player LED support.
Definition GenericHID.hpp:53
@ kRumble
Rumble support.
Definition GenericHID.hpp:55
@ kMonoLed
Mono LED support.
Definition GenericHID.hpp:49
@ kRgbLed
RGB LED support.
Definition GenericHID.hpp:51
@ kTriggerRumble
Trigger rumble support.
Definition GenericHID.hpp:57
@ kNone
No outputs supported.
Definition GenericHID.hpp:47
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...
GenericHID::SupportedOutputs GetSupportedOutputs() const
Get the supported outputs of the HID.
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
Definition DriverStation.hpp:83