SDL_hidapi_zuiki.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. Copyright (C) 2025 Zuiki Inc.
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include "SDL_internal.h"
  20. #ifdef SDL_JOYSTICK_HIDAPI
  21. #include "../SDL_sysjoystick.h"
  22. #include "SDL_hidapijoystick_c.h"
  23. #include "SDL_hidapi_rumble.h"
  24. #ifdef SDL_JOYSTICK_HIDAPI_ZUIKI
  25. #define GYRO_SCALE (1024.0f / 32768.0f * SDL_PI_F / 180.0f) // Calculate scaling factor based on gyroscope data range and radians
  26. #define ACCEL_SCALE (8.0f / 32768.0f * SDL_STANDARD_GRAVITY) // Calculate acceleration scaling factor based on gyroscope data range and standard gravity
  27. #define FILTER_SIZE 11 // Must be an odd number
  28. #define MAX_RETRY_COUNT 10 // zuiki device initialization retry count
  29. // Define this if you want to log all packets from the controller
  30. #if 0
  31. #define DEBUG_ZUIKI_PROTOCOL
  32. #endif
  33. typedef struct {
  34. float buffer[FILTER_SIZE];
  35. uint8_t index;
  36. uint8_t count;
  37. } MedianFilter_t;
  38. typedef struct
  39. {
  40. Uint8 last_state[USB_PACKET_LENGTH];
  41. bool sensors_supported; // Sensor enabled status flag
  42. Uint64 sensor_timestamp_ns; // Sensor timestamp (nanoseconds, cumulative update)
  43. float sensor_rate;
  44. MedianFilter_t filter_gyro_x;
  45. MedianFilter_t filter_gyro_y;
  46. MedianFilter_t filter_gyro_z;
  47. } SDL_DriverZUIKI_Context;
  48. static float median_filter_update(MedianFilter_t* mf, float input) {
  49. mf->buffer[mf->index] = input;
  50. mf->index = (mf->index + 1) % FILTER_SIZE;
  51. if (mf->count < FILTER_SIZE) mf->count++;
  52. float temp[FILTER_SIZE];
  53. SDL_memcpy(temp, mf->buffer, sizeof(temp));
  54. for (int i = 0; i < mf->count - 1; i++) {
  55. for (int j = i + 1; j < mf->count; j++) {
  56. if (temp[i] > temp[j]) {
  57. float t = temp[i];
  58. temp[i] = temp[j];
  59. temp[j] = t;
  60. }
  61. }
  62. }
  63. return temp[mf->count / 2];
  64. }
  65. static void HIDAPI_DriverZUIKI_RegisterHints(SDL_HintCallback callback, void *userdata)
  66. {
  67. SDL_AddHintCallback(SDL_HINT_JOYSTICK_HIDAPI_ZUIKI, callback, userdata);
  68. }
  69. static void HIDAPI_DriverZUIKI_UnregisterHints(SDL_HintCallback callback, void *userdata)
  70. {
  71. SDL_RemoveHintCallback(SDL_HINT_JOYSTICK_HIDAPI_ZUIKI, callback, userdata);
  72. }
  73. static bool HIDAPI_DriverZUIKI_IsEnabled(void)
  74. {
  75. return SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI_ZUIKI, SDL_GetHintBoolean(SDL_HINT_JOYSTICK_HIDAPI, SDL_HIDAPI_DEFAULT));
  76. }
  77. static bool HIDAPI_DriverZUIKI_IsSupportedDevice(SDL_HIDAPI_Device *device, const char *name, SDL_GamepadType type, Uint16 vendor_id, Uint16 product_id, Uint16 version, int interface_number, int interface_class, int interface_subclass, int interface_protocol)
  78. {
  79. if (vendor_id == USB_VENDOR_ZUIKI) {
  80. switch (product_id) {
  81. case USB_PRODUCT_ZUIKI_MASCON_PRO:
  82. case USB_PRODUCT_ZUIKI_EVOTOP_UWB_DINPUT:
  83. case USB_PRODUCT_ZUIKI_EVOTOP_PC_DINPUT:
  84. case USB_PRODUCT_ZUIKI_EVOTOP_PC_BT:
  85. return true;
  86. default:
  87. break;
  88. }
  89. }
  90. return false;
  91. }
  92. static bool HIDAPI_DriverZUIKI_InitDevice(SDL_HIDAPI_Device *device)
  93. {
  94. Uint8 data[USB_PACKET_LENGTH * 2];
  95. SDL_DriverZUIKI_Context *ctx = (SDL_DriverZUIKI_Context *)SDL_calloc(1, sizeof(*ctx));
  96. if (!ctx) {
  97. return false;
  98. }
  99. device->context = ctx;
  100. ctx->sensors_supported = false;
  101. // Read report data once for device initialization
  102. int size = -1;
  103. Uint8 retry_count = 0;
  104. while (retry_count < MAX_RETRY_COUNT) {
  105. size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 10);
  106. if (size > 0) {
  107. break;
  108. }
  109. retry_count++;
  110. }
  111. if (size <= 0) {
  112. return false;
  113. }
  114. switch (device->product_id) {
  115. case USB_PRODUCT_ZUIKI_MASCON_PRO:
  116. HIDAPI_SetDeviceName(device, "ZUIKI MASCON PRO");
  117. break;
  118. case USB_PRODUCT_ZUIKI_EVOTOP_PC_DINPUT:
  119. ctx->sensors_supported = true;
  120. ctx->sensor_rate = 200.0f;
  121. break;
  122. case USB_PRODUCT_ZUIKI_EVOTOP_UWB_DINPUT:
  123. ctx->sensors_supported = true;
  124. ctx->sensor_rate = 100.0f;
  125. break;
  126. case USB_PRODUCT_ZUIKI_EVOTOP_PC_BT:
  127. if (size > 0 && data[16] != 0) {
  128. ctx->sensors_supported = true;
  129. ctx->sensor_rate = 50.0f;
  130. }
  131. HIDAPI_SetDeviceName(device, "ZUIKI EVOTOP");
  132. break;
  133. default:
  134. break;
  135. }
  136. return HIDAPI_JoystickConnected(device, NULL);
  137. }
  138. static int HIDAPI_DriverZUIKI_GetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id)
  139. {
  140. return -1;
  141. }
  142. static void HIDAPI_DriverZUIKI_SetDevicePlayerIndex(SDL_HIDAPI_Device *device, SDL_JoystickID instance_id, int player_index)
  143. {
  144. }
  145. #ifndef DEG2RAD
  146. #define DEG2RAD(x) ((float)(x) * (float)(SDL_PI_F / 180.f))
  147. #endif
  148. static bool HIDAPI_DriverZUIKI_OpenJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  149. {
  150. SDL_DriverZUIKI_Context *ctx = (SDL_DriverZUIKI_Context *)device->context;
  151. SDL_AssertJoysticksLocked();
  152. SDL_zeroa(ctx->last_state);
  153. joystick->nbuttons = 11;
  154. joystick->naxes = SDL_GAMEPAD_AXIS_COUNT;
  155. joystick->nhats = 1;
  156. if (ctx->sensors_supported) {
  157. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_GYRO, ctx->sensor_rate);
  158. SDL_PrivateJoystickAddSensor(joystick, SDL_SENSOR_ACCEL, ctx->sensor_rate);
  159. }
  160. return true;
  161. }
  162. static bool HIDAPI_DriverZUIKI_RumbleJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble)
  163. {
  164. Uint8 rumble_packet[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  165. rumble_packet[4] = low_frequency_rumble >> 8;
  166. rumble_packet[5] = high_frequency_rumble >> 8;
  167. if (SDL_HIDAPI_SendRumble(device, rumble_packet, sizeof(rumble_packet)) != sizeof(rumble_packet)) {
  168. return SDL_SetError("Couldn't send rumble packet");
  169. }
  170. return true;
  171. }
  172. static bool HIDAPI_DriverZUIKI_RumbleJoystickTriggers(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble)
  173. {
  174. return SDL_Unsupported();
  175. }
  176. static Uint32 HIDAPI_DriverZUIKI_GetJoystickCapabilities(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  177. {
  178. Uint32 caps = 0;
  179. caps |= SDL_JOYSTICK_CAP_RUMBLE;
  180. return caps;
  181. }
  182. static bool HIDAPI_DriverZUIKI_SetJoystickLED(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
  183. {
  184. return SDL_Unsupported();
  185. }
  186. static bool HIDAPI_DriverZUIKI_SendJoystickEffect(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, const void *data, int size)
  187. {
  188. if (SDL_HIDAPI_SendRumble(device, data, size) != size) {
  189. return SDL_SetError("Couldn't send rumble packet");
  190. }
  191. return true;
  192. }
  193. static bool HIDAPI_DriverZUIKI_SetJoystickSensorsEnabled(SDL_HIDAPI_Device *device, SDL_Joystick *joystick, bool enabled)
  194. {
  195. SDL_DriverZUIKI_Context *ctx = (SDL_DriverZUIKI_Context *)device->context;
  196. if (ctx->sensors_supported) {
  197. return true;
  198. }
  199. return SDL_Unsupported();
  200. }
  201. static void HIDAPI_DriverZUIKI_HandleOldStatePacket(SDL_Joystick *joystick, SDL_DriverZUIKI_Context *ctx, Uint8 *data, int size)
  202. {
  203. Sint16 axis;
  204. Uint64 timestamp = SDL_GetTicksNS();
  205. if (ctx->last_state[2] != data[2]) {
  206. Uint8 hat;
  207. switch (data[2]) {
  208. case 0:
  209. hat = SDL_HAT_UP;
  210. break;
  211. case 1:
  212. hat = SDL_HAT_RIGHTUP;
  213. break;
  214. case 2:
  215. hat = SDL_HAT_RIGHT;
  216. break;
  217. case 3:
  218. hat = SDL_HAT_RIGHTDOWN;
  219. break;
  220. case 4:
  221. hat = SDL_HAT_DOWN;
  222. break;
  223. case 5:
  224. hat = SDL_HAT_LEFTDOWN;
  225. break;
  226. case 6:
  227. hat = SDL_HAT_LEFT;
  228. break;
  229. case 7:
  230. hat = SDL_HAT_LEFTUP;
  231. break;
  232. default:
  233. hat = SDL_HAT_CENTERED;
  234. break;
  235. }
  236. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  237. }
  238. if (ctx->last_state[0] != data[0]) {
  239. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[0] & 0x01) != 0));
  240. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[0] & 0x02) != 0));
  241. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[0] & 0x04) != 0));
  242. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[0] & 0x08) != 0));
  243. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[0] & 0x10) != 0));
  244. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[0] & 0x20) != 0));
  245. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, (data[0] & 0x40) ? SDL_MAX_SINT16 : SDL_MIN_SINT16);
  246. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, (data[0] & 0x80) ? SDL_MAX_SINT16 : SDL_MIN_SINT16);
  247. }
  248. if (ctx->last_state[1] != data[1]) {
  249. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[1] & 0x01) != 0));
  250. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[1] & 0x02) != 0));
  251. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[1] & 0x04) != 0));
  252. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[1] & 0x08) != 0));
  253. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[1] & 0x10) != 0));
  254. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_MISC1, ((data[1] & 0x20) != 0));
  255. /* todo for switch C key */
  256. }
  257. #define READ_STICK_AXIS(offset) \
  258. (data[offset] == 0x7f ? 0 : (Sint16)HIDAPI_RemapVal((float)((int)data[offset] - 0x7f), -0x7f, 0xff - 0x7f, SDL_MIN_SINT16, SDL_MAX_SINT16))
  259. {
  260. axis = READ_STICK_AXIS(3);
  261. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  262. axis = READ_STICK_AXIS(4);
  263. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  264. axis = READ_STICK_AXIS(5);
  265. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  266. axis = READ_STICK_AXIS(6);
  267. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  268. }
  269. #undef READ_STICK_AXIS
  270. if (ctx->sensors_supported) {
  271. Uint64 sensor_timestamp = timestamp;
  272. float gyro_values[3];
  273. gyro_values[0] = median_filter_update(&ctx->filter_gyro_x, LOAD16(data[8], data[9]) * GYRO_SCALE);
  274. gyro_values[1] = median_filter_update(&ctx->filter_gyro_y, LOAD16(data[12], data[13]) * GYRO_SCALE);
  275. gyro_values[2] = median_filter_update(&ctx->filter_gyro_z, -LOAD16(data[10], data[11]) * GYRO_SCALE);
  276. float accel_values[3];
  277. accel_values[0] = LOAD16(data[14], data[15]) * ACCEL_SCALE;
  278. accel_values[2] = -LOAD16(data[16], data[17]) * ACCEL_SCALE;
  279. accel_values[1] = LOAD16(data[18], data[19]) * ACCEL_SCALE;
  280. #ifdef DEBUG_ZUIKI_PROTOCOL
  281. SDL_Log("Gyro raw: %d, %d, %d -> scaled: %.2f, %.2f, %.2f rad/s",
  282. LOAD16(data[8], data[9]), LOAD16(data[10], data[11]), LOAD16(data[12], data[13]),
  283. gyro_values[0], gyro_values[1], gyro_values[2]);
  284. SDL_Log("Accel raw: %d, %d, %d -> scaled: %.2f, %.2f, %.2f m/s²",
  285. LOAD16(data[14], data[15]), LOAD16(data[16], data[17]), LOAD16(data[18], data[19]),
  286. accel_values[0], accel_values[1], accel_values[2]);
  287. #endif
  288. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, gyro_values, 3);
  289. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_values, 3);
  290. }
  291. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  292. }
  293. static void HIDAPI_DriverZUIKI_Handle_EVOTOP_PCBT_StatePacket(SDL_Joystick *joystick, SDL_DriverZUIKI_Context *ctx, Uint8 *data, int size)
  294. {
  295. Sint16 axis;
  296. Uint64 timestamp = SDL_GetTicksNS();
  297. axis = (Sint16)HIDAPI_RemapVal((float)(data[2] << 8 | data[1]), 0x0000, 0xffff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  298. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTX, axis);
  299. axis = (Sint16)HIDAPI_RemapVal((float)(data[4] << 8 | data[3]), 0x0000, 0xffff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  300. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFTY, axis);
  301. axis = (Sint16)HIDAPI_RemapVal((float)(data[6] << 8 | data[5]), 0x0000, 0xffff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  302. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTX, axis);
  303. axis = (Sint16)HIDAPI_RemapVal((float)(data[8] << 8 | data[7]), 0x0000, 0xffff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  304. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHTY, axis);
  305. axis = (Sint16)HIDAPI_RemapVal((float)(data[10] << 8 | data[9]), 0x0000, 0x03ff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  306. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_LEFT_TRIGGER, axis);
  307. axis = (Sint16)HIDAPI_RemapVal((float)(data[12] << 8 | data[11]), 0x0000, 0x03ff, SDL_MIN_SINT16, SDL_MAX_SINT16);
  308. SDL_SendJoystickAxis(timestamp, joystick, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER, axis);
  309. if (ctx->last_state[13] != data[13]) {
  310. Uint8 hat;
  311. switch (data[13]) {
  312. case 1:
  313. hat = SDL_HAT_UP;
  314. break;
  315. case 2:
  316. hat = SDL_HAT_RIGHTUP;
  317. break;
  318. case 3:
  319. hat = SDL_HAT_RIGHT;
  320. break;
  321. case 4:
  322. hat = SDL_HAT_RIGHTDOWN;
  323. break;
  324. case 5:
  325. hat = SDL_HAT_DOWN;
  326. break;
  327. case 6:
  328. hat = SDL_HAT_LEFTDOWN;
  329. break;
  330. case 7:
  331. hat = SDL_HAT_LEFT;
  332. break;
  333. case 8:
  334. hat = SDL_HAT_LEFTUP;
  335. break;
  336. default:
  337. hat = SDL_HAT_CENTERED;
  338. break;
  339. }
  340. SDL_SendJoystickHat(timestamp, joystick, 0, hat);
  341. }
  342. if (ctx->last_state[14] != data[14]) {
  343. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_SOUTH, ((data[14] & 0x01) != 0));
  344. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_EAST, ((data[14] & 0x02) != 0));
  345. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_WEST, ((data[14] & 0x08) != 0));
  346. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_NORTH, ((data[14] & 0x10) != 0));
  347. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_SHOULDER, ((data[14] & 0x40) != 0));
  348. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_SHOULDER, ((data[14] & 0x80) != 0));
  349. }
  350. if (ctx->last_state[15] != data[15]) {
  351. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_BACK, ((data[15] & 0x04) != 0));
  352. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_START, ((data[15] & 0x08) != 0));
  353. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_GUIDE, ((data[15] & 0x10) != 0));
  354. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_LEFT_STICK, ((data[15] & 0x20) != 0));
  355. SDL_SendJoystickButton(timestamp, joystick, SDL_GAMEPAD_BUTTON_RIGHT_STICK, ((data[15] & 0x40) != 0));
  356. }
  357. if (ctx->sensors_supported) {
  358. Uint64 sensor_timestamp = timestamp;
  359. float gyro_values[3];
  360. gyro_values[0] = median_filter_update(&ctx->filter_gyro_x, LOAD16(data[17], data[18]) * GYRO_SCALE);
  361. gyro_values[1] = median_filter_update(&ctx->filter_gyro_y, LOAD16(data[21], data[22]) * GYRO_SCALE);
  362. gyro_values[2] = median_filter_update(&ctx->filter_gyro_z, -LOAD16(data[19], data[20]) * GYRO_SCALE);
  363. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_GYRO, sensor_timestamp, gyro_values, 3);
  364. float accel_values[3];
  365. accel_values[0] = LOAD16(data[23], data[24]) * ACCEL_SCALE;
  366. accel_values[2] = -LOAD16(data[25], data[26]) * ACCEL_SCALE;
  367. accel_values[1] = LOAD16(data[27], data[28]) * ACCEL_SCALE;
  368. SDL_SendJoystickSensor(timestamp, joystick, SDL_SENSOR_ACCEL, sensor_timestamp, accel_values, 3);
  369. #ifdef DEBUG_ZUIKI_PROTOCOL
  370. SDL_Log("Gyro raw: %d, %d, %d -> scaled: %.2f, %.2f, %.2f rad/s",
  371. LOAD16(data[17], data[18]), LOAD16(data[19], data[20]), LOAD16(data[21], data[22]),
  372. gyro_values[0], gyro_values[1], gyro_values[2]);
  373. SDL_Log("Accel raw: %d, %d, %d -> scaled: %.2f, %.2f, %.2f m/s²",
  374. LOAD16(data[23], data[24]), LOAD16(data[25], data[26]), LOAD16(data[27], data[28]),
  375. accel_values[0], accel_values[1], accel_values[2]);
  376. #endif
  377. }
  378. SDL_memcpy(ctx->last_state, data, SDL_min(size, sizeof(ctx->last_state)));
  379. }
  380. static bool HIDAPI_DriverZUIKI_UpdateDevice(SDL_HIDAPI_Device *device)
  381. {
  382. SDL_DriverZUIKI_Context *ctx = (SDL_DriverZUIKI_Context *)device->context;
  383. SDL_Joystick *joystick = NULL;
  384. Uint8 data[USB_PACKET_LENGTH];
  385. int size = 0;
  386. if (device->num_joysticks > 0) {
  387. joystick = SDL_GetJoystickFromID(device->joysticks[0]);
  388. } else {
  389. return false;
  390. }
  391. while ((size = SDL_hid_read_timeout(device->dev, data, sizeof(data), 0)) > 0) {
  392. #ifdef DEBUG_ZUIKI_PROTOCOL
  393. HIDAPI_DumpPacket("ZUIKI packet: size = %d", data, size);
  394. #endif
  395. if (!joystick) {
  396. continue;
  397. }
  398. if (device->product_id == USB_PRODUCT_ZUIKI_EVOTOP_PC_BT) {
  399. HIDAPI_DriverZUIKI_Handle_EVOTOP_PCBT_StatePacket(joystick, ctx, data, size);
  400. } else if (device->product_id == USB_PRODUCT_ZUIKI_EVOTOP_PC_DINPUT
  401. || device->product_id == USB_PRODUCT_ZUIKI_MASCON_PRO
  402. || device->product_id == USB_PRODUCT_ZUIKI_EVOTOP_UWB_DINPUT) {
  403. HIDAPI_DriverZUIKI_HandleOldStatePacket(joystick, ctx, data, size);
  404. }
  405. }
  406. if (size < 0) {
  407. // Read error, device is disconnected
  408. HIDAPI_JoystickDisconnected(device, device->joysticks[0]);
  409. }
  410. return (size >= 0);
  411. }
  412. static void HIDAPI_DriverZUIKI_CloseJoystick(SDL_HIDAPI_Device *device, SDL_Joystick *joystick)
  413. {
  414. }
  415. static void HIDAPI_DriverZUIKI_FreeDevice(SDL_HIDAPI_Device *device)
  416. {
  417. }
  418. SDL_HIDAPI_DeviceDriver SDL_HIDAPI_DriverZUIKI = {
  419. SDL_HINT_JOYSTICK_HIDAPI_ZUIKI,
  420. true,
  421. HIDAPI_DriverZUIKI_RegisterHints,
  422. HIDAPI_DriverZUIKI_UnregisterHints,
  423. HIDAPI_DriverZUIKI_IsEnabled,
  424. HIDAPI_DriverZUIKI_IsSupportedDevice,
  425. HIDAPI_DriverZUIKI_InitDevice,
  426. HIDAPI_DriverZUIKI_GetDevicePlayerIndex,
  427. HIDAPI_DriverZUIKI_SetDevicePlayerIndex,
  428. HIDAPI_DriverZUIKI_UpdateDevice,
  429. HIDAPI_DriverZUIKI_OpenJoystick,
  430. HIDAPI_DriverZUIKI_RumbleJoystick,
  431. HIDAPI_DriverZUIKI_RumbleJoystickTriggers,
  432. HIDAPI_DriverZUIKI_GetJoystickCapabilities,
  433. HIDAPI_DriverZUIKI_SetJoystickLED,
  434. HIDAPI_DriverZUIKI_SendJoystickEffect,
  435. HIDAPI_DriverZUIKI_SetJoystickSensorsEnabled,
  436. HIDAPI_DriverZUIKI_CloseJoystick,
  437. HIDAPI_DriverZUIKI_FreeDevice,
  438. };
  439. #endif // SDL_JOYSTICK_HIDAPI_ZUIKI
  440. #endif // SDL_JOYSTICK_HIDAPI