You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.).
4
6
5
7
## Features
8
+
6
9
The joystick or gamepad can have the following features:
7
10
- Buttons (default: 32)
8
11
- Up to 2 Hat Switches
@@ -15,13 +18,14 @@ The joystick or gamepad can have the following features:
15
18
- Steering (up to 16-bit precision)
16
19
17
20
## Installation Instructions
18
-
Copy the `Joystick` folder to the Arduino libraries folder. Once the folder has been copied, the Joystick library should appear in the Arduino IDE list of libraries. The examples should also appear in the examples menu in the Arduino IDE.
19
-
### Microsoft Windows
20
-
On Microsoft Windows machines, this is typically `%userprofile%\Documents\Arduino\libraries`. The `deploy.bat` file can be executed to install the Joystick folder on Microsoft Windows machines (assuming a default Arduino installation).
21
-
### Linux
22
-
On Linux machines, this is typically `$HOME/Arduino/libraries`. The `deploy.sh` file can be executed to install the Joystick folder on Linux machines (assuming a default Arduino installation). [Thanks to @Nihlus (Jarl Gullberg) for his help with this.]
21
+
22
+
The following instructions can be used to install the latest version of the library in the Arduino IDE (thanks to [@per1234](https://github.com/per1234) for this update):
2. In the Arduino IDE, select `Sketch` > `Include Library` > `Add .ZIP Library...`. Browse to where the downloaded ZIP file is located and click `Open`. The Joystick library's examples will now appear under `File` > `Examples` > `Joystick`.
23
26
24
27
## Examples
28
+
25
29
The following example Arduino sketch files are included in this library:
26
30
27
31
-`JoystickTest` - Simple test of the Joystick library. It exercises many of the Joystick library’s functions when pin A0 is grounded.
@@ -35,42 +39,46 @@ The following example Arduino sketch files are included in this library:
35
39
36
40
### Simple example
37
41
38
-
#include <Joystick.h>
39
-
40
-
// Create the Joystick
41
-
Joystick_ Joystick;
42
-
43
-
// Constant that maps the phyical pin to the joystick button.
44
-
const int pinToButtonMap = 9;
45
-
46
-
void setup() {
47
-
// Initialize Button Pins
48
-
pinMode(pinToButtonMap, INPUT_PULLUP);
49
-
50
-
// Initialize Joystick Library
51
-
Joystick.begin();
52
-
}
53
-
54
-
// Last state of the button
55
-
int lastButtonState = 0;
56
-
57
-
void loop() {
58
-
59
-
// Read pin values
60
-
int currentButtonState = !digitalRead(pinToButtonMap);
61
-
if (currentButtonState != lastButtonState)
62
-
{
63
-
Joystick.setButton(0, currentButtonState);
64
-
lastButtonState = currentButtonState;
65
-
}
66
-
67
-
delay(50);
42
+
```C++
43
+
#include<Joystick.h>
44
+
45
+
// Create the Joystick
46
+
Joystick_ Joystick;
47
+
48
+
// Constant that maps the physical pin to the joystick button.
49
+
constint pinToButtonMap = 9;
50
+
51
+
voidsetup() {
52
+
// Initialize Button Pins
53
+
pinMode(pinToButtonMap, INPUT_PULLUP);
54
+
55
+
// Initialize Joystick Library
56
+
Joystick.begin();
57
+
}
58
+
59
+
// Last state of the button
60
+
int lastButtonState = 0;
61
+
62
+
voidloop() {
63
+
64
+
// Read pin values
65
+
int currentButtonState = !digitalRead(pinToButtonMap);
66
+
if (currentButtonState != lastButtonState)
67
+
{
68
+
Joystick.setButton(0, currentButtonState);
69
+
lastButtonState = currentButtonState;
68
70
}
69
71
72
+
delay(50);
73
+
}
74
+
```
75
+
70
76
## Joystick Library API
77
+
71
78
The following API is available if the Joystick library in included in a sketch file.
72
79
73
80
### Joystick\_(...)
81
+
74
82
Constructor used to initialize and setup the Joystick. The following optional parameters are available:
75
83
76
84
-`uint8_t hidReportId` - Default: `0x03` - Indicates the joystick's HID report ID. This value must be unique if you are creating multiple instances of Joystick. Do not use `0x01` or `0x02` as they are used by the built-in Arduino Keyboard and Mouse libraries.
@@ -99,90 +107,119 @@ The following constants define the default values for the constructor parameters
99
107
-`JOYSTICK_DEFAULT_HATSWITCH_COUNT` is set to `2`
100
108
101
109
### Joystick.begin(bool initAutoSendState)
110
+
102
111
Starts emulating a game controller connected to a computer. By default, all methods update the game controller state immediately. If `initAutoSendState` is set to `false`, the `Joystick.sendState` method must be called to update the game controller state.
103
112
104
113
### Joystick.end()
114
+
105
115
Stops the game controller emulation to a connected computer.
Sets the state (`0` or `1`) of the specified button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.). The value is `1` if the button is pressed and `0` if the button is released.
175
208
176
209
### Joystick.pressButton(uint8_t button)
210
+
177
211
Press the indicated button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.).
178
212
179
213
### Joystick.releaseButton(uint8_t button)
214
+
180
215
Release the indicated button (range: `0` - (`buttonCount - 1`)). The button is the 0-based button number (i.e. button #1 is `0`, button #2 is `1`, etc.).
Sets the value of the specified hat switch. The hatSwitch is 0-based (i.e. hat switch #1 is `0` and hat switch #2 is `1`). The value is from 0° to 360°, but in 45° increments. Any value less than 45° will be rounded down (i.e. 44° is rounded down to 0°, 89° is rounded down to 45°, etc.). Set the value to `JOYSTICK_HATSWITCH_RELEASE` or `-1` to release the hat switch.
184
220
185
221
### Joystick.sendState()
222
+
186
223
Sends the updated joystick state to the host computer. Only needs to be called if `AutoSendState` is `false` (see `Joystick.begin` for more details).
187
224
188
225
See the [Wiki](https://github.com/MHeironimus/ArduinoJoystickLibrary/wiki) for more details on things like FAQ, supported boards, testing, etc.
0 commit comments