2022-01-15 14:22:45 -07:00
|
|
|
package frc4388.utility.controller;
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import edu.wpi.first.hal.HAL;
|
2022-01-15 13:12:31 -07:00
|
|
|
|
|
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public class XboxControllerRaw implements IHandController {
|
|
|
|
|
public static final int LEFT_X_AXIS = 0;
|
|
|
|
|
public static final int LEFT_Y_AXIS = 1;
|
|
|
|
|
public static final int LEFT_TRIGGER_AXIS = 2;
|
|
|
|
|
public static final int RIGHT_TRIGGER_AXIS = 3;
|
|
|
|
|
public static final int RIGHT_X_AXIS = 4;
|
|
|
|
|
public static final int RIGHT_Y_AXIS = 5;
|
|
|
|
|
public static final int LEFT_RIGHT_DPAD_AXIS = 6;
|
|
|
|
|
public static final int TOP_BOTTOM_DPAD_AXIS = 6;
|
|
|
|
|
|
|
|
|
|
public static final int A_BUTTON = 0;
|
|
|
|
|
public static final int B_BUTTON = 1;
|
|
|
|
|
public static final int X_BUTTON = 2;
|
|
|
|
|
public static final int Y_BUTTON = 3;
|
|
|
|
|
public static final int LEFT_BUMPER_BUTTON = 4;
|
|
|
|
|
public static final int RIGHT_BUMPER_BUTTON = 5;
|
|
|
|
|
public static final int BACK_BUTTON = 6;
|
|
|
|
|
public static final int START_BUTTON = 7;
|
|
|
|
|
|
|
|
|
|
public static final int LEFT_JOYSTICK_BUTTON = 8;
|
|
|
|
|
public static final int RIGHT_JOYSTICK_BUTTON = 9;
|
2022-01-15 14:31:09 -07:00
|
|
|
|
2022-01-15 15:52:38 -07:00
|
|
|
private static final double DEADZONE = 0.08;
|
2022-01-15 13:12:31 -07:00
|
|
|
|
2022-01-15 15:52:38 -07:00
|
|
|
private int m_ID;
|
2022-01-15 14:22:45 -07:00
|
|
|
private int m_buttons = 0;
|
|
|
|
|
private int m_numButtons;
|
|
|
|
|
private ByteBuffer m_buttonCountBuffer = ByteBuffer.allocateDirect(1);
|
|
|
|
|
float[] m_axes = new float[HAL.kMaxJoystickAxes];
|
2022-01-15 15:52:38 -07:00
|
|
|
|
|
|
|
|
public XboxControllerRaw(int id) {
|
|
|
|
|
m_ID = id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public void updateInput() {
|
2022-01-15 15:52:38 -07:00
|
|
|
HAL.getJoystickAxes((byte) m_ID, m_axes);
|
|
|
|
|
m_buttons = HAL.getJoystickButtons((byte) m_ID, m_buttonCountBuffer);
|
2022-01-15 14:22:45 -07:00
|
|
|
m_numButtons = m_buttonCountBuffer.get(0);
|
|
|
|
|
}
|
2022-01-15 13:12:31 -07:00
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public int getNumButtons() {
|
|
|
|
|
return m_numButtons;
|
|
|
|
|
}
|
2022-01-15 13:12:31 -07:00
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public boolean getButton(int index) {
|
|
|
|
|
return (m_buttons & 1 << index) > 0;
|
|
|
|
|
}
|
2022-01-15 14:31:09 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks if the input falls within the deadzone.
|
|
|
|
|
* @param input from an axis on the controller
|
|
|
|
|
* @return true if input falls in deadzone, false if input falls outside deadzone
|
|
|
|
|
*/
|
|
|
|
|
public static boolean inDeadZone(double input){
|
|
|
|
|
return (Math.abs(input) < DEADZONE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates an input to have a deadzone around the 0 position
|
|
|
|
|
* @param input from an axis on the controller
|
|
|
|
|
* @return updated input
|
|
|
|
|
*/
|
|
|
|
|
private double getAxisWithDeadZoneCheck(double input){
|
|
|
|
|
if(inDeadZone(input)){
|
|
|
|
|
return 0.0;
|
|
|
|
|
} else {
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public double getLeftXAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[LEFT_X_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
2022-01-15 13:12:31 -07:00
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public double getLeftYAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[LEFT_Y_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
2022-01-15 13:12:31 -07:00
|
|
|
|
2022-01-15 14:22:45 -07:00
|
|
|
public double getRightXAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[RIGHT_X_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getRightYAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[RIGHT_Y_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getLeftTriggerAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[LEFT_TRIGGER_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getRightTriggerAxis() {
|
2022-01-15 14:31:09 -07:00
|
|
|
return getAxisWithDeadZoneCheck(m_axes[RIGHT_TRIGGER_AXIS]);
|
2022-01-15 14:22:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDpadAngle() {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|