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

Commit 3e907b3

Browse files
authored
Merge pull request #20 from nekuneko/master
Added HID Gamepad hat support
2 parents e7b8920 + 99e0567 commit 3e907b3

File tree

2 files changed

+120
-17
lines changed

2 files changed

+120
-17
lines changed

tinyusb/src/class/hid/hid.h

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,97 @@ typedef enum
143143

144144
/** @} */
145145

146+
//--------------------------------------------------------------------+
147+
// GAMEPAD
148+
//--------------------------------------------------------------------+
149+
/** \addtogroup ClassDriver_HID_Gamepad Gamepad
150+
* @{ */
151+
152+
/* From https://www.kernel.org/doc/html/latest/input/gamepad.html
153+
____________________________ __
154+
/ [__ZL__] [__ZR__] \ |
155+
/ [__ TL __] [__ TR __] \ | Front Triggers
156+
__/________________________________\__ __|
157+
/ _ \ |
158+
/ /\ __ (N) \ |
159+
/ || __ |MO| __ _ _ \ | Main Pad
160+
| <===DP===> |SE| |ST| (W) -|- (E) | |
161+
\ || ___ ___ _ / |
162+
/\ \/ / \ / \ (S) /\ __|
163+
/ \________ | LS | ____ | RS | ________/ \ |
164+
| / \ \___/ / \ \___/ / \ | | Control Sticks
165+
| / \_____/ \_____/ \ | __|
166+
| / \ |
167+
\_____/ \_____/
168+
169+
|________|______| |______|___________|
170+
D-Pad Left Right Action Pad
171+
Stick Stick
172+
173+
|_____________|
174+
Menu Pad
175+
176+
Most gamepads have the following features:
177+
- Action-Pad 4 buttons in diamonds-shape (on the right side) NORTH, SOUTH, WEST and EAST.
178+
- D-Pad (Direction-pad) 4 buttons (on the left side) that point up, down, left and right.
179+
- Menu-Pad Different constellations, but most-times 2 buttons: SELECT - START.
180+
- Analog-Sticks provide freely moveable sticks to control directions, Analog-sticks may also
181+
provide a digital button if you press them.
182+
- Triggers are located on the upper-side of the pad in vertical direction. The upper buttons
183+
are normally named Left- and Right-Triggers, the lower buttons Z-Left and Z-Right.
184+
- Rumble Many devices provide force-feedback features. But are mostly just simple rumble motors.
185+
*/
186+
187+
/// HID Gamepad Protocol Report.
188+
typedef struct TU_ATTR_PACKED
189+
{
190+
int8_t x; ///< Delta x movement of left analog-stick
191+
int8_t y; ///< Delta y movement of left analog-stick
192+
int8_t z; ///< Delta z movement of right analog-joystick
193+
int8_t rz; ///< Delta Rz movement of right analog-joystick
194+
int8_t rx; ///< Delta Rx movement of analog left trigger
195+
int8_t ry; ///< Delta Ry movement of analog right trigger
196+
uint8_t hat; ///< Buttons mask for currently pressed buttons in the DPad/hat
197+
uint16_t buttons; ///< Buttons mask for currently pressed buttons
198+
}hid_gamepad_report_t;
199+
200+
/// Standard Gamepad Buttons Bitmap (from Linux input event codes)
201+
typedef enum
202+
{
203+
GAMEPAD_BUTTON_A = TU_BIT(0), ///< A/South button
204+
GAMEPAD_BUTTON_B = TU_BIT(1), ///< B/East button
205+
GAMEPAD_BUTTON_C = TU_BIT(2), ///< C button
206+
GAMEPAD_BUTTON_X = TU_BIT(3), ///< X/North button
207+
GAMEPAD_BUTTON_Y = TU_BIT(4), ///< Y/West button
208+
GAMEPAD_BUTTON_Z = TU_BIT(5), ///< Z button
209+
GAMEPAD_BUTTON_TL = TU_BIT(6), ///< L1 button
210+
GAMEPAD_BUTTON_TR = TU_BIT(7), ///< R1 button
211+
GAMEPAD_BUTTON_TL2 = TU_BIT(8), ///< L2 button
212+
GAMEPAD_BUTTON_TR2 = TU_BIT(9), ///< R2 button
213+
GAMEPAD_BUTTON_SELECT = TU_BIT(10), ///< Select button
214+
GAMEPAD_BUTTON_START = TU_BIT(11), ///< Start button
215+
GAMEPAD_BUTTON_MODE = TU_BIT(12), ///< Mode button
216+
GAMEPAD_BUTTON_THUMBL = TU_BIT(13), ///< L3 button
217+
GAMEPAD_BUTTON_THUMBR = TU_BIT(14), ///< R3 button
218+
//GAMEPAD_BUTTON_ = TU_BIT(15), ///< Undefined button
219+
}hid_gamepad_button_bm_t;
220+
221+
/// Standard Gamepad HAT/DPAD Buttons Bitmap (from Linux input event codes)
222+
typedef enum
223+
{
224+
GAMEPAD_HAT_CENTERED = 0, ///< DPAD_CENTERED
225+
GAMEPAD_HAT_UP = 1, ///< DPAD_UP
226+
GAMEPAD_HAT_UP_RIGHT = 2, ///< DPAD_UP_RIGHT
227+
GAMEPAD_HAT_RIGHT = 3, ///< DPAD_RIGHT
228+
GAMEPAD_HAT_DOWN_RIGHT = 4, ///< DPAD_DOWN_RIGHT
229+
GAMEPAD_HAT_DOWN = 5, ///< DPAD_DOWN
230+
GAMEPAD_HAT_DOWN_LEFT = 6, ///< DPAD_DOWN_LEFT
231+
GAMEPAD_HAT_LEFT = 7, ///< DPAD_LEFT
232+
GAMEPAD_HAT_UP_LEFT = 8, ///< DPAD_UP_LEFT
233+
}hid_gamepad_hat_bm_t;
234+
235+
/// @}
236+
146237
//--------------------------------------------------------------------+
147238
// MOUSE
148239
//--------------------------------------------------------------------+
@@ -157,7 +248,7 @@ typedef struct TU_ATTR_PACKED
157248
int8_t y; /**< Current delta y movement on the mouse. */
158249
int8_t wheel; /**< Current delta wheel movement on the mouse. */
159250
int8_t pan; // using AC Pan
160-
} hid_mouse_report_t;
251+
}hid_mouse_report_t;
161252

