-
Notifications
You must be signed in to change notification settings - Fork 29
/
Controller.cpp
528 lines (471 loc) · 13 KB
/
Controller.cpp
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#include "Controller.hpp"
#include <array>
#include <algorithm>
#ifdef _DEBUG
#include <iostream>
#endif
#include <string>
#include <limits>
#include <vector>
#include "hidapi.h"
#include "XOutput.hpp"
#include "Config.hpp"
using namespace XOutput;
namespace Procon {
using std::array;
void SetDefaultCalibration(CalibrationData &dat) {
constexpr uchar ucmin = std::numeric_limits<uchar>::min();
constexpr uchar ucmax = std::numeric_limits<uchar>::max();
dat.leftCenter.x = ucmax / 2;
dat.leftCenter.y = ucmax / 2;
dat.rightCenter = dat.leftCenter;
dat.left.x.min = dat.leftCenter.x;
dat.left.x.max = dat.leftCenter.x;
dat.left.y = dat.left.x;
dat.right = dat.left;
}
void HIDCloser::operator()(hid_device *ptr) {
if (ptr != nullptr)
hid_close(ptr);
}
void zeroPadState(ExpandedPadState &state) {
state.xinState = { 0 };
state.leftStick = { 0 };
state.rightStick = { 0 };
state.sharePressed = false;
}
Controller::Controller(uchar port) :device(nullptr), port(port) {
SetDefaultCalibration(calib);
}
Controller::Controller(Controller &&) = default;
Controller& Controller::operator=(Controller &&) = default;
Controller::~Controller() {
if (_connected) {
XOutputUnPlug(port);
}
if (device) {
static const array<uchar, 2> disconnect{ 0x80, 0x05 };
exchange(disconnect);
}
}
};
namespace {
using std::array;
using Procon::uchar;
using Procon::StickPoint;
using Procon::StickRange;
using Procon::CalibrationData;
// openDevice
const array<uchar, 2> getMAC{ 0x80, 0x01 };
const array<uchar, 2> handshake{ 0x80, 0x02 };
const array<uchar, 2> switchBaudrate{ 0x80, 0x03 };
const array<uchar, 2> HIDOnlyMode{ 0x80, 0x04 };
const array<uchar, 1> enable{ 0x01 };
constexpr uchar rumbleCommand{ 0x48 };
constexpr uchar imuDataCommand{ 0x40 };
constexpr uchar ledCommand{ 0x30 };
const array<uchar, 1> led{ 0x1 };
// pollInput
constexpr uchar getInput{ 0x1f };
const array<uchar, 0> empty{};
struct InputPacket {
uint8_t header[8];
uint8_t unknown[5];
uint8_t rightButtons;
uint8_t middleButtons;
uint8_t leftButtons;
uint8_t sticks[6];
};
constexpr double lerp(double min, double max, double t) {
return (1.0 - t) * min + t * max;
}
// stick is current stick location, range is min/max of stick, center is center point of stick
void calibrateToRange(const StickPoint &stick, const StickRange &range, const StickPoint ¢er, short &outx, short &outy) {
constexpr short smax = std::numeric_limits<short>::max();
constexpr short smin = std::numeric_limits<short>::min();
outx = static_cast<short>(
smax * std::clamp(
(static_cast<double>(stick.x) - center.x) / static_cast<double>(range.x.max - range.x.min) * 2.0,
-1.0,
1.0
)
);
outy = static_cast<short>(
smax *std::clamp(
(static_cast<double>(stick.y) - center.y) / static_cast<double>(range.y.max - range.y.min) * 2.0,
-1.0,
1.0
)
);
}
short expandUChar(uchar c) {
constexpr uchar ucmax = std::numeric_limits<uchar>::max();
constexpr short smax = std::numeric_limits<short>::max();
constexpr short smin = std::numeric_limits<short>::min();
double d = std::clamp(static_cast<double>(c) / ucmax, 0.0, 1.0);
return static_cast<short>( lerp(smin, smax, d) );
}
};
namespace Procon {
void Controller::openDevice(hid_device_info *dev) {
using namespace std::this_thread;
using namespace std::chrono;
if (dev == nullptr)
throw ControllerException("Unable to open controller device: dev was nullptr.");
if (dev->product_id != Procon_ID)
throw ControllerException("Unable to open controller device: product id was not a Switch Pro Controller.");
device.reset(hid_open_path(dev->path));
if (!device)
throw ControllerException("Unable to open controller device: device path could not be opened.");
//vController.ProductId = dev->product_id;
//vController.VendorId = dev->vendor_id;
if (!exchange(handshake)) {
throw ControllerException("Handshake failed.");
}
exchange(switchBaudrate);
exchange(handshake);
exchange(HIDOnlyMode);
sendSubcommand(0x1, rumbleCommand, enable);
sendSubcommand(0x1, imuDataCommand, enable);
sendSubcommand(0x1, ledCommand, led);
if (XOutputPlugIn(port) != ERROR_SUCCESS) {
device.reset(nullptr);
throw ControllerException("Unable to plugin XOutput controller.");
}
_connected = true;
sleep_for(milliseconds(100));
//updateStatus();
}
};
namespace {
using std::vector;
using std::tuple;
using std::array;
using namespace Procon;
const std::array<Button, 8> JoyconLBitmap =
{
Button::DPadDown,
Button::DPadUp,
Button::DPadRight,
Button::DPadLeft,
Button::None,
Button::None,
Button::L,
Button::LZ
};
const std::array<Button, 8> JoyconRBitmap = {
Button::Y,
Button::X,
Button::B,
Button::A,
Button::None,
Button::None,
Button::R,
Button::RZ
};
const std::array<Button, 8> JoyconMidBitmap = {
Button::Minus,
Button::Plus,
Button::RStick,
Button::LStick,
Button::Home,
Button::Share,
Button::None,
Button::None
};
const array<Button, 8>& getButtonMap(ButtonSource s) {
switch (s) {
case ButtonSource::Left:
return JoyconLBitmap;
case ButtonSource::Middle:
return JoyconMidBitmap;
case ButtonSource::Right:
return JoyconRBitmap;
default:
throw std::logic_error("Unknown ButtonSource passed to getButtonMap");
}
}
void pullButtonsFromByte(uchar c, ButtonSource src, vector<tuple<Button, bool>> &out) {
const array<Button, 8>& map = getButtonMap(src);
for (uchar i{ 0 }; i < 8; ++i) {
if (map[i] == Button::None) continue;
out.push_back(std::make_tuple(map[i], (c & (1 << i)) != 0));
}
}
constexpr unsigned short buttonToReportBits(Button b){
switch (b) {
case Button::DPadUp:
return 0x0001;
case Button::DPadDown:
return 0x0002;
case Button::DPadLeft:
return 0x0004;
case Button::DPadRight:
return 0x0008;
case Button::Plus:
return 0x0010;
case Button::Minus:
return 0x0020;
case Button::LStick:
return 0x0040;
case Button::RStick:
return 0x0080;
case Button::L:
return 0x0100;
case Button::R:
return 0x0200;
case Button::Home:
return 0x0400; // Undocumented
// NOTICE: A and B are swapped, and X and Y are swapped.
case Button::A:
return 0x2000;
case Button::B:
return 0x1000;
case Button::X:
return 0x8000;
case Button::Y:
return 0x4000;
default:
return 0x0000;
}
}
constexpr unsigned short buttonToReportBitsOld(Button b) {
switch (b) {
case Button::DPadUp:
return 0x0001;
case Button::DPadDown:
return 0x0002;
case Button::DPadLeft:
return 0x0004;
case Button::DPadRight:
return 0x0008;
case Button::Plus:
return 0x0010;
case Button::Minus:
return 0x0020;
case Button::LStick:
return 0x0040;
case Button::RStick:
return 0x0080;
case Button::L:
return 0x0100;
case Button::R:
return 0x0200;
case Button::Home:
return 0x0400; // Undocumented
case Button::A:
return 0x1000;
case Button::B:
return 0x2000;
case Button::X:
return 0x4000;
case Button::Y:
return 0x8000;
default:
return 0x0000;
}
}
const std::string buttonConfigName{ "bMatchButtonLabels" };
void mapButtonToState(Button b, ExpandedPadState &state) {
switch (b) {
case Button::LZ:
state.xinState.bLeftTrigger = std::numeric_limits<BYTE>::max();
return;
case Button::RZ:
state.xinState.bRightTrigger = std::numeric_limits<BYTE>::max();
return;
case Button::Share:
state.sharePressed = true;
return;
default:
if (Config::get<bool>(buttonConfigName).value_or(false)) {
state.xinState.wButtons |= buttonToReportBitsOld(b);
}
else {
state.xinState.wButtons |= buttonToReportBits(b);
}
return;
}
}
void updateCalibrationRangeStick(const StickPoint &input, StickRange &cal) {
using std::max;
using std::min;
cal.x.max = max(cal.x.max, input.x);
cal.x.min = min(cal.x.min, input.x);
cal.y.max = max(cal.y.max, input.y);
cal.y.min = min(cal.y.min, input.y);
}
void updateCalibrationRange(const ExpandedPadState &state, CalibrationData &cal) {
updateCalibrationRangeStick(state.leftStick, cal.left);
updateCalibrationRangeStick(state.rightStick, cal.right);
}
#ifdef _DEBUG
using std::string;
const string buttonA{ "A" };
const string buttonB{ "B" };
const string buttonX{ "X" };
const string buttonY{ "Y" };
const string buttonLStick{ "Left Stick" };
const string buttonRStick{ "Right Stick" };
const string buttonL{ "L" };
const string buttonR{ "R" };
const string buttonLZ{ "LZ" };
const string buttonRZ{ "RZ" };
const string buttonHome{ "Home" };
const string buttonShare{ "Share" };
const string buttonPlus{ "Plus" };
const string buttonMinus{ "Minus" };
const string buttonDPUp{ "DPad Up" };
const string buttonDPLeft{ "DPad Left" };
const string buttonDPRight{ "DPad Right" };
const string buttonDPDown{ "DPad Down" };
const string buttonNone{ "None" };
const string buttonUnknown{ "Unknown" };
const std::string& buttonToString(Button b) {
switch (b) {
case Button::A:
return buttonA;
case Button::B:
return buttonB;
case Button::X:
return buttonX;
case Button::Y:
return buttonY;
case Button::LStick:
return buttonLStick;
case Button::RStick:
return buttonRStick;
case Button::L:
return buttonL;
case Button::LZ:
return buttonLZ;
case Button::R:
return buttonR;
case Button::RZ:
return buttonRZ;
case Button::Home:
return buttonHome;
case Button::Share:
return buttonShare;
case Button::Plus:
return buttonPlus;
case Button::Minus:
return buttonMinus;
case Button::DPadDown:
return buttonDPDown;
case Button::DPadUp:
return buttonDPUp;
case Button::DPadLeft:
return buttonDPLeft;
case Button::DPadRight:
return buttonDPRight;
case Button::None:
return buttonNone;
default:
return buttonUnknown;
}
}
#endif //#ifdef _DEBUG
void mapInputToState(const InputPacket &p, CalibrationData &cal, ExpandedPadState &state) {
state.leftStick.x = ((p.sticks[1] & 0x0F) << 4) | ((p.sticks[0] & 0xF0) >> 4);
state.leftStick.y = p.sticks[2];
state.rightStick.x = ((p.sticks[4] & 0x0F) << 4) | ((p.sticks[3] & 0xF0) >> 4);
state.rightStick.y = p.sticks[5];
updateCalibrationRange(state, cal);
// Sets state.xinState's sticks
calibrateToRange(state.leftStick, cal.left, cal.leftCenter, state.xinState.sThumbLX, state.xinState.sThumbLY);
calibrateToRange(state.rightStick, cal.right, cal.rightCenter, state.xinState.sThumbRX, state.xinState.sThumbRY);
vector<tuple<Button, bool>> buttons;
pullButtonsFromByte(p.leftButtons, ButtonSource::Left, buttons);
pullButtonsFromByte(p.rightButtons, ButtonSource::Right, buttons);
pullButtonsFromByte(p.middleButtons, ButtonSource::Middle, buttons);
for (auto pair : buttons) {
if (std::get<1>(pair)) {
#ifdef _DEBUG
std::cout << buttonToString(std::get<0>(pair)) << ' ';
#endif
mapButtonToState(std::get<0>(pair), state);
}
}
}
unsigned char operator ""_uc(unsigned long long t) {
return static_cast<unsigned char>(t);
}
}; //namespace
namespace Procon {
void Controller::pollInput() {
if (!device)
return;
auto dat = sendCommand(getInput, empty);
if (!dat) {
throw ControllerException("Error sending getInput command.");
}
if (dat.value()[0] != 0x30) {
InputPacket p;
memcpy(&p, dat.value().data(), sizeof(InputPacket));
zeroPadState(padStatus);
mapInputToState(p, calib, padStatus);
DWORD err;
if ((err = XOutputSetState(port, &padStatus.xinState)) != ERROR_SUCCESS) {
std::string errMsg{ "XOutput Error: " };
errMsg += std::to_string(err);
throw ControllerException(errMsg);
}
}
//updateStatus();
}
bool Controller::connected() const {
return _connected;
}
uchar Controller::getPort() const {
return port;
}
const ExpandedPadState& Controller::getState() const {
return padStatus;
}
void Controller::setCalibrationCenter(const StickPoint &left, const StickPoint &right) {
calib.leftCenter = left;
calib.rightCenter = right;
}
void Controller::updateStatus() {
if (clock::now() < lastStatus + std::chrono::milliseconds(100)) {
return;
}
uchar vibrate{ 0 };
uchar led{ 0 };
uchar smallMotor{ 0 };
uchar bigMotor{ 0 };
XOutputGetState(port, &vibrate, &bigMotor, &smallMotor, &led);
if (vibrate != 0) {
sendRumble(bigMotor, 0);
sendRumble(0, smallMotor);
}
array<uchar, 1> ledData{ static_cast<uchar>(0x1 << led) };
sendSubcommand(0x1, ledCommand, ledData);
lastStatus = clock::now();
}
Controller::exchangeArray Controller::sendRumble(uchar largeMotor, uchar smallMotor){
std::array<uchar, 9> buf{
static_cast<uchar>(rumbleCounter++ & 0xF),
0x80_uc,
0x00_uc,
0x40_uc,
0x40_uc,
0x80_uc,
0x00_uc,
0x40_uc,
0x40_uc
};
if (largeMotor != 0) {
buf[1] = buf[5] = 0x08;
buf[2] = buf[6] = largeMotor;
}
else if (smallMotor != 0) {
buf[1] = buf[5] = 0x10;
buf[2] = buf[6] = smallMotor;
}
return sendCommand(0x10, buf);
}
ControllerException::ControllerException(const std::string& what) : runtime_error(what) {}
ControllerException::ControllerException(const char* what) : runtime_error(what) {}
};