-
Notifications
You must be signed in to change notification settings - Fork 4
/
gamecube_mapping.c
223 lines (190 loc) · 6.54 KB
/
gamecube_mapping.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
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
/* GC to N64 : Gamecube controller to N64 adapter firmware
Copyright (C) 2011-2017 Raphael Assenat <[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 <stdlib.h>
#include "gamecube_mapping.h"
#include "eeprom.h"
#include "main.h"
// input
struct mapping_controller_unit g_gamecube_status[] = {
[MAP_GC_BTN_A] = { .type = TYPE_BTN, },
[MAP_GC_BTN_B] = { .type = TYPE_BTN, },
[MAP_GC_BTN_Z] = { .type = TYPE_BTN, },
[MAP_GC_BTN_START] = { .type = TYPE_BTN, },
[MAP_GC_BTN_L] = { .type = TYPE_BTN, },
[MAP_GC_BTN_R] = { .type = TYPE_BTN, },
[MAP_GC_AXB_C_UP] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_NEG, },
[MAP_GC_AXB_C_DOWN] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_AXB_C_LEFT] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_NEG, },
[MAP_GC_AXB_C_RIGHT] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_BTN_DPAD_UP] = { .type = TYPE_BTN, },
[MAP_GC_BTN_DPAD_DOWN] = { .type = TYPE_BTN, },
[MAP_GC_BTN_DPAD_LEFT] = { .type = TYPE_BTN, },
[MAP_GC_BTN_DPAD_RIGHT] = { .type = TYPE_BTN, },
[MAP_GC_AXIS_LEFT_RIGHT]= { .type = TYPE_AXIS },
[MAP_GC_AXIS_UP_DOWN] = { .type = TYPE_AXIS },
[MAP_GC_BTN_X] = { .type = TYPE_BTN, },
[MAP_GC_BTN_Y] = { .type = TYPE_BTN, },
[MAP_GC_AXB_JOY_UP] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_NEG, },
[MAP_GC_AXB_JOY_DOWN] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_AXB_JOY_LEFT] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_NEG, },
[MAP_GC_AXB_JOY_RIGHT] = { .type = TYPE_AXIS_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_AXB_L_SLIDER] = { .type = TYPE_NEG_SLD_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_AXB_R_SLIDER] = { .type = TYPE_NEG_SLD_TO_BTN, .thres = THRES_25_POS, },
[MAP_GC_AXIS_C_LEFT_RIGHT] = { .type = TYPE_AXIS },
[MAP_GC_AXIS_C_UP_DOWN] = { .type = TYPE_AXIS },
[MAP_GC_NONE] = { .type = TYPE_NONE },
};
static char gc_x_origin = 0;
static char gc_y_origin = 0;
void setOriginsFromReport(const unsigned char gcr[GC_REPORT_SIZE])
{
// Signed origin
gc_x_origin = gcr[0]^0x80;
gc_y_origin = gcr[1]^0x80;
}
/*
* \return N64 axis data (unsigned)
* */
int calb(char orig, unsigned char val)
{
short tmp;
long mult = 26000; // V1.6
char dz=0;
if (g_eeprom_data.conversion_mode == CONVERSION_MODE_OLD_1v5) {
mult = 25000;
}
if (g_eeprom_data.deadzone_enabled) {
dz = 12;
mult = 30000; // V1.6
if (g_eeprom_data.conversion_mode == CONVERSION_MODE_OLD_1v5) {
mult = 29000;
}
}
tmp = (signed char)(val^0x80) - orig;
if (dz) {
if (tmp > 0) {
if (tmp < dz) {
tmp = 0;
} else {
tmp -= dz;
}
}
else if (tmp < 0) {
if (tmp > -dz) {
tmp = 0;
} else {
tmp += dz;
}
}
}
/* Apply translation, unless we are in 'extended' mode where gamecube
* values are just sent directly without transformation. */
if (g_eeprom_data.conversion_mode != CONVERSION_MODE_EXTENDED) {
tmp = tmp * mult / 32000L;
}
if (tmp<=-127)
tmp = -127;
if (tmp>127)
tmp = 127;
return tmp;
}
void gamecubeXYtoN64(unsigned char x, unsigned char y, char *dst_x, char *dst_y)
{
unsigned char abs_y, abs_x;
long sig_x, sig_y;
long sx, sy;
int n64_maxval = 80; // Version 1.6
// The lower, the stronger the correction. 32768 means null correction
//long l = 1700;
//long l = 16000;
long l = 256; // Version 1.6
if (g_eeprom_data.conversion_mode == CONVERSION_MODE_EXTENDED) {
sig_x = calb(gc_x_origin, x);
sig_y = calb(gc_y_origin, y);
*dst_x = sig_x;
*dst_y = sig_y;
return;
}
if (g_eeprom_data.conversion_mode == CONVERSION_MODE_OLD_1v5) {
// Provide a way to use the old parameters, in case
// it turns out the new values are not good.
l = 512;
n64_maxval = 127;
}
sig_x = calb(gc_x_origin, x);
sig_y = calb(gc_y_origin, y);
abs_y = abs(sig_y);
abs_x = abs(sig_x);
if (1) {
sx = sig_x + sig_x * abs_y / l;
sy = sig_y + sig_y * abs_x / l;
} else {
// Direct conversion (for testing. Not good for use, corners do not
// reach maximum values)
sx = sig_x;
sy = sig_y;
}
if (sx<=-n64_maxval)
sx = -n64_maxval;
if (sx>n64_maxval)
sx = n64_maxval;
if (sy<=-n64_maxval)
sy = -n64_maxval;
if (sy>n64_maxval)
sy = n64_maxval;
*dst_x = sx;
*dst_y = sy;
}
/*
static unsigned char gamecubeXtoN64(unsigned char raw)
{
return calb(gc_x_origin, raw);
//return ((char)raw) * 24000L / 32767L;
}
static unsigned char gamecubeYtoN64(unsigned char raw)
{
return calb(gc_y_origin, raw);
//return ((char)raw) * 24000L / 32767L;
}
*/
void gc_report_to_mapping(const unsigned char gcr[GC_REPORT_SIZE], struct mapping_controller_unit *gcs)
{
gcs[MAP_GC_BTN_A].value = gcr[6] & 0x10;
gcs[MAP_GC_BTN_B].value = gcr[6] & 0x08;
gcs[MAP_GC_BTN_Z].value = gcr[6] & 0x80;
gcs[MAP_GC_BTN_START].value = gcr[6] & 0x01;
gcs[MAP_GC_BTN_L].value = gcr[6] & 0x20;
gcs[MAP_GC_BTN_R].value = gcr[6] & 0x40;
gcs[MAP_GC_AXB_C_UP].value = gcr[3] ^ 0x80;
gcs[MAP_GC_AXB_C_DOWN].value = gcr[3] ^ 0x80;
gcs[MAP_GC_AXB_C_LEFT].value = gcr[2] ^ 0x80;
gcs[MAP_GC_AXB_C_RIGHT].value = gcr[2] ^ 0x80;
gcs[MAP_GC_BTN_DPAD_UP].value = gcr[7] & 0x01;
gcs[MAP_GC_BTN_DPAD_DOWN].value = gcr[7] & 0x02;
gcs[MAP_GC_BTN_DPAD_LEFT].value = gcr[7] & 0x08;
gcs[MAP_GC_BTN_DPAD_RIGHT].value = gcr[7] & 0x04;
gamecubeXYtoN64(gcr[0], gcr[1], &gcs[MAP_GC_AXIS_LEFT_RIGHT].value, &gcs[MAP_GC_AXIS_UP_DOWN].value);
// gcs[MAP_GC_AXIS_LEFT_RIGHT].value = gamecubeXtoN64(gcr[0]);
// gcs[MAP_GC_AXIS_UP_DOWN].value = gamecubeYtoN64(gcr[1]);
gcs[MAP_GC_BTN_X].value = gcr[6] & 0x04;
gcs[MAP_GC_BTN_Y].value = gcr[6] & 0x02;
gcs[MAP_GC_AXB_JOY_UP].value = gcs[MAP_GC_AXIS_UP_DOWN].value;
gcs[MAP_GC_AXB_JOY_DOWN].value = gcs[MAP_GC_AXIS_UP_DOWN].value;
gcs[MAP_GC_AXB_JOY_LEFT].value = gcs[MAP_GC_AXIS_LEFT_RIGHT].value;
gcs[MAP_GC_AXB_JOY_RIGHT].value = gcs[MAP_GC_AXIS_LEFT_RIGHT].value;
gcs[MAP_GC_AXB_L_SLIDER].value = gcr[4] ^ 0x80;
gcs[MAP_GC_AXB_R_SLIDER].value = gcr[5] ^ 0x80;
gcs[MAP_GC_AXIS_C_LEFT_RIGHT].value = gcs[MAP_GC_AXB_C_LEFT].value;
gcs[MAP_GC_AXIS_C_UP_DOWN].value = gcs[MAP_GC_AXB_C_UP].value;
}