Skip to content

Commit d9771e8

Browse files
committed
Added GamepadExample
Fix issue with API documentation
1 parent 3a490c1 commit d9771e8

File tree

2 files changed

+107
-15
lines changed

2 files changed

+107
-15
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Simple gamepad example that demonstraits how to read five Arduino
2+
// digital pins and map them to the Arduino Joystick library.
3+
//
4+
// The digital pins 2 - 6 are grounded when they are pressed.
5+
// Pin 2 = UP
6+
// Pin 3 = RIGHT
7+
// Pin 4 = DOWN
8+
// Pin 5 = LEFT
9+
// Pin 6 = FIRE
10+
//
11+
// NOTE: This sketch file is for use with Arduino Leonardo and
12+
// Arduino Micro only.
13+
//
14+
// by Matthew Heironimus
15+
// 2016-11-24
16+
//--------------------------------------------------------------------
17+
18+
#include <Joystick.h>
19+
20+
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
21+
1, 0, // Button Count, Hat Switch Count
22+
true, true, false, // X and Y, but no Z Axis
23+
false, false, false, // No Rx, Ry, or Rz
24+
false, false, // No rudder or throttle
25+
false, false, false); // No accelerator, brake, or steering
26+
27+
void setup() {
28+
// Initialize Button Pins
29+
pinMode(2, INPUT_PULLUP);
30+
pinMode(3, INPUT_PULLUP);
31+
pinMode(4, INPUT_PULLUP);
32+
pinMode(5, INPUT_PULLUP);
33+
pinMode(6, INPUT_PULLUP);
34+
35+
// Initialize Joystick Library
36+
Joystick.begin();
37+
Joystick.setXAxisRange(-1, 1);
38+
Joystick.setYAxisRange(-1, 1);
39+
}
40+
41+
// Last state of the buttons
42+
int lastButtonState[5] = {0,0,0,0,0};
43+
44+
void loop() {
45+
46+
// Read pin values
47+
for (int index = 0; index < 5; index++)
48+
{
49+
int currentButtonState = !digitalRead(index + 2);
50+
if (currentButtonState != lastButtonState[index])
51+
{
52+
switch (index) {
53+
case 0: // UP
54+
if (currentButtonState == 1) {
55+
Joystick.setYAxis(-1);
56+
} else {
57+
Joystick.setYAxis(0);
58+
}
59+
break;
60+
case 1: // RIGHT
61+
if (currentButtonState == 1) {
62+
Joystick.setXAxis(1);
63+
} else {
64+
Joystick.setXAxis(0);
65+
}
66+
break;
67+
case 2: // DOWN
68+
if (currentButtonState == 1) {
69+
Joystick.setYAxis(1);
70+
} else {
71+
Joystick.setYAxis(0);
72+
}
73+
break;
74+
case 3: // LEFT
75+
if (currentButtonState == 1) {
76+
Joystick.setXAxis(-1);
77+
} else {
78+
Joystick.setXAxis(0);
79+
}
80+
break;
81+
case 4: // FIRE
82+
Joystick.setButton(0, currentButtonState);
83+
break;
84+
}
85+
lastButtonState[index] = currentButtonState;
86+
}
87+
}
88+
89+
delay(10);
90+
}
91+

