-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path3ds.js
198 lines (155 loc) · 4.41 KB
/
3ds.js
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
const dgram = require('dgram');
const xinput = require('./xinput');
const socket = dgram.createSocket('udp4');
const config = require('./config/config');
const buttons = require('./config/button_map');
const M_SQRT1_2 = 1/Math.sqrt(2);
const CPAD_BOUND = 0x5d0;
const CPP_BOUND = 0x7f;
const gamePadConfig = { interval: config.interval };
const gamepad = xinput.WrapController(0, gamePadConfig);
startInputRedirection();
function startInputRedirection() {
setInterval(() => {
let state = xinput.GetState(0);
const hidPad = setButtons(state);
const touchScreenState = setTouchscreen(state);
const circlePadState = setCirclePad();
const newDSInputs = setNewDSInputs(state);
const interfaceButtons = 0;
const buffer = Buffer.allocUnsafe(20);
buffer.writeUInt32LE(hidPad, 0);
buffer.writeUInt32LE(touchScreenState, 4);
buffer.writeUInt32LE(circlePadState, 8);
buffer.writeUInt32LE(newDSInputs, 12);
buffer.writeUInt32LE(interfaceButtons, 16);
socket.send(buffer, config.port, config.ipAddress, err => {
if (err) {
console.log(err);
}
});
}, 5);
}
/**
* Map the appropriate XBox controller buttons
* to 3DS controls
*
* @param state
* @returns {number}
*/
function setButtons(state) {
let hidPad = 0xfff;
if (state.buttons.a) {
hidPad &= ~(1 << buttons.A);
}
if (state.buttons.b) {
hidPad &= ~(1 << buttons.B);
}
if (state.control.back) {
hidPad &= ~(1 << (buttons.SELECT));
}
if (state.control.start) {
hidPad &= ~(1 << (buttons.START));
}
if (state.dpad.right) {
hidPad &= ~(1 << (buttons.RIGHT));
}
if (state.dpad.left) {
hidPad &= ~(1 << (buttons.LEFT));
}
if (state.dpad.up) {
hidPad &= ~(1 << (buttons.UP));
}
if (state.dpad.down) {
hidPad &= ~(1 << (buttons.DOWN));
}
if (state.trigger.right) {
hidPad &= ~(1 << (buttons.R1));
}
if (state.trigger.left) {
hidPad &= ~(1 << (buttons.L1));
}
if (state.buttons.x) {
hidPad &= ~(1 << (buttons.X))
}
if (state.buttons.y) {
hidPad &= ~(1 << (buttons.Y))
}
return hidPad;
};
/**
* Map bumpers to R2 and L2
*
* @param state
* @returns {number}
*/
function setIRButtons(state) {
let irButtonState = 0;
if (state.shoulder.right) {
irButtonState |= 1 << (buttons.R2 - 11)
}
if (state.shoulder.left) {
irButtonState |= 1 << (buttons.L2 - 11)
}
return irButtonState;
}
/**
* Map the touchscreen
*
* @param state
* @returns {number}
*/
function setTouchscreen(state) {
return 0x2000000;
// TODO: Creaete a GUI touchscreen
}
/**
* Map the left joystick to the analog 3DS joystick
*
* @returns {number}
*/
function setCirclePad() {
let circlePadState = 0x7ff7ff;
const lx = gamepad.getNormalizedState('leftstick', 'x');
const ly = gamepad.getNormalizedState('leftstick', 'y');
if (lx != 0.0 || ly != 0.0) {
let x = parseInt(lx * CPAD_BOUND + 0x800);
let y = parseInt(ly * CPAD_BOUND + 0x800);
x = x >= 0xfff ? (lx < 0.0 ? 0x000 : 0xfff) : x;
y = y >= 0xfff ? (ly < 0.0 ? 0x000 : 0xfff) : y;
circlePadState = (y << 12) | x;
}
return circlePadState;
}
/**
* Map NEW 3DS L2, R2, and C-Stick to left and right
* bumpers and right joystick.
*
* @param state
* @returns {number}
*/
function setNewDSInputs(state) {
let cStickState = 0x80800081;
const rx = gamepad.getNormalizedState('rightstick', 'x');
const ry = gamepad.getNormalizedState('rightstick', 'y');
const irButtonsState = setIRButtons(state);
if (rx != 0.0 || ry != 0.0 || irButtonsState != 0) {
// We have to rotate the c-stick position 45°. Thanks, Nintendo.
let x = toUint32(M_SQRT1_2 * (rx + ry) * CPP_BOUND + 0x80);
let y = toUint32(M_SQRT1_2 * (ry - rx) * CPP_BOUND + 0x80);
x = x >= 0xff ? (rx < 0.0 ? 0x00 : 0xff) : x;
y = y >= 0xff ? (ry < 0.0 ? 0x00 : 0xff) : y;
cStickState = toUint32((y << 24) | (x << 16) | (irButtonsState << 8) | 0x81);
}
return cStickState;
}
function modulo(a, b) {
return a - Math.floor(a/b)*b;
}
function toUint32(x) {
return modulo(toInteger(x), Math.pow(2, 32));
}
function toInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) : Math.floor(x);
}