MHeironimus / ArduinoJoystickLibrary

An Arduino library that adds one or more joysticks to the list of HID devices an Arduino Leonardo or Arduino Micro can support.
GNU Lesser General Public License v3.0
2.1k stars 409 forks source link

Way to set axes by index rather than name #165

Open ColdFrontWI opened 4 years ago

ColdFrontWI commented 4 years ago

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

ColdFrontWI commented 4 years ago

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:

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