btdrv-mitm: improve handling of analog stick values

This commit is contained in:
ndeadly 2020-07-01 15:21:05 +02:00
parent 24de4fa145
commit 6177f83489
5 changed files with 17 additions and 57 deletions

View file

@ -1,5 +1,4 @@
#include <cstring>
#include <cmath>
#include <switch.h>
#include <stratosphere.hpp>
@ -10,7 +9,7 @@ namespace controller {
namespace {
const constexpr uint8_t dualshock4_joystick_nbits = 8;
const constexpr float scale_factor = float(UINT12_MAX) / UINT8_MAX;
}
@ -65,32 +64,11 @@ namespace controller {
}
}
void Dualshock4Controller::mapStickValues(SwitchStickData *dst, const Dualshock4StickData *src) {
dst->x = static_cast<uint16_t>(src->x * (powf(2, 12) - 1) / UINT8_MAX) & 0xfff;
dst->y = static_cast<uint16_t>((UINT8_MAX - src->y) * (powf(2, 12) - 1) / UINT8_MAX) & 0xfff;
/*
dst->dx = unsigned_to_signed(src->x, dualshock4_joystick_nbits);
dst->dy = -unsigned_to_signed(src->y, dualshock4_joystick_nbits);
float angle = atan2(dst->dy, dst->dx);
float magnitude = hypot(dst->dx, dst->dy);
if (magnitude < m_innerDeadzone) {
dst->dx = 0;
dst->dy = 0;
}
else if (magnitude > m_outerDeadzone) {
dst->dx = JOYSTICK_MAX * cos(angle);
dst->dy = JOYSTICK_MAX * sin(angle);
}
*/
}
void Dualshock4Controller::handleInputReport0x01(const Dualshock4ReportData *src, SwitchReportData *dst) {
this->mapStickValues(&dst->report0x30.left_stick, &src->report0x01.left_stick);
this->mapStickValues(&dst->report0x30.right_stick, &src->report0x01.right_stick);
dst->report0x30.left_stick.x = static_cast<uint16_t>(src->report0x01.left_stick.x * scale_factor) & 0xfff;
dst->report0x30.left_stick.y = static_cast<uint16_t>((UINT8_MAX - src->report0x01.left_stick.y) * scale_factor) & 0xfff;
dst->report0x30.right_stick.x = static_cast<uint16_t>(src->report0x01.right_stick.x * scale_factor) & 0xfff;
dst->report0x30.right_stick.y = static_cast<uint16_t>((UINT8_MAX - src->report0x01.right_stick.y) * scale_factor) & 0xfff;
dst->report0x30.buttons.dpad_down = (src->report0x01.buttons.dpad == Dualshock4DPad_S) ||
(src->report0x01.buttons.dpad == Dualshock4DPad_SE) ||
@ -126,8 +104,10 @@ namespace controller {
}
void Dualshock4Controller::handleInputReport0x11(const Dualshock4ReportData *src, SwitchReportData *dst) {
this->mapStickValues(&dst->report0x30.left_stick, &src->report0x11.left_stick);
this->mapStickValues(&dst->report0x30.right_stick, &src->report0x11.right_stick);
dst->report0x30.left_stick.x = static_cast<uint16_t>(src->report0x11.left_stick.x * scale_factor) & 0xfff;
dst->report0x30.left_stick.y = static_cast<uint16_t>((UINT8_MAX - src->report0x11.left_stick.y) * scale_factor) & 0xfff;
dst->report0x30.right_stick.x = static_cast<uint16_t>(src->report0x11.right_stick.x * scale_factor) & 0xfff;
dst->report0x30.right_stick.y = static_cast<uint16_t>((UINT8_MAX - src->report0x11.right_stick.y) * scale_factor) & 0xfff;
dst->report0x30.buttons.dpad_down = (src->report0x11.buttons.dpad == Dualshock4DPad_S) ||
(src->report0x11.buttons.dpad == Dualshock4DPad_SE) ||

View file

@ -2,7 +2,6 @@
#include "bluetoothcontroller.hpp"
#include "switchcontroller.hpp"
namespace controller {
enum Dualshock4ControllerVariant {
@ -113,7 +112,6 @@ namespace controller {
void convertReportFormat(const HidReport *inReport, HidReport *outReport);
private:
void mapStickValues(SwitchStickData *dst, const Dualshock4StickData *src);
void handleInputReport0x01(const Dualshock4ReportData *src, SwitchReportData *dst);
void handleInputReport0x11(const Dualshock4ReportData *src, SwitchReportData *dst);
};

View file

@ -1,6 +1,8 @@
#pragma once
#include "bluetoothcontroller.hpp"
#define UINT12_MAX 0xfff
namespace controller {
enum BatteryLevel {

View file

@ -1,5 +1,4 @@
#include <cstring>
#include <cmath>
#include <stratosphere.hpp>
#include "xboxone.hpp"
@ -8,7 +7,7 @@ namespace controller {
namespace {
const constexpr uint8_t xboxone_joystick_nbits = 16;
const constexpr float scale_factor = float(UINT12_MAX) / UINT16_MAX;
}
@ -41,30 +40,12 @@ namespace controller {
}
}
void XboxOneController::mapStickValues(SwitchStickData *dst, const XboxOneStickData *src) {
dst->x = static_cast<uint16_t>(src->x * (powf(2, 12) - 1) / UINT16_MAX) & 0xfff;
dst->y = static_cast<uint16_t>((UINT16_MAX - src->y) * (powf(2, 12) - 1) / UINT16_MAX) & 0xfff;
/*
dst->dx = unsigned_to_signed(src->x, xboxone_joystick_nbits);
dst->dy = -unsigned_to_signed(src->y, xboxone_joystick_nbits);
float angle = atan2(dst->dy, dst->dx);
float magnitude = hypot(dst->dx, dst->dy);
if (magnitude < m_innerDeadzone) {
dst->dx = 0;
dst->dy = 0;
}
else if (magnitude > m_outerDeadzone) {
dst->dx = JOYSTICK_MAX * cos(angle);
dst->dy = JOYSTICK_MAX * sin(angle);
}
*/
}
void XboxOneController::handleInputReport0x01(const XboxOneReportData *src, SwitchReportData *dst) {
this->mapStickValues(&dst->report0x30.left_stick, &src->report0x01.left_stick);
this->mapStickValues(&dst->report0x30.right_stick, &src->report0x01.right_stick);
dst->report0x30.left_stick.x = static_cast<uint16_t>(src->report0x01.left_stick.x * scale_factor) & 0xfff;
dst->report0x30.left_stick.y = static_cast<uint16_t>((UINT16_MAX - src->report0x01.left_stick.y) * scale_factor) & 0xfff;
dst->report0x30.right_stick.x = static_cast<uint16_t>(src->report0x01.right_stick.x * scale_factor) & 0xfff;
dst->report0x30.right_stick.y = static_cast<uint16_t>((UINT16_MAX - src->report0x01.right_stick.y) * scale_factor) & 0xfff;
dst->report0x30.buttons.dpad_down = (src->report0x01.buttons.dpad == XboxOneDPad_S) ||
(src->report0x01.buttons.dpad == XboxOneDPad_SE) ||

View file

@ -65,7 +65,6 @@ namespace controller {
void convertReportFormat(const HidReport *inReport, HidReport *outReport);
private:
void mapStickValues(SwitchStickData *dst, const XboxOneStickData *src);
void handleInputReport0x01(const XboxOneReportData *src, SwitchReportData *dst);
void handleInputReport0x02(const XboxOneReportData *src, SwitchReportData *dst);