-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromicro-gamepad.ino
74 lines (65 loc) · 2 KB
/
promicro-gamepad.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <Joystick.h>
#define OFFSET_DPAD 18
#define OFFSET_BUTTON 2
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
6, 0, // Button Count, Hat Switch Count
true, true, false, // X and Y, but no Z Axis
false, false, false, // No Rx, Ry, or Rz
false, false, // No rudder or throttle
false, false, false); // No accelerator, brake, or steering
void setup() {
// Initialize Button Pins
pinMode(18, INPUT_PULLUP); // D-pad Up
pinMode(19, INPUT_PULLUP); // D-pad Right
pinMode(20, INPUT_PULLUP); // D-pad Down
pinMode(21, INPUT_PULLUP); // D-pad Left
pinMode(2, INPUT_PULLUP); // Button 1
pinMode(3, INPUT_PULLUP); // Button 2
pinMode(4, INPUT_PULLUP); // Button 3
pinMode(5, INPUT_PULLUP); // Button 4
pinMode(6, INPUT_PULLUP); // Button 5
pinMode(7, INPUT_PULLUP); // Button 6
// Initialize Joystick Library
Joystick.begin();
Joystick.setXAxisRange(-1, 1);
Joystick.setYAxisRange(-1, 1);
}
// Last state of the buttons
int lastDpadState[4] = {0,0,0,0}; // Up, Right, Down, Left
int lastButtonState[6] = {0,0,0,0,0,0}; // Button 1 - 6
void loop() {
updateDpad();
updateButton();
delay(10);
}
void updateDpad() {
for (int i = 0; i <= 3; i++) {
int currentDpadState = !digitalRead(i + OFFSET_DPAD);
if (currentDpadState != lastDpadState[i]) {
switch (i) {
case 0:
Joystick.setYAxis(-currentDpadState);
break;
case 1:
Joystick.setXAxis(currentDpadState);
break;
case 2:
Joystick.setYAxis(currentDpadState);
break;
case 3:
Joystick.setXAxis(-currentDpadState);
break;
}
lastDpadState[i] = currentDpadState;
}
}
}
void updateButton() {
for (int i = 0; i <= 5; i++) {
int currentButtonState = !digitalRead(i + OFFSET_BUTTON);
if (currentButtonState != lastButtonState[i]) {
Joystick.setButton(i, currentButtonState);
lastButtonState[i] = currentButtonState;
}
}
}