From d3406ce5dd6daf903101c713aeaeec26d89da479 Mon Sep 17 00:00:00 2001 From: gdsports Date: Sat, 9 Mar 2019 15:50:23 -0800 Subject: [PATCH] Add support for Thrustmaster T.16000M FCS joystick --- examples/HID/t16km/t16km.ino | 104 +++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 examples/HID/t16km/t16km.ino diff --git a/examples/HID/t16km/t16km.ino b/examples/HID/t16km/t16km.ino new file mode 100644 index 00000000..00bf2997 --- /dev/null +++ b/examples/HID/t16km/t16km.ino @@ -0,0 +1,104 @@ +/* Simplified Thrustmaster T.16000M FCS Joystick Report Parser */ + +#include +#include +#include + +// Satisfy the IDE, which needs to see the include statment in the ino too. +#ifdef dobogusinclude +#include +#endif +#include + +// Thrustmaster T.16000M HID report +struct GamePadEventData +{ + uint16_t buttons; + uint8_t hat; + uint16_t x; + uint16_t y; + uint8_t twist; + uint8_t slider; +}__attribute__((packed)); + +class JoystickEvents +{ +public: + virtual void OnGamePadChanged(const GamePadEventData *evt); +}; + +#define RPT_GAMEPAD_LEN sizeof(GamePadEventData) + +class JoystickReportParser : public HIDReportParser +{ + JoystickEvents *joyEvents; + + uint8_t oldPad[RPT_GAMEPAD_LEN]; + +public: + JoystickReportParser(JoystickEvents *evt); + + virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); +}; + + +JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : + joyEvents(evt) +{} + +void JoystickReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) +{ + // Checking if there are changes in report since the method was last called + bool match = (sizeof(oldPad) == len) && (memcmp(oldPad, buf, len) == 0); + + // Calling Game Pad event handler + if (!match && joyEvents) { + joyEvents->OnGamePadChanged((const GamePadEventData*)buf); + memcpy(oldPad, buf, len); + } +} + +void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt) +{ + Serial.print("X: "); + PrintHex(evt->x, 0x80); + Serial.print(" Y: "); + PrintHex(evt->y, 0x80); + Serial.print(" Hat Switch: "); + PrintHex(evt->hat, 0x80); + Serial.print(" Twist: "); + PrintHex(evt->twist, 0x80); + Serial.print(" Slider: "); + PrintHex(evt->slider, 0x80); + Serial.print(" Buttons: "); + PrintHex(evt->buttons, 0x80); + Serial.println(); +} + +USB Usb; +USBHub Hub(&Usb); +HIDUniversal Hid(&Usb); +JoystickEvents JoyEvents; +JoystickReportParser Joy(&JoyEvents); + +void setup() +{ + Serial.begin( 115200 ); +#if !defined(__MIPSEL__) + while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection +#endif + Serial.println("Start"); + + if (Usb.Init() == -1) + Serial.println("OSC did not start."); + + delay( 200 ); + + if (!Hid.SetReportParser(0, &Joy)) + ErrorMessage(PSTR("SetReportParser"), 1 ); +} + +void loop() +{ + Usb.Task(); +}