summaryrefslogtreecommitdiffstats
path: root/src/input_common/helpers/joycon_protocol/generic_functions.cpp
blob: cbd9ff4f8b154ed9363220996429bde999bf262d (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/logging/log.h"
#include "input_common/helpers/joycon_protocol/generic_functions.h"

namespace InputCommon::Joycon {

GenericProtocol::GenericProtocol(std::shared_ptr<JoyconHandle> handle)
    : JoyconCommonProtocol(std::move(handle)) {}

DriverResult GenericProtocol::EnablePassiveMode() {
    SetBlocking();
    const auto result = SetReportMode(ReportMode::SIMPLE_HID_MODE);
    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::EnableActiveMode() {
    SetBlocking();
    const auto result = SetReportMode(ReportMode::STANDARD_FULL_60HZ);
    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::GetDeviceInfo(DeviceInfo& device_info) {
    std::vector<u8> output;
    SetBlocking();

    const auto result = SendSubCommand(SubCommand::REQ_DEV_INFO, {}, output);

    device_info = {};
    if (result == DriverResult::Success) {
        memcpy(&device_info, output.data(), sizeof(DeviceInfo));
    }

    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::GetControllerType(ControllerType& controller_type) {
    return GetDeviceType(controller_type);
}

DriverResult GenericProtocol::EnableImu(bool enable) {
    const std::array<u8, 1> buffer{static_cast<u8>(enable ? 1 : 0)};
    std::vector<u8> output;
    SetBlocking();
    const auto result = SendSubCommand(SubCommand::ENABLE_IMU, buffer, output);
    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
                                           AccelerometerSensitivity asen,
                                           AccelerometerPerformance afrec) {
    const std::array<u8, 4> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
                                   static_cast<u8>(gfrec), static_cast<u8>(afrec)};
    std::vector<u8> output;
    SetBlocking();
    const auto result = SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer, output);
    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::GetBattery(u32& battery_level) {
    battery_level = 0;
    return DriverResult::NotSupported;
}

DriverResult GenericProtocol::GetColor(Color& color) {
    std::vector<u8> buffer;
    SetBlocking();
    const auto result = ReadSPI(CalAddr::COLOR_DATA, 12, buffer);
    SetNonBlocking();

    color = {};
    if (result == DriverResult::Success) {
        color.body = static_cast<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
        color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
        color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
        color.right_grip = static_cast<u32>((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]);
    }

    return result;
}

DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
    std::vector<u8> buffer;
    SetBlocking();
    const auto result = ReadSPI(CalAddr::SERIAL_NUMBER, 16, buffer);
    SetNonBlocking();

    serial_number = {};
    if (result == DriverResult::Success) {
        memcpy(serial_number.data(), buffer.data() + 1, sizeof(SerialNumber));
    }

    return result;
}

DriverResult GenericProtocol::GetTemperature(u32& temperature) {
    // Not all devices have temperature sensor
    temperature = 25;
    return DriverResult::NotSupported;
}

DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
    DeviceInfo device_info{};

    const auto result = GetDeviceInfo(device_info);
    version = device_info.firmware;

    return result;
}

DriverResult GenericProtocol::SetHomeLight() {
    static constexpr std::array<u8, 3> buffer{0x0f, 0xf0, 0x00};
    std::vector<u8> output;
    SetBlocking();

    const auto result = SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer, output);

    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::SetLedBusy() {
    return DriverResult::NotSupported;
}

DriverResult GenericProtocol::SetLedPattern(u8 leds) {
    const std::array<u8, 1> buffer{leds};
    std::vector<u8> output;
    SetBlocking();

    const auto result = SendSubCommand(SubCommand::SET_PLAYER_LIGHTS, buffer, output);

    SetNonBlocking();
    return result;
}

DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) {
    return SetLedPattern(static_cast<u8>(leds << 4));
}

} // namespace InputCommon::Joycon