Added functions to get names, HID types and isXbox descriptors from joysticks.

Change-Id: I3cd6ebc04d64398f05001cd008696e13ee1ab6ea
This commit is contained in:
jmanning
2015-06-15 12:34:57 -04:00
committed by Kevin O'Connor
parent 1c9dffc301
commit bf4ccf13d9
10 changed files with 313 additions and 5 deletions

View File

@@ -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);

View File

@@ -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)

View File

@@ -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;

View File

@@ -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();

View File

@@ -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

View File

@@ -9,6 +9,7 @@
//#include "NetworkCommunication/UsageReporting.h"
#include "WPIErrors.h"
#include <math.h>
#include <string.h>
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<HIDType>(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);
}

View File

@@ -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.

View File

@@ -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

View File

@@ -477,6 +477,9 @@ public class FRCNetworkCommunicationsLibrary extends JNIWrapper {
case 5:
return HALAllianceStationID.Blue3;
default:
return null;
}
}
public static int kMaxJoystickAxes = 12;
public static int kMaxJoystickPOVs = 12;

View File

@@ -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