librmb 1.0
Rambunction 4330 Utility Library
Loading...
Searching...
No Matches
LogitechGamepad.h
1#pragma once
2
3#include "wpi/MathExtras.h"
4#include <frc2/command/button/CommandGenericHID.h>
5#include <frc2/command/button/Trigger.h>
6
7#include <iostream>
8
9namespace rmb {
10
11class LogitechGamepad : public frc2::CommandGenericHID {
12public:
13 struct Axes {
14 constexpr static int leftX = 1;
15 constexpr static int leftY = 0;
16 constexpr static int leftTrigger = 2;
17
18 constexpr static int rightX = 5;
19 constexpr static int rightY = 4;
20 constexpr static int rightTrigger = 3;
21 };
22
23 struct Buttons {
24 constexpr static int leftStick = 9;
25 constexpr static int rightStick = 10;
26
27 constexpr static int rightBumper = 6;
28 constexpr static int leftBumper = 5;
29
30 constexpr static int X = 3;
31 constexpr static int Y = 4;
32 constexpr static int A = 1;
33 constexpr static int B = 2;
34
35 constexpr static int backButton = 7;
36 constexpr static int startButton = 8;
37 };
38
39 LogitechGamepad(int channel, double deadZone = 0.0,
40 bool squareOutputs = false)
41 : frc2::CommandGenericHID(channel), deadZone(deadZone),
42 squareOutputs(squareOutputs) {}
43
44 double GetLeftX() const {
45 double raw = -GetRawAxis(Axes::leftX);
46 if (std::abs(raw) < deadZone) {
47 return 0.0;
48 }
49
50 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
51 (1.0 - deadZone); // Normalize from range 0.0-1.0
52
53 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
54 }
55 frc2::Trigger LeftXLessThan(double threshold) const {
56 return AxisLessThan(Axes::leftX, threshold);
57 }
58 frc2::Trigger LeftXGreaterThan(double threshold) const {
59 return AxisGreaterThan(Axes::leftX, threshold);
60 }
61
62 double GetLeftY() const {
63 double raw = GetRawAxis(Axes::leftY);
64 if (std::abs(raw) < deadZone) {
65 return 0.0;
66 }
67
68 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
69 (1.0 - deadZone); // Normalize from range 0.0-1.0
70
71 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
72 }
73
74 frc2::Trigger LeftYLessThan(double threshold) const {
75 return AxisLessThan(Axes::leftY, threshold);
76 }
77 frc2::Trigger LeftYgreaterThan(double threshold) const {
78 return AxisGreaterThan(Axes::leftY, threshold);
79 }
80
81 bool GetLeftStickButton() const { return GetRawButton(Buttons::leftStick); }
82 bool GetLeftStickButtonPressed() {
83 return GetRawButtonPressed(Buttons::leftStick);
84 }
85 bool GetLeftStickButtonReleased() {
86 return GetRawButtonPressed(Buttons::leftStick);
87 }
88 frc2::Trigger LeftStickButton() const { return Button(Buttons::leftStick); }
89
90 double GetRightX() const {
91 double raw = -GetRawAxis(Axes::rightX);
92 if (std::abs(raw) < deadZone) {
93 return 0.0;
94 }
95
96 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
97 (1.0 - deadZone); // Normalize from range 0.0-1.0
98
99 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
100 }
101
102 frc2::Trigger RightXLessThan(double threshold) const {
103 return AxisLessThan(Axes::rightX, threshold);
104 }
105 frc2::Trigger RightXgreaterThan(double threshold) const {
106 return AxisGreaterThan(Axes::rightX, threshold);
107 }
108
109 double GetRightY() const {
110 double raw = GetRawAxis(Axes::rightY);
111 if (std::abs(raw) < deadZone) {
112 return 0.0;
113 }
114
115 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
116 (1.0 - deadZone); // Normalize from range 0.0-1.0
117
118 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
119 }
120
121 frc2::Trigger RightYLessThan(double threshold) const {
122 return AxisLessThan(Axes::rightY, threshold);
123 }
124 frc2::Trigger RightYGearterThan(double threshold) const {
125 return AxisGreaterThan(Axes::rightY, threshold);
126 }
127
128 bool GetRightStickButton() const { return GetRawButton(Buttons::rightStick); }
129 bool GetRightStickButtonPressed() {
130 return GetRawButtonPressed(Buttons::rightStick);
131 }
132 bool GetRightStickButtonReleased() {
133 return GetRawButtonPressed(Buttons::rightStick);
134 }
135 frc2::Trigger RightStickButton() const { return Button(Buttons::rightStick); }
136
137 double GetLeftTrigger() const {
138 double raw = GetRawAxis(Axes::leftTrigger);
139 if (std::abs(raw) < deadZone) {
140 return 0.0;
141 }
142
143 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
144 (1.0 - deadZone); // Normalize from range 0.0-1.0
145
146 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
147 }
148 frc2::Trigger LeftTriggerLessThan(double threshold) const {
149 return AxisLessThan(Axes::leftTrigger, threshold);
150 }
151 frc2::Trigger LeftTriggergreaterThan(double threshold) const {
152 return AxisGreaterThan(Axes::leftTrigger, threshold);
153 }
154
155 double GetRightTrigger() const {
156 double raw = GetRawAxis(Axes::rightTrigger);
157 if (std::abs(raw) < deadZone) {
158 return 0.0;
159 }
160
161 raw = wpi::sgn(raw) * (std::abs(raw) - deadZone) /
162 (1.0 - deadZone); // Normalize from range 0.0-1.0
163
164 return squareOutputs ? std::copysign(raw * raw, raw) : raw;
165 }
166 frc2::Trigger RightTriggerLessThan(double threshold) const {
167 return AxisLessThan(Axes::rightTrigger, threshold);
168 }
169 frc2::Trigger RightTriggerGreaterThan(double threshold) const {
170 return AxisGreaterThan(Axes::rightTrigger, threshold);
171 }
172
173 bool GetLeftBumper() const { return GetRawButton(Buttons::leftBumper); }
174 bool GetLeftBumperPressed() {
175 return GetRawButtonPressed(Buttons::leftBumper);
176 }
177 bool GetLeftBumperReleased() {
178 return GetRawButtonPressed(Buttons::leftBumper);
179 }
180 frc2::Trigger LeftBumper() const { return Button(Buttons::leftBumper); }
181
182 bool GetRightBumper() const { return GetRawButton(Buttons::rightBumper); }
183 bool GetRightBumperPressed() {
184 return GetRawButtonPressed(Buttons::rightBumper);
185 }
186 bool GetRightBumperReleased() {
187 return GetRawButtonPressed(Buttons::rightBumper);
188 }
189 frc2::Trigger RightBumper() const { return Button(Buttons::rightBumper); }
190
191 bool GetX() const { return GetRawButton(Buttons::X); }
192 bool GetXPressed() { return GetRawButtonPressed(Buttons::X); }
193 bool GetXReleased() { return GetRawButtonReleased(Buttons::X); }
194 frc2::Trigger X() const { return Button(Buttons::X); }
195
196 bool GetY() const { return GetRawButton(Buttons::Y); }
197 bool GetYPressed() { return GetRawButtonPressed(Buttons::Y); }
198 bool GetYReleased() { return GetRawButtonReleased(Buttons::Y); }
199 frc2::Trigger Y() const { return Button(Buttons::Y); }
200
201 bool GetA() const { return GetRawButton(Buttons::A); }
202 bool GetAPressed() { return GetRawButtonPressed(Buttons::A); }
203 bool GetAReleased() { return GetRawButtonReleased(Buttons::A); }
204 frc2::Trigger A() const { return Button(Buttons::A); }
205
206 bool GetB() const { return GetRawButton(Buttons::B); }
207 bool GetBPressed() { return GetRawButtonPressed(Buttons::B); }
208 bool GetBReleased() { return GetRawButtonReleased(Buttons::B); }
209 frc2::Trigger B() const { return Button(Buttons::B); }
210
211 bool GetBackButton() const { return GetRawButton(Buttons::backButton); }
212 bool GetBackButtonPressed() {
213 return GetRawButtonPressed(Buttons::backButton);
214 }
215 bool GetBackButtonReleased() {
216 return GetRawButtonReleased(Buttons::backButton);
217 }
218 frc2::Trigger BackButton() const { return Button(Buttons::backButton); }
219
220 bool GetStartButton() const { return GetRawButton(Buttons::startButton); }
221 bool GetStartButtonPressed() {
222 return GetRawButtonPressed(Buttons::startButton);
223 }
224 bool GetStartButtonReleased() {
225 return GetRawButtonReleased(Buttons::startButton);
226 }
227 frc2::Trigger StartButton() const { return Button(Buttons::startButton); }
228
229private:
230 double deadZone;
231 bool squareOutputs;
232};
233} // namespace rmb
Definition LogitechGamepad.h:11
the master namespace of librmb
Definition LogitechGamepad.h:9
Definition LogitechGamepad.h:13
Definition LogitechGamepad.h:23