summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/hid/controllers/console_sixaxis.cpp
blob: ea7e8f18fd5762d566ab0b269a356ab1f5dd4567 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2021 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "common/settings.h"
#include "core/core_timing.h"
#include "core/hid/emulated_console.h"
#include "core/hid/hid_core.h"
#include "core/hle/service/hid/controllers/console_sixaxis.h"

namespace Service::HID {
constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3C200;

Controller_ConsoleSixAxis::Controller_ConsoleSixAxis(Core::HID::HIDCore& hid_core_)
    : ControllerBase{hid_core_} {
    console = hid_core.GetEmulatedConsole();
}

Controller_ConsoleSixAxis::~Controller_ConsoleSixAxis() = default;

void Controller_ConsoleSixAxis::OnInit() {}

void Controller_ConsoleSixAxis::OnRelease() {}

void Controller_ConsoleSixAxis::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                         std::size_t size) {
    seven_six_axis.header.timestamp = core_timing.GetCPUTicks();
    seven_six_axis.header.total_entry_count = 17;

    if (!IsControllerActivated() || !is_transfer_memory_set) {
        seven_six_axis.header.entry_count = 0;
        seven_six_axis.header.last_entry_index = 0;
        return;
    }
    seven_six_axis.header.entry_count = 16;

    const auto& last_entry =
        seven_six_axis.sevensixaxis_states[seven_six_axis.header.last_entry_index];
    seven_six_axis.header.last_entry_index = (seven_six_axis.header.last_entry_index + 1) % 17;
    auto& cur_entry = seven_six_axis.sevensixaxis_states[seven_six_axis.header.last_entry_index];

    cur_entry.sampling_number = last_entry.sampling_number + 1;
    cur_entry.sampling_number2 = cur_entry.sampling_number;

    // Try to read sixaxis sensor states
    const auto motion_status = console->GetMotion();

    console_six_axis.is_seven_six_axis_sensor_at_rest = motion_status.is_at_rest;

    cur_entry.accel = motion_status.accel;
    // Zero gyro values as they just mess up with the camera
    // Note: Probably a correct sensivity setting must be set
    cur_entry.gyro = {};
    cur_entry.quaternion = {
        {
            motion_status.quaternion.xyz.y,
            motion_status.quaternion.xyz.x,
            -motion_status.quaternion.w,
        },
        -motion_status.quaternion.xyz.z,
    };

    console_six_axis.sampling_number++;
    // TODO(German77): Find the purpose of those values
    console_six_axis.verticalization_error = 0.0f;
    console_six_axis.gyro_bias = {0.0f, 0.0f, 0.0f};

    // Update console six axis shared memory
    std::memcpy(data + SHARED_MEMORY_OFFSET, &console_six_axis, sizeof(console_six_axis));
    // Update seven six axis transfer memory
    std::memcpy(transfer_memory, &seven_six_axis, sizeof(seven_six_axis));
}

void Controller_ConsoleSixAxis::SetTransferMemoryPointer(u8* t_mem) {
    is_transfer_memory_set = true;
    transfer_memory = t_mem;
}

void Controller_ConsoleSixAxis::ResetTimestamp() {
    auto& cur_entry = seven_six_axis.sevensixaxis_states[seven_six_axis.header.last_entry_index];
    cur_entry.sampling_number = 0;
    cur_entry.sampling_number2 = 0;
}
} // namespace Service::HID