rumble.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cmath>
  5. #include "common/logging/log.h"
  6. #include "input_common/helpers/joycon_protocol/rumble.h"
  7. namespace InputCommon::Joycon {
  8. RumbleProtocol::RumbleProtocol(std::shared_ptr<JoyconHandle> handle)
  9. : JoyconCommonProtocol(std::move(handle)) {}
  10. DriverResult RumbleProtocol::EnableRumble(bool is_enabled) {
  11. LOG_DEBUG(Input, "Enable Rumble");
  12. const std::array<u8, 1> buffer{static_cast<u8>(is_enabled ? 1 : 0)};
  13. std::vector<u8> output;
  14. SetBlocking();
  15. const auto result = SendSubCommand(SubCommand::ENABLE_VIBRATION, buffer, output);
  16. SetNonBlocking();
  17. return result;
  18. }
  19. DriverResult RumbleProtocol::SendVibration(const VibrationValue& vibration) {
  20. std::array<u8, sizeof(DefaultVibrationBuffer)> buffer{};
  21. if (vibration.high_amplitude <= 0.0f && vibration.low_amplitude <= 0.0f) {
  22. return SendVibrationReport(DefaultVibrationBuffer);
  23. }
  24. // Protect joycons from damage from strong vibrations
  25. const f32 clamp_amplitude =
  26. 1.0f / std::max(1.0f, vibration.high_amplitude + vibration.low_amplitude);
  27. const u16 encoded_high_frequency = EncodeHighFrequency(vibration.high_frequency);
  28. const u8 encoded_high_amplitude =
  29. EncodeHighAmplitude(vibration.high_amplitude * clamp_amplitude);
  30. const u8 encoded_low_frequency = EncodeLowFrequency(vibration.low_frequency);
  31. const u16 encoded_low_amplitude = EncodeLowAmplitude(vibration.low_amplitude * clamp_amplitude);
  32. buffer[0] = static_cast<u8>(encoded_high_frequency & 0xFF);
  33. buffer[1] = static_cast<u8>(encoded_high_amplitude | ((encoded_high_frequency >> 8) & 0x01));
  34. buffer[2] = static_cast<u8>(encoded_low_frequency | ((encoded_low_amplitude >> 8) & 0x80));
  35. buffer[3] = static_cast<u8>(encoded_low_amplitude & 0xFF);
  36. // Duplicate rumble for now
  37. buffer[4] = buffer[0];
  38. buffer[5] = buffer[1];
  39. buffer[6] = buffer[2];
  40. buffer[7] = buffer[3];
  41. return SendVibrationReport(buffer);
  42. }
  43. u16 RumbleProtocol::EncodeHighFrequency(f32 frequency) const {
  44. const u8 new_frequency =
  45. static_cast<u8>(std::clamp(std::log2(frequency / 10.0f) * 32.0f, 0.0f, 255.0f));
  46. return static_cast<u16>((new_frequency - 0x60) * 4);
  47. }
  48. u8 RumbleProtocol::EncodeLowFrequency(f32 frequency) const {
  49. const u8 new_frequency =
  50. static_cast<u8>(std::clamp(std::log2(frequency / 10.0f) * 32.0f, 0.0f, 255.0f));
  51. return static_cast<u8>(new_frequency - 0x40);
  52. }
  53. u8 RumbleProtocol::EncodeHighAmplitude(f32 amplitude) const {
  54. /* More information about these values can be found here:
  55. * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
  56. */
  57. static constexpr std::array<std::pair<f32, int>, 101> high_fequency_amplitude{
  58. std::pair<f32, int>{0.0f, 0x0},
  59. {0.01f, 0x2},
  60. {0.012f, 0x4},
  61. {0.014f, 0x6},
  62. {0.017f, 0x8},
  63. {0.02f, 0x0a},
  64. {0.024f, 0x0c},
  65. {0.028f, 0x0e},
  66. {0.033f, 0x10},
  67. {0.04f, 0x12},
  68. {0.047f, 0x14},
  69. {0.056f, 0x16},
  70. {0.067f, 0x18},
  71. {0.08f, 0x1a},
  72. {0.095f, 0x1c},
  73. {0.112f, 0x1e},
  74. {0.117f, 0x20},
  75. {0.123f, 0x22},
  76. {0.128f, 0x24},
  77. {0.134f, 0x26},
  78. {0.14f, 0x28},
  79. {0.146f, 0x2a},
  80. {0.152f, 0x2c},
  81. {0.159f, 0x2e},
  82. {0.166f, 0x30},
  83. {0.173f, 0x32},
  84. {0.181f, 0x34},
  85. {0.189f, 0x36},
  86. {0.198f, 0x38},
  87. {0.206f, 0x3a},
  88. {0.215f, 0x3c},
  89. {0.225f, 0x3e},
  90. {0.23f, 0x40},
  91. {0.235f, 0x42},
  92. {0.24f, 0x44},
  93. {0.245f, 0x46},
  94. {0.251f, 0x48},
  95. {0.256f, 0x4a},
  96. {0.262f, 0x4c},
  97. {0.268f, 0x4e},
  98. {0.273f, 0x50},
  99. {0.279f, 0x52},
  100. {0.286f, 0x54},
  101. {0.292f, 0x56},
  102. {0.298f, 0x58},
  103. {0.305f, 0x5a},
  104. {0.311f, 0x5c},
  105. {0.318f, 0x5e},
  106. {0.325f, 0x60},
  107. {0.332f, 0x62},
  108. {0.34f, 0x64},
  109. {0.347f, 0x66},
  110. {0.355f, 0x68},
  111. {0.362f, 0x6a},
  112. {0.37f, 0x6c},
  113. {0.378f, 0x6e},
  114. {0.387f, 0x70},
  115. {0.395f, 0x72},
  116. {0.404f, 0x74},
  117. {0.413f, 0x76},
  118. {0.422f, 0x78},
  119. {0.431f, 0x7a},
  120. {0.44f, 0x7c},
  121. {0.45f, 0x7e},
  122. {0.46f, 0x80},
  123. {0.47f, 0x82},
  124. {0.48f, 0x84},
  125. {0.491f, 0x86},
  126. {0.501f, 0x88},
  127. {0.512f, 0x8a},
  128. {0.524f, 0x8c},
  129. {0.535f, 0x8e},
  130. {0.547f, 0x90},
  131. {0.559f, 0x92},
  132. {0.571f, 0x94},
  133. {0.584f, 0x96},
  134. {0.596f, 0x98},
  135. {0.609f, 0x9a},
  136. {0.623f, 0x9c},
  137. {0.636f, 0x9e},
  138. {0.65f, 0xa0},
  139. {0.665f, 0xa2},
  140. {0.679f, 0xa4},
  141. {0.694f, 0xa6},
  142. {0.709f, 0xa8},
  143. {0.725f, 0xaa},
  144. {0.741f, 0xac},
  145. {0.757f, 0xae},
  146. {0.773f, 0xb0},
  147. {0.79f, 0xb2},
  148. {0.808f, 0xb4},
  149. {0.825f, 0xb6},
  150. {0.843f, 0xb8},
  151. {0.862f, 0xba},
  152. {0.881f, 0xbc},
  153. {0.9f, 0xbe},
  154. {0.92f, 0xc0},
  155. {0.94f, 0xc2},
  156. {0.96f, 0xc4},
  157. {0.981f, 0xc6},
  158. {1.003f, 0xc8},
  159. };
  160. for (const auto& [amplitude_value, code] : high_fequency_amplitude) {
  161. if (amplitude <= amplitude_value) {
  162. return static_cast<u8>(code);
  163. }
  164. }
  165. return static_cast<u8>(high_fequency_amplitude[high_fequency_amplitude.size() - 1].second);
  166. }
  167. u16 RumbleProtocol::EncodeLowAmplitude(f32 amplitude) const {
  168. /* More information about these values can be found here:
  169. * https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md
  170. */
  171. static constexpr std::array<std::pair<f32, int>, 101> high_fequency_amplitude{
  172. std::pair<f32, int>{0.0f, 0x0040},
  173. {0.01f, 0x8040},
  174. {0.012f, 0x0041},
  175. {0.014f, 0x8041},
  176. {0.017f, 0x0042},
  177. {0.02f, 0x8042},
  178. {0.024f, 0x0043},
  179. {0.028f, 0x8043},
  180. {0.033f, 0x0044},
  181. {0.04f, 0x8044},
  182. {0.047f, 0x0045},
  183. {0.056f, 0x8045},
  184. {0.067f, 0x0046},
  185. {0.08f, 0x8046},
  186. {0.095f, 0x0047},
  187. {0.112f, 0x8047},
  188. {0.117f, 0x0048},
  189. {0.123f, 0x8048},
  190. {0.128f, 0x0049},
  191. {0.134f, 0x8049},
  192. {0.14f, 0x004a},
  193. {0.146f, 0x804a},
  194. {0.152f, 0x004b},
  195. {0.159f, 0x804b},
  196. {0.166f, 0x004c},
  197. {0.173f, 0x804c},
  198. {0.181f, 0x004d},
  199. {0.189f, 0x804d},
  200. {0.198f, 0x004e},
  201. {0.206f, 0x804e},
  202. {0.215f, 0x004f},
  203. {0.225f, 0x804f},
  204. {0.23f, 0x0050},
  205. {0.235f, 0x8050},
  206. {0.24f, 0x0051},
  207. {0.245f, 0x8051},
  208. {0.251f, 0x0052},
  209. {0.256f, 0x8052},
  210. {0.262f, 0x0053},
  211. {0.268f, 0x8053},
  212. {0.273f, 0x0054},
  213. {0.279f, 0x8054},
  214. {0.286f, 0x0055},
  215. {0.292f, 0x8055},
  216. {0.298f, 0x0056},
  217. {0.305f, 0x8056},
  218. {0.311f, 0x0057},
  219. {0.318f, 0x8057},
  220. {0.325f, 0x0058},
  221. {0.332f, 0x8058},
  222. {0.34f, 0x0059},
  223. {0.347f, 0x8059},
  224. {0.355f, 0x005a},
  225. {0.362f, 0x805a},
  226. {0.37f, 0x005b},
  227. {0.378f, 0x805b},
  228. {0.387f, 0x005c},
  229. {0.395f, 0x805c},
  230. {0.404f, 0x005d},
  231. {0.413f, 0x805d},
  232. {0.422f, 0x005e},
  233. {0.431f, 0x805e},
  234. {0.44f, 0x005f},
  235. {0.45f, 0x805f},
  236. {0.46f, 0x0060},
  237. {0.47f, 0x8060},
  238. {0.48f, 0x0061},
  239. {0.491f, 0x8061},
  240. {0.501f, 0x0062},
  241. {0.512f, 0x8062},
  242. {0.524f, 0x0063},
  243. {0.535f, 0x8063},
  244. {0.547f, 0x0064},
  245. {0.559f, 0x8064},
  246. {0.571f, 0x0065},
  247. {0.584f, 0x8065},
  248. {0.596f, 0x0066},
  249. {0.609f, 0x8066},
  250. {0.623f, 0x0067},
  251. {0.636f, 0x8067},
  252. {0.65f, 0x0068},
  253. {0.665f, 0x8068},
  254. {0.679f, 0x0069},
  255. {0.694f, 0x8069},
  256. {0.709f, 0x006a},
  257. {0.725f, 0x806a},
  258. {0.741f, 0x006b},
  259. {0.757f, 0x806b},
  260. {0.773f, 0x006c},
  261. {0.79f, 0x806c},
  262. {0.808f, 0x006d},
  263. {0.825f, 0x806d},
  264. {0.843f, 0x006e},
  265. {0.862f, 0x806e},
  266. {0.881f, 0x006f},
  267. {0.9f, 0x806f},
  268. {0.92f, 0x0070},
  269. {0.94f, 0x8070},
  270. {0.96f, 0x0071},
  271. {0.981f, 0x8071},
  272. {1.003f, 0x0072},
  273. };
  274. for (const auto& [amplitude_value, code] : high_fequency_amplitude) {
  275. if (amplitude <= amplitude_value) {
  276. return static_cast<u16>(code);
  277. }
  278. }
  279. return static_cast<u16>(high_fequency_amplitude[high_fequency_amplitude.size() - 1].second);
  280. }
  281. } // namespace InputCommon::Joycon