2020-09-02 19:20:25 +00:00
|
|
|
/*
|
2021-01-26 03:03:26 +00:00
|
|
|
* Copyright (c) 2020-2021 ndeadly
|
2020-09-02 19:20:25 +00:00
|
|
|
*
|
2021-01-26 03:03:26 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
2020-09-02 19:20:25 +00:00
|
|
|
*
|
2021-01-26 03:03:26 +00:00
|
|
|
* This program is distributed in the hope 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.
|
2020-09-02 19:20:25 +00:00
|
|
|
*
|
|
|
|
* 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 "xiaomi_controller.hpp"
|
2021-06-14 20:01:51 +00:00
|
|
|
#include "controller_utils.hpp"
|
2020-09-02 19:20:25 +00:00
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::controller {
|
|
|
|
|
2020-09-03 14:56:57 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-09-04 15:55:37 +00:00
|
|
|
constexpr uint8_t init_packet[] = {0x20, 0x00, 0x00}; // packet to init vibration apparently
|
|
|
|
|
2020-09-03 14:56:57 +00:00
|
|
|
const constexpr float stick_scale_factor = float(UINT12_MAX) / UINT8_MAX;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-04 15:55:37 +00:00
|
|
|
Result XiaomiController::Initialize(void) {
|
|
|
|
R_TRY(EmulatedSwitchController::Initialize());
|
|
|
|
s_output_report.size = sizeof(init_packet);
|
|
|
|
std::memcpy(s_output_report.data, init_packet, sizeof(init_packet));
|
|
|
|
R_TRY(bluetooth::hid::report::SendHidReport(&m_address, &s_output_report));
|
|
|
|
return ams::ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void XiaomiController::UpdateControllerState(const bluetooth::HidReport *report) {
|
|
|
|
auto xiaomi_report = reinterpret_cast<const XiaomiReportData *>(&report->data);
|
2020-09-02 19:20:25 +00:00
|
|
|
|
|
|
|
switch(xiaomi_report->id) {
|
2020-09-03 14:56:57 +00:00
|
|
|
case 0x04:
|
2020-10-03 15:22:21 +00:00
|
|
|
this->HandleInputReport0x04(xiaomi_report);
|
2020-09-03 14:56:57 +00:00
|
|
|
break;
|
2020-09-02 19:20:25 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
void XiaomiController::HandleInputReport0x04(const XiaomiReportData *src) {
|
2021-06-14 20:01:51 +00:00
|
|
|
m_battery = convert_battery_100(src->input0x04.battery);
|
2020-09-04 16:01:37 +00:00
|
|
|
|
2021-06-20 20:39:28 +00:00
|
|
|
m_left_stick.SetData(
|
2020-09-03 14:56:57 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x04.left_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x04.left_stick.y)) & 0xfff
|
|
|
|
);
|
2021-06-20 20:39:28 +00:00
|
|
|
m_right_stick.SetData(
|
2020-09-03 14:56:57 +00:00
|
|
|
static_cast<uint16_t>(stick_scale_factor * src->input0x04.right_stick.x) & 0xfff,
|
|
|
|
static_cast<uint16_t>(stick_scale_factor * (UINT8_MAX - src->input0x04.right_stick.y)) & 0xfff
|
|
|
|
);
|
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.dpad_down = (src->input0x04.buttons.dpad == XiaomiDPad_S) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_SE) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_SW);
|
|
|
|
m_buttons.dpad_up = (src->input0x04.buttons.dpad == XiaomiDPad_N) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_NE) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_NW);
|
|
|
|
m_buttons.dpad_right = (src->input0x04.buttons.dpad == XiaomiDPad_E) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_NE) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_SE);
|
|
|
|
m_buttons.dpad_left = (src->input0x04.buttons.dpad == XiaomiDPad_W) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_NW) ||
|
|
|
|
(src->input0x04.buttons.dpad == XiaomiDPad_SW);
|
2020-09-03 14:56:57 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.A = src->input0x04.buttons.B;
|
|
|
|
m_buttons.B = src->input0x04.buttons.A;
|
|
|
|
m_buttons.X = src->input0x04.buttons.Y;
|
|
|
|
m_buttons.Y = src->input0x04.buttons.X;
|
2020-09-03 14:56:57 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.R = src->input0x04.buttons.R1;
|
|
|
|
m_buttons.ZR = src->input0x04.buttons.R2;
|
|
|
|
m_buttons.L = src->input0x04.buttons.L1;
|
|
|
|
m_buttons.ZL = src->input0x04.buttons.L2;
|
2020-09-03 14:56:57 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.minus = src->input0x04.buttons.back;
|
|
|
|
m_buttons.plus = src->input0x04.buttons.menu;
|
2020-09-03 14:56:57 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.lstick_press = src->input0x04.buttons.lstick_press;
|
|
|
|
m_buttons.rstick_press = src->input0x04.buttons.rstick_press;
|
2020-09-03 14:56:57 +00:00
|
|
|
|
2020-10-03 15:22:21 +00:00
|
|
|
m_buttons.home = src->input0x04.home;
|
2020-09-03 14:56:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 19:20:25 +00:00
|
|
|
}
|