-
Notifications
You must be signed in to change notification settings - Fork 1
/
android_gamepad.ino
107 lines (80 loc) · 2.5 KB
/
android_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* wolfshow's usb_gamepad for arduino micro v1.0
*
*/
#include <Joystick.h>
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD, 16, 1,
true, true, true, false, false, true, false, false, false, false, false);
class CButton {
public:
int pin = NULL;
int lastState = 0;
CButton(int p) {
pin = p;
}
};
CButton Buttons[12] ={0,1,2,3,4,5,6,7,8,9,10,11};
CButton Dpad[4] = {12,13,A4,A5};
int lastDpadState[4] = {0,0,0,0};
int JoyHat = 0;
void setup() {
for(int i=0 ; i< 12 ;i++) {
pinMode(Buttons[i].pin, INPUT_PULLUP);
}
for(int i=0 ; i< 4 ;i++) {
pinMode(Dpad[i].pin, INPUT_PULLUP);
}
Joystick.begin();
Joystick.setXAxisRange(-512, 512);
Joystick.setYAxisRange(-512, 512);
Joystick.setZAxisRange(-512, 512);
Joystick.setRzAxisRange(-512, 512);
Joystick.setHatSwitch(0,-1);
}
void JButtonStates() {
Joystick.setXAxis(analogRead(A0) - 512);
Joystick.setYAxis(512 - analogRead(A1));
Joystick.setZAxis(512 - analogRead(A2));
Joystick.setRzAxis(analogRead(A3) - 512);
for (int i = 0; i < 12; i++) {
int currentState = !digitalRead(Buttons[i].pin);
if (currentState != Buttons[i].lastState) {
Joystick.setButton(i, currentState);
Buttons[i].lastState = currentState;
}
}
bool valueChanged = false;
for (int i = 0; i < 4; i++)
{
int currentButtonState = !digitalRead(Dpad[i].pin);
if (currentButtonState != lastDpadState[i])
{
valueChanged = true;
lastDpadState[i] = currentButtonState;
}
}
if(valueChanged)
{
int JoyHat = 0;
if ( lastDpadState[0] == 1 ) JoyHat += 2;
if ( lastDpadState[2] == 1 ) JoyHat += 3;
if ( lastDpadState[1] == 1 ) JoyHat += 6;
if ( lastDpadState[3] == 1 ) JoyHat += 12;
switch(JoyHat) {
case 2: Joystick.setHatSwitch(0,0); break; // up
case 3: Joystick.setHatSwitch(0,180); break; // down
case 6: Joystick.setHatSwitch(0,270); break; // left
case 8: Joystick.setHatSwitch(0,315); break; // up-left
case 9: Joystick.setHatSwitch(0,225); break; // down-left
case 12: Joystick.setHatSwitch(0,90); break; // right
case 14: Joystick.setHatSwitch(0,45); break; // up-right
case 15: Joystick.setHatSwitch(0,135); break; // down-right
default: Joystick.setHatSwitch(0,-1);
}
}
}
void loop() {
JoyHat = 0;
JButtonStates();
delay(10);
}