ArduinoJoystickLibrary
ArduinoJoystickLibrary copied to clipboard
Way to set axes by index rather than name
Describe your feature request
A way to set axis values base on a numeric index rather than a specific axis name to allow looping. This could come in the form of a new function:
// Current function
setXAxis(xAxisValue);
// New function
setAxis(axisIndex, axisValue);
Additional context
I am using the library to create game controllers, and it is fantastic! One challenge is that I have to duplicate the code for each axis. I would like to be able to write a loop to apply the same code to all of the axes. Here's what I'm thinking:
Current version, has to be repeated for every axis.
// This code is more complicated if there is any smoothing or other conditionals applied.
// Code for the first axis
newXAxisValue = analogRead(xAxisPin);
if(oldXAxisValue != newXAxisValue)
{
setXAxis(newXAxisValue);
oldXAxisValue = newXAxisValue;
}
// Code for the second axis
newYAxis = analogRead(yAxisPin);
if(oldYAxisValue != newYAxisValue)
{
setYAxis(newYAxisValue);
oldYAxisValue = newYAxisValue;
}
// Have to repeat the code above for any additional axes...
New function, would allow for looping over as many axes as you wanted. This would be especially nice with more complex code, e.g. smoothing via a rolling average.
// Single loop that sets ALL axes
// Write the code only once for as many axes as are used
for(i = 0; i < numAxes; i++)
{
newAxisValue[i] = analogRead(axisPin[i]);
if(newAxisValue[i] != oldAxisValue[i])
{
setAxis(i, newAxisValue[i]); // THIS IS A NEW FUNCTION
oldAxisValue[i] = newAxisValue[i];
}
}
Thanks, Eric
For what it's worth, I realized shortly after posting this that you can write a custom function that does this. That said, it's a pretty ugly brute-force approach, and I think there may be a more elegant / efficient way to manage it with a native function in the libary.
A couple of comments on this program:
- The function takes two inputs:
- An index that defines which axis to set
- The value to set the axis to
- I can only seem to get seven axes to work in Windows: X, Y, Z, Rx, Ry, Rz, and Throttle--the others are not recognized. As a result, I made Throttle the seventh axis (index 6) even thought it is the eight specified in declaring the Joystick class.
- In this program, I've named my Joystick class JS1 so that is specified in the function. This would have to be edited if different name is used. I haven't worked out how to pass a specific Joystick to the function, but I'mguessing that would not be too difficult.
Code below...
Thanks! Eric
// Function declaration
void setAxis(int axisIndex, int axisSetValue);
void setup()
{
// Some code here
}
void loop()
{
// Some code here
}
// The function itself
void setAxis(int axisIndex, int axisSetValue)
{
if(axisIndex == 0)
JS1.setXAxis(axisSetValue);
else if(axisIndex == 1)
JS1.setYAxis(axisSetValue);
else if(axisIndex == 2)
JS1.setZAxis(axisSetValue);
else if(axisIndex == 3)
JS1.setRxAxis(axisSetValue);
else if(axisIndex == 4)
JS1.setRyAxis(axisSetValue);
else if(axisIndex == 5)
JS1.setRzAxis(axisSetValue);
// NOTE RUDDER AND THROTTLE SWAPPED
// THROTTLE WORKS AS 7th AXIS, RUDDER DOES NOT
else if(axisIndex == 7) // Note it's 7
JS1.setRudder(axisSetValue);
else if(axisIndex == 6) // Note it's 6
JS1.setThrottle(axisSetValue);
else if(axisIndex == 8)
JS1.setAccelerator(axisSetValue);
else if(axisIndex == 9)
JS1.setBrake(axisSetValue);
else if(axisIndex == 10)
JS1.setSteering(axisSetValue);
}