2023-10-01 04:12:25 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com> and contributors.
|
2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
#include "analog_controller.h"
|
2022-07-11 13:03:29 +00:00
|
|
|
#include "host.h"
|
2020-12-11 08:59:40 +00:00
|
|
|
#include "settings.h"
|
2020-05-08 05:13:05 +00:00
|
|
|
#include "system.h"
|
2023-08-27 06:00:06 +00:00
|
|
|
|
|
|
|
#include "util/imgui_manager.h"
|
2023-08-23 12:06:48 +00:00
|
|
|
#include "util/input_manager.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/state_wrapper.h"
|
2023-08-27 06:00:06 +00:00
|
|
|
|
2023-10-01 04:12:25 +00:00
|
|
|
#include "common/bitutils.h"
|
2023-08-27 06:00:06 +00:00
|
|
|
#include "common/log.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
|
|
|
|
#include "IconsFontAwesome5.h"
|
|
|
|
|
2020-09-17 14:40:54 +00:00
|
|
|
#include <cmath>
|
2023-08-27 06:00:06 +00:00
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
Log_SetChannel(AnalogController);
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
AnalogController::AnalogController(u32 index) : Controller(index)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2022-08-10 03:03:15 +00:00
|
|
|
m_status_byte = 0x5A;
|
2019-12-15 11:58:27 +00:00
|
|
|
m_axis_state.fill(0x80);
|
2022-08-10 03:03:15 +00:00
|
|
|
m_rumble_config.fill(0xFF);
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
AnalogController::~AnalogController() = default;
|
|
|
|
|
|
|
|
ControllerType AnalogController::GetType() const
|
|
|
|
{
|
|
|
|
return ControllerType::AnalogController;
|
|
|
|
}
|
|
|
|
|
2022-10-09 04:10:42 +00:00
|
|
|
bool AnalogController::InAnalogMode() const
|
|
|
|
{
|
|
|
|
return m_analog_mode;
|
|
|
|
}
|
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
void AnalogController::Reset()
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
m_command = Command::Idle;
|
|
|
|
m_command_step = 0;
|
|
|
|
m_rx_buffer.fill(0x00);
|
|
|
|
m_tx_buffer.fill(0x00);
|
2019-12-15 11:58:27 +00:00
|
|
|
m_analog_mode = false;
|
|
|
|
m_configuration_mode = false;
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
for (u32 i = 0; i < NUM_MOTORS; i++)
|
|
|
|
{
|
|
|
|
if (m_motor_state[i] != 0)
|
|
|
|
SetMotorState(i, 0);
|
|
|
|
}
|
2020-06-30 14:33:45 +00:00
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
m_dualshock_enabled = false;
|
2020-11-25 01:16:54 +00:00
|
|
|
ResetRumbleConfig();
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
m_status_byte = 0x5A;
|
|
|
|
|
2020-12-11 00:48:19 +00:00
|
|
|
if (m_force_analog_on_reset)
|
2020-12-11 08:59:40 +00:00
|
|
|
{
|
2023-05-08 03:07:19 +00:00
|
|
|
if (g_settings.controller_disable_analog_mode_forcing || System::IsRunningUnknownGame())
|
2020-12-11 08:59:40 +00:00
|
|
|
{
|
2022-08-10 03:03:15 +00:00
|
|
|
Host::AddIconOSDMessage(
|
|
|
|
fmt::format("Controller{}AnalogMode", m_index), ICON_FA_GAMEPAD,
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_STR("OSDMessage",
|
|
|
|
"Analog mode forcing is disabled by game settings. Controller will start in digital mode."),
|
2020-12-11 08:59:40 +00:00
|
|
|
10.0f);
|
|
|
|
}
|
|
|
|
else
|
2022-08-10 03:03:15 +00:00
|
|
|
{
|
|
|
|
SetAnalogMode(true, false);
|
|
|
|
}
|
2020-12-11 08:59:40 +00:00
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 14:09:32 +00:00
|
|
|
bool AnalogController::DoState(StateWrapper& sw, bool apply_input_state)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2020-12-16 14:09:32 +00:00
|
|
|
if (!Controller::DoState(sw, apply_input_state))
|
2019-12-15 11:58:27 +00:00
|
|
|
return false;
|
|
|
|
|
2020-05-08 05:13:05 +00:00
|
|
|
const bool old_analog_mode = m_analog_mode;
|
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
sw.Do(&m_analog_mode);
|
2021-04-14 08:13:34 +00:00
|
|
|
sw.Do(&m_dualshock_enabled);
|
2020-11-13 15:23:19 +00:00
|
|
|
sw.DoEx(&m_legacy_rumble_unlocked, 44, false);
|
2019-12-15 11:58:27 +00:00
|
|
|
sw.Do(&m_configuration_mode);
|
|
|
|
sw.Do(&m_command_param);
|
2021-04-14 08:13:34 +00:00
|
|
|
sw.DoEx(&m_status_byte, 55, static_cast<u8>(0x5A));
|
2020-12-16 14:09:32 +00:00
|
|
|
|
|
|
|
u16 button_state = m_button_state;
|
|
|
|
sw.DoEx(&button_state, 44, static_cast<u16>(0xFFFF));
|
|
|
|
if (apply_input_state)
|
|
|
|
m_button_state = button_state;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
sw.Do(&m_command);
|
2019-12-15 13:22:53 +00:00
|
|
|
|
2020-11-25 01:16:54 +00:00
|
|
|
sw.DoEx(&m_rumble_config, 45, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
|
|
|
|
sw.DoEx(&m_rumble_config_large_motor_index, 45, -1);
|
|
|
|
sw.DoEx(&m_rumble_config_small_motor_index, 45, -1);
|
|
|
|
sw.DoEx(&m_analog_toggle_queued, 45, false);
|
|
|
|
|
2019-12-15 13:22:53 +00:00
|
|
|
MotorState motor_state = m_motor_state;
|
|
|
|
sw.Do(&motor_state);
|
|
|
|
|
|
|
|
if (sw.IsReading())
|
|
|
|
{
|
|
|
|
for (u8 i = 0; i < NUM_MOTORS; i++)
|
|
|
|
SetMotorState(i, motor_state[i]);
|
2020-05-08 05:13:05 +00:00
|
|
|
|
|
|
|
if (old_analog_mode != m_analog_mode)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddIconOSDMessage(fmt::format("Controller{}AnalogMode", m_index), ICON_FA_GAMEPAD,
|
|
|
|
fmt::format(m_analog_mode ?
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} switched to analog mode.") :
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} switched to digital mode."),
|
|
|
|
m_index + 1u),
|
|
|
|
5.0f);
|
2020-05-08 05:13:05 +00:00
|
|
|
}
|
2019-12-15 13:22:53 +00:00
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float AnalogController::GetBindState(u32 index) const
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (index >= static_cast<u32>(Button::Count))
|
|
|
|
{
|
|
|
|
const u32 sub_index = index - static_cast<u32>(Button::Count);
|
|
|
|
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
|
|
|
|
return 0.0f;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
return static_cast<float>(m_half_axis_state[sub_index]) * (1.0f / 255.0f);
|
|
|
|
}
|
|
|
|
else if (index < static_cast<u32>(Button::Analog))
|
|
|
|
{
|
|
|
|
return static_cast<float>(((m_button_state >> index) & 1u) ^ 1u);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-04-03 17:51:08 +00:00
|
|
|
return 0.0f;
|
2022-07-11 13:03:29 +00:00
|
|
|
}
|
2021-04-03 17:51:08 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void AnalogController::SetBindState(u32 index, float value)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
if (index == static_cast<s32>(Button::Analog))
|
|
|
|
{
|
|
|
|
// analog toggle
|
2022-09-22 03:50:31 +00:00
|
|
|
if (value >= m_button_deadzone)
|
2022-07-11 13:03:29 +00:00
|
|
|
{
|
|
|
|
if (m_command == Command::Idle)
|
|
|
|
ProcessAnalogModeToggle();
|
|
|
|
else
|
|
|
|
m_analog_toggle_queued = true;
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (index >= static_cast<u32>(Button::Count))
|
|
|
|
{
|
|
|
|
const u32 sub_index = index - static_cast<u32>(Button::Count);
|
|
|
|
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
|
|
|
|
return;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-10-21 11:14:27 +00:00
|
|
|
const u8 u8_value = static_cast<u8>(std::clamp(value * m_analog_sensitivity * 255.0f, 0.0f, 255.0f));
|
2022-07-11 13:03:29 +00:00
|
|
|
if (u8_value == m_half_axis_state[sub_index])
|
|
|
|
return;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
m_half_axis_state[sub_index] = u8_value;
|
2021-01-23 16:52:52 +00:00
|
|
|
System::SetRunaheadReplayFlag();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#define MERGE(pos, neg) \
|
|
|
|
((m_half_axis_state[static_cast<u32>(pos)] != 0) ? (127u + ((m_half_axis_state[static_cast<u32>(pos)] + 1u) / 2u)) : \
|
|
|
|
(127u - (m_half_axis_state[static_cast<u32>(neg)] / 2u)))
|
|
|
|
switch (static_cast<HalfAxis>(sub_index))
|
|
|
|
{
|
|
|
|
case HalfAxis::LLeft:
|
|
|
|
case HalfAxis::LRight:
|
2022-09-22 03:50:31 +00:00
|
|
|
m_axis_state[static_cast<u8>(Axis::LeftX)] = ((m_invert_left_stick & 1u) != 0u) ?
|
|
|
|
MERGE(HalfAxis::LLeft, HalfAxis::LRight) :
|
|
|
|
MERGE(HalfAxis::LRight, HalfAxis::LLeft);
|
2022-07-11 13:03:29 +00:00
|
|
|
break;
|
2021-04-03 17:51:08 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
case HalfAxis::LDown:
|
|
|
|
case HalfAxis::LUp:
|
2022-09-22 03:50:31 +00:00
|
|
|
m_axis_state[static_cast<u8>(Axis::LeftY)] = ((m_invert_left_stick & 2u) != 0u) ?
|
|
|
|
MERGE(HalfAxis::LUp, HalfAxis::LDown) :
|
|
|
|
MERGE(HalfAxis::LDown, HalfAxis::LUp);
|
2022-07-11 13:03:29 +00:00
|
|
|
break;
|
2021-04-03 17:51:08 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
case HalfAxis::RLeft:
|
|
|
|
case HalfAxis::RRight:
|
2022-09-22 03:50:31 +00:00
|
|
|
m_axis_state[static_cast<u8>(Axis::RightX)] = ((m_invert_right_stick & 1u) != 0u) ?
|
|
|
|
MERGE(HalfAxis::RLeft, HalfAxis::RRight) :
|
|
|
|
MERGE(HalfAxis::RRight, HalfAxis::RLeft);
|
2022-07-11 13:03:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case HalfAxis::RDown:
|
|
|
|
case HalfAxis::RUp:
|
2022-09-22 03:50:31 +00:00
|
|
|
m_axis_state[static_cast<u8>(Axis::RightY)] = ((m_invert_right_stick & 2u) != 0u) ?
|
|
|
|
MERGE(HalfAxis::RUp, HalfAxis::RDown) :
|
|
|
|
MERGE(HalfAxis::RDown, HalfAxis::RUp);
|
2022-07-11 13:03:29 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2021-02-22 00:12:41 +00:00
|
|
|
}
|
2020-05-08 05:13:05 +00:00
|
|
|
|
2022-10-21 11:14:27 +00:00
|
|
|
if (m_analog_deadzone > 0.0f)
|
|
|
|
{
|
|
|
|
#define MERGE_F(pos, neg) \
|
|
|
|
((m_half_axis_state[static_cast<u32>(pos)] != 0) ? \
|
|
|
|
(static_cast<float>(m_half_axis_state[static_cast<u32>(pos)]) / 255.0f) : \
|
|
|
|
(static_cast<float>(m_half_axis_state[static_cast<u32>(neg)]) / -255.0f))
|
|
|
|
|
|
|
|
float pos_x, pos_y;
|
|
|
|
if (static_cast<HalfAxis>(sub_index) < HalfAxis::RLeft)
|
|
|
|
{
|
|
|
|
pos_x = ((m_invert_left_stick & 1u) != 0u) ? MERGE_F(HalfAxis::LLeft, HalfAxis::LRight) :
|
|
|
|
MERGE_F(HalfAxis::LRight, HalfAxis::LLeft);
|
|
|
|
pos_y = ((m_invert_left_stick & 2u) != 0u) ? MERGE_F(HalfAxis::LUp, HalfAxis::LDown) :
|
|
|
|
MERGE_F(HalfAxis::LDown, HalfAxis::LUp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos_x = ((m_invert_right_stick & 1u) != 0u) ? MERGE_F(HalfAxis::RLeft, HalfAxis::RRight) :
|
|
|
|
MERGE_F(HalfAxis::RRight, HalfAxis::RLeft);
|
|
|
|
;
|
|
|
|
pos_y = ((m_invert_right_stick & 2u) != 0u) ? MERGE_F(HalfAxis::RUp, HalfAxis::RDown) :
|
|
|
|
MERGE_F(HalfAxis::RDown, HalfAxis::RUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InCircularDeadzone(m_analog_deadzone, pos_x, pos_y))
|
|
|
|
{
|
|
|
|
// Set to 127 (center).
|
|
|
|
if (static_cast<HalfAxis>(sub_index) < HalfAxis::RLeft)
|
|
|
|
m_axis_state[static_cast<u8>(Axis::LeftX)] = m_axis_state[static_cast<u8>(Axis::LeftY)] = 127;
|
|
|
|
else
|
|
|
|
m_axis_state[static_cast<u8>(Axis::RightX)] = m_axis_state[static_cast<u8>(Axis::RightY)] = 127;
|
|
|
|
}
|
|
|
|
#undef MERGE_F
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#undef MERGE
|
|
|
|
|
2020-05-08 05:13:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
const u16 bit = u16(1) << static_cast<u8>(index);
|
2021-01-23 16:52:52 +00:00
|
|
|
|
2022-09-22 03:50:31 +00:00
|
|
|
if (value >= m_button_deadzone)
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
|
|
|
if (m_button_state & bit)
|
|
|
|
System::SetRunaheadReplayFlag();
|
|
|
|
|
|
|
|
m_button_state &= ~(bit);
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
else
|
2021-01-23 16:52:52 +00:00
|
|
|
{
|
|
|
|
if (!(m_button_state & bit))
|
|
|
|
System::SetRunaheadReplayFlag();
|
|
|
|
|
|
|
|
m_button_state |= bit;
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 05:47:00 +00:00
|
|
|
u32 AnalogController::GetButtonStateBits() const
|
|
|
|
{
|
|
|
|
// flip bits, native data is active low
|
|
|
|
return m_button_state ^ 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2021-01-04 06:54:16 +00:00
|
|
|
std::optional<u32> AnalogController::GetAnalogInputBytes() const
|
|
|
|
{
|
|
|
|
return m_axis_state[static_cast<size_t>(Axis::LeftY)] << 24 | m_axis_state[static_cast<size_t>(Axis::LeftX)] << 16 |
|
|
|
|
m_axis_state[static_cast<size_t>(Axis::RightY)] << 8 | m_axis_state[static_cast<size_t>(Axis::RightX)];
|
|
|
|
}
|
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
void AnalogController::ResetTransferState()
|
|
|
|
{
|
2020-11-24 01:35:34 +00:00
|
|
|
if (m_analog_toggle_queued)
|
|
|
|
{
|
2021-02-22 00:12:41 +00:00
|
|
|
ProcessAnalogModeToggle();
|
2020-11-24 01:35:34 +00:00
|
|
|
m_analog_toggle_queued = false;
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
m_command = Command::Idle;
|
|
|
|
m_command_step = 0;
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 03:03:15 +00:00
|
|
|
void AnalogController::SetAnalogMode(bool enabled, bool show_message)
|
2019-12-15 13:22:53 +00:00
|
|
|
{
|
|
|
|
if (m_analog_mode == enabled)
|
|
|
|
return;
|
|
|
|
|
2020-05-08 05:32:39 +00:00
|
|
|
Log_InfoPrintf("Controller %u switched to %s mode.", m_index + 1u, enabled ? "analog" : "digital");
|
2022-08-10 03:03:15 +00:00
|
|
|
if (show_message)
|
|
|
|
{
|
2023-08-19 14:08:03 +00:00
|
|
|
Host::AddIconOSDMessage(fmt::format("Controller{}AnalogMode", m_index), ICON_FA_GAMEPAD,
|
|
|
|
fmt::format(enabled ?
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} switched to analog mode.") :
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} switched to digital mode."),
|
|
|
|
m_index + 1u),
|
|
|
|
5.0f);
|
2022-08-10 03:03:15 +00:00
|
|
|
}
|
2019-12-15 13:22:53 +00:00
|
|
|
m_analog_mode = enabled;
|
|
|
|
}
|
|
|
|
|
2021-02-22 00:12:41 +00:00
|
|
|
void AnalogController::ProcessAnalogModeToggle()
|
|
|
|
{
|
|
|
|
if (m_analog_locked)
|
|
|
|
{
|
2022-08-10 03:03:15 +00:00
|
|
|
Host::AddIconOSDMessage(
|
|
|
|
fmt::format("Controller{}AnalogMode", m_index), ICON_FA_GAMEPAD,
|
2023-08-19 14:08:03 +00:00
|
|
|
fmt::format(m_analog_mode ?
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} is locked to analog mode by the game.") :
|
|
|
|
TRANSLATE_FS("AnalogController", "Controller {} is locked to digital mode by the game."),
|
2022-08-10 03:03:15 +00:00
|
|
|
m_index + 1u),
|
|
|
|
5.0f);
|
2021-02-22 00:12:41 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-10 03:03:15 +00:00
|
|
|
SetAnalogMode(!m_analog_mode, true);
|
2021-02-22 00:12:41 +00:00
|
|
|
ResetRumbleConfig();
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
|
|
|
m_status_byte = 0x00;
|
2021-02-22 00:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void AnalogController::SetMotorState(u32 motor, u8 value)
|
2019-12-15 13:22:53 +00:00
|
|
|
{
|
|
|
|
DebugAssert(motor < NUM_MOTORS);
|
2022-07-11 13:03:29 +00:00
|
|
|
if (m_motor_state[motor] != value)
|
|
|
|
{
|
|
|
|
m_motor_state[motor] = value;
|
|
|
|
UpdateHostVibration();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalogController::UpdateHostVibration()
|
|
|
|
{
|
|
|
|
std::array<float, NUM_MOTORS> hvalues;
|
|
|
|
for (u32 motor = 0; motor < NUM_MOTORS; motor++)
|
|
|
|
{
|
|
|
|
// Curve from https://github.com/KrossX/Pokopom/blob/master/Pokopom/Input_XInput.cpp#L210
|
|
|
|
const u8 state = m_motor_state[motor];
|
2022-07-25 07:36:38 +00:00
|
|
|
const double x = static_cast<double>(std::min<u32>(state + static_cast<u32>(m_rumble_bias), 255));
|
2022-07-11 13:03:29 +00:00
|
|
|
const double strength = 0.006474549734772402 * std::pow(x, 3.0) - 1.258165252213538 * std::pow(x, 2.0) +
|
|
|
|
156.82454281087692 * x + 3.637978807091713e-11;
|
|
|
|
|
|
|
|
hvalues[motor] = (state != 0) ? static_cast<float>(strength / 65535.0) : 0.0f;
|
|
|
|
}
|
|
|
|
|
2023-08-23 12:06:48 +00:00
|
|
|
InputManager::SetPadVibrationIntensity(m_index, hvalues[0], hvalues[1]);
|
2019-12-15 13:22:53 +00:00
|
|
|
}
|
|
|
|
|
2020-10-29 14:30:04 +00:00
|
|
|
u8 AnalogController::GetExtraButtonMaskLSB() const
|
|
|
|
{
|
2020-12-08 10:36:19 +00:00
|
|
|
if (!m_analog_dpad_in_digital_mode || m_analog_mode || m_configuration_mode)
|
2020-10-29 14:30:04 +00:00
|
|
|
return 0xFF;
|
|
|
|
|
|
|
|
static constexpr u8 NEG_THRESHOLD = static_cast<u8>(128.0f - (127.0 * 0.5f));
|
|
|
|
static constexpr u8 POS_THRESHOLD = static_cast<u8>(128.0f + (127.0 * 0.5f));
|
|
|
|
|
|
|
|
const bool left = (m_axis_state[static_cast<u8>(Axis::LeftX)] <= NEG_THRESHOLD);
|
|
|
|
const bool right = (m_axis_state[static_cast<u8>(Axis::LeftX)] >= POS_THRESHOLD);
|
|
|
|
const bool up = (m_axis_state[static_cast<u8>(Axis::LeftY)] <= NEG_THRESHOLD);
|
|
|
|
const bool down = (m_axis_state[static_cast<u8>(Axis::LeftY)] >= POS_THRESHOLD);
|
|
|
|
|
|
|
|
return ~((static_cast<u8>(left) << static_cast<u8>(Button::Left)) |
|
|
|
|
(static_cast<u8>(right) << static_cast<u8>(Button::Right)) |
|
|
|
|
(static_cast<u8>(up) << static_cast<u8>(Button::Up)) |
|
|
|
|
(static_cast<u8>(down) << static_cast<u8>(Button::Down)));
|
|
|
|
}
|
|
|
|
|
2020-11-25 01:16:54 +00:00
|
|
|
void AnalogController::ResetRumbleConfig()
|
|
|
|
{
|
|
|
|
m_rumble_config.fill(0xFF);
|
|
|
|
|
|
|
|
m_rumble_config_large_motor_index = -1;
|
|
|
|
m_rumble_config_small_motor_index = -1;
|
|
|
|
|
|
|
|
SetMotorState(LargeMotor, 0);
|
|
|
|
SetMotorState(SmallMotor, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnalogController::SetMotorStateForConfigIndex(int index, u8 value)
|
|
|
|
{
|
|
|
|
if (m_rumble_config_small_motor_index == index)
|
|
|
|
SetMotorState(SmallMotor, ((value & 0x01) != 0) ? 255 : 0);
|
|
|
|
else if (m_rumble_config_large_motor_index == index)
|
|
|
|
SetMotorState(LargeMotor, value);
|
|
|
|
}
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
u8 AnalogController::GetResponseNumHalfwords() const
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
return 0x3;
|
|
|
|
|
|
|
|
return (0x1 + m_digital_mode_extra_halfwords);
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 AnalogController::GetModeID() const
|
|
|
|
{
|
|
|
|
if (m_configuration_mode)
|
|
|
|
return 0xF;
|
|
|
|
|
|
|
|
if (m_analog_mode)
|
|
|
|
return 0x7;
|
|
|
|
|
|
|
|
return 0x4;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 AnalogController::GetIDByte() const
|
|
|
|
{
|
|
|
|
return Truncate8((GetModeID() << 4) | GetResponseNumHalfwords());
|
|
|
|
}
|
|
|
|
|
2019-12-15 11:58:27 +00:00
|
|
|
bool AnalogController::Transfer(const u8 data_in, u8* data_out)
|
|
|
|
{
|
|
|
|
bool ack;
|
2021-01-01 01:12:55 +00:00
|
|
|
m_rx_buffer[m_command_step] = data_in;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
switch (m_command)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::Idle:
|
2021-02-21 23:20:31 +00:00
|
|
|
{
|
|
|
|
*data_out = 0xFF;
|
|
|
|
|
|
|
|
if (data_in == 0x01)
|
|
|
|
{
|
2022-07-11 13:03:29 +00:00
|
|
|
Log_DebugPrintf("ACK controller access");
|
2021-02-21 23:20:31 +00:00
|
|
|
m_command = Command::Ready;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log_DevPrintf("Unknown data_in = 0x%02X", data_in);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Command::Ready:
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
|
|
|
if (data_in == 0x42)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::ReadPad;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (data_in == 0x43)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::ConfigModeSetMode;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x44)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::SetAnalogMode;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2021-01-30 08:53:43 +00:00
|
|
|
|
|
|
|
ResetRumbleConfig();
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x45)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::GetAnalogMode;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x01, 0x02, BoolToUInt8(m_analog_mode), 0x02, 0x01, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x46)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::Command46;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x47)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::Command47;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x4C)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::Command4C;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else if (m_configuration_mode && data_in == 0x4D)
|
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
Assert(m_command_step == 0);
|
|
|
|
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
|
|
|
|
m_command = Command::GetSetRumble;
|
2021-04-14 08:13:34 +00:00
|
|
|
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
2021-01-01 01:12:55 +00:00
|
|
|
|
2020-11-26 13:54:55 +00:00
|
|
|
m_rumble_config_large_motor_index = -1;
|
|
|
|
m_rumble_config_small_motor_index = -1;
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-11 04:59:36 +00:00
|
|
|
if (m_configuration_mode)
|
|
|
|
Log_ErrorPrintf("Unimplemented config mode command 0x%02X", data_in);
|
|
|
|
|
2021-03-14 08:40:36 +00:00
|
|
|
*data_out = 0xFF;
|
|
|
|
return false;
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::ReadPad:
|
2019-12-15 13:22:53 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
const int rumble_index = m_command_step - 2;
|
2019-12-15 13:22:53 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
switch (m_command_step)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
m_tx_buffer[m_command_step] = Truncate8(m_button_state) & GetExtraButtonMaskLSB();
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
m_tx_buffer[m_command_step] = Truncate8(m_button_state >> 8);
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
{
|
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
2021-01-30 08:53:43 +00:00
|
|
|
else
|
2021-01-01 01:12:55 +00:00
|
|
|
{
|
2021-01-30 08:53:43 +00:00
|
|
|
bool legacy_rumble_on = (m_rx_buffer[2] & 0xC0) == 0x40 && (m_rx_buffer[3] & 0x01) != 0;
|
|
|
|
SetMotorState(SmallMotor, legacy_rumble_on ? 255 : 0);
|
2021-01-01 01:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::RightX)];
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::RightY)];
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::LeftX)];
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::LeftY)];
|
|
|
|
|
2021-04-14 08:13:34 +00:00
|
|
|
if (m_dualshock_enabled)
|
2021-01-01 01:12:55 +00:00
|
|
|
SetMotorStateForConfigIndex(rumble_index, data_in);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-12-15 13:22:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::ConfigModeSetMode:
|
2019-12-15 13:22:53 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
if (!m_configuration_mode)
|
2020-11-25 01:16:54 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
switch (m_command_step)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
m_tx_buffer[m_command_step] = Truncate8(m_button_state) & GetExtraButtonMaskLSB();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
m_tx_buffer[m_command_step] = Truncate8(m_button_state >> 8);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::RightX)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 5:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::RightY)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::LeftX)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 7:
|
|
|
|
{
|
|
|
|
if (m_configuration_mode || m_analog_mode)
|
|
|
|
m_tx_buffer[m_command_step] = m_axis_state[static_cast<u8>(Axis::LeftY)];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2020-11-25 01:16:54 +00:00
|
|
|
}
|
2021-01-01 01:12:55 +00:00
|
|
|
|
|
|
|
if (m_command_step == (static_cast<s32>(m_response_length) - 1))
|
2020-11-05 06:45:58 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
m_configuration_mode = (m_rx_buffer[2] == 1);
|
2021-11-07 19:50:05 +00:00
|
|
|
|
|
|
|
if (m_configuration_mode)
|
2021-04-14 08:13:34 +00:00
|
|
|
{
|
|
|
|
m_dualshock_enabled = true;
|
|
|
|
m_status_byte = 0x5A;
|
|
|
|
}
|
2021-11-07 19:50:05 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
Log_DevPrintf("0x%02x(%s) config mode", m_rx_buffer[2], m_configuration_mode ? "enter" : "leave");
|
2020-11-05 06:45:58 +00:00
|
|
|
}
|
2019-12-15 13:22:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::SetAnalogMode:
|
2020-11-25 01:16:54 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
if (m_command_step == 2)
|
|
|
|
{
|
|
|
|
Log_DevPrintf("analog mode val 0x%02x", data_in);
|
2020-11-25 01:16:54 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (data_in == 0x00 || data_in == 0x01)
|
2022-08-10 03:03:15 +00:00
|
|
|
SetAnalogMode((data_in == 0x01), true);
|
2021-01-01 01:12:55 +00:00
|
|
|
}
|
|
|
|
else if (m_command_step == 3)
|
|
|
|
{
|
|
|
|
Log_DevPrintf("analog mode lock 0x%02x", data_in);
|
2020-11-25 01:16:54 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (data_in == 0x02 || data_in == 0x03)
|
|
|
|
m_analog_locked = (data_in == 0x03);
|
|
|
|
}
|
2020-11-25 01:16:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::GetAnalogMode:
|
2020-11-25 01:16:54 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
// Intentionally empty, analog mode byte is set in reply buffer when command is first received
|
2020-11-25 01:16:54 +00:00
|
|
|
}
|
|
|
|
break;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::Command46:
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2021-01-30 07:50:45 +00:00
|
|
|
if (m_command_step == 2)
|
2021-01-01 01:12:55 +00:00
|
|
|
{
|
2021-01-30 07:50:45 +00:00
|
|
|
if (data_in == 0x00)
|
|
|
|
{
|
|
|
|
m_tx_buffer[4] = 0x01;
|
|
|
|
m_tx_buffer[5] = 0x02;
|
|
|
|
m_tx_buffer[6] = 0x00;
|
|
|
|
m_tx_buffer[7] = 0x0A;
|
|
|
|
}
|
|
|
|
else if (data_in == 0x01)
|
|
|
|
{
|
|
|
|
m_tx_buffer[4] = 0x01;
|
|
|
|
m_tx_buffer[5] = 0x01;
|
|
|
|
m_tx_buffer[6] = 0x01;
|
|
|
|
m_tx_buffer[7] = 0x14;
|
|
|
|
}
|
2021-01-01 01:12:55 +00:00
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::Command47:
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2021-01-30 07:50:45 +00:00
|
|
|
if (m_command_step == 2 && data_in != 0x00)
|
|
|
|
{
|
|
|
|
m_tx_buffer[4] = 0x00;
|
|
|
|
m_tx_buffer[5] = 0x00;
|
|
|
|
m_tx_buffer[6] = 0x00;
|
|
|
|
m_tx_buffer[7] = 0x00;
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::Command4C:
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
if (m_command_step == 2)
|
|
|
|
{
|
|
|
|
if (data_in == 0x00)
|
|
|
|
m_tx_buffer[5] = 0x04;
|
|
|
|
else if (data_in == 0x01)
|
|
|
|
m_tx_buffer[5] = 0x07;
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
case Command::GetSetRumble:
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2021-01-01 01:12:55 +00:00
|
|
|
int rumble_index = m_command_step - 2;
|
|
|
|
if (rumble_index >= 0)
|
|
|
|
{
|
|
|
|
m_tx_buffer[m_command_step] = m_rumble_config[rumble_index];
|
|
|
|
m_rumble_config[rumble_index] = data_in;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (data_in == 0x00)
|
|
|
|
m_rumble_config_small_motor_index = rumble_index;
|
|
|
|
else if (data_in == 0x01)
|
|
|
|
m_rumble_config_large_motor_index = rumble_index;
|
|
|
|
}
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (m_command_step == 7)
|
|
|
|
{
|
|
|
|
if (m_rumble_config_large_motor_index == -1)
|
|
|
|
SetMotorState(LargeMotor, 0);
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (m_rumble_config_small_motor_index == -1)
|
|
|
|
SetMotorState(SmallMotor, 0);
|
|
|
|
}
|
2020-12-16 23:11:53 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
DefaultCaseIsUnreachable();
|
|
|
|
}
|
2020-11-26 13:54:55 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
*data_out = m_tx_buffer[m_command_step];
|
2020-11-26 13:54:55 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
m_command_step = (m_command_step + 1) % m_response_length;
|
|
|
|
ack = (m_command_step == 0) ? false : true;
|
2020-11-26 13:54:55 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
if (m_command_step == 0)
|
|
|
|
{
|
|
|
|
m_command = Command::Idle;
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
Log_DebugPrintf("Rx: %02x %02x %02x %02x %02x %02x %02x %02x", m_rx_buffer[0], m_rx_buffer[1], m_rx_buffer[2],
|
|
|
|
m_rx_buffer[3], m_rx_buffer[4], m_rx_buffer[5], m_rx_buffer[6], m_rx_buffer[7]);
|
|
|
|
Log_DebugPrintf("Tx: %02x %02x %02x %02x %02x %02x %02x %02x", m_tx_buffer[0], m_tx_buffer[1], m_tx_buffer[2],
|
|
|
|
m_tx_buffer[3], m_tx_buffer[4], m_tx_buffer[5], m_tx_buffer[6], m_tx_buffer[7]);
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2021-01-01 01:12:55 +00:00
|
|
|
m_rx_buffer.fill(0x00);
|
|
|
|
m_tx_buffer.fill(0x00);
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ack;
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
std::unique_ptr<AnalogController> AnalogController::Create(u32 index)
|
2019-12-15 11:58:27 +00:00
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
return std::make_unique<AnalogController>(index);
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static const Controller::ControllerBindingInfo s_binding_info[] = {
|
|
|
|
#define BUTTON(name, display_name, button, genb) \
|
2019-12-15 11:58:27 +00:00
|
|
|
{ \
|
2023-01-15 04:00:51 +00:00
|
|
|
name, display_name, static_cast<u32>(button), InputBindingInfo::Type::Button, genb \
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
2022-07-11 13:03:29 +00:00
|
|
|
#define AXIS(name, display_name, halfaxis, genb) \
|
2019-12-15 11:58:27 +00:00
|
|
|
{ \
|
2022-07-11 13:03:29 +00:00
|
|
|
name, display_name, static_cast<u32>(AnalogController::Button::Count) + static_cast<u32>(halfaxis), \
|
2023-01-15 04:00:51 +00:00
|
|
|
InputBindingInfo::Type::HalfAxis, genb \
|
2019-12-15 11:58:27 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:15:53 +00:00
|
|
|
// clang-format off
|
|
|
|
BUTTON("Up", TRANSLATE_NOOP("AnalogController", "D-Pad Up"), AnalogController::Button::Up, GenericInputBinding::DPadUp),
|
|
|
|
BUTTON("Right", TRANSLATE_NOOP("AnalogController", "D-Pad Right"), AnalogController::Button::Right, GenericInputBinding::DPadRight),
|
|
|
|
BUTTON("Down", TRANSLATE_NOOP("AnalogController", "D-Pad Down"), AnalogController::Button::Down, GenericInputBinding::DPadDown),
|
|
|
|
BUTTON("Left", TRANSLATE_NOOP("AnalogController", "D-Pad Left"), AnalogController::Button::Left, GenericInputBinding::DPadLeft),
|
|
|
|
BUTTON("Triangle", TRANSLATE_NOOP("AnalogController", "Triangle"), AnalogController::Button::Triangle, GenericInputBinding::Triangle),
|
|
|
|
BUTTON("Circle", TRANSLATE_NOOP("AnalogController", "Circle"), AnalogController::Button::Circle, GenericInputBinding::Circle),
|
|
|
|
BUTTON("Cross", TRANSLATE_NOOP("AnalogController", "Cross"), AnalogController::Button::Cross, GenericInputBinding::Cross),
|
|
|
|
BUTTON("Square", TRANSLATE_NOOP("AnalogController", "Square"), AnalogController::Button::Square, GenericInputBinding::Square),
|
|
|
|
BUTTON("Select", TRANSLATE_NOOP("AnalogController", "Select"), AnalogController::Button::Select, GenericInputBinding::Select),
|
|
|
|
BUTTON("Start", TRANSLATE_NOOP("AnalogController", "Start"), AnalogController::Button::Start, GenericInputBinding::Start),
|
|
|
|
BUTTON("Analog", TRANSLATE_NOOP("AnalogController", "Analog Toggle"), AnalogController::Button::Analog, GenericInputBinding::System),
|
|
|
|
BUTTON("L1", TRANSLATE_NOOP("AnalogController", "L1"), AnalogController::Button::L1, GenericInputBinding::L1),
|
|
|
|
BUTTON("R1", TRANSLATE_NOOP("AnalogController", "R1"), AnalogController::Button::R1, GenericInputBinding::R1),
|
|
|
|
BUTTON("L2", TRANSLATE_NOOP("AnalogController", "L2"), AnalogController::Button::L2, GenericInputBinding::L2),
|
|
|
|
BUTTON("R2", TRANSLATE_NOOP("AnalogController", "R2"), AnalogController::Button::R2, GenericInputBinding::R2),
|
|
|
|
BUTTON("L3", TRANSLATE_NOOP("AnalogController", "L3"), AnalogController::Button::L3, GenericInputBinding::L3),
|
|
|
|
BUTTON("R3", TRANSLATE_NOOP("AnalogController", "R3"), AnalogController::Button::R3, GenericInputBinding::R3),
|
|
|
|
|
|
|
|
AXIS("LLeft", TRANSLATE_NOOP("AnalogController", "Left Stick Left"), AnalogController::HalfAxis::LLeft, GenericInputBinding::LeftStickLeft),
|
|
|
|
AXIS("LRight", TRANSLATE_NOOP("AnalogController", "Left Stick Right"), AnalogController::HalfAxis::LRight, GenericInputBinding::LeftStickRight),
|
|
|
|
AXIS("LDown", TRANSLATE_NOOP("AnalogController", "Left Stick Down"), AnalogController::HalfAxis::LDown, GenericInputBinding::LeftStickDown),
|
|
|
|
AXIS("LUp", TRANSLATE_NOOP("AnalogController", "Left Stick Up"), AnalogController::HalfAxis::LUp, GenericInputBinding::LeftStickUp),
|
|
|
|
AXIS("RLeft", TRANSLATE_NOOP("AnalogController", "Right Stick Left"), AnalogController::HalfAxis::RLeft, GenericInputBinding::RightStickLeft),
|
|
|
|
AXIS("RRight", TRANSLATE_NOOP("AnalogController", "Right Stick Right"), AnalogController::HalfAxis::RRight, GenericInputBinding::RightStickRight),
|
|
|
|
AXIS("RDown", TRANSLATE_NOOP("AnalogController", "Right Stick Down"), AnalogController::HalfAxis::RDown, GenericInputBinding::RightStickDown),
|
|
|
|
AXIS("RUp", TRANSLATE_NOOP("AnalogController", "Right Stick Up"), AnalogController::HalfAxis::RUp, GenericInputBinding::RightStickUp),
|
2023-08-23 12:06:48 +00:00
|
|
|
// clang-format on
|
2019-12-15 11:58:27 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
#undef AXIS
|
2019-12-15 11:58:27 +00:00
|
|
|
#undef BUTTON
|
2022-07-11 13:03:29 +00:00
|
|
|
};
|
|
|
|
|
2023-08-19 13:43:37 +00:00
|
|
|
static const char* s_invert_settings[] = {TRANSLATE_NOOP("AnalogController", "Not Inverted"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Invert Left/Right"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Invert Up/Down"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Invert Left/Right + Up/Down"), nullptr};
|
2022-09-22 03:50:31 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
static const SettingInfo s_settings[] = {
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::Boolean, "ForceAnalogOnReset", TRANSLATE_NOOP("AnalogController", "Force Analog Mode on Reset"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Forces the controller to analog mode when the console is reset/powered on."),
|
2023-09-03 04:30:26 +00:00
|
|
|
"true", nullptr, nullptr, nullptr, nullptr, nullptr, 0.0f},
|
2022-07-11 13:03:29 +00:00
|
|
|
{SettingInfo::Type::Boolean, "AnalogDPadInDigitalMode",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("AnalogController", "Use Analog Sticks for D-Pad in Digital Mode"),
|
|
|
|
TRANSLATE_NOOP("AnalogController",
|
2023-08-19 14:08:03 +00:00
|
|
|
"Allows you to use the analog sticks to control the d-pad in digital mode, as well as the buttons."),
|
2023-09-03 04:30:26 +00:00
|
|
|
"true", nullptr, nullptr, nullptr, nullptr, nullptr, 0.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::Float, "AnalogDeadzone", TRANSLATE_NOOP("AnalogController", "Analog Deadzone"),
|
|
|
|
TRANSLATE_NOOP("AnalogController",
|
2023-08-19 14:08:03 +00:00
|
|
|
"Sets the analog stick deadzone, i.e. the fraction of the stick movement which will be ignored."),
|
2022-09-22 03:50:31 +00:00
|
|
|
"0.00f", "0.00f", "1.00f", "0.01f", "%.0f%%", nullptr, 100.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::Float, "AnalogSensitivity", TRANSLATE_NOOP("AnalogController", "Analog Sensitivity"),
|
|
|
|
TRANSLATE_NOOP(
|
2022-07-11 13:03:29 +00:00
|
|
|
"AnalogController",
|
2022-08-10 03:03:15 +00:00
|
|
|
"Sets the analog stick axis scaling factor. A value between 130% and 140% is recommended when using recent "
|
2022-07-11 13:03:29 +00:00
|
|
|
"controllers, e.g. DualShock 4, Xbox One Controller."),
|
2022-09-22 03:50:31 +00:00
|
|
|
"1.33f", "0.01f", "2.00f", "0.01f", "%.0f%%", nullptr, 100.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::Float, "ButtonDeadzone", TRANSLATE_NOOP("AnalogController", "Button/Trigger Deadzone"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Sets the deadzone for activating buttons/triggers, "
|
2023-08-19 14:08:03 +00:00
|
|
|
"i.e. the fraction of the trigger which will be ignored."),
|
2023-01-07 01:28:13 +00:00
|
|
|
"0.25", "0.00", "1.00", "0.01", "%.0f%%", nullptr, 100.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::Integer, "VibrationBias", TRANSLATE_NOOP("AnalogController", "Vibration Bias"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Sets the rumble bias value. If rumble in some games is too weak or not "
|
2023-08-19 14:08:03 +00:00
|
|
|
"functioning, try increasing this value."),
|
2022-09-22 03:50:31 +00:00
|
|
|
"8", "0", "255", "1", "%d", nullptr, 1.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::IntegerList, "InvertLeftStick", TRANSLATE_NOOP("AnalogController", "Invert Left Stick"),
|
2023-08-19 14:08:03 +00:00
|
|
|
TRANSLATE_NOOP("AnalogController", "Inverts the direction of the left analog stick."), "0", "0", "3", nullptr,
|
|
|
|
nullptr, s_invert_settings, 0.0f},
|
2023-08-19 13:43:37 +00:00
|
|
|
{SettingInfo::Type::IntegerList, "InvertRightStick", TRANSLATE_NOOP("AnalogController", "Invert Right Stick"),
|
|
|
|
TRANSLATE_NOOP("AnalogController", "Inverts the direction of the right analog stick."), "0", "0", "3", nullptr,
|
2023-01-15 04:00:51 +00:00
|
|
|
nullptr, s_invert_settings, 0.0f},
|
2022-09-22 03:50:31 +00:00
|
|
|
};
|
2022-07-11 13:03:29 +00:00
|
|
|
|
|
|
|
const Controller::ControllerInfo AnalogController::INFO = {ControllerType::AnalogController,
|
|
|
|
"AnalogController",
|
2023-08-19 13:43:37 +00:00
|
|
|
TRANSLATE_NOOP("ControllerType", "Analog Controller"),
|
2022-07-11 13:03:29 +00:00
|
|
|
s_binding_info,
|
|
|
|
s_settings,
|
|
|
|
Controller::VibrationCapabilities::LargeSmallMotors};
|
|
|
|
|
|
|
|
void AnalogController::LoadSettings(SettingsInterface& si, const char* section)
|
|
|
|
{
|
|
|
|
Controller::LoadSettings(si, section);
|
|
|
|
m_force_analog_on_reset = si.GetBoolValue(section, "ForceAnalogOnReset", true);
|
|
|
|
m_analog_dpad_in_digital_mode = si.GetBoolValue(section, "AnalogDPadInDigitalMode", true);
|
|
|
|
m_analog_deadzone = std::clamp(si.GetFloatValue(section, "AnalogDeadzone", DEFAULT_STICK_DEADZONE), 0.0f, 1.0f);
|
|
|
|
m_analog_sensitivity =
|
|
|
|
std::clamp(si.GetFloatValue(section, "AnalogSensitivity", DEFAULT_STICK_SENSITIVITY), 0.01f, 3.0f);
|
2022-09-22 03:50:31 +00:00
|
|
|
m_button_deadzone = std::clamp(si.GetFloatValue(section, "ButtonDeadzone", DEFAULT_BUTTON_DEADZONE), 0.0f, 1.0f);
|
2022-07-11 13:03:29 +00:00
|
|
|
m_rumble_bias = static_cast<u8>(std::min<u32>(si.GetIntValue(section, "VibrationBias", 8), 255));
|
2022-09-22 03:50:31 +00:00
|
|
|
m_invert_left_stick = static_cast<u8>(si.GetIntValue(section, "InvertLeftStick", 0));
|
|
|
|
m_invert_right_stick = static_cast<u8>(si.GetIntValue(section, "InvertRightStick", 0));
|
2020-06-30 14:33:45 +00:00
|
|
|
}
|