forked from bootsector/XInputPadMicro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XInputPad.c
150 lines (123 loc) · 5.02 KB
/
XInputPad.c
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
/*
.k8GOGGNqkSFS5XkqXPSkSkkqXXFS5kSkSS15U22F2515U2uujuu1U1u2U1U2uUuFS.
:0qE JS5uuJuuFFX51jU2SSk12jU2SSXF5uuu15SFS5k12ujj21S5kFS5S12jJYu11
5XS: 1UYYLu. vUUX U22r SUF SUF ;YYLuU5
1F5i NNSkS7 2uLJui 51u S5. .PX .XX LJvLLu1.
kUk 0iLk5FFu vuYY2: 5F Xkk7 78 E0 i0 GEXPXk2uLLvLLuk
X25, 8O 2kX0 5YJUi M 555 PkXk i q1FU 7 ONNkP12YLvLvLYS
S25 8888 888 5uY5 FuS PS50 . FuUU 7 uJvLvLLJ2i
kUF SJjU. P02UF P25k . Su2Y v 2LLvLvLL17
S21 XJj88 0u 1uY2. X2k . k11E v 7;ii:JuJvLvLvJ2:
2257 jqv Pqq 1LJur PP. 7 EX: q OqqXP51JYvLvYYS.
X2F kXkXSXk kJYLU: O ,Z 0PXZ i ii q0 i:::,,.jLLvLLuF'
ik1k ;qkPj .uJvYu: UN : XU2F : S5S iJLLvjUF8
:PSq 72uLLLui uSi .; 2uY1 r. 72j1 LYYLYJSU88
XqE2 rP12juJuu1FX55U5FqXXSXkXF1juUkkPSXSPXPXPF1Jju5FkFSFXFSF5uujUu5j28V
.uGOZESS5S5SFkkPkPkXkPXqXPXqXXFkSkkPXPXPkqSkSS1521252121U2u2u12Suv7
*
* Arduino Micro (Leonardo) XInput Pad Emulator firmware
*
* Copyright (c) 2017
* Bruno Freitas - [email protected]
* Jon Wilson - [email protected]
* Kevin Mackett - [email protected]
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "XInputPad.h"
USB_JoystickReport_Data_t gamepad_state;
void (*padUSBConnectEventCallback)(void) = NULL;
void (*padUSBDisconnectEventCallback)(void) = NULL;
/** Event handler for the USB_Connect event. This indicates that the device is enumerating via the status LEDs and
* starts the library USB task to begin the enumeration and USB management process.
*/
void EVENT_USB_Device_Connect(void) {
if(padUSBConnectEventCallback)
padUSBConnectEventCallback();
}
/** Event handler for the USB_Disconnect event. This indicates that the device is no longer connected to a host via
* the status LEDs and stops the USB management and joystick reporting tasks.
*/
void EVENT_USB_Device_Disconnect(void) {
if(padUSBDisconnectEventCallback)
padUSBDisconnectEventCallback();
}
/** Event handler for the USB_ConfigurationChanged event. This is fired when the host set the current configuration
* of the USB device after enumeration - the device endpoints are configured and the joystick reporting task started.
*/
void EVENT_USB_Device_ConfigurationChanged(void) {
/* Setup HID Report Endpoint */
Endpoint_ConfigureEndpoint(JOYSTICK_EPADDR_IN, EP_TYPE_INTERRUPT, 20, 1);
Endpoint_ConfigureEndpoint((ENDPOINT_DIR_IN | 3), EP_TYPE_INTERRUPT, 32, 1);
}
/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
* the device from the USB host before passing along unhandled control requests to the library for processing
* internally.
*/
void EVENT_USB_Device_ControlRequest(void) {
/* Handle HID Class specific requests */
switch (USB_ControlRequest.bRequest) {
case HID_REQ_GetReport:
if (USB_ControlRequest.bmRequestType
== (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE)) {
Endpoint_ClearSETUP();
/* Write the report data to the control endpoint */
Endpoint_Write_Control_Stream_LE(&gamepad_state, 20);
Endpoint_ClearOUT();
}
break;
}
}
/** Function to manage HID report generation and transmission to the host. */
void HID_Task(void) {
/* Device must be connected and configured for the task to run */
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
/* Select the Joystick Report Endpoint */
Endpoint_SelectEndpoint(JOYSTICK_EPADDR_IN);
/* Check to see if the host is ready for another packet */
if (Endpoint_IsINReady()) {
/* Write Joystick Report Data */
Endpoint_Write_Stream_LE(&gamepad_state, 20, NULL);
/* Finalize the stream transfer to send the last packet */
Endpoint_ClearIN();
}
}
void xbox_reset_pad_status(void) {
memset(&gamepad_state, 0x00, sizeof(USB_JoystickReport_Data_t));
gamepad_state.rsize = 20;
}
void xbox_send_pad_state(void) {
HID_Task();
USB_USBTask();
}
void xbox_reset_watchdog(void) {
wdt_reset();
}
void xbox_init(bool watchdog) {
if (watchdog) {
wdt_enable(WDTO_2S);
} else {
wdt_disable();
}
xbox_reset_pad_status();
USB_Init();
sei();
}
void xbox_set_connect_callback(void (*callbackPtr)(void)) {
padUSBConnectEventCallback = callbackPtr;
}
void xbox_set_disconnect_callback(void (*callbackPtr)(void)) {
padUSBDisconnectEventCallback = callbackPtr;
}