2020-07-09 19:20:08 +00:00
|
|
|
#include "dualshock4.hpp"
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
#include <cstring>
|
2020-06-10 21:16:08 +00:00
|
|
|
#include <switch.h>
|
2020-06-17 23:36:56 +00:00
|
|
|
#include <stratosphere.hpp>
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
#include "../btdrv_mitm_logging.hpp"
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
namespace ams::controller {
|
2020-06-02 21:24:40 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-07-11 11:52:56 +00:00
|
|
|
const constexpr float stickScaleFactor = float(UINT12_MAX) / UINT8_MAX;
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-10 19:37:31 +00:00
|
|
|
const Dualshock4LedColour playerLedColours[] = {
|
|
|
|
{0x00, 0x00, 0x7f}, // blue
|
|
|
|
{0x7f, 0x00, 0x00}, // red
|
|
|
|
{0x00, 0x7f, 0x00}, // green
|
|
|
|
{0x7f, 0x00, 0x7f} // pink
|
|
|
|
};
|
|
|
|
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
Dualshock4Controller::Dualshock4Controller(const bluetooth::Address *address)
|
2020-06-10 21:15:26 +00:00
|
|
|
: BluetoothController(ControllerType_Dualshock4, address) {
|
2020-06-30 21:19:05 +00:00
|
|
|
|
2020-06-10 21:15:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
Result Dualshock4Controller::initialize(void) {
|
2020-06-10 21:16:08 +00:00
|
|
|
R_TRY(BluetoothController::initialize());
|
2020-07-10 19:37:31 +00:00
|
|
|
|
|
|
|
u8 ind;
|
|
|
|
randomGet(&ind, 1);
|
|
|
|
auto colour = playerLedColours[ind >> 6];
|
|
|
|
|
|
|
|
Dualshock4OutputReport0x11 report = {0xa2, 0x11, 0xc0, 0x20, 0xf3, 0x04, 0x00, 0x00, 0x00, colour.r, colour.g, colour.b};
|
2020-06-10 21:16:08 +00:00
|
|
|
report.crc = crc32Calculate(report.data, sizeof(report.data));
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
bluetooth::HidReport hidReport = {};
|
|
|
|
hidReport.size = sizeof(report) - 1;
|
|
|
|
std::memcpy(&hidReport.data, &report.data[1], hidReport.size);
|
|
|
|
|
|
|
|
R_TRY(btdrvSetHidReport(&m_address, HidReportType_OutputReport, &hidReport));
|
2020-06-10 21:16:08 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-06-09 20:39:30 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
void Dualshock4Controller::convertReportFormat(const bluetooth::HidReport *inReport, bluetooth::HidReport *outReport) {
|
|
|
|
auto ds4Report = reinterpret_cast<const Dualshock4ReportData *>(&inReport->data);
|
|
|
|
auto switchReport = reinterpret_cast<SwitchReportData *>(&outReport->data);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
outReport->size = 0x31;
|
|
|
|
switchReport->id = 0x30;
|
|
|
|
switchReport->report0x30.conn_info = 0x0;
|
|
|
|
switchReport->report0x30.battery = 0x8;
|
2020-06-09 20:39:30 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
switch(ds4Report->id) {
|
2020-06-09 20:39:30 +00:00
|
|
|
case 0x01:
|
2020-07-11 11:43:21 +00:00
|
|
|
this->handleInputReport0x01(ds4Report, switchReport);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x11:
|
2020-07-11 11:43:21 +00:00
|
|
|
this->handleInputReport0x11(ds4Report, switchReport);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-07-11 11:43:21 +00:00
|
|
|
BTDRV_LOG_FMT("DS4: RECEIVED REPORT [0x%02x]", ds4Report->id);
|
2020-06-09 20:39:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
void Dualshock4Controller::handleInputReport0x01(const Dualshock4ReportData *src, SwitchReportData *dst) {
|
2020-07-01 23:34:13 +00:00
|
|
|
packStickData(&dst->report0x30.left_stick,
|
2020-07-11 11:52:56 +00:00
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->report0x01.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->report0x01.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
|
|
|
packStickData(&dst->report0x30.right_stick,
|
2020-07-11 11:52:56 +00:00
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->report0x01.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->report0x01.right_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
dst->report0x30.buttons.dpad_down = (src->report0x01.buttons.dpad == Dualshock4DPad_S) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_SE) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_SW);
|
|
|
|
dst->report0x30.buttons.dpad_up = (src->report0x01.buttons.dpad == Dualshock4DPad_N) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_NE) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_NW);
|
|
|
|
dst->report0x30.buttons.dpad_right = (src->report0x01.buttons.dpad == Dualshock4DPad_E) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_NE) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_SE);
|
|
|
|
dst->report0x30.buttons.dpad_left = (src->report0x01.buttons.dpad == Dualshock4DPad_W) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_NW) ||
|
|
|
|
(src->report0x01.buttons.dpad == Dualshock4DPad_SW);
|
|
|
|
|
|
|
|
dst->report0x30.buttons.A = src->report0x01.buttons.circle;
|
|
|
|
dst->report0x30.buttons.B = src->report0x01.buttons.cross;
|
|
|
|
dst->report0x30.buttons.X = src->report0x01.buttons.triangle;
|
|
|
|
dst->report0x30.buttons.Y = src->report0x01.buttons.square;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.R = src->report0x01.buttons.R1;
|
|
|
|
dst->report0x30.buttons.ZR = src->report0x01.buttons.R2;
|
|
|
|
dst->report0x30.buttons.L = src->report0x01.buttons.L1;
|
|
|
|
dst->report0x30.buttons.ZL = src->report0x01.buttons.L2;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.minus = src->report0x01.buttons.share;
|
|
|
|
dst->report0x30.buttons.plus = src->report0x01.buttons.options;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.lstick_press = src->report0x01.buttons.L3;
|
|
|
|
dst->report0x30.buttons.rstick_press = src->report0x01.buttons.R3;
|
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
dst->report0x30.buttons.capture = src->report0x01.buttons.tpad;
|
|
|
|
dst->report0x30.buttons.home = src->report0x01.buttons.ps;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 20:39:30 +00:00
|
|
|
void Dualshock4Controller::handleInputReport0x11(const Dualshock4ReportData *src, SwitchReportData *dst) {
|
2020-07-09 19:22:14 +00:00
|
|
|
//dst->report0x30.battery = (((src->report0x11.battery / 64) + 1) << 1) & 0xf ;
|
|
|
|
|
2020-07-01 23:34:13 +00:00
|
|
|
packStickData(&dst->report0x30.left_stick,
|
2020-07-11 11:52:56 +00:00
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->report0x11.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->report0x11.left_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
|
|
|
packStickData(&dst->report0x30.right_stick,
|
2020-07-11 11:52:56 +00:00
|
|
|
static_cast<uint16_t>(stickScaleFactor * src->report0x11.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stickScaleFactor * (UINT8_MAX - src->report0x11.right_stick.y)) & 0xfff
|
2020-07-01 23:34:13 +00:00
|
|
|
);
|
2020-06-09 20:39:30 +00:00
|
|
|
|
|
|
|
dst->report0x30.buttons.dpad_down = (src->report0x11.buttons.dpad == Dualshock4DPad_S) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_SE) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_SW);
|
|
|
|
dst->report0x30.buttons.dpad_up = (src->report0x11.buttons.dpad == Dualshock4DPad_N) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_NE) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_NW);
|
|
|
|
dst->report0x30.buttons.dpad_right = (src->report0x11.buttons.dpad == Dualshock4DPad_E) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_NE) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_SE);
|
|
|
|
dst->report0x30.buttons.dpad_left = (src->report0x11.buttons.dpad == Dualshock4DPad_W) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_NW) ||
|
|
|
|
(src->report0x11.buttons.dpad == Dualshock4DPad_SW);
|
|
|
|
|
|
|
|
dst->report0x30.buttons.A = src->report0x11.buttons.circle;
|
|
|
|
dst->report0x30.buttons.B = src->report0x11.buttons.cross;
|
|
|
|
dst->report0x30.buttons.X = src->report0x11.buttons.triangle;
|
|
|
|
dst->report0x30.buttons.Y = src->report0x11.buttons.square;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.R = src->report0x11.buttons.R1;
|
|
|
|
dst->report0x30.buttons.ZR = src->report0x11.buttons.R2;
|
|
|
|
dst->report0x30.buttons.L = src->report0x11.buttons.L1;
|
|
|
|
dst->report0x30.buttons.ZL = src->report0x11.buttons.L2;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.minus = src->report0x11.buttons.share;
|
|
|
|
dst->report0x30.buttons.plus = src->report0x11.buttons.options;
|
|
|
|
|
|
|
|
dst->report0x30.buttons.lstick_press = src->report0x11.buttons.L3;
|
|
|
|
dst->report0x30.buttons.rstick_press = src->report0x11.buttons.R3;
|
2020-06-02 21:24:40 +00:00
|
|
|
|
2020-07-11 11:43:21 +00:00
|
|
|
dst->report0x30.buttons.capture = src->report0x11.buttons.tpad;
|
|
|
|
dst->report0x30.buttons.home = src->report0x11.buttons.ps;
|
2020-06-02 21:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|