-
Notifications
You must be signed in to change notification settings - Fork 1
/
Xpad.c
173 lines (165 loc) · 5.4 KB
/
Xpad.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <linux/joystick.h>
#include <stdint.h>
#include "Xpad.h"
#define SRC_ORIGINAL_XPAD 0
#define SRC_XBOX_360 1
uint8_t sourceType = SRC_XBOX_360;
uint8_t map(signed short x) {
long in_min = -32767;
long in_max = 32767;
long out_max = 255;
return (uint8_t) ((x - in_min) * (out_max) / (in_max - in_min));
}
void handle360(XpadReport_Data_t *dst, struct js_event *event) {
switch (event->type) {
case JS_EVENT_BUTTON: {
uint8_t button = (uint8_t) (event->value ? 0xFF : 0);
uint8_t buttonDigital = (uint8_t) (event->value ? 1 : 0);
switch (event->number) {
case 0:
dst->BUTTON_A = button;
break;
case 1:
dst->BUTTON_B = button;
break;
case 4:
dst->BUTTON_BLACK = button;
break;
case 2:
dst->BUTTON_X = button;
break;
case 3:
dst->BUTTON_Y = button;
break;
case 5:
dst->BUTTON_WHITE = button;
break;
case 6:
dst->BUTTON_BACK = buttonDigital;
break;
case 7:
dst->BUTTON_START = buttonDigital;
break;
case 8:
dst->UNUSED_1 = buttonDigital;
break;
case 9:
dst->BUTTON_L3 = buttonDigital;
break;
case 10:
dst->BUTTON_R3 = buttonDigital;
break;
default:
break;
}
break;
}
case JS_EVENT_AXIS: {
switch (event->number) {
case 0:
dst->LEFT_STICK_X = event->value;
break;
case 1:
dst->LEFT_STICK_Y = -event->value;
break;
case 2:
dst->TRIGGER_LEFT = map(event->value);
break;
case 3:
dst->RIGHT_STICK_X = event->value;
break;
case 4:
dst->RIGHT_STICK_Y = -event->value;
break;
case 5:
dst->TRIGGER_RIGHT = map(event->value);
break;
case 6:
dst->DPAD_LEFT = (uint8_t) (event->value == -32767 ? 1 : 0);
dst->DPAD_RIGHT = (uint8_t) (event->value == 32767 ? 1 : 0);
break;
case 7:
dst->DPAD_UP = (uint8_t) (event->value == -32767 ? 1 : 0);
dst->DPAD_DOWN = (uint8_t) (event->value == 32767 ? 1 : 0);
break;
default:
break;
}
break;
}
default:
return;
}
}
void handleOriginalXbox(XpadReport_Data_t *dst, struct js_event *event) {
switch (event->type) {
case JS_EVENT_BUTTON: {
uint8_t button = (uint8_t) (event->value ? 0xFF : 0);
switch (event->number) {
case 0:
dst->BUTTON_A = button;
break;
case 1:
dst->BUTTON_B = button;
break;
case 2:
dst->BUTTON_BLACK = button;
break;
case 3:
dst->BUTTON_X = button;
break;
case 4:
dst->BUTTON_Y = button;
break;
case 5:
dst->BUTTON_WHITE = button;
break;
default:
break;
}
break;
}
case JS_EVENT_AXIS: {
switch (event->number) {
case 0:
dst->LEFT_STICK_Y = event->value;
break;
case 1:
dst->LEFT_STICK_X = event->value;
break;
case 2:
dst->TRIGGER_LEFT = map(event->value);
break;
case 3:
dst->RIGHT_STICK_Y = event->value;
break;
case 4:
dst->RIGHT_STICK_X = event->value;
break;
case 5:
dst->TRIGGER_RIGHT = map(event->value);
break;
case 6:
dst->DPAD_LEFT = (uint8_t) (event->value == -32767 ? 1 : 0);
dst->DPAD_RIGHT = (uint8_t) (event->value == 32767 ? 1 : 0);
break;
case 7:
dst->DPAD_UP = (uint8_t) (event->value == -32767 ? 1 : 0);
dst->DPAD_DOWN = (uint8_t) (event->value == 32767 ? 1 : 0);
break;
default:
break;
}
break;
}
default:
return;
}
}
void convertEventToReport(XpadReport_Data_t *dst, struct js_event *event) {
if (sourceType == SRC_ORIGINAL_XPAD) {
handleOriginalXbox(dst, event);
} else if (sourceType == SRC_XBOX_360) {
handle360(dst, event);
}
}