README.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ The following example Arduino sketch files are included in this library:
2828
- `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.
2929
- `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.
3030
- `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.
31+
- `GamepadExample` - Creates a simple Gamepad with an Up, Down, Left, Right, and Fire button.
3132
- `DrivingControllerTest` - Creates a Driving Controller and tests 4 buttons, the Steering, Brake, and Accelerator when pin A0 is grounded.
3233
- `FlightControllerTest` - Creates a Flight Controller and tests 32 buttons, the X and Y axis, the Throttle, and the Rudder when pin A0 is grounded.
3334
- `HatSwitchTest` - Creates a joystick with two hat switches. Grounding pins 4 - 11 cause the hat switches to change position.
@@ -106,79 +107,79 @@ Stops the game controller emulation to a connected computer.
106107
### Joystick.setXAxisRange(int16_t minimum, int16_t maximum)
107108
Sets the range of values that will be used for the X axis. Default: `0` to `1023`
108109

109-
### Joystick.setXAxis(byte value)
110+
### Joystick.setXAxis(int16_t value)
110111
Sets the X axis value. See `setXAxisRange` for the range.
111112

112113
### Joystick.setYAxisRange(int16_t minimum, int16_t maximum)
113114
Sets the range of values that will be used for the Y axis. Default: `0` to `1023`
114115

115-
### Joystick.setYAxis(byte value)
116+
### Joystick.setYAxis(int16_t value)
116117
Sets the Y axis value. See `setYAxisRange` for the range.
117118

118119
### Joystick.setZAxisRange(int16_t minimum, int16_t maximum)
119120
Sets the range of values that will be used for the Z axis. Default: `0` to `1023`
120121

121-
### Joystick.setZAxis(byte value)
122+
### Joystick.setZAxis(int16_t value)
122123
Sets the Z axis value. See `setZAxisRange` for the range.
123124

124125
### Joystick.setRxAxisRange(int16_t minimum, int16_t maximum)
125126
Sets the range of values that will be used for the X axis rotation. Default: `0` to `1023`
126127

127-
### Joystick.setRxAxis(int value)
128+
### Joystick.setRxAxis(int16_t value)
128129
Sets the X axis rotation value. See `setRxAxisRange` for the range.
129130

130131
### Joystick.setRyAxisRange(int16_t minimum, int16_t maximum)
131132
Sets the range of values that will be used for the Y axis rotation. Default: `0` to `1023`
132133

133-
### Joystick.setRyAxis(int value)
134+
### Joystick.setRyAxis(int16_t value)
134135
Sets the Y axis rotation value. See `setRyAxisRange` for the range.
135136

136137
### Joystick.setRzAxisRange(int16_t minimum, int16_t maximum)
137138
Sets the range of values that will be used for the Z axis rotation. Default: `0` to `1023`
138139

139-
### Joystick.setRzAxis(int value)
140+
### Joystick.setRzAxis(int16_t value)
140141
Sets the Z axis rotation value. See `setRzAxisRange` for the range.
141142

142143
### Joystick.setRudderRange(int16_t minimum, int16_t maximum)
143144
Sets the range of values that will be used for the Rudder. Default: `0` to `1023`
144145

145-
### Joystick.setRudder(byte value)
146+
### Joystick.setRudder(int16_t value)
146147
Sets the Rudder value. See `setRudderRange` for the range.
147148

148149
### Joystick.setThrottleRange(int16_t minimum, int16_t maximum)
149150
Sets the range of values that will be used for the Throttle. Default: `0` to `1023`
150151

151-
### Joystick.setThrottle(byte value)
152+
### Joystick.setThrottle(int16_t value)
152153
Sets the Throttle value. See `setThrottleRange` for the range.
153154

154155
### Joystick.setAcceleratorRange(int16_t minimum, int16_t maximum)
155156
Sets the range of values that will be used for the Accelerator. Default: `0` to `1023`
156157

157-
### Joystick.setAccelerator(byte value)
158+
### Joystick.setAccelerator(int16_t value)
158159
Sets the Accelerator value. See `setAcceleratorRange` for the range.
159160

160161
### Joystick.setBrakeRange(int16_t minimum, int16_t maximum)
161162
Sets the range of values that will be used for the Brake. Default: `0` to `1023`
162163

163-
### Joystick.setBrake(byte value)
164+
### Joystick.setBrake(int16_t value)
164165
Sets the Brake value. See `setBrakeRange` for the range.
165166

166167
### Joystick.setSteeringRange(int16_t minimum, int16_t maximum)
167168
Sets the range of values that will be used for the Steering. Default: `0` to `1023`
168169

169-
### Joystick.setSteering(byte value)
170+
### Joystick.setSteering(int16_t value)
170171
Sets the Steering value. See `setSteeringRange` for the range.
171172

172-
### Joystick.setButton(byte button, byte value)
173+
### Joystick.setButton(uint8_t button, uint8_t value)
173174
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.
174175

175-
### Joystick.pressButton(byte button)
176+
### Joystick.pressButton(uint8_t button)
176177
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.).
177178

178-
### Joystick.releaseButton(byte button)
179+
### Joystick.releaseButton(uint8_t button)
179180
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.).
180181

181-
### Joystick.setHatSwitch(byte hatSwitch, int value)
182+
### Joystick.setHatSwitch(int8_t hatSwitch, int16_t value)
182183
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.
183184

184185
### Joystick.sendState()

0 commit comments

Comments
 (0)