2022-12-04 11:03:45 +00:00
|
|
|
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> and contributors.
|
|
|
|
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
|
|
|
|
2020-11-13 07:36:36 +00:00
|
|
|
#pragma once
|
|
|
|
#include "controller.h"
|
|
|
|
#include <array>
|
|
|
|
#include <memory>
|
|
|
|
#include <optional>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
class AnalogJoystick final : public Controller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Axis : u8
|
|
|
|
{
|
|
|
|
LeftX,
|
|
|
|
LeftY,
|
|
|
|
RightX,
|
|
|
|
RightY,
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class Button : u8
|
|
|
|
{
|
|
|
|
Select = 0,
|
|
|
|
L3 = 1,
|
|
|
|
R3 = 2,
|
|
|
|
Start = 3,
|
|
|
|
Up = 4,
|
|
|
|
Right = 5,
|
|
|
|
Down = 6,
|
|
|
|
Left = 7,
|
|
|
|
L2 = 8,
|
|
|
|
R2 = 9,
|
|
|
|
L1 = 10,
|
|
|
|
R1 = 11,
|
|
|
|
Triangle = 12,
|
|
|
|
Circle = 13,
|
|
|
|
Cross = 14,
|
|
|
|
Square = 15,
|
|
|
|
Mode = 16,
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
enum class HalfAxis : u8
|
|
|
|
{
|
|
|
|
LLeft,
|
|
|
|
LRight,
|
|
|
|
LDown,
|
|
|
|
LUp,
|
|
|
|
RLeft,
|
|
|
|
RRight,
|
|
|
|
RDown,
|
|
|
|
RUp,
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Controller::ControllerInfo INFO;
|
|
|
|
|
2020-11-13 07:36:36 +00:00
|
|
|
AnalogJoystick(u32 index);
|
|
|
|
~AnalogJoystick() override;
|
|
|
|
|
|
|
|
static std::unique_ptr<AnalogJoystick> Create(u32 index);
|
|
|
|
|
|
|
|
ControllerType GetType() const override;
|
2022-10-09 04:10:42 +00:00
|
|
|
bool InAnalogMode() const override;
|
2020-11-13 07:36:36 +00:00
|
|
|
|
|
|
|
void Reset() override;
|
2020-12-16 14:09:32 +00:00
|
|
|
bool DoState(StateWrapper& sw, bool apply_input_state) override;
|
2020-11-13 07:36:36 +00:00
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float GetBindState(u32 index) const override;
|
|
|
|
void SetBindState(u32 index, float value) override;
|
2020-12-06 05:47:00 +00:00
|
|
|
u32 GetButtonStateBits() const override;
|
2021-01-04 06:54:16 +00:00
|
|
|
std::optional<u32> GetAnalogInputBytes() const override;
|
2020-11-13 07:36:36 +00:00
|
|
|
|
|
|
|
void ResetTransferState() override;
|
|
|
|
bool Transfer(const u8 data_in, u8* data_out) override;
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
void LoadSettings(SettingsInterface& si, const char* section) override;
|
2021-05-10 03:35:45 +00:00
|
|
|
|
2020-11-13 07:36:36 +00:00
|
|
|
private:
|
|
|
|
enum class TransferState : u8
|
|
|
|
{
|
|
|
|
Idle,
|
2021-02-21 23:20:31 +00:00
|
|
|
Ready,
|
2020-11-13 07:36:36 +00:00
|
|
|
IDMSB,
|
|
|
|
ButtonsLSB,
|
|
|
|
ButtonsMSB,
|
|
|
|
RightAxisX,
|
|
|
|
RightAxisY,
|
|
|
|
LeftAxisX,
|
|
|
|
LeftAxisY
|
|
|
|
};
|
|
|
|
|
|
|
|
u16 GetID() const;
|
|
|
|
void ToggleAnalogMode();
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
float m_analog_deadzone = 0.0f;
|
|
|
|
float m_analog_sensitivity = 1.33f;
|
2022-10-21 11:14:27 +00:00
|
|
|
u8 m_invert_left_stick = 0;
|
|
|
|
u8 m_invert_right_stick = 0;
|
2020-11-13 07:36:36 +00:00
|
|
|
|
|
|
|
// On original hardware, the mode toggle is a switch rather than a button, so we'll enable Analog Mode by default
|
|
|
|
bool m_analog_mode = true;
|
|
|
|
|
|
|
|
// buttons are active low
|
|
|
|
u16 m_button_state = UINT16_C(0xFFFF);
|
|
|
|
|
|
|
|
std::array<u8, static_cast<u8>(Axis::Count)> m_axis_state{};
|
|
|
|
|
2022-07-11 13:03:29 +00:00
|
|
|
// both directions of axis state, merged to m_axis_state
|
|
|
|
std::array<u8, static_cast<u32>(HalfAxis::Count)> m_half_axis_state{};
|
|
|
|
|
2020-11-13 07:36:36 +00:00
|
|
|
TransferState m_transfer_state = TransferState::Idle;
|
|
|
|
};
|