2022-Robot
Robot code for 2022 FRC Season by Argos, FRC team #1756
Loading...
Searching...
No Matches
xbox_controller.h
Go to the documentation of this file.
1
4
5#pragma once
6
7#include <frc/GenericHID.h>
8
9#include <array>
10
12#include "vibration.h"
13
14namespace argos_lib {
15
16 class XboxController : public frc::GenericHID {
17 public:
22
23 enum class Button {
24 kA = 1,
25 kB = 2,
26 kX = 3,
27 kY = 4,
28 kBumperLeft = 5,
29 kBumperRight = 6,
30 kBack = 7,
31 kStart = 8,
32 kStickLeft = 9,
33 kStickRight = 10,
34 kLeftTrigger = 11,
35 kRightTrigger = 12,
36 kUp = 13,
37 kRight = 14,
38 kDown = 15,
39 kLeft = 16,
40 COUNT
41 };
42
46 struct UpdateStatus {
47 bool pressed = false;
48 bool released = false;
49 bool debouncePress = false;
50 bool debounceRelease = false;
51 bool rawActive = false;
52 bool debounceActive = false;
53 };
54
55 enum class Axis { kLeftX = 0, kLeftY = 1, kLeftTrigger = 2, kRightTrigger = 3, kRightX = 4, kRightY = 5, COUNT };
56
57 XboxController() = delete;
63 explicit XboxController(int port);
64
71 void SetButtonDebounce(Button targetButton, DebounceSettings newSettings);
72
79 void SwapSettings(XboxController& other);
80
87 [[nodiscard]] double GetX(JoystickHand hand) const;
94 [[nodiscard]] double GetY(JoystickHand hand) const;
101 [[nodiscard]] double GetTriggerAxis(JoystickHand hand) const;
102
109 [[nodiscard]] bool GetDebouncedButton(Button buttonIdx);
116 [[nodiscard]] bool GetDebouncedButtonPressed(Button buttonIdx);
123 [[nodiscard]] bool GetDebouncedButtonReleased(Button buttonIdx);
124
133 [[nodiscard]] bool GetDebouncedButton(std::initializer_list<Button> buttonCombo);
142 [[nodiscard]] bool GetDebouncedButtonPressed(std::initializer_list<Button> buttonCombo);
151 [[nodiscard]] bool GetDebouncedButtonReleased(std::initializer_list<Button> buttonCombo);
152
159 [[nodiscard]] bool GetRawButton(Button buttonIdx);
166 [[nodiscard]] bool GetRawButtonPressed(Button buttonIdx);
173 [[nodiscard]] bool GetRawButtonReleased(Button buttonIdx);
174
183 [[nodiscard]] bool GetRawButton(std::initializer_list<Button> buttonCombo);
191 [[nodiscard]] bool GetRawButtonPressed(std::initializer_list<Button> buttonCombo);
199 [[nodiscard]] bool GetRawButtonReleased(std::initializer_list<Button> buttonCombo);
200
207
213 void SetVibration(VibrationModel newVibrationModel);
214
218 void UpdateVibration();
219
228
229 private:
233 struct DPadButtons {
234 bool up = false;
235 bool right = false;
236 bool down = false;
237 bool left = false;
238 };
239
246
247 constexpr static double analogTriggerThresh = 0.5;
248
249 std::array<DebounceSettings, static_cast<int>(Button::COUNT)> m_buttonDebounceSettings;
250 std::array<bool, static_cast<int>(Button::COUNT)> m_buttonDebounceStatus;
251 std::array<bool, static_cast<int>(Button::COUNT)> m_rawButtonStatus;
252 std::array<std::chrono::time_point<std::chrono::steady_clock>, static_cast<int>(Button::COUNT)>
254
256 };
257
258} // namespace argos_lib
Definition: xbox_controller.h:16
void SetVibration(VibrationModel newVibrationModel)
Sets a new vibration pattern and updates vibration output based on that new model.
Definition: xbox_controller.cpp:135
Button
Definition: xbox_controller.h:23
bool GetDebouncedButtonReleased(Button buttonIdx)
Detect if a button just transitioned from active to inactive after applying debounce.
Definition: xbox_controller.cpp:51
std::array< bool, static_cast< int >(Button::COUNT)> m_buttonDebounceStatus
Definition: xbox_controller.h:250
void SwapSettings(XboxController &other)
Swap all configurations (debounce, etc) between this and other controller. Useful in conjunction with...
Definition: xbox_controller.cpp:20
VibrationModel m_vibrationModel
Active vibration model.
Definition: xbox_controller.h:255
void UpdateVibration()
Update vibration output based on current vibration model.
Definition: xbox_controller.cpp:140
std::array< std::chrono::time_point< std::chrono::steady_clock >, static_cast< int >(Button::COUNT)> m_buttonDebounceTransitionTime
Time when new value was first seen.
Definition: xbox_controller.h:253
std::array< bool, static_cast< int >(Button::COUNT)> m_rawButtonStatus
Definition: xbox_controller.h:251
JoystickHand
Replaces legacy joystick hand API for WPILib.
Definition: xbox_controller.h:21
bool GetRawButtonPressed(Button buttonIdx)
Detect if a button just transitioned from inactive to active.
Definition: xbox_controller.cpp:91
UpdateStatus UpdateButton(Button buttonIdx)
Determines the new status of a button. This is used by the other status retrieval functions.
Definition: xbox_controller.cpp:146
std::array< DebounceSettings, static_cast< int >(Button::COUNT)> m_buttonDebounceSettings
Definition: xbox_controller.h:249
static constexpr double analogTriggerThresh
Percent trigger pressed to consider as a button press.
Definition: xbox_controller.h:247
double GetY(JoystickHand hand) const
Get Y joystick percent from specified joystick.
Definition: xbox_controller.cpp:35
VibrationModel GetVibration() const
Get the active vibration model.
Definition: xbox_controller.cpp:131
bool GetRawButtonReleased(Button buttonIdx)
Detect if a button just transitioned from active to inactive.
Definition: xbox_controller.cpp:95
DPadButtons GetPOVButtons()
Convert POV angle to usable DPad button values.
Definition: xbox_controller.cpp:229
bool GetRawButton(Button buttonIdx)
Get the status of button.
Definition: xbox_controller.cpp:87
double GetTriggerAxis(JoystickHand hand) const
Get percent from specified controller trigger button.
Definition: xbox_controller.cpp:39
bool GetDebouncedButton(Button buttonIdx)
Get the status of button after applying debounce.
Definition: xbox_controller.cpp:43
void SetButtonDebounce(Button targetButton, DebounceSettings newSettings)
Configure debounce for a specified button.
Definition: xbox_controller.cpp:16
double GetX(JoystickHand hand) const
Get X joystick percent from specified joystick.
Definition: xbox_controller.cpp:31
bool GetDebouncedButtonPressed(Button buttonIdx)
Detect if a button just transitioned from inactive to active after applying debounce.
Definition: xbox_controller.cpp:47
Axis
Definition: xbox_controller.h:55
Definition: swap_controllers_command.h:12
std::function< VibrationStatus()> VibrationModel
Definition: vibration.h:21
Definition: debounce_settings.h:11
Parsed directional pad button states.
Definition: xbox_controller.h:233
bool left
Left active (including adjacent diagonals)
Definition: xbox_controller.h:237
bool down
Down active (including adjacent diagonals)
Definition: xbox_controller.h:236
bool up
Up active (including adjacent diagonals)
Definition: xbox_controller.h:234
bool right
Right active (including adjacent diagonals)
Definition: xbox_controller.h:235
State of an individual button.
Definition: xbox_controller.h:46
bool debounceActive
Button status after debounce applied.
Definition: xbox_controller.h:52
bool pressed
Transitioned from inactive to active.
Definition: xbox_controller.h:47
bool debouncePress
Transitioned from inactive to active after debounce applied.
Definition: xbox_controller.h:49
bool debounceRelease
Transitioned from active to inactive after debounce applied.
Definition: xbox_controller.h:50
bool rawActive
Raw button status.
Definition: xbox_controller.h:51
bool released
Transitioned from active to inactive.
Definition: xbox_controller.h:48