162253
/// Standard Mouse Buttons Bitmap
163254
typedef enum

tinyusb/src/class/hid/hid_device.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -248,14 +248,37 @@ 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+
// | X | Y | Z | Rz | Rx | Ry (1 byte each) | hat/DPAD (1 byte) | Button Map (2 bytes) |
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__ \
259+
/* 8 bit X, Y, Z, Rz, Rx, Ry (min -127, max 127 ) */ \
260+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
261+
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
262+
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
263+
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
264+
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
265+
HID_USAGE ( HID_USAGE_DESKTOP_RX ) ,\
266+
HID_USAGE ( HID_USAGE_DESKTOP_RY ) ,\
267+
HID_LOGICAL_MIN ( 0x81 ) ,\
268+
HID_LOGICAL_MAX ( 0x7f ) ,\
269+
HID_REPORT_COUNT ( 6 ) ,\
270+
HID_REPORT_SIZE ( 8 ) ,\
271+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
272+
/* 8 bit DPad/Hat Button Map */ \
273+
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
274+
HID_USAGE ( HID_USAGE_DESKTOP_HAT_SWITCH ) ,\
275+
HID_LOGICAL_MIN ( 1 ) ,\
276+
HID_LOGICAL_MAX ( 8 ) ,\
277+
HID_PHYSICAL_MIN ( 0 ) ,\
278+
HID_PHYSICAL_MAX_N ( 315, 2 ) ,\
279+
HID_REPORT_COUNT ( 1 ) ,\
280+
HID_REPORT_SIZE ( 8 ) ,\
281+
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
259282
/* 16 bit Button Map */ \
260283
HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\
261284
HID_USAGE_MIN ( 1 ) ,\
@@ -265,17 +288,6 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t idle_rate);
265288
HID_REPORT_COUNT ( 16 ) ,\
266289
HID_REPORT_SIZE ( 1 ) ,\
267290
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
268-
/* X, Y, Z, Rz (min -127, max 127 ) */ \
269-
HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\
270-
HID_LOGICAL_MIN ( 0x81 ) ,\
271-
HID_LOGICAL_MAX ( 0x7f ) ,\
272-
HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\
273-
HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\
274-
HID_USAGE ( HID_USAGE_DESKTOP_Z ) ,\
275-
HID_USAGE ( HID_USAGE_DESKTOP_RZ ) ,\
276-
HID_REPORT_COUNT ( 4 ) ,\
277-
HID_REPORT_SIZE ( 8 ) ,\
278-
HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\
279291
HID_COLLECTION_END \
280292

281293
// HID Generic Input & Output

0 commit comments

Comments
 (0)