Skip to content

Commit

Permalink
Merge pull request #238 from MHeironimus/unity-fix
Browse files Browse the repository at this point in the history
Version 2.1.0 - Unity Fix
  • Loading branch information
MHeironimus authored Mar 30, 2022
2 parents e800604 + 3144b33 commit dabdcd4
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 236 deletions.
81 changes: 44 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Arduino Joystick Library

#### Version 2.0.8
#### Version 2.1.0

This library can be used with Arduino IDE 1.6.6 (or above) to add one or more joysticks (or gamepads) to the list of HID devices an [Arduino Leonardo](https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) or [Arduino Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro) (or any Arduino clone that is based on the ATmega32u4) can support. This library will also work with the [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue), thanks to [@Palakis](https://github.com/Palakis). A complete list of supported boards can be found in the [Wiki](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Supported-Boards). This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).
This library can be used with Arduino IDE 1.6.6 or above (see [Wiki - Testing Details](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Testing-Details) for more information) to add one or more joysticks (or gamepads) to the list of HID devices an [Arduino Leonardo](https://www.arduino.cc/en/Main/ArduinoBoardLeonardo) or [Arduino Micro](https://www.arduino.cc/en/Main/ArduinoBoardMicro) (or any Arduino clone that is based on the ATmega32u4) can support. This library will also work with the [Arduino Due](https://www.arduino.cc/en/Main/ArduinoBoardDue), thanks to [@Palakis](https://github.com/Palakis). A complete list of supported boards can be found in the [Wiki - Supported Boards](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki/Supported-Boards). This will not work with Arduino IDE 1.6.5 (or below) or with non-32u4 based Arduino devices (e.g. Arduino UNO, Arduino MEGA, etc.).

## Features

Expand All @@ -26,18 +26,6 @@ The following instructions can be used to install the latest version of the libr

## Examples

The following example Arduino sketch files are included in this library:

- `JoystickTest` - Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
- `MultipleJoystickTest` - Creates 4 Joysticks using the library and exercises the first 16 buttons, the X axis, and the Y axis of each joystick when pin A0 is grounded.
- `JoystickButton` - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
- `JoystickKeyboard` - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
- `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino Pro Micro digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). NOTE: This sketch is for the Arduino Pro Micro only.

### Simple example

```C++
Expand Down Expand Up @@ -74,6 +62,25 @@ void loop() {
}
```

### Included Examples

The following example Arduino sketch files are included in this library:

#### Simple Samples

- `JoystickButton` - Creates a Joystick and maps pin 9 to button 0 of the joystick, pin 10 to button 1, pin 11 to button 2, and pin 12 to button 3.
- `JoystickKeyboard` - Creates a Joystick and a Keyboard. Maps pin 9 to Joystick Button 0, pin 10 to Joystick Button 1, pin 11 to Keyboard key 1, and pin 12 to Keyboard key 2.
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
- `ArcadeStickExample` - Simple arcade stick example that demonstrates how to read twelve Arduino Pro Micro digital pins and map them to the library (thanks to [@nebhead](https://github.com/nebhead) for this example). NOTE: This sketch is for the Arduino Pro Micro only.

#### Used for Testing

- `JoystickTest` - Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
- `MultipleJoystickTest` - Creates 4 Joysticks using the library (each with a slightly different configuration) and exercises the first 16 buttons (if present), the X axis, and the Y axis of each joystick when pin A0 is grounded.
- `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.

## Joystick Library API

The following API is available if the Joystick library in included in a sketch file.
Expand Down Expand Up @@ -113,93 +120,93 @@ Starts emulating a game controller connected to a computer. By default, all meth

### Joystick.end()

Stops the game controller emulation to a connected computer.
Stops the game controller emulation to a connected computer (Note: just like the Arduino `Keyboard.h` and `Mouse.h` libraries, the `end()` function does not actually do anything).

### Joystick.setXAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setXAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the X axis. Default: `0` to `1023`

### Joystick.setXAxis(int16_t value)
### Joystick.setXAxis(int32_t value)

Sets the X axis value. See `setXAxisRange` for the range.

### Joystick.setYAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setYAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Y axis. Default: `0` to `1023`

### Joystick.setYAxis(int16_t value)
### Joystick.setYAxis(int32_t value)

Sets the Y axis value. See `setYAxisRange` for the range.

### Joystick.setZAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setZAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Z axis. Default: `0` to `1023`

### Joystick.setZAxis(int16_t value)
### Joystick.setZAxis(int32_t value)

Sets the Z axis value. See `setZAxisRange` for the range.

### Joystick.setRxAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setRxAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the X axis rotation. Default: `0` to `1023`

### Joystick.setRxAxis(int16_t value)
### Joystick.setRxAxis(int32_t value)

Sets the X axis rotation value. See `setRxAxisRange` for the range.

### Joystick.setRyAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setRyAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Y axis rotation. Default: `0` to `1023`

### Joystick.setRyAxis(int16_t value)
### Joystick.setRyAxis(int32_t value)

Sets the Y axis rotation value. See `setRyAxisRange` for the range.

### Joystick.setRzAxisRange(int16_t minimum, int16_t maximum)
### Joystick.setRzAxisRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Z axis rotation. Default: `0` to `1023`

### Joystick.setRzAxis(int16_t value)
### Joystick.setRzAxis(int32_t value)

Sets the Z axis rotation value. See `setRzAxisRange` for the range.

### Joystick.setRudderRange(int16_t minimum, int16_t maximum)
### Joystick.setRudderRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Rudder. Default: `0` to `1023`

### Joystick.setRudder(int16_t value)
### Joystick.setRudder(int32_t value)

Sets the Rudder value. See `setRudderRange` for the range.

### Joystick.setThrottleRange(int16_t minimum, int16_t maximum)
### Joystick.setThrottleRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Throttle. Default: `0` to `1023`

### Joystick.setThrottle(int16_t value)
### Joystick.setThrottle(int32_t value)

Sets the Throttle value. See `setThrottleRange` for the range.

### Joystick.setAcceleratorRange(int16_t minimum, int16_t maximum)
### Joystick.setAcceleratorRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Accelerator. Default: `0` to `1023`

### Joystick.setAccelerator(int16_t value)
### Joystick.setAccelerator(int32_t value)

Sets the Accelerator value. See `setAcceleratorRange` for the range.

### Joystick.setBrakeRange(int16_t minimum, int16_t maximum)
### Joystick.setBrakeRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Brake. Default: `0` to `1023`

### Joystick.setBrake(int16_t value)
### Joystick.setBrake(int32_t value)

Sets the Brake value. See `setBrakeRange` for the range.

### Joystick.setSteeringRange(int16_t minimum, int16_t maximum)
### Joystick.setSteeringRange(int32_t minimum, int32_t maximum)

Sets the range of values that will be used for the Steering. Default: `0` to `1023`

### Joystick.setSteering(int16_t value)
### Joystick.setSteering(int32_t value)

Sets the Steering value. See `setSteeringRange` for the range.

Expand Down
12 changes: 9 additions & 3 deletions examples/JoystickTest/JoystickTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// above.
// 2016-05-13 Updated to use new dynamic Joystick library
// that can be customized.
// 2022-03-29 Updated to work with version 2.1.0 of the
// Joystick library.
//------------------------------------------------------------

#include "Joystick.h"
Expand Down Expand Up @@ -105,18 +107,22 @@ void testXYAxis(unsigned int currentStep)

void testZAxis(unsigned int currentStep)
{
int z;

if (currentStep < 128)
{
Joystick.setZAxis(-currentStep);
z = -currentStep;
}
else if (currentStep < 256 + 128)
{
Joystick.setZAxis(currentStep - 128 - 127);
z = currentStep - 128 - 127;
}
else if (currentStep < 256 + 128 + 127)
{
Joystick.setZAxis(127 - (currentStep - 383));
z = 127 - (currentStep - 383);
}

Joystick.setZAxis(z);
}

void testHatSwitch(unsigned int currentStep)
Expand Down
34 changes: 22 additions & 12 deletions examples/MultipleJoystickTest/MultipleJoystickTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
// Each joystick has a unique configuration.
//
// Matthew Heironimus
// 2016-05-13
// 2016-05-13 - Original Version
// 2022-03-29 - Updated to work with Joystick Library v2.1.0
//------------------------------------------------------------
#include <Joystick.h>

#define JOYSTICK_COUNT 4

Joystick_ Joystick[JOYSTICK_COUNT] = {
Joystick_(0x03, JOYSTICK_TYPE_GAMEPAD, 4, 2, true, true, false, false, false, false, false, false, false, false, false),
Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK, 8, 1, true, true, true, true, false, false, false, false, false, false, false),
Joystick_(0x05, JOYSTICK_TYPE_MULTI_AXIS, 16, 0, false, true, false, true, false, false, true, true, false, false, false),
Joystick_(0x06, JOYSTICK_TYPE_MULTI_AXIS, 32, 1, true, true, false, true, true, false, false, false, true, true, true)
Joystick_(0x03, JOYSTICK_TYPE_JOYSTICK, 4, 2, true, true, false, false, false, false, false, false, false, false, false),
Joystick_(0x04, JOYSTICK_TYPE_JOYSTICK, 8, 1, true, true, true, true, false, false, false, false, false, false, false),
Joystick_(0x05, JOYSTICK_TYPE_JOYSTICK, 16, 0, false, true, false, true, false, false, true, true, false, false, false),
Joystick_(0x06, JOYSTICK_TYPE_JOYSTICK, 32, 1, true, true, false, true, true, false, false, false, false, false, false)
};

// Set to true to test "Auto Send" mode or false to test "Manual Send" mode.
Expand Down Expand Up @@ -74,28 +75,37 @@ void testMultiButtonPush(int joystickId, unsigned int currentStep)

void testXYAxis(int joystickId, unsigned int currentStep)
{
int x;
int y;

if (currentStep < 255)
{
Joystick[joystickId].setXAxis(currentStep - 127);
Joystick[joystickId].setYAxis(-127);
x = currentStep - 127;
y = -127;
}
else if (currentStep < 510)
{
Joystick[joystickId].setYAxis(currentStep - 255 - 127);
x = 127;
y = currentStep - 255 - 127;
}
else if (currentStep < 765)
{
Joystick[joystickId].setXAxis(127 - (currentStep - 510));
x = 127 - (currentStep - 510);
y = 127;
}
else if (currentStep < 1020)
{
Joystick[joystickId].setYAxis(127 - (currentStep - 765));
x = -127;
y = 127 - (currentStep - 765);
}
else if (currentStep <= 1020 + 127)
{
Joystick[joystickId].setXAxis(currentStep - 1020 - 127);
Joystick[joystickId].setYAxis(currentStep - 1020 - 127);
x = currentStep - 1020 - 127;
y = currentStep - 1020 - 127;
}

Joystick[joystickId].setXAxis(x);
Joystick[joystickId].setYAxis(y);
}

void setup() {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Joystick
version=2.0.8
version=2.1.0
author=Matthew Heironimus
maintainer=Matthew Heironimus <[email protected]>
sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad.
Expand Down
Loading

0 comments on commit dabdcd4

Please sign in to comment.