generic_functions.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/logging/log.h"
  4. #include "input_common/helpers/joycon_protocol/generic_functions.h"
  5. namespace InputCommon::Joycon {
  6. GenericProtocol::GenericProtocol(std::shared_ptr<JoyconHandle> handle)
  7. : JoyconCommonProtocol(std::move(handle)) {}
  8. DriverResult GenericProtocol::EnablePassiveMode() {
  9. ScopedSetBlocking sb(this);
  10. return SetReportMode(ReportMode::SIMPLE_HID_MODE);
  11. }
  12. DriverResult GenericProtocol::EnableActiveMode() {
  13. ScopedSetBlocking sb(this);
  14. return SetReportMode(ReportMode::STANDARD_FULL_60HZ);
  15. }
  16. DriverResult GenericProtocol::SetLowPowerMode(bool enable) {
  17. ScopedSetBlocking sb(this);
  18. const std::array<u8, 1> buffer{static_cast<u8>(enable ? 1 : 0)};
  19. return SendSubCommand(SubCommand::LOW_POWER_MODE, buffer);
  20. }
  21. DriverResult GenericProtocol::TriggersElapsed() {
  22. ScopedSetBlocking sb(this);
  23. return SendSubCommand(SubCommand::TRIGGERS_ELAPSED, {});
  24. }
  25. DriverResult GenericProtocol::GetDeviceInfo(DeviceInfo& device_info) {
  26. ScopedSetBlocking sb(this);
  27. std::vector<u8> output;
  28. const auto result = SendSubCommand(SubCommand::REQ_DEV_INFO, {}, output);
  29. device_info = {};
  30. if (result == DriverResult::Success) {
  31. memcpy(&device_info, output.data(), sizeof(DeviceInfo));
  32. }
  33. return result;
  34. }
  35. DriverResult GenericProtocol::GetControllerType(ControllerType& controller_type) {
  36. return GetDeviceType(controller_type);
  37. }
  38. DriverResult GenericProtocol::EnableImu(bool enable) {
  39. ScopedSetBlocking sb(this);
  40. const std::array<u8, 1> buffer{static_cast<u8>(enable ? 1 : 0)};
  41. return SendSubCommand(SubCommand::ENABLE_IMU, buffer);
  42. }
  43. DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
  44. AccelerometerSensitivity asen,
  45. AccelerometerPerformance afrec) {
  46. ScopedSetBlocking sb(this);
  47. const std::array<u8, 4> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
  48. static_cast<u8>(gfrec), static_cast<u8>(afrec)};
  49. return SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer);
  50. }
  51. DriverResult GenericProtocol::GetBattery(u32& battery_level) {
  52. // This function is meant to request the high resolution battery status
  53. battery_level = 0;
  54. return DriverResult::NotSupported;
  55. }
  56. DriverResult GenericProtocol::GetColor(Color& color) {
  57. ScopedSetBlocking sb(this);
  58. std::vector<u8> buffer;
  59. const auto result = ReadSPI(CalAddr::COLOR_DATA, 12, buffer);
  60. color = {};
  61. if (result == DriverResult::Success) {
  62. color.body = static_cast<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
  63. color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
  64. color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
  65. color.right_grip = static_cast<u32>((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]);
  66. }
  67. return result;
  68. }
  69. DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
  70. ScopedSetBlocking sb(this);
  71. std::vector<u8> buffer;
  72. const auto result = ReadSPI(CalAddr::SERIAL_NUMBER, 16, buffer);
  73. serial_number = {};
  74. if (result == DriverResult::Success) {
  75. memcpy(serial_number.data(), buffer.data() + 1, sizeof(SerialNumber));
  76. }
  77. return result;
  78. }
  79. DriverResult GenericProtocol::GetTemperature(u32& temperature) {
  80. // Not all devices have temperature sensor
  81. temperature = 25;
  82. return DriverResult::NotSupported;
  83. }
  84. DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
  85. DeviceInfo device_info{};
  86. const auto result = GetDeviceInfo(device_info);
  87. version = device_info.firmware;
  88. return result;
  89. }
  90. DriverResult GenericProtocol::SetHomeLight() {
  91. ScopedSetBlocking sb(this);
  92. static constexpr std::array<u8, 3> buffer{0x0f, 0xf0, 0x00};
  93. return SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer);
  94. }
  95. DriverResult GenericProtocol::SetLedBusy() {
  96. return DriverResult::NotSupported;
  97. }
  98. DriverResult GenericProtocol::SetLedPattern(u8 leds) {
  99. ScopedSetBlocking sb(this);
  100. const std::array<u8, 1> buffer{leds};
  101. return SendSubCommand(SubCommand::SET_PLAYER_LIGHTS, buffer);
  102. }
  103. DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) {
  104. return SetLedPattern(static_cast<u8>(leds << 4));
  105. }
  106. } // namespace InputCommon::Joycon