-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnespad-31.ino
224 lines (144 loc) · 4.63 KB
/
snespad-31.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
snespad-31
Copyright (C) 2014 Serge van Namen <serge@se-cured.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITSNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <SNESpad.h>
// Macro's for button states.
#define BUTTON_PRESSED(state, btn) ((state) & (btn))
#define CURRENTLY_PRESSED(btn) BUTTON_PRESSED(state, btn)
#define PREVIOUSLY_PRESSED(btn) BUTTON_PRESSED(prev_state, btn)
#define DEBUGGING 1
// debugging
#ifdef DEBUGGING
#define DEBUG(...) Serial.println(state, BIN)
#else
#define DEBUG(...)
#endif
// Generic Joystick/Gamepad mapping for the SNES buttons.
#define BUTTON_A 3
#define BUTTON_B 2
#define BUTTON_Y 1
#define BUTTON_X 4
#define BUTTON_UP 0
#define BUTTON_RIGHT 90
#define BUTTON_DOWN 180
#define BUTTON_LEFT 270
#define BUTTON_PAD_UNPRESSED -1
#define BUTTON_UP_RIGHT 45
#define BUTTON_UP_LEFT 315
#define BUTTON_DOWN_RIGHT 135
#define BUTTON_DOWN_LEFT 225
#define BUTTON_SELECT 9
#define BUTTON_START 10
#define BUTTON_L 5
#define BUTTON_R 6
// here be the latch, data and clock
// be sure your soldering is correct.
SNESpad controller = SNESpad(1,0,2);
int state = 0;
int prev_state = 0;
void setup() {
#ifdef DEBUGGING
Serial.begin(9600);
#endif
}
void loop() {
// by default the Joystick.{button,hat} call
// directly sends the button presses to the OS,
// this is not what we want.
Joystick.useManualSend(true);
state = controller.buttons();
//DEBUG();
if (CURRENTLY_PRESSED(SNES_A)){
Joystick.button(BUTTON_A, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_A)) {
Joystick.button(BUTTON_A, 0);
}
if (CURRENTLY_PRESSED(SNES_B)){
Joystick.button(BUTTON_B, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_B)) {
Joystick.button(BUTTON_B, 0);
}
if (CURRENTLY_PRESSED(SNES_Y)){
Joystick.button(BUTTON_Y, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_Y)) {
Joystick.button(BUTTON_Y, 0);
}
if (CURRENTLY_PRESSED(SNES_X)){
Joystick.button(BUTTON_X, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_X)) {
Joystick.button(BUTTON_X, 0);
}
// Here some nasty if statements for compatibility of
// gamepads. If a combination of directions is pressed send
// the right Joystick.hat angle, else just parse them as 'normal'
// direction buttons.
if (CURRENTLY_PRESSED(SNES_UP) && CURRENTLY_PRESSED(SNES_RIGHT)) {
Joystick.hat(BUTTON_UP_RIGHT);
}else if (CURRENTLY_PRESSED(SNES_UP) && CURRENTLY_PRESSED(SNES_LEFT)) {
Joystick.hat(BUTTON_UP_LEFT);
}else if (CURRENTLY_PRESSED(SNES_DOWN) && CURRENTLY_PRESSED(SNES_LEFT)) {
Joystick.hat(BUTTON_DOWN_LEFT);
}else if (CURRENTLY_PRESSED(SNES_DOWN) && CURRENTLY_PRESSED(SNES_RIGHT)) {
Joystick.hat(BUTTON_DOWN_RIGHT);
// No directional combinations are pressed in this state,
// handle them as normal directional presses.
}else{
if (CURRENTLY_PRESSED(SNES_UP)){
Joystick.hat(BUTTON_UP);
}
if (CURRENTLY_PRESSED(SNES_DOWN)){
Joystick.hat(BUTTON_DOWN);
}
if (CURRENTLY_PRESSED(SNES_LEFT)){
Joystick.hat(BUTTON_LEFT);
}
if (CURRENTLY_PRESSED(SNES_RIGHT)){
Joystick.hat(BUTTON_RIGHT);
}
}
if (CURRENTLY_PRESSED(SNES_START)){
Joystick.button(BUTTON_START, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_START)) {
Joystick.button(BUTTON_START, 0);
}
if (CURRENTLY_PRESSED(SNES_SELECT)){
Joystick.button(BUTTON_SELECT, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_SELECT)) {
Joystick.button(BUTTON_SELECT, 0);
}
if (CURRENTLY_PRESSED(SNES_L)){
Joystick.button(BUTTON_L, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_L)) {
Joystick.button(BUTTON_L, 0);
}
if (CURRENTLY_PRESSED(SNES_R)){
Joystick.button(BUTTON_R, 1);
}
else if (PREVIOUSLY_PRESSED(SNES_R)) {
Joystick.button(BUTTON_R, 0);
}
// keep a previous state to simulate button release.
prev_state = state;
if (!(CURRENTLY_PRESSED(SNES_LEFT) || CURRENTLY_PRESSED(SNES_RIGHT) || CURRENTLY_PRESSED(SNES_UP) || CURRENTLY_PRESSED(SNES_DOWN))) {
Joystick.hat(BUTTON_PAD_UNPRESSED);
}
Joystick.send_now();
}