WPILibC++ 2023.4.3
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:
27
28 enum HIDType {
45 kHID1stPerson = 24
46 };
47
48 explicit GenericHID(int port);
49 virtual ~GenericHID() = default;
50
51 GenericHID(GenericHID&&) = default;
53
54 /**
55 * Get the button value (starting at button 1).
56 *
57 * The buttons are returned in a single 16 bit value with one bit representing
58 * the state of each button. The appropriate button is returned as a boolean
59 * value.
60 *
61 * This method returns true if the button is being held down at the time
62 * that this method is being called.
63 *
64 * @param button The button number to be read (starting at 1)
65 * @return The state of the button.
66 */
67 bool GetRawButton(int button) const;
68
69 /**
70 * Whether the button was pressed since the last check. %Button indexes begin
71 * at 1.
72 *
73 * This method returns true if the button went from not pressed to held down
74 * since the last time this method was called. This is useful if you only
75 * want to call a function once when you press the button.
76 *
77 * @param button The button index, beginning at 1.
78 * @return Whether the button was pressed since the last check.
79 */
80 bool GetRawButtonPressed(int button);
81
82 /**
83 * Whether the button was released since the last check. %Button indexes begin
84 * at 1.
85 *
86 * This method returns true if the button went from held down to not pressed
87 * since the last time this method was called. This is useful if you only
88 * want to call a function once when you release the button.
89 *
90 * @param button The button index, beginning at 1.
91 * @return Whether the button was released since the last check.
92 */
93 bool GetRawButtonReleased(int button);
94
95 /**
96 * Constructs an event instance around this button's digital signal.
97 *
98 * @param button the button index
99 * @param loop the event loop instance to attach the event to.
100 * @return an event instance representing the button's digital signal attached
101 * to the given loop.
102 */
103 BooleanEvent Button(int button, EventLoop* loop) const;
104
105 /**
106 * Get the value of the axis.
107 *
108 * @param axis The axis to read, starting at 0.
109 * @return The value of the axis.
110 */
111 double GetRawAxis(int axis) const;
112
113 /**
114 * Get the angle in degrees of a POV on the HID.
115 *
116 * The POV angles start at 0 in the up direction, and increase clockwise
117 * (e.g. right is 90, upper-left is 315).
118 *
119 * @param pov The index of the POV to read (starting at 0)
120 * @return the angle of the POV in degrees, or -1 if the POV is not pressed.
121 */
122 int GetPOV(int pov = 0) const;
123
124 /**
125 * Constructs a BooleanEvent instance based around this angle of a POV on the
126 * HID.
127 *
128 * <p>The POV angles start at 0 in the up direction, and increase clockwise
129 * (eg right is 90, upper-left is 315).
130 *
131 * @param loop the event loop instance to attach the event to.
132 * @param angle POV angle in degrees, or -1 for the center / not pressed.
133 * @return a BooleanEvent instance based around this angle of a POV on the
134 * HID.
135 */
136 BooleanEvent POV(int angle, EventLoop* loop) const;
137
138 /**
139 * Constructs a BooleanEvent instance based around this angle of a POV on the
140 * HID.
141 *
142 * <p>The POV angles start at 0 in the up direction, and increase clockwise
143 * (eg right is 90, upper-left is 315).
144 *
145 * @param loop the event loop instance to attach the event to.
146 * @param pov index of the POV to read (starting at 0). Defaults to 0.
147 * @param angle POV angle in degrees, or -1 for the center / not pressed.
148 * @return a BooleanEvent instance based around this angle of a POV on the
149 * HID.
150 */
151 BooleanEvent POV(int pov, int angle, EventLoop* loop) const;
152
153 /**
154 * Constructs a BooleanEvent instance based around the 0 degree angle (up) of
155 * the default (index 0) POV on the HID.
156 *
157 * @return a BooleanEvent instance based around the 0 degree angle of a POV on
158 * the HID.
159 */
161
162 /**
163 * Constructs a BooleanEvent instance based around the 45 degree angle (right
164 * up) of the default (index 0) POV on the HID.
165 *
166 * @return a BooleanEvent instance based around the 45 degree angle of a POV
167 * on the HID.
168 */
170
171 /**
172 * Constructs a BooleanEvent instance based around the 90 degree angle (right)
173 * of the default (index 0) POV on the HID.
174 *
175 * @return a BooleanEvent instance based around the 90 degree angle of a POV
176 * on the HID.
177 */
179
180 /**
181 * Constructs a BooleanEvent instance based around the 135 degree angle (right
182 * down) of the default (index 0) POV on the HID.
183 *
184 * @return a BooleanEvent instance based around the 135 degree angle of a POV
185 * on the HID.
186 */
188
189 /**
190 * Constructs a BooleanEvent instance based around the 180 degree angle (down)
191 * of the default (index 0) POV on the HID.
192 *
193 * @return a BooleanEvent instance based around the 180 degree angle of a POV
194 * on the HID.
195 */
197
198 /**
199 * Constructs a BooleanEvent instance based around the 225 degree angle (down
200 * left) of the default (index 0) POV on the HID.
201 *
202 * @return a BooleanEvent instance based around the 225 degree angle of a POV
203 * on the HID.
204 */
206
207 /**
208 * Constructs a BooleanEvent instance based around the 270 degree angle (left)
209 * of the default (index 0) POV on the HID.
210 *
211 * @return a BooleanEvent instance based around the 270 degree angle of a POV
212 * on the HID.
213 */
215
216 /**
217 * Constructs a BooleanEvent instance based around the 315 degree angle (left
218 * up) of the default (index 0) POV on the HID.
219 *
220 * @return a BooleanEvent instance based around the 315 degree angle of a POV
221 * on the HID.
222 */
224
225 /**
226 * Constructs a BooleanEvent instance based around the center (not pressed) of
227 * the default (index 0) POV on the HID.
228 *
229 * @return a BooleanEvent instance based around the center of a POV on the
230 * HID.
231 */
233
234 /**
235 * Constructs an event instance that is true when the axis value is less than
236 * threshold
237 *
238 * @param axis The axis to read, starting at 0.
239 * @param threshold The value below which this trigger should return true.
240 * @param loop the event loop instance to attach the event to.
241 * @return an event instance that is true when the axis value is less than the
242 * provided threshold.
243 */
244 BooleanEvent AxisLessThan(int axis, double threshold, EventLoop* loop) const;
245
246 /**
247 * Constructs an event instance that is true when the axis value is greater
248 * than threshold
249 *
250 * @param axis The axis to read, starting at 0.
251 * @param threshold The value above which this trigger should return true.
252 * @param loop the event loop instance to attach the event to.
253 * @return an event instance that is true when the axis value is greater than
254 * the provided threshold.
255 */
256 BooleanEvent AxisGreaterThan(int axis, double threshold,
257 EventLoop* loop) const;
258
259 /**
260 * Get the number of axes for the HID.
261 *
262 * @return the number of axis for the current HID
263 */
264 int GetAxisCount() const;
265
266 /**
267 * Get the number of POVs for the HID.
268 *
269 * @return the number of POVs for the current HID
270 */
271 int GetPOVCount() const;
272
273 /**
274 * Get the number of buttons for the HID.
275 *
276 * @return the number of buttons on the current HID
277 */
278 int GetButtonCount() const;
279
280 /**
281 * Get if the HID is connected.
282 *
283 * @return true if the HID is connected
284 */
285 bool IsConnected() const;
286
287 /**
288 * Get the type of the HID.
289 *
290 * @return the type of the HID.
291 */
293
294 /**
295 * Get the name of the HID.
296 *
297 * @return the name of the HID.
298 */
299 std::string GetName() const;
300
301 /**
302 * Get the axis type of a joystick axis.
303 *
304 * @return the axis type of a joystick axis.
305 */
306 int GetAxisType(int axis) const;
307
308 /**
309 * Get the port number of the HID.
310 *
311 * @return The port number of the HID.
312 */
313 int GetPort() const;
314
315 /**
316 * Set a single HID output value for the HID.
317 *
318 * @param outputNumber The index of the output to set (1-32)
319 * @param value The value to set the output to
320 */
321 void SetOutput(int outputNumber, bool value);
322
323 /**
324 * Set all output values for the HID.
325 *
326 * @param value The 32 bit output value (1 bit for each output)
327 */
328 void SetOutputs(int value);
329
330 /**
331 * Set the rumble output for the HID.
332 *
333 * The DS currently supports 2 rumble values, left rumble and right rumble.
334 *
335 * @param type Which rumble value to set
336 * @param value The normalized value (0 to 1) to set the rumble to
337 */
339
340 private:
341 int m_port;
342 int m_outputs = 0;
343 uint16_t m_leftRumble = 0;
344 uint16_t m_rightRumble = 0;
345};
346
347} // namespace frc
This class provides an easy way to link actions to inputs.
Definition: BooleanEvent.h:31
The loop polling BooleanEvent objects and executing the actions bound to them.
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
Definition: GenericHID.h:26
@ kBothRumble
Definition: GenericHID.h:26
@ kLeftRumble
Definition: GenericHID.h:26
@ kRightRumble
Definition: GenericHID.h:26
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
Definition: GenericHID.h:28
@ kXInputArcadeStick
Definition: GenericHID.h:33
@ kXInputDrumKit
Definition: GenericHID.h:38
@ kHIDJoystick
Definition: GenericHID.h:41
@ kHIDGamepad
Definition: GenericHID.h:42
@ kXInputWheel
Definition: GenericHID.h:32
@ kHID1stPerson
Definition: GenericHID.h:45
@ kXInputGuitar
Definition: GenericHID.h:36
@ kXInputFlightStick
Definition: GenericHID.h:34
@ kXInputArcadePad
Definition: GenericHID.h:40
@ kUnknown
Definition: GenericHID.h:29
@ kHIDFlight
Definition: GenericHID.h:44
@ kXInputGuitar3
Definition: GenericHID.h:39
@ kXInputGuitar2
Definition: GenericHID.h:37
@ kXInputDancePad
Definition: GenericHID.h:35
@ kHIDDriving
Definition: GenericHID.h:43
@ kXInputGamepad
Definition: GenericHID.h:31
@ kXInputUnknown
Definition: GenericHID.h:30
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...
Definition: core.h:1240
type
Definition: core.h:575
::uint16_t uint16_t
Definition: Meta.h:54
Definition: AprilTagFieldLayout.h:22