/
DaemonBiteArcadeEncoder.ino
217 lines (190 loc) · 6.75 KB
/
DaemonBiteArcadeEncoder.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
/* DaemonBite Arcade Encoder
* Author: Mikael Norrgård <mick@daemonbite.com>
*
* Copyright (c) 2020 Mikael Norrgård <http://daemonbite.com>
*
* GNU GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include "Gamepad.h"
//#define PS3 // PS3 (ScpToolkit) compatibility (Comment out for joystick=X/Y-Axis and B11/B12 as normal buttons)
//#define NEOGEO
#define DEBOUNCE 0 // 1=Diddly-squat-Delay-Debouncing™ activated, 0=Debounce deactivated
#define DEBOUNCE_TIME 10 // Debounce time in milliseconds
//#define DEBUG // Enables debugging (sends debug data to usb serial)
#ifdef NEOGEO
const char *gp_serial = "NeoGeo to USB";
#else
const char *gp_serial = "Daemonbite Arcade";
#endif
#define UP 0x80
#define DOWN 0x40
#define LEFT 0x20
#define RIGHT 0x10
Gamepad_ Gamepad; // Set up USB HID gamepad
bool usbUpdate = false; // Should gamepad data be sent to USB?
bool debounce = DEBOUNCE; // Debounce?
uint8_t pin; // Used in for loops
uint32_t millisNow = 0; // Used for Diddly-squat-Delay-Debouncing™
uint8_t axesDirect = 0x0f;
uint8_t axes = 0x0f;
uint8_t axesPrev = 0x0f;
uint8_t axesBits[4] = {0x10,0x20,0x40,0x80};
uint32_t axesMillis[4];
uint16_t buttonsDirect = 0;
uint16_t buttons = 0;
uint16_t buttonsPrev = 0;
uint16_t buttonsBits[12] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800};
uint32_t buttonsMillis[12];
#ifdef DEBUG
char buf[16];
uint32_t millisSent = 0;
#endif
void setup()
{
// Axes
DDRF &= ~B11110000; // Set A0-A3 as inputs
PORTF |= B11110000; // Enable internal pull-up resistors
// Buttons
DDRD &= ~B10011111; // Set PD0-PD4 and PD7 as inputs
PORTD |= B10011111; // Enable internal pull-up resistors
DDRB &= ~B01111110; // Set PB1-PB6 as inputs
PORTB |= B01111110; // Enable internal pull-up resistors
// Debounce selector switch (currently disabled)
DDRE &= ~B01000000; // Pin 7 as input
PORTE |= B01000000; // Enable internal pull-up resistor
// Initialize debouncing timestamps
for(pin=0; pin<4; pin++)
axesMillis[pin]=0;
for(pin=0; pin<12; pin++)
buttonsMillis[pin]=0;
Gamepad.reset();
#ifdef DEBUG
Serial.begin(115200);
#endif
}
void loop()
{
// Get current time, the millis() function should take about 2µs to complete
millisNow = millis();
// Check debounce selector switch
// debounce = (PINE & B01000000) ? false : true;
for(uint8_t i=0; i<10; i++) // One iteration (when debounce is enabled) takes approximately 35µs to complete, so we don't need to check the time between every iteration
{
// Read axis and button inputs (bitwise NOT results in a 1 when button/axis pressed)
axesDirect = ~(PINF & B11110000);
buttonsDirect = ~((PIND & B00011111) | ((PIND & B10000000) << 4) | ((PINB & B01111110) << 4));
if(debounce)
{
// Debounce axes
for(pin=0; pin<4; pin++)
{
// Check if the current pin state is different to the stored state and that enough time has passed since last change
if((axesDirect & axesBits[pin]) != (axes & axesBits[pin]) && (millisNow - axesMillis[pin]) > DEBOUNCE_TIME)
{
// Toggle the pin, we can safely do this because we know the current state is different to the stored state
axes ^= axesBits[pin];
// Update the timestamp for the pin
axesMillis[pin] = millisNow;
}
}
// Debounce buttons
for(pin=0; pin<12; pin++)
{
// Check if the current pin state is different to the stored state and that enough time has passed since last change
if((buttonsDirect & buttonsBits[pin]) != (buttons & buttonsBits[pin]) && (millisNow - buttonsMillis[pin]) > DEBOUNCE_TIME)
{
// Toggle the pin, we can safely do this because we know the current state is different to the stored state
buttons ^= buttonsBits[pin];
// Update the timestamp for the pin
buttonsMillis[pin] = millisNow;
}
}
}
else
{
axes = axesDirect;
buttons = buttonsDirect;
}
// Has axis inputs changed?
if(axes != axesPrev)
{
#ifdef PS3
Gamepad._GamepadReport.hat = dpad2hat(axes);
#else
// UP + DOWN = UP, SOCD (Simultaneous Opposite Cardinal Directions) Cleaner
if(axes & B10000000)
Gamepad._GamepadReport.Y = -1;
else if(axes & B01000000)
Gamepad._GamepadReport.Y = 1;
else
Gamepad._GamepadReport.Y = 0;
// UP + DOWN = NEUTRAL
//Gamepad._GamepadReport.Y = ((axes & B01000000)>>6) - ((axes & B10000000)>>7);
// LEFT + RIGHT = NEUTRAL
Gamepad._GamepadReport.X = ((axes & B00010000)>>4) - ((axes & B00100000)>>5);
#endif
axesPrev = axes;
usbUpdate = true;
}
// Has button inputs changed?
if(buttons != buttonsPrev)
{
#ifdef PS3
Gamepad._GamepadReport.buttons = buttons & 0x3FF;
if(buttons & 0x400) // B11
Gamepad._GamepadReport.Z = -1;
else if(buttons & 0x800) // B12
Gamepad._GamepadReport.Z = 1;
else
Gamepad._GamepadReport.Z = 0;
#else
Gamepad._GamepadReport.buttons = buttons;
#endif
buttonsPrev = buttons;
usbUpdate = true;
}
// Should gamepad data be sent to USB?
if(usbUpdate)
{
Gamepad.send();
usbUpdate = false;
#ifdef DEBUG
sprintf(buf, "%06lu: %d%d%d%d", millisNow-millisSent, ((axes & 0x10)>>4), ((axes & 0x20)>>5), ((axes & 0x40)>>6), ((axes & 0x80)>>7) );
Serial.print(buf);
sprintf(buf, " %d%d%d%d", (buttons & 0x01), ((buttons & 0x02)>>1), ((buttons & 0x04)>>2), ((buttons & 0x08)>>3) );
Serial.println(buf);
millisSent = millisNow;
#endif
}
}
}
uint8_t dpad2hat(uint8_t dpad)
{
switch(dpad & (UP|DOWN|LEFT|RIGHT))
{
case UP: return 0;
case UP|RIGHT: return 1;
case RIGHT: return 2;
case DOWN|RIGHT: return 3;
case DOWN: return 4;
case DOWN|LEFT: return 5;
case LEFT: return 6;
case UP|LEFT: return 7;
}
return 15;
}