2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
package org.wpilib.simulation;
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2026-03-17 17:19:58 -07:00
|
|
|
import java.util.EnumSet;
|
2025-11-07 19:55:43 -05:00
|
|
|
import org.wpilib.driverstation.GenericHID;
|
2026-04-18 19:56:45 -07:00
|
|
|
import org.wpilib.driverstation.POVDirection;
|
2020-07-15 00:33:57 -07:00
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Class to control a simulated generic joystick. */
|
2020-07-15 00:33:57 -07:00
|
|
|
public class GenericHIDSim {
|
2024-01-05 07:35:59 -08:00
|
|
|
/** GenericHID port. */
|
2020-07-15 00:33:57 -07:00
|
|
|
protected final int m_port;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs from a GenericHID object.
|
|
|
|
|
*
|
|
|
|
|
* @param joystick joystick to simulate
|
|
|
|
|
*/
|
|
|
|
|
public GenericHIDSim(GenericHID joystick) {
|
|
|
|
|
m_port = joystick.getPort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs from a joystick port number.
|
|
|
|
|
*
|
|
|
|
|
* @param port port number
|
|
|
|
|
*/
|
|
|
|
|
public GenericHIDSim(int port) {
|
|
|
|
|
m_port = port;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 22:45:16 -08:00
|
|
|
/** Updates joystick data so that new values are visible to the user program. */
|
2020-07-15 00:33:57 -07:00
|
|
|
public void notifyNewData() {
|
|
|
|
|
DriverStationSim.notifyNewData();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the value of a given button.
|
|
|
|
|
*
|
|
|
|
|
* @param button the button to set
|
|
|
|
|
* @param value the new value
|
|
|
|
|
*/
|
2020-07-15 00:33:57 -07:00
|
|
|
public void setRawButton(int button, boolean value) {
|
|
|
|
|
DriverStationSim.setJoystickButton(m_port, button, value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the value of a given axis.
|
|
|
|
|
*
|
|
|
|
|
* @param axis the axis to set
|
|
|
|
|
* @param value the new value
|
|
|
|
|
*/
|
2020-07-15 00:33:57 -07:00
|
|
|
public void setRawAxis(int axis, double value) {
|
|
|
|
|
DriverStationSim.setJoystickAxis(m_port, axis, value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the value of a given POV.
|
|
|
|
|
*
|
|
|
|
|
* @param pov the POV to set
|
|
|
|
|
* @param value the new value
|
|
|
|
|
*/
|
2026-04-18 19:56:45 -07:00
|
|
|
public void setPOV(int pov, POVDirection value) {
|
2020-07-15 00:33:57 -07:00
|
|
|
DriverStationSim.setJoystickPOV(m_port, pov, value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the value of the default POV (port 0).
|
|
|
|
|
*
|
|
|
|
|
* @param value the new value
|
|
|
|
|
*/
|
2026-04-18 19:56:45 -07:00
|
|
|
public void setPOV(POVDirection value) {
|
2020-07-15 00:33:57 -07:00
|
|
|
setPOV(0, value);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-25 23:03:50 -07:00
|
|
|
/**
|
|
|
|
|
* Set the maximum axis index for this device.
|
|
|
|
|
*
|
|
|
|
|
* @param maximumIndex the new maximum axis index
|
|
|
|
|
*/
|
|
|
|
|
public void setAxesMaximumIndex(int maximumIndex) {
|
|
|
|
|
DriverStationSim.setJoystickAxesMaximumIndex(m_port, maximumIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the axis count of this device.
|
|
|
|
|
*
|
|
|
|
|
* @param count the new axis count
|
|
|
|
|
*/
|
2025-10-25 23:03:50 -07:00
|
|
|
public void setAxesAvailable(int count) {
|
|
|
|
|
DriverStationSim.setJoystickAxesAvailable(m_port, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the maximum POV index for this device.
|
|
|
|
|
*
|
|
|
|
|
* @param maximumIndex the new maximum POV index
|
|
|
|
|
*/
|
|
|
|
|
public void setPOVsMaximumIndex(int maximumIndex) {
|
|
|
|
|
DriverStationSim.setJoystickPOVsMaximumIndex(m_port, maximumIndex);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the POV count of this device.
|
|
|
|
|
*
|
|
|
|
|
* @param count the new POV count
|
|
|
|
|
*/
|
2025-10-25 23:03:50 -07:00
|
|
|
public void setPOVsAvailable(int count) {
|
|
|
|
|
DriverStationSim.setJoystickPOVsAvailable(m_port, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the maximum button index for this device.
|
|
|
|
|
*
|
|
|
|
|
* @param maximumIndex the new maximum button index
|
|
|
|
|
*/
|
|
|
|
|
public void setButtonsMaximumIndex(int maximumIndex) {
|
|
|
|
|
DriverStationSim.setJoystickButtonsMaximumIndex(m_port, maximumIndex);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the button count of this device.
|
|
|
|
|
*
|
|
|
|
|
* @param count the new button count
|
|
|
|
|
*/
|
2025-10-25 23:03:50 -07:00
|
|
|
public void setButtonsAvailable(long count) {
|
|
|
|
|
DriverStationSim.setJoystickButtonsAvailable(m_port, count);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Set the type of this device.
|
|
|
|
|
*
|
|
|
|
|
* @param type the new device type
|
|
|
|
|
*/
|
2025-11-17 14:36:14 -08:00
|
|
|
public void setGamepadType(GenericHID.HIDType type) {
|
|
|
|
|
DriverStationSim.setJoystickGamepadType(m_port, type.value);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-11-17 14:36:14 -08:00
|
|
|
* Set the supported outputs of this device.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-11-17 14:36:14 -08:00
|
|
|
* @param supportedOutputs the new supported outputs
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2026-03-17 17:19:58 -07:00
|
|
|
public void setSupportedOutputs(EnumSet<GenericHID.SupportedOutput> supportedOutputs) {
|
|
|
|
|
int supportedOutputsInt = 0;
|
|
|
|
|
for (GenericHID.SupportedOutput output : supportedOutputs) {
|
|
|
|
|
supportedOutputsInt |= output.getValue();
|
|
|
|
|
}
|
|
|
|
|
DriverStationSim.setJoystickSupportedOutputs(m_port, supportedOutputsInt);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-11-17 14:36:14 -08:00
|
|
|
* Set the name of this device.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-11-17 14:36:14 -08:00
|
|
|
* @param name the new device name
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-11-17 14:36:14 -08:00
|
|
|
public void setName(String name) {
|
|
|
|
|
DriverStationSim.setJoystickName(m_port, name);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
2025-11-17 14:36:14 -08:00
|
|
|
* Get the led color set.
|
2021-01-11 21:55:45 -08:00
|
|
|
*
|
2025-11-17 14:36:14 -08:00
|
|
|
* @return the led color set
|
2021-01-11 21:55:45 -08:00
|
|
|
*/
|
2025-11-17 14:36:14 -08:00
|
|
|
public int getLeds() {
|
|
|
|
|
return DriverStationSim.getJoystickLeds(m_port);
|
2020-07-15 00:33:57 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 21:55:45 -08:00
|
|
|
/**
|
|
|
|
|
* Get the joystick rumble.
|
|
|
|
|
*
|
|
|
|
|
* @param type the rumble to read
|
|
|
|
|
* @return the rumble value
|
|
|
|
|
*/
|
2020-07-15 00:33:57 -07:00
|
|
|
public double getRumble(GenericHID.RumbleType type) {
|
2025-12-06 09:17:48 -08:00
|
|
|
int intType =
|
|
|
|
|
switch (type) {
|
2026-03-17 17:19:58 -07:00
|
|
|
case LEFT_RUMBLE -> 0;
|
|
|
|
|
case RIGHT_RUMBLE -> 1;
|
|
|
|
|
case LEFT_TRIGGER_RUMBLE -> 2;
|
|
|
|
|
case RIGHT_TRIGGER_RUMBLE -> 3;
|
2025-12-06 09:17:48 -08:00
|
|
|
};
|
2025-11-17 14:36:14 -08:00
|
|
|
int value = DriverStationSim.getJoystickRumble(m_port, intType);
|
2020-07-15 00:33:57 -07:00
|
|
|
return value / 65535.0;
|
|
|
|
|
}
|
|
|
|
|
}
|