Skip to content
This repository was archived by the owner on May 18, 2021. It is now read-only.

Commit dd1be07

Browse files
committed
Added BLE HID Gamepad & Hat support
1 parent 85f6aaa commit dd1be07

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

tinyusb/src/class/hid/hid.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,15 @@ typedef enum
149149
/** \addtogroup ClassDriver_HID_Gamepad Gamepad
150150
* @{ */
151151

152-
/// Standard HID Boot Protocol Gampad Report.
152+
/// Standard HID Boot Protocol Gamepad Report.
153153
typedef struct TU_ATTR_PACKED
154154
{
155-
uint16_t buttons; /**< buttons mask for currently pressed buttons in the gamepad. */
156-
int8_t x; /**< Current delta x movement of the gamepad joystick 1. */
157-
int8_t y; /**< Current delta y movement of the gamepad joystick 1. */
158-
int8_t z; /**< Current delta z movement of the gamepad joystick 2. */
159-
int8_t r_z; /**< Current delta r_z movement of the gamepad joystick 2. */
155+
uint16_t buttons; /**< buttons mask for currently pressed buttons in the gamepad. */
156+
int8_t x; /**< Current delta x movement of the gamepad joystick 1. */
157+
int8_t y; /**< Current delta y movement of the gamepad joystick 1. */
158+
int8_t z; /**< Current delta z movement of the gamepad joystick 2. */
159+
int8_t r_z; /**< Current delta r_z movement of the gamepad joystick 2. */
160+
uint8_t hat; /**< buttons mask for currently pressed buttons in the gamepad hat */
160161
}hid_gamepad_report_t;
161162

162163
/// Standard Gamepad Buttons Bitmap (from Linux input event codes)
@@ -180,6 +181,20 @@ typedef enum
180181
GAMEPAD_BUTTON_ = TU_BIT(15), ///< Undefined button
181182
}hid_gamepad_button_bm_t;
182183

184+
/// Standard Gamepad HAT/DPAD Buttons Bitmap (from Linux input event codes)
185+
typedef enum
186+
{
187+
GAMEPAD_HAT_CENTERED = 0, ///< DPAD_CENTERED
188+
GAMEPAD_HAT_UP = 1, ///< DPAD_UP
189+
GAMEPAD_HAT_UP_RIGHT = 2, ///< DPAD_UP_RIGHT
190+
GAMEPAD_HAT_RIGHT = 3, ///< DPAD_RIGHT
191+
GAMEPAD_HAT_DOWN_RIGHT = 4, ///< DPAD_DOWN_RIGHT
192+
GAMEPAD_HAT_DOWN = 5, ///< DPAD_DOWN
193+
GAMEPAD_HAT_DOWN_LEFT = 6, ///< DPAD_DOWN_LEFT
194+
GAMEPAD_HAT_LEFT = 7, ///< DPAD_LEFT
195+
GAMEPAD_HAT_UP_LEFT = 8, ///< DPAD_UP_LEFT
196+
}hid_gamepad_hat_bm_t;
197+
183198
/// @}
184199

185200
//--------------------------------------------------------------------+
@@ -196,7 +211,7 @@ typedef struct TU_ATTR_PACKED
196211
int8_t y; /**< Current delta y movement on the mouse. */
197212
int8_t wheel; /**< Current delta wheel movement on the mouse. */
198213
int8_t pan; // using AC Pan
199-
} hid_mouse_report_t;
214+
}hid_mouse_report_t;
200215

201216
/// Standard Mouse Buttons Bitmap
202217
typedef enum

tinyusb/src/class/hid/hid_device.h

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate);
248248
HID_COLLECTION_END \
249249

250250
// Gamepad Report Descriptor Template
251-
// with 16 buttons and 2 joysticks with following layout
252-
// | Button Map (2 bytes) | X | Y | Z | Rz
251+
// with 16 buttons, 2 joysticks and 1 hat/dpad with following layout
252+
// | Button Map (2 bytes) | X | Y | Z | Rz (1 byte each) | hat/DPAD (1 byte)
253253
#define TUD_HID_REPORT_DESC_GAMEPAD(...) \
254-
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
255-
HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\
256-
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
254+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
255+
HID_USAGE ( HID_USAGE_DESKTOP_GAMEPAD ) ,\
256+
HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\
257257
/* Report ID if any */\
258258
__VA_ARGS__ \
259259
/* 16 bit Button Map */ \
@@ -265,17 +265,27 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate);
265265
HID_REPORT_COUNT ( 16 ) ,\
266266
HID_REPORT_SIZE ( 1 ) ,\
267267
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
268-
/* X, Y, Z, Rz (min -127, max 127 ) */ \
268+
/* 8 bit X, Y, Z, Rz (min -127, max 127 ) */ \
269269
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
270-
HID_LOGICAL_MIN ( 0x81 ) ,\
271-
HID_LOGICAL_MAX ( 0x7f ) ,\
272270
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
273271
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
274272
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
275273
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
274+
HID_LOGICAL_MIN ( 0x81 ) ,\
275+
HID_LOGICAL_MAX ( 0x7f ) ,\
276276
HID_REPORT_COUNT ( 4 ) ,\
277277
HID_REPORT_SIZE ( 8 ) ,\
278278
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
279+
/* 8 bit Hat Button Map */ \
280+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
281+
HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
282+
HID_LOGICAL_MIN ( 1 ) ,\
283+
HID_LOGICAL_MAX ( 8 ) ,\
284+
HID_PHYSICAL_MIN ( 0 ) ,\
285+
HID_PHYSICAL_MAX ( 315 ) ,\
286+
HID_REPORT_COUNT ( 1 ) ,\
287+
HID_REPORT_SIZE ( 8 ) ,\
288+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
279289
HID_COLLECTION_END \
280290

281291
// HID Generic Input & Output

0 commit comments

Comments
 (0)