diff --git a/hal/include/HAL/HAL.hpp b/hal/include/HAL/HAL.hpp index c1605d9748..ea5ff402f7 100644 --- a/hal/include/HAL/HAL.hpp +++ b/hal/include/HAL/HAL.hpp @@ -191,7 +191,7 @@ struct HALJoystickDescriptor { uint8_t type; char name[256]; uint8_t axisCount; - uint8_t axisTypes; + uint8_t axisTypes[kMaxJoystickAxes]; uint8_t buttonCount; uint8_t povCount; }; @@ -231,6 +231,10 @@ extern "C" int HALGetJoystickPOVs(uint8_t joystickNum, HALJoystickPOVs *povs); int HALGetJoystickButtons(uint8_t joystickNum, HALJoystickButtons *buttons); int HALGetJoystickDescriptor(uint8_t joystickNum, HALJoystickDescriptor *desc); + int HALGetJoystickIsXbox(uint8_t joystickNum); + int HALGetJoystickType(uint8_t joystickNum); + const char* HALGetJoystickName(uint8_t joystickNum); + int HALGetJoystickAxisType(uint8_t joystickNum, uint8_t axis); int HALSetJoystickOutputs(uint8_t joystickNum, uint32_t outputs, uint16_t leftRumble, uint16_t rightRumble); int HALGetMatchTime(float *matchTime); diff --git a/hal/lib/Athena/HAL.cpp b/hal/lib/Athena/HAL.cpp index 98a0126442..b24f8dc95c 100644 --- a/hal/lib/Athena/HAL.cpp +++ b/hal/lib/Athena/HAL.cpp @@ -214,7 +214,57 @@ int HALGetJoystickButtons(uint8_t joystickNum, HALJoystickButtons *buttons) int HALGetJoystickDescriptor(uint8_t joystickNum, HALJoystickDescriptor *desc) { return FRC_NetworkCommunication_getJoystickDesc(joystickNum, &desc->isXbox, &desc->type, (char *)(&desc->name), - &desc->axisCount, &desc->axisTypes, &desc->buttonCount, &desc->povCount); + &desc->axisCount, (uint8_t *)&desc->axisTypes, &desc->buttonCount, &desc->povCount); +} + +int HALGetJoystickIsXbox(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return 0; + }else + { + return joystickDesc.isXbox; + } +} + +int HALGetJoystickType(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return -1; + } else + { + return joystickDesc.type; + } +} + +const char* HALGetJoystickName(uint8_t joystickNum) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + const char* retval = ""; + return retval; + } else + { + const char* retval(joystickDesc.name); + return retval; + } +} + +int HALGetJoystickAxisType(uint8_t joystickNum, int axis) +{ + HALJoystickDescriptor joystickDesc; + if(HALGetJoystickDescriptor(joystickNum, &joystickDesc)<0) + { + return -1; + } else + { + return joystickDesc.axisTypes[axis]; + } } int HALSetJoystickOutputs(uint8_t joystickNum, uint32_t outputs, uint16_t leftRumble, uint16_t rightRumble) diff --git a/wpilibc/wpilibC++Devices/include/DriverStation.h b/wpilibc/wpilibC++Devices/include/DriverStation.h index 0397ca3260..297aa2a826 100644 --- a/wpilibc/wpilibC++Devices/include/DriverStation.h +++ b/wpilibc/wpilibC++Devices/include/DriverStation.h @@ -40,7 +40,12 @@ public: int GetStickAxisCount(uint32_t stick); int GetStickPOVCount(uint32_t stick); int GetStickButtonCount(uint32_t stick); - + + bool GetJoystickIsXbox(uint32_t stick); + int GetJoystickType(uint32_t stick); + std::string GetJoystickName(uint32_t stick); + int GetJoystickAxisType(uint32_t stick, uint8_t axis); + bool IsEnabled(); bool IsDisabled(); bool IsAutonomous(); @@ -100,6 +105,7 @@ private: HALJoystickAxes m_joystickAxes[kJoystickPorts]; HALJoystickPOVs m_joystickPOVs[kJoystickPorts]; HALJoystickButtons m_joystickButtons[kJoystickPorts]; + HALJoystickDescriptor m_joystickDescriptor[kJoystickPorts]; Task m_task; SEMAPHORE_ID m_newControlData; MULTIWAIT_ID m_packetDataAvailableMultiWait; diff --git a/wpilibc/wpilibC++Devices/include/Joystick.h b/wpilibc/wpilibC++Devices/include/Joystick.h index 7ee06f6afb..c648be5789 100644 --- a/wpilibc/wpilibC++Devices/include/Joystick.h +++ b/wpilibc/wpilibC++Devices/include/Joystick.h @@ -41,7 +41,11 @@ public: { kLeftRumble, kRightRumble } RumbleType; - + typedef enum + { + kUnknown = -1, kXInputUnknown = 0, kXInputGamepad = 1, kXInputWheel = 2, kXInputArcadeStick = 3, kXInputFlightStick = 4, kXInputDancePad = 5, kXInputGuitar = 6, kXInputGuitar2 = 7, + kXInputDrumKit = 8, kXInputGuitar3 = 11, kXInputArcadePad = 19, kHIDJoystick = 20, kHIDGamepad = 21, kHIDDriving = 22, kHIDFlight = 23, kHID1stPerson = 24 + } HIDType; explicit Joystick(uint32_t port); Joystick(uint32_t port, uint32_t numAxisTypes, uint32_t numButtonTypes); virtual ~Joystick(); @@ -69,6 +73,11 @@ public: virtual float GetDirectionRadians(); virtual float GetDirectionDegrees(); + bool GetIsXbox(); + Joystick::HIDType GetType(); + std::string GetName(); + int GetAxisType(uint8_t axis); + int GetAxisCount(); int GetButtonCount(); int GetPOVCount(); diff --git a/wpilibc/wpilibC++Devices/src/DriverStation.cpp b/wpilibc/wpilibC++Devices/src/DriverStation.cpp index 2b2347a9c2..b159a8d56c 100644 --- a/wpilibc/wpilibC++Devices/src/DriverStation.cpp +++ b/wpilibc/wpilibC++Devices/src/DriverStation.cpp @@ -49,6 +49,9 @@ DriverStation::DriverStation() m_joystickAxes[i].count = 0; m_joystickPOVs[i].count = 0; m_joystickButtons[i].count = 0; + m_joystickDescriptor[i].isXbox = 0; + m_joystickDescriptor[i].type = -1; + m_joystickDescriptor[i].name[0] = '\0'; } // Create a new semaphore m_packetDataAvailableMultiWait = initializeMultiWait(); @@ -146,6 +149,7 @@ void DriverStation::GetData() HALGetJoystickAxes(stick, &m_joystickAxes[stick]); HALGetJoystickPOVs(stick, &m_joystickPOVs[stick]); HALGetJoystickButtons(stick, &m_joystickButtons[stick]); + HALGetJoystickDescriptor(stick, &m_joystickDescriptor[stick]); } giveSemaphore(m_newControlData); } @@ -195,6 +199,70 @@ int DriverStation::GetStickAxisCount(uint32_t stick) } /** + *Returns the name of the joystick at the given port + * + *@param stick The joystick port number + *@return The name of the joystick at the given port + */ +std::string DriverStation::GetJoystickName(uint32_t stick) +{ + if (stick >= kJoystickPorts) + { + wpi_setWPIError(BadJoystickIndex); + } + std::string retVal(m_joystickDescriptor[0].name); + return retVal; +} + +/** + *Returns the type of joystick at a given port + * + *@param stick The joystick port number + *@return The HID type of joystick at the given port + */ +int DriverStation::GetJoystickType(uint32_t stick) +{ + if (stick >= kJoystickPorts) + { + wpi_setWPIError(BadJoystickIndex); + return -1; + } + return (int)m_joystickDescriptor[stick].type; +} + +/** + *Returns a boolean indicating if the controller is an xbox controller. + * + *@param stick The joystick port number + *@return A boolean that is true if the controller is an xbox controller. + */ +bool DriverStation::GetJoystickIsXbox(uint32_t stick) +{ + if (stick >= kJoystickPorts) + { + wpi_setWPIError(BadJoystickIndex); + return false; + } + return (bool)m_joystickDescriptor[stick].isXbox; +} + +/** + *Returns the types of Axes on a given joystick port + * + *@param stick The joystick port number and the target axis + *@return What type of axis the axis is reporting to be + */ +int DriverStation::GetJoystickAxisType(uint32_t stick, uint8_t axis) +{ + if (stick >= kJoystickPorts) + { + wpi_setWPIError(BadJoystickIndex); + return -1; + } + return m_joystickDescriptor[stick].axisTypes[axis]; +} + +/** * Returns the number of POVs on a given joystick port * * @param stick The joystick port number diff --git a/wpilibc/wpilibC++Devices/src/Joystick.cpp b/wpilibc/wpilibC++Devices/src/Joystick.cpp index 46a9f08791..8d2a8c13c9 100644 --- a/wpilibc/wpilibC++Devices/src/Joystick.cpp +++ b/wpilibc/wpilibC++Devices/src/Joystick.cpp @@ -9,6 +9,7 @@ //#include "NetworkCommunication/UsageReporting.h" #include "WPIErrors.h" #include +#include const uint32_t Joystick::kDefaultXAxis; const uint32_t Joystick::kDefaultYAxis; @@ -275,6 +276,42 @@ int Joystick::GetAxisCount() return m_ds->GetStickAxisCount(m_port); } +/** + * Get the value of isXbox for the joystick. + * + * @return A boolean that is true if the joystick is an xbox controller. + */ +bool Joystick::GetIsXbox() +{ + return m_ds->GetJoystickIsXbox(m_port); +} + +/** + * Get the HID type of the controller. + * + * @return the HID type of the controller. + */ +Joystick::HIDType Joystick::GetType() +{ + return static_cast(m_ds->GetJoystickType(m_port)); +} + +/** + * Get the name of the joystick. + * + * @return the name of the controller. + */ +std::string Joystick::GetName() +{ + return m_ds->GetJoystickName(m_port); +} + +//int Joystick::GetAxisType(uint8_t axis) +//{ +// return m_ds->GetJoystickAxisType(m_port, axis); +//} + + /** * Get the number of axis for a joystick * @@ -389,3 +426,5 @@ void Joystick::SetOutputs(uint32_t value) { m_outputs = value; HALSetJoystickOutputs(m_port, m_outputs, m_leftRumble, m_rightRumble); } + + diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java index 8a35d256bb..82bf8337d6 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/DriverStation.java @@ -57,6 +57,10 @@ public class DriverStation implements RobotState.Interface { private short[][] m_joystickAxes = new short[kJoystickPorts][FRCNetworkCommunicationsLibrary.kMaxJoystickAxes]; private short[][] m_joystickPOVs = new short[kJoystickPorts][FRCNetworkCommunicationsLibrary.kMaxJoystickPOVs]; private HALJoystickButtons[] m_joystickButtons = new HALJoystickButtons[kJoystickPorts]; + private int[] m_joystickIsXbox = new int[kJoystickPorts]; + private int[] m_joystickType = new int[kJoystickPorts]; + private String[] m_joystickName = new String[kJoystickPorts]; + private int[][] m_joystickAxisType = new int[kJoystickPorts][FRCNetworkCommunicationsLibrary.kMaxJoystickAxes]; private Thread m_thread; private final Object m_dataSem; @@ -273,7 +277,7 @@ public class DriverStation implements RobotState.Interface { return m_joystickPOVs[stick][pov]; } - /** +/** * Returns the number of POVs on a given joystick port * * @param stick The joystick port number @@ -343,6 +347,68 @@ public class DriverStation implements RobotState.Interface { return m_joystickButtons[stick].count; } + /** + * Gets the value of isXbox on a joystick + * + * @param stick The joystick port number + * @return A boolean that returns the value of isXbox + */ + public synchronized boolean getJoystickIsXbox(int stick){ + + if(stick < 0 || stick >= kJoystickPorts) { + throw new RuntimeException("Joystick index is out of range, should be 0-5"); + } + //TODO: Remove this when calling for descriptor on empty stick no longer crashes + if(1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) { + reportJoystickUnpluggedError("WARNING: Joystick on port " + stick + " not available, check if controller is plugged in\n"); + return false; + } + boolean retVal = false; + if(FRCNetworkCommunicationsLibrary.HALGetJoystickIsXbox((byte)stick)==1) + { + retVal = true; + } + return retVal; + } + + /** + * Gets the value of type on a joystick + * + * @param stick The joystick port number + * @return The value of type + */ + public synchronized int getJoystickType(int stick){ + + if(stick < 0 || stick >= kJoystickPorts) { + throw new RuntimeException("Joystick index is out of range, should be 0-5"); + } + //TODO: Remove this when calling for descriptor on empty stick no longer crashes + if(1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) { + reportJoystickUnpluggedError("WARNING: Joystick on port " + stick + " not available, check if controller is plugged in\n"); + return -1; + } + return FRCNetworkCommunicationsLibrary.HALGetJoystickType((byte) stick); + } + + /** + * Gets the name of the joystick at a port + * + * @param stick The joystick port number + * @return The value of name + */ + public synchronized String getJoystickName(int stick){ + + if(stick < 0 || stick >= kJoystickPorts) { + throw new RuntimeException("Joystick index is out of range, should be 0-5"); + } + //TODO: Remove this when calling for descriptor on empty stick no longer crashes + if(1 > m_joystickButtons[stick].count && 1 > m_joystickAxes[stick].length) { + reportJoystickUnpluggedError("WARNING: Joystick on port " + stick + " not available, check if controller is plugged in\n"); + return ""; + } + return FRCNetworkCommunicationsLibrary.HALGetJoystickName((byte)stick); + } + /** * Gets a value indicating whether the Driver Station requires the * robot to be enabled. diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Joystick.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Joystick.java index 07de15e725..c6ae7a5923 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Joystick.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/Joystick.java @@ -412,6 +412,33 @@ public class Joystick extends GenericHID { m_axes[axis.value] = (byte) channel; } + /** + * Get the value of isXbox for the current joystick. + * + * @param value A boolean that is true if the controller is an xbox controller. + */ + public boolean getIsXbox() { + return m_ds.getJoystickIsXbox(m_port); + } + + /** + * Get the HID type of the current joystick. + * + * @param value The HID type value of the current joystick. + */ + public int getType() { + return m_ds.getJoystickType(m_port); + } + + /** + * Get the name of the current joystick. + * + * @param value The name of the current joystick. + */ + public String getName() { + return m_ds.getJoystickName(m_port); + } + /** * Set the rumble output for the joystick. The DS currently supports 2 rumble values, * left rumble and right rumble diff --git a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/communication/FRCNetworkCommunicationsLibrary.java b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/communication/FRCNetworkCommunicationsLibrary.java index 1b0cd6b0ec..374b842983 100644 --- a/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/communication/FRCNetworkCommunicationsLibrary.java +++ b/wpilibj/wpilibJavaDevices/src/main/java/edu/wpi/first/wpilibj/communication/FRCNetworkCommunicationsLibrary.java @@ -477,6 +477,9 @@ public class FRCNetworkCommunicationsLibrary extends JNIWrapper { public static native short[] HALGetJoystickPOVs(byte joystickNum); public static native int HALGetJoystickButtons(byte joystickNum, ByteBuffer count); public static native int HALSetJoystickOutputs(byte joystickNum, int outputs, short leftRumble, short rightRumble); + public static native int HALGetJoystickIsXbox(byte joystickNum); + public static native int HALGetJoystickType(byte joystickNum); + public static native String HALGetJoystickName(byte joystickNum); public static native float HALGetMatchTime(); public static native boolean HALGetSystemActive(IntBuffer status); public static native boolean HALGetBrownedOut(IntBuffer status); diff --git a/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp b/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp index 7a6da776f0..5c2297c6c9 100644 --- a/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp +++ b/wpilibj/wpilibJavaJNI/lib/FRCNetworkCommunicationsLibrary.cpp @@ -355,6 +355,42 @@ JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommun return HALSetJoystickOutputs(port, outputs, leftRumble, rightRumble); } +/* + * Class: edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary + * Method: HALGetJoystickIsXbox + * Signature: (B)I + */ +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary_HALGetJoystickIsXbox + (JNIEnv *, jclass, jbyte port) + { + NETCOMM_LOG(logDEBUG) << "Calling HALGetJoystickIsXbox"; + return HALGetJoystickIsXbox(port); + } + +/* + * Class: edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary + * Method: HALGetJoystickType + * Signature: (B)I + */ +JNIEXPORT jint JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary_HALGetJoystickType + (JNIEnv *, jclass, jbyte port) + { + NETCOMM_LOG(logDEBUG) << "Calling HALGetJoystickType"; + return HALGetJoystickType(port); + } + +/* + * Class: edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary + * Method: HALGetJoystickName + * Signature: (B)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary_HALGetJoystickName + (JNIEnv * env, jclass, jbyte port) + { + NETCOMM_LOG(logDEBUG) << "Calling HALGetJoystickName"; + return env->NewStringUTF(HALGetJoystickName(port)); + } + /* * Class: edu_wpi_first_wpilibj_communication_FRCNetworkCommunicationsLibrary * Method: setNewDataSem