poller.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/poller.h"
  5. namespace InputCommon::Joycon {
  6. JoyconPoller::JoyconPoller(ControllerType device_type_, JoyStickCalibration left_stick_calibration_,
  7. JoyStickCalibration right_stick_calibration_,
  8. MotionCalibration motion_calibration_)
  9. : device_type{device_type_}, left_stick_calibration{left_stick_calibration_},
  10. right_stick_calibration{right_stick_calibration_}, motion_calibration{motion_calibration_} {}
  11. void JoyconPoller::SetCallbacks(const JoyconCallbacks& callbacks_) {
  12. callbacks = std::move(callbacks_);
  13. }
  14. void JoyconPoller::ReadActiveMode(std::span<u8> buffer, const MotionStatus& motion_status,
  15. const RingStatus& ring_status) {
  16. InputReportActive data{};
  17. memcpy(&data, buffer.data(), sizeof(InputReportActive));
  18. switch (device_type) {
  19. case ControllerType::Left:
  20. UpdateActiveLeftPadInput(data, motion_status);
  21. break;
  22. case ControllerType::Right:
  23. UpdateActiveRightPadInput(data, motion_status);
  24. break;
  25. case ControllerType::Pro:
  26. UpdateActiveProPadInput(data, motion_status);
  27. break;
  28. default:
  29. break;
  30. }
  31. if (ring_status.is_enabled) {
  32. UpdateRing(data.ring_input, ring_status);
  33. }
  34. callbacks.on_battery_data(data.battery_status);
  35. }
  36. void JoyconPoller::ReadPassiveMode(std::span<u8> buffer) {
  37. InputReportPassive data{};
  38. memcpy(&data, buffer.data(), sizeof(InputReportPassive));
  39. switch (device_type) {
  40. case ControllerType::Left:
  41. UpdatePassiveLeftPadInput(data);
  42. break;
  43. case ControllerType::Right:
  44. UpdatePassiveRightPadInput(data);
  45. break;
  46. case ControllerType::Pro:
  47. UpdatePassiveProPadInput(data);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. void JoyconPoller::ReadNfcIRMode(std::span<u8> buffer, const MotionStatus& motion_status) {
  54. // This mode is compatible with the active mode
  55. ReadActiveMode(buffer, motion_status, {});
  56. }
  57. void JoyconPoller::UpdateColor(const Color& color) {
  58. callbacks.on_color_data(color);
  59. }
  60. void JoyconPoller::UpdateAmiibo(const Joycon::TagInfo& tag_info) {
  61. callbacks.on_amiibo_data(tag_info);
  62. }
  63. void JoyconPoller::UpdateCamera(const std::vector<u8>& camera_data, IrsResolution format) {
  64. callbacks.on_camera_data(camera_data, format);
  65. }
  66. void JoyconPoller::UpdateRing(s16 value, const RingStatus& ring_status) {
  67. float normalized_value = static_cast<float>(value - ring_status.default_value);
  68. if (normalized_value > 0) {
  69. normalized_value = normalized_value /
  70. static_cast<float>(ring_status.max_value - ring_status.default_value);
  71. }
  72. if (normalized_value < 0) {
  73. normalized_value = normalized_value /
  74. static_cast<float>(ring_status.default_value - ring_status.min_value);
  75. }
  76. callbacks.on_ring_data(normalized_value);
  77. }
  78. void JoyconPoller::UpdateActiveLeftPadInput(const InputReportActive& input,
  79. const MotionStatus& motion_status) {
  80. static constexpr std::array<Joycon::PadButton, 11> left_buttons{
  81. Joycon::PadButton::Down, Joycon::PadButton::Up, Joycon::PadButton::Right,
  82. Joycon::PadButton::Left, Joycon::PadButton::LeftSL, Joycon::PadButton::LeftSR,
  83. Joycon::PadButton::L, Joycon::PadButton::ZL, Joycon::PadButton::Minus,
  84. Joycon::PadButton::Capture, Joycon::PadButton::StickL,
  85. };
  86. const u32 raw_button =
  87. static_cast<u32>(input.button_input[2] | ((input.button_input[1] & 0b00101001) << 16));
  88. for (std::size_t i = 0; i < left_buttons.size(); ++i) {
  89. const bool button_status = (raw_button & static_cast<u32>(left_buttons[i])) != 0;
  90. const int button = static_cast<int>(left_buttons[i]);
  91. callbacks.on_button_data(button, button_status);
  92. }
  93. const u16 raw_left_axis_x =
  94. static_cast<u16>(input.left_stick_state[0] | ((input.left_stick_state[1] & 0xf) << 8));
  95. const u16 raw_left_axis_y =
  96. static_cast<u16>((input.left_stick_state[1] >> 4) | (input.left_stick_state[2] << 4));
  97. const f32 left_axis_x = GetAxisValue(raw_left_axis_x, left_stick_calibration.x);
  98. const f32 left_axis_y = GetAxisValue(raw_left_axis_y, left_stick_calibration.y);
  99. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickX), left_axis_x);
  100. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickY), left_axis_y);
  101. if (motion_status.is_enabled) {
  102. auto left_motion = GetMotionInput(input, motion_status);
  103. // Rotate motion axis to the correct direction
  104. left_motion.accel_y = -left_motion.accel_y;
  105. left_motion.accel_z = -left_motion.accel_z;
  106. left_motion.gyro_x = -left_motion.gyro_x;
  107. callbacks.on_motion_data(static_cast<int>(PadMotion::LeftMotion), left_motion);
  108. }
  109. }
  110. void JoyconPoller::UpdateActiveRightPadInput(const InputReportActive& input,
  111. const MotionStatus& motion_status) {
  112. static constexpr std::array<Joycon::PadButton, 11> right_buttons{
  113. Joycon::PadButton::Y, Joycon::PadButton::X, Joycon::PadButton::B,
  114. Joycon::PadButton::A, Joycon::PadButton::RightSL, Joycon::PadButton::RightSR,
  115. Joycon::PadButton::R, Joycon::PadButton::ZR, Joycon::PadButton::Plus,
  116. Joycon::PadButton::Home, Joycon::PadButton::StickR,
  117. };
  118. const u32 raw_button =
  119. static_cast<u32>((input.button_input[0] << 8) | (input.button_input[1] << 16));
  120. for (std::size_t i = 0; i < right_buttons.size(); ++i) {
  121. const bool button_status = (raw_button & static_cast<u32>(right_buttons[i])) != 0;
  122. const int button = static_cast<int>(right_buttons[i]);
  123. callbacks.on_button_data(button, button_status);
  124. }
  125. const u16 raw_right_axis_x =
  126. static_cast<u16>(input.right_stick_state[0] | ((input.right_stick_state[1] & 0xf) << 8));
  127. const u16 raw_right_axis_y =
  128. static_cast<u16>((input.right_stick_state[1] >> 4) | (input.right_stick_state[2] << 4));
  129. const f32 right_axis_x = GetAxisValue(raw_right_axis_x, right_stick_calibration.x);
  130. const f32 right_axis_y = GetAxisValue(raw_right_axis_y, right_stick_calibration.y);
  131. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickX), right_axis_x);
  132. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickY), right_axis_y);
  133. if (motion_status.is_enabled) {
  134. auto right_motion = GetMotionInput(input, motion_status);
  135. // Rotate motion axis to the correct direction
  136. right_motion.accel_x = -right_motion.accel_x;
  137. right_motion.accel_y = -right_motion.accel_y;
  138. right_motion.gyro_z = -right_motion.gyro_z;
  139. callbacks.on_motion_data(static_cast<int>(PadMotion::RightMotion), right_motion);
  140. }
  141. }
  142. void JoyconPoller::UpdateActiveProPadInput(const InputReportActive& input,
  143. const MotionStatus& motion_status) {
  144. static constexpr std::array<Joycon::PadButton, 18> pro_buttons{
  145. Joycon::PadButton::Down, Joycon::PadButton::Up, Joycon::PadButton::Right,
  146. Joycon::PadButton::Left, Joycon::PadButton::L, Joycon::PadButton::ZL,
  147. Joycon::PadButton::Minus, Joycon::PadButton::Capture, Joycon::PadButton::Y,
  148. Joycon::PadButton::X, Joycon::PadButton::B, Joycon::PadButton::A,
  149. Joycon::PadButton::R, Joycon::PadButton::ZR, Joycon::PadButton::Plus,
  150. Joycon::PadButton::Home, Joycon::PadButton::StickL, Joycon::PadButton::StickR,
  151. };
  152. const u32 raw_button = static_cast<u32>(input.button_input[2] | (input.button_input[0] << 8) |
  153. (input.button_input[1] << 16));
  154. for (std::size_t i = 0; i < pro_buttons.size(); ++i) {
  155. const bool button_status = (raw_button & static_cast<u32>(pro_buttons[i])) != 0;
  156. const int button = static_cast<int>(pro_buttons[i]);
  157. callbacks.on_button_data(button, button_status);
  158. }
  159. const u16 raw_left_axis_x =
  160. static_cast<u16>(input.left_stick_state[0] | ((input.left_stick_state[1] & 0xf) << 8));
  161. const u16 raw_left_axis_y =
  162. static_cast<u16>((input.left_stick_state[1] >> 4) | (input.left_stick_state[2] << 4));
  163. const u16 raw_right_axis_x =
  164. static_cast<u16>(input.right_stick_state[0] | ((input.right_stick_state[1] & 0xf) << 8));
  165. const u16 raw_right_axis_y =
  166. static_cast<u16>((input.right_stick_state[1] >> 4) | (input.right_stick_state[2] << 4));
  167. const f32 left_axis_x = GetAxisValue(raw_left_axis_x, left_stick_calibration.x);
  168. const f32 left_axis_y = GetAxisValue(raw_left_axis_y, left_stick_calibration.y);
  169. const f32 right_axis_x = GetAxisValue(raw_right_axis_x, right_stick_calibration.x);
  170. const f32 right_axis_y = GetAxisValue(raw_right_axis_y, right_stick_calibration.y);
  171. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickX), left_axis_x);
  172. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickY), left_axis_y);
  173. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickX), right_axis_x);
  174. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickY), right_axis_y);
  175. if (motion_status.is_enabled) {
  176. auto pro_motion = GetMotionInput(input, motion_status);
  177. pro_motion.gyro_x = -pro_motion.gyro_x;
  178. pro_motion.accel_y = -pro_motion.accel_y;
  179. pro_motion.accel_z = -pro_motion.accel_z;
  180. callbacks.on_motion_data(static_cast<int>(PadMotion::LeftMotion), pro_motion);
  181. callbacks.on_motion_data(static_cast<int>(PadMotion::RightMotion), pro_motion);
  182. }
  183. }
  184. void JoyconPoller::UpdatePassiveLeftPadInput(const InputReportPassive& input) {
  185. static constexpr std::array<PassivePadButton, 11> left_buttons{
  186. PassivePadButton::Down_A, PassivePadButton::Right_X, PassivePadButton::Left_B,
  187. PassivePadButton::Up_Y, PassivePadButton::SL, PassivePadButton::SR,
  188. PassivePadButton::L_R, PassivePadButton::ZL_ZR, PassivePadButton::Minus,
  189. PassivePadButton::Capture, PassivePadButton::StickL,
  190. };
  191. for (auto left_button : left_buttons) {
  192. const bool button_status = (input.button_input & static_cast<u32>(left_button)) != 0;
  193. const int button = static_cast<int>(left_button);
  194. callbacks.on_button_data(button, button_status);
  195. }
  196. const auto [left_axis_x, left_axis_y] =
  197. GetPassiveAxisValue(static_cast<PassivePadStick>(input.stick_state));
  198. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickX), left_axis_x);
  199. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickY), left_axis_y);
  200. }
  201. void JoyconPoller::UpdatePassiveRightPadInput(const InputReportPassive& input) {
  202. static constexpr std::array<PassivePadButton, 11> right_buttons{
  203. PassivePadButton::Down_A, PassivePadButton::Right_X, PassivePadButton::Left_B,
  204. PassivePadButton::Up_Y, PassivePadButton::SL, PassivePadButton::SR,
  205. PassivePadButton::L_R, PassivePadButton::ZL_ZR, PassivePadButton::Plus,
  206. PassivePadButton::Home, PassivePadButton::StickR,
  207. };
  208. for (auto right_button : right_buttons) {
  209. const bool button_status = (input.button_input & static_cast<u32>(right_button)) != 0;
  210. const int button = static_cast<int>(right_button);
  211. callbacks.on_button_data(button, button_status);
  212. }
  213. const auto [right_axis_x, right_axis_y] =
  214. GetPassiveAxisValue(static_cast<PassivePadStick>(input.stick_state));
  215. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickX), right_axis_x);
  216. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickY), right_axis_y);
  217. }
  218. void JoyconPoller::UpdatePassiveProPadInput(const InputReportPassive& input) {
  219. static constexpr std::array<PassivePadButton, 14> pro_buttons{
  220. PassivePadButton::Down_A, PassivePadButton::Right_X, PassivePadButton::Left_B,
  221. PassivePadButton::Up_Y, PassivePadButton::SL, PassivePadButton::SR,
  222. PassivePadButton::L_R, PassivePadButton::ZL_ZR, PassivePadButton::Minus,
  223. PassivePadButton::Plus, PassivePadButton::Capture, PassivePadButton::Home,
  224. PassivePadButton::StickL, PassivePadButton::StickR,
  225. };
  226. for (auto pro_button : pro_buttons) {
  227. const bool button_status = (input.button_input & static_cast<u32>(pro_button)) != 0;
  228. const int button = static_cast<int>(pro_button);
  229. callbacks.on_button_data(button, button_status);
  230. }
  231. const auto [left_axis_x, left_axis_y] =
  232. GetPassiveAxisValue(static_cast<PassivePadStick>(input.stick_state & 0xf));
  233. const auto [right_axis_x, right_axis_y] =
  234. GetPassiveAxisValue(static_cast<PassivePadStick>(input.stick_state >> 4));
  235. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickX), left_axis_x);
  236. callbacks.on_stick_data(static_cast<int>(PadAxes::LeftStickY), left_axis_y);
  237. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickX), right_axis_x);
  238. callbacks.on_stick_data(static_cast<int>(PadAxes::RightStickY), right_axis_y);
  239. }
  240. f32 JoyconPoller::GetAxisValue(u16 raw_value, Joycon::JoyStickAxisCalibration calibration) const {
  241. const f32 value = static_cast<f32>(raw_value - calibration.center);
  242. if (value > 0.0f) {
  243. return value / calibration.max;
  244. }
  245. return value / calibration.min;
  246. }
  247. std::pair<f32, f32> JoyconPoller::GetPassiveAxisValue(PassivePadStick raw_value) const {
  248. switch (raw_value) {
  249. case PassivePadStick::Right:
  250. return {1.0f, 0.0f};
  251. case PassivePadStick::RightDown:
  252. return {1.0f, -1.0f};
  253. case PassivePadStick::Down:
  254. return {0.0f, -1.0f};
  255. case PassivePadStick::DownLeft:
  256. return {-1.0f, -1.0f};
  257. case PassivePadStick::Left:
  258. return {-1.0f, 0.0f};
  259. case PassivePadStick::LeftUp:
  260. return {-1.0f, 1.0f};
  261. case PassivePadStick::Up:
  262. return {0.0f, 1.0f};
  263. case PassivePadStick::UpRight:
  264. return {1.0f, 1.0f};
  265. case PassivePadStick::Neutral:
  266. default:
  267. return {0.0f, 0.0f};
  268. }
  269. }
  270. f32 JoyconPoller::GetAccelerometerValue(s16 raw, const MotionSensorCalibration& cal,
  271. AccelerometerSensitivity sensitivity) const {
  272. const f32 value = raw * (1.0f / (cal.scale - cal.offset)) * 4;
  273. switch (sensitivity) {
  274. case Joycon::AccelerometerSensitivity::G2:
  275. return value / 4.0f;
  276. case Joycon::AccelerometerSensitivity::G4:
  277. return value / 2.0f;
  278. case Joycon::AccelerometerSensitivity::G8:
  279. return value;
  280. case Joycon::AccelerometerSensitivity::G16:
  281. return value * 2.0f;
  282. }
  283. return value;
  284. }
  285. f32 JoyconPoller::GetGyroValue(s16 raw, const MotionSensorCalibration& cal,
  286. GyroSensitivity sensitivity) const {
  287. const f32 value = (raw - cal.offset) * (936.0f / (cal.scale - cal.offset)) / 360.0f;
  288. switch (sensitivity) {
  289. case Joycon::GyroSensitivity::DPS250:
  290. return value / 8.0f;
  291. case Joycon::GyroSensitivity::DPS500:
  292. return value / 4.0f;
  293. case Joycon::GyroSensitivity::DPS1000:
  294. return value / 2.0f;
  295. case Joycon::GyroSensitivity::DPS2000:
  296. return value;
  297. }
  298. return value;
  299. }
  300. s16 JoyconPoller::GetRawIMUValues(std::size_t sensor, size_t axis,
  301. const InputReportActive& input) const {
  302. return input.motion_input[(sensor * 3) + axis];
  303. }
  304. MotionData JoyconPoller::GetMotionInput(const InputReportActive& input,
  305. const MotionStatus& motion_status) const {
  306. MotionData motion{};
  307. const auto& accel_cal = motion_calibration.accelerometer;
  308. const auto& gyro_cal = motion_calibration.gyro;
  309. const s16 raw_accel_x = input.motion_input[1];
  310. const s16 raw_accel_y = input.motion_input[0];
  311. const s16 raw_accel_z = input.motion_input[2];
  312. const s16 raw_gyro_x = input.motion_input[4];
  313. const s16 raw_gyro_y = input.motion_input[3];
  314. const s16 raw_gyro_z = input.motion_input[5];
  315. motion.delta_timestamp = motion_status.delta_time;
  316. motion.accel_x =
  317. GetAccelerometerValue(raw_accel_x, accel_cal[1], motion_status.accelerometer_sensitivity);
  318. motion.accel_y =
  319. GetAccelerometerValue(raw_accel_y, accel_cal[0], motion_status.accelerometer_sensitivity);
  320. motion.accel_z =
  321. GetAccelerometerValue(raw_accel_z, accel_cal[2], motion_status.accelerometer_sensitivity);
  322. motion.gyro_x = GetGyroValue(raw_gyro_x, gyro_cal[1], motion_status.gyro_sensitivity);
  323. motion.gyro_y = GetGyroValue(raw_gyro_y, gyro_cal[0], motion_status.gyro_sensitivity);
  324. motion.gyro_z = GetGyroValue(raw_gyro_z, gyro_cal[2], motion_status.gyro_sensitivity);
  325. // TODO(German77): Return all three samples data
  326. return motion;
  327. }
  328. } // namespace InputCommon::Joycon