generic_functions.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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::GetDeviceInfo(DeviceInfo& device_info) {
  17. ScopedSetBlocking sb(this);
  18. std::vector<u8> output;
  19. const auto result = SendSubCommand(SubCommand::REQ_DEV_INFO, {}, output);
  20. device_info = {};
  21. if (result == DriverResult::Success) {
  22. memcpy(&device_info, output.data(), sizeof(DeviceInfo));
  23. }
  24. return result;
  25. }
  26. DriverResult GenericProtocol::GetControllerType(ControllerType& controller_type) {
  27. return GetDeviceType(controller_type);
  28. }
  29. DriverResult GenericProtocol::EnableImu(bool enable) {
  30. ScopedSetBlocking sb(this);
  31. const std::array<u8, 1> buffer{static_cast<u8>(enable ? 1 : 0)};
  32. return SendSubCommand(SubCommand::ENABLE_IMU, buffer);
  33. }
  34. DriverResult GenericProtocol::SetImuConfig(GyroSensitivity gsen, GyroPerformance gfrec,
  35. AccelerometerSensitivity asen,
  36. AccelerometerPerformance afrec) {
  37. ScopedSetBlocking sb(this);
  38. const std::array<u8, 4> buffer{static_cast<u8>(gsen), static_cast<u8>(asen),
  39. static_cast<u8>(gfrec), static_cast<u8>(afrec)};
  40. return SendSubCommand(SubCommand::SET_IMU_SENSITIVITY, buffer);
  41. }
  42. DriverResult GenericProtocol::GetBattery(u32& battery_level) {
  43. // This function is meant to request the high resolution battery status
  44. battery_level = 0;
  45. return DriverResult::NotSupported;
  46. }
  47. DriverResult GenericProtocol::GetColor(Color& color) {
  48. ScopedSetBlocking sb(this);
  49. std::vector<u8> buffer;
  50. const auto result = ReadSPI(CalAddr::COLOR_DATA, 12, buffer);
  51. color = {};
  52. if (result == DriverResult::Success) {
  53. color.body = static_cast<u32>((buffer[0] << 16) | (buffer[1] << 8) | buffer[2]);
  54. color.buttons = static_cast<u32>((buffer[3] << 16) | (buffer[4] << 8) | buffer[5]);
  55. color.left_grip = static_cast<u32>((buffer[6] << 16) | (buffer[7] << 8) | buffer[8]);
  56. color.right_grip = static_cast<u32>((buffer[9] << 16) | (buffer[10] << 8) | buffer[11]);
  57. }
  58. return result;
  59. }
  60. DriverResult GenericProtocol::GetSerialNumber(SerialNumber& serial_number) {
  61. ScopedSetBlocking sb(this);
  62. std::vector<u8> buffer;
  63. const auto result = ReadSPI(CalAddr::SERIAL_NUMBER, 16, buffer);
  64. serial_number = {};
  65. if (result == DriverResult::Success) {
  66. memcpy(serial_number.data(), buffer.data() + 1, sizeof(SerialNumber));
  67. }
  68. return result;
  69. }
  70. DriverResult GenericProtocol::GetTemperature(u32& temperature) {
  71. // Not all devices have temperature sensor
  72. temperature = 25;
  73. return DriverResult::NotSupported;
  74. }
  75. DriverResult GenericProtocol::GetVersionNumber(FirmwareVersion& version) {
  76. DeviceInfo device_info{};
  77. const auto result = GetDeviceInfo(device_info);
  78. version = device_info.firmware;
  79. return result;
  80. }
  81. DriverResult GenericProtocol::SetHomeLight() {
  82. ScopedSetBlocking sb(this);
  83. static constexpr std::array<u8, 3> buffer{0x0f, 0xf0, 0x00};
  84. return SendSubCommand(SubCommand::SET_HOME_LIGHT, buffer);
  85. }
  86. DriverResult GenericProtocol::SetLedBusy() {
  87. return DriverResult::NotSupported;
  88. }
  89. DriverResult GenericProtocol::SetLedPattern(u8 leds) {
  90. ScopedSetBlocking sb(this);
  91. const std::array<u8, 1> buffer{leds};
  92. return SendSubCommand(SubCommand::SET_PLAYER_LIGHTS, buffer);
  93. }
  94. DriverResult GenericProtocol::SetLedBlinkPattern(u8 leds) {
  95. return SetLedPattern(static_cast<u8>(leds << 4));
  96. }
  97. } // namespace InputCommon::Joycon