ringcon.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/ringcon.h"
  5. namespace InputCommon::Joycon {
  6. RingConProtocol::RingConProtocol(std::shared_ptr<JoyconHandle> handle)
  7. : JoyconCommonProtocol(std::move(handle)) {}
  8. DriverResult RingConProtocol::EnableRingCon() {
  9. LOG_DEBUG(Input, "Enable Ringcon");
  10. ScopedSetBlocking sb(this);
  11. DriverResult result{DriverResult::Success};
  12. if (result == DriverResult::Success) {
  13. result = SetReportMode(ReportMode::STANDARD_FULL_60HZ);
  14. }
  15. if (result == DriverResult::Success) {
  16. result = EnableMCU(true);
  17. }
  18. if (result == DriverResult::Success) {
  19. const MCUConfig config{
  20. .command = MCUCommand::ConfigureMCU,
  21. .sub_command = MCUSubCommand::SetDeviceMode,
  22. .mode = MCUMode::Standby,
  23. .crc = {},
  24. };
  25. result = ConfigureMCU(config);
  26. }
  27. return result;
  28. }
  29. DriverResult RingConProtocol::DisableRingCon() {
  30. LOG_DEBUG(Input, "Disable RingCon");
  31. ScopedSetBlocking sb(this);
  32. DriverResult result{DriverResult::Success};
  33. if (result == DriverResult::Success) {
  34. result = EnableMCU(false);
  35. }
  36. is_enabled = false;
  37. return result;
  38. }
  39. DriverResult RingConProtocol::StartRingconPolling() {
  40. LOG_DEBUG(Input, "Enable Ringcon");
  41. ScopedSetBlocking sb(this);
  42. DriverResult result{DriverResult::Success};
  43. bool is_connected = false;
  44. if (result == DriverResult::Success) {
  45. result = IsRingConnected(is_connected);
  46. }
  47. if (result == DriverResult::Success && is_connected) {
  48. LOG_INFO(Input, "Ringcon detected");
  49. result = ConfigureRing();
  50. }
  51. if (result == DriverResult::Success) {
  52. is_enabled = true;
  53. }
  54. return result;
  55. }
  56. DriverResult RingConProtocol::IsRingConnected(bool& is_connected) {
  57. LOG_DEBUG(Input, "IsRingConnected");
  58. constexpr std::size_t max_tries = 28;
  59. std::vector<u8> output;
  60. std::size_t tries = 0;
  61. is_connected = false;
  62. do {
  63. const auto result = SendSubCommand(SubCommand::GET_EXTERNAL_DEVICE_INFO, {}, output);
  64. if (result != DriverResult::Success) {
  65. return result;
  66. }
  67. if (tries++ >= max_tries) {
  68. return DriverResult::NoDeviceDetected;
  69. }
  70. } while (output[16] != static_cast<u8>(ExternalDeviceId::RingController));
  71. is_connected = true;
  72. return DriverResult::Success;
  73. }
  74. DriverResult RingConProtocol::ConfigureRing() {
  75. LOG_DEBUG(Input, "ConfigureRing");
  76. static constexpr std::array<u8, 37> ring_config{
  77. 0x06, 0x03, 0x25, 0x06, 0x00, 0x00, 0x00, 0x00, 0x1C, 0x16, 0xED, 0x34, 0x36,
  78. 0x00, 0x00, 0x00, 0x0A, 0x64, 0x0B, 0xE6, 0xA9, 0x22, 0x00, 0x00, 0x04, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x90, 0xA8, 0xE1, 0x34, 0x36};
  80. const DriverResult result = SendSubCommand(SubCommand::SET_EXTERNAL_FORMAT_CONFIG, ring_config);
  81. if (result != DriverResult::Success) {
  82. return result;
  83. }
  84. static constexpr std::array<u8, 4> ringcon_data{0x04, 0x01, 0x01, 0x02};
  85. return SendSubCommand(SubCommand::ENABLE_EXTERNAL_POLLING, ringcon_data);
  86. }
  87. bool RingConProtocol::IsEnabled() const {
  88. return is_enabled;
  89. }
  90. } // namespace InputCommon::Joycon