RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
JoystickBase.java
1/*
2 * Copyright (C) 2016 John H. Gaby
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Contact: robotics@gabysoft.com
17 */
18
19package robotCore;
20
21
32
33public class JoystickBase extends GenericHID
34{
35 static JoystickBase m_rootJoystick = null;
36 JoystickBase m_joystick;
37 boolean m_gamepad = false;
38 double m_x = 0;
39 double m_y = 0;
40 double m_rx = 0;
41 double m_ry = 0;
42 double m_throttle = 0;
43 double m_pov = 0;
44 double m_twist = 0;
45 int m_buttons = 0;
46
48 public static JoystickBase getInstance()
49 {
50 if (m_rootJoystick == null)
51 {
52 new JoystickBase();
53 }
54
55 return(m_rootJoystick);
56 }
58
59 private JoystickBase()
60 {
61 if (m_rootJoystick == null)
62 {
63 m_rootJoystick = this;
64 }
65
66 m_joystick = m_rootJoystick;
67 }
68
73 public JoystickBase(int id)
74 {
75 m_joystick = getInstance();
76 }
77
78 public void getAllJoystickData(
79 int stick,
80 float[] axesArray,
81 short[] rawAxesArray,
82 byte[] povsArray,
83 float[] touchpadFingersArray,
84 long[] buttonsAndMetadata) {
85
86 java.util.Arrays.fill(axesArray, 0f);
87 java.util.Arrays.fill(rawAxesArray, (short) 0);
88 java.util.Arrays.fill(povsArray, (byte) 0);
89 java.util.Arrays.fill(touchpadFingersArray, 0f);
90 java.util.Arrays.fill(buttonsAndMetadata, 0L);
91
92 if (isGamepad()) {
93 axesArray[0] = (float) getX();
94 axesArray[1] = (float) getY();
95 axesArray[2] = (float) getRX();
96 axesArray[3] = (float) getRY();
97 // TODO - need left & right trigger
98 } else {
99 axesArray[0] = (float) getX();
100 axesArray[1] = (float) getY();
101 axesArray[2] = (float) getTwist();
102 axesArray[3] = (float) getThrottle();
103 }
104
105 buttonsAndMetadata[0] = 0b01111; // Only support the 4 axis at this time (TODO - change if left & right trigger added)
106 buttonsAndMetadata[2] = 0b0111111111111; // Support 12 buttons
107 buttonsAndMetadata[3] = m_buttons; // Buttons states
108
109 // if (m_joystick.m_buttons != 0)
110 // {
111 // Logger.log("JoystickBase", 1, String.format("buttons = 0x%x", m_buttons));
112 // }
113
114 }
115
119 public boolean isGamepad()
120 {
121 // System.out.println(String.format("m_gamepad = %b", m_joystick.m_gamepad));
122 return(m_joystick.m_gamepad);
123 }
124
128 @Override
129 public double getX()
130 {
131 return(m_joystick.m_x);
132 }
133
137 @Override
138 public double getY()
139 {
140 return(m_joystick.m_y);
141 }
142
147 public double getRX()
148 {
149 return(m_joystick.m_rx);
150 }
151
156 public double getRY()
157 {
158 return(m_joystick.m_ry);
159 }
160
168 public double getThrottle()
169 {
170 return(m_joystick.m_throttle);
171 }
172
177 public double getTwist() {
178 return(m_joystick.m_twist);
179 }
180
185 public double getPOV()
186 {
187 return(m_joystick.m_pov);
188 }
189
193 @Override
194 public boolean getRawButton(final int button)
195 {
196 if (button > 0)
197 {
198 return((m_joystick.m_buttons & (1 << (button - 1))) != 0);
199 }
200
201 return(false);
202 }
203
205 public void setData(boolean gamepad, double x, double y, double rx, double ry, double throttle, double pov, double twist, int buttons)
206 {
207 // System.out.println(String.format("gamepad=%b", gamepad));
208
209 m_gamepad = gamepad;
210 m_x = x;
211 m_y = y;
212 m_rx = rx;
213 m_ry = ry;
214 m_throttle = throttle;
215 m_twist = twist;
216 m_pov = pov;
217 m_buttons = buttons;
218 }
220}
The GeneriacHID class is the base class for input devices such as joysticks.
boolean getRawButton(final int button)