wrap up hid device refactor
This commit is contained in:
@@ -126,6 +126,14 @@ void tud_cdc_rx_cb(uint8_t itf)
|
||||
// USB HID
|
||||
//--------------------------------------------------------------------+
|
||||
#if CFG_TUD_HID
|
||||
|
||||
// Must match with ID declared by HID Report Descriptor, better to be in header file
|
||||
enum
|
||||
{
|
||||
REPORT_ID_KEYBOARD = 1,
|
||||
REPORT_ID_MOUSE
|
||||
};
|
||||
|
||||
void usb_hid_task(void)
|
||||
{
|
||||
// Poll every 10ms
|
||||
@@ -137,6 +145,7 @@ void usb_hid_task(void)
|
||||
|
||||
uint32_t const btn = board_button_read();
|
||||
|
||||
// Remote wakeup
|
||||
if ( tud_suspended() && btn )
|
||||
{
|
||||
// Wake up host if we are in suspend mode
|
||||
@@ -144,35 +153,40 @@ void usb_hid_task(void)
|
||||
tud_remote_wakeup();
|
||||
}
|
||||
|
||||
#if 1
|
||||
/*------------- Mouse -------------*/
|
||||
if ( tud_hid_ready() )
|
||||
{
|
||||
if ( btn )
|
||||
{
|
||||
int8_t const delta = 5;
|
||||
tud_hid_mouse_move(REPORT_ID_MOUSE, delta, delta); // right + down
|
||||
|
||||
// delay a bit before attempt to send keyboard report
|
||||
board_delay(2);
|
||||
}
|
||||
}
|
||||
|
||||
/*------------- Keyboard -------------*/
|
||||
if ( tud_hid_ready() )
|
||||
{
|
||||
// use to avoid send multiple consecutive zero report for keyboard
|
||||
static bool has_key = false;
|
||||
|
||||
if ( btn )
|
||||
{
|
||||
uint8_t keycode[6] = { 0 };
|
||||
keycode[0] = HID_KEY_A;
|
||||
|
||||
tud_hid_keyboard_report(0, 0, keycode);
|
||||
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
|
||||
|
||||
has_key = true;
|
||||
}else
|
||||
{
|
||||
tud_hid_keyboard_key_release(0);
|
||||
// send empty key report if previously has key pressed
|
||||
if (has_key) tud_hid_keyboard_key_release(REPORT_ID_KEYBOARD);
|
||||
has_key = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
/*------------- Mouse -------------*/
|
||||
if ( tud_hid_mouse_ready() )
|
||||
{
|
||||
enum { DELTA = 5 };
|
||||
|
||||
if ( btn & 0x01 ) tud_hid_mouse_move(-DELTA, 0); // left
|
||||
if ( btn & 0x02 ) tud_hid_mouse_move( DELTA, 0); // right
|
||||
if ( btn & 0x04 ) tud_hid_mouse_move( 0 , -DELTA); // up
|
||||
if ( btn & 0x08 ) tud_hid_mouse_move( 0 , DELTA); // down
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
*
|
||||
* Note: All CFG_TUD_DESC_* are relevant only if CFG_TUD_DESC_AUTO is enabled
|
||||
*/
|
||||
#define CFG_TUD_DESC_AUTO 1
|
||||
#define CFG_TUD_DESC_AUTO 0
|
||||
|
||||
// LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number
|
||||
// Therefore we need to force endpoint number to correct type on lpc17xx
|
||||
@@ -91,19 +91,11 @@
|
||||
//------------- CLASS -------------//
|
||||
#define CFG_TUD_CDC 1
|
||||
#define CFG_TUD_MSC 1
|
||||
#define CFG_TUD_HID 1
|
||||
|
||||
#define CFG_TUD_MIDI 0
|
||||
#define CFG_TUD_CUSTOM_CLASS 0
|
||||
|
||||
#define CFG_TUD_HID 1
|
||||
#define CFG_TUD_HID_KEYBOARD 1
|
||||
#define CFG_TUD_HID_MOUSE 0
|
||||
|
||||
/* Use Boot Protocol for Keyboard, Mouse. Enable this will create separated HID interface
|
||||
* require more IN endpoints. If disabled, they they are all packed into a single
|
||||
* multiple report interface called "Generic". */
|
||||
#define CFG_TUD_HID_KEYBOARD_BOOT 1
|
||||
#define CFG_TUD_HID_MOUSE_BOOT 0
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
// CDC
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
@@ -26,19 +26,14 @@
|
||||
|
||||
#include "tusb.h"
|
||||
|
||||
// If HID Generic interface is generated
|
||||
#define AUTO_DESC_HID_GENERIC (CFG_TUD_HID && ((CFG_TUD_HID_KEYBOARD && !CFG_TUD_HID_KEYBOARD_BOOT) || \
|
||||
(CFG_TUD_HID_MOUSE && !CFG_TUD_HID_MOUSE_BOOT)) )
|
||||
|
||||
/* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug.
|
||||
* Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC.
|
||||
*
|
||||
* Auto ProductID layout's Bitmap:
|
||||
* [MSB] HID Generic | Boot Mouse | Boot Keyboard | MSC | CDC [LSB]
|
||||
* [MSB] HID | MSC | CDC [LSB]
|
||||
*/
|
||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | \
|
||||
_PID_MAP(HID_KEYBOARD, 2) | _PID_MAP(HID_MOUSE, 3) | (AUTO_DESC_HID_GENERIC << 4) )
|
||||
#define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) )
|
||||
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) )
|
||||
|
||||
//------------- Device Descriptors -------------//
|
||||
tusb_desc_device_t const desc_device =
|
||||
@@ -72,42 +67,83 @@ tusb_desc_device_t const desc_device =
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
//------------- HID Report Descriptor -------------//
|
||||
enum
|
||||
{
|
||||
REPORT_ID_KEYBOARD = 1,
|
||||
REPORT_ID_MOUSE
|
||||
};
|
||||
|
||||
uint8_t const desc_hid_report[] =
|
||||
{
|
||||
HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD), ),
|
||||
HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE), )
|
||||
};
|
||||
|
||||
//------------- Configuration Descriptor -------------//
|
||||
enum {
|
||||
#if CFG_TUD_CDC
|
||||
ITF_NUM_CDC = 0,
|
||||
ITF_NUM_CDC_DATA,
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_MSC
|
||||
ITF_NUM_MSC,
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_HID
|
||||
ITF_NUM_HID,
|
||||
#endif
|
||||
|
||||
ITF_NUM_TOTAL
|
||||
};
|
||||
|
||||
enum {
|
||||
CONFIG_DESC_LEN = sizeof(tusb_desc_configuration_t) + CFG_TUD_CDC*TUD_CDC_DESC_LEN + CFG_TUD_MSC*TUD_MSC_DESC_LEN + CFG_TUD_HID*TUD_HID_DESC_LEN
|
||||
};
|
||||
|
||||
uint8_t const desc_configuration[] =
|
||||
{
|
||||
// Config: self-powered with remote wakeup support, max power up to 100 mA
|
||||
TUD_CONFIG_DESCRIPTOR(ITF_NUM_TOTAL, 0, CONFIG_DESC_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
|
||||
|
||||
#if CFG_TUD_CDC
|
||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, 0x02, 0x82, 64),
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_MSC
|
||||
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 5, 0x03, 0x83, 64), // highspeed 512
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_HID
|
||||
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 6, HID_PROTOCOL_KEYBOARD, sizeof(desc_hid_report), 0x84, 16, 10)
|
||||
#endif
|
||||
};
|
||||
|
||||
//------------- String Descriptors -------------//
|
||||
// array of pointer to string descriptors
|
||||
uint16_t const * const string_desc_arr [] =
|
||||
{
|
||||
// 0: is supported language = English
|
||||
TUD_DESC_STRCONV(0x0409),
|
||||
// 0: is supported language = English
|
||||
TUD_DESC_STRCONV(0x0409),
|
||||
|
||||
// 1: Manufacturer
|
||||
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
|
||||
// 1: Manufacturer
|
||||
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
|
||||
|
||||
// 2: Product
|
||||
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
|
||||
// 2: Product
|
||||
TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
|
||||
|
||||
// 3: Serials, should use chip ID
|
||||
TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
|
||||
// 3: Serials, should use chip ID
|
||||
TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
|
||||
|
||||
#if CFG_TUD_CDC
|
||||
// 4: CDC Interface
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
|
||||
#endif
|
||||
// 4: CDC Interface
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','c','d','c'),
|
||||
|
||||
#if CFG_TUD_MSC
|
||||
// 5: MSC Interface
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_HID_KEYBOARD
|
||||
// 6: Keyboard
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','k','e','y','b','o','a','r','d'),
|
||||
#endif
|
||||
|
||||
#if CFG_TUD_HID_MOUSE
|
||||
// 7: Mouse
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','m', 'o','u','s','e'),
|
||||
#endif
|
||||
// 5: MSC Interface
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','m','s','c'),
|
||||
|
||||
// 6: HID
|
||||
TUD_DESC_STRCONV('t','u','s','b',' ','h','i','d')
|
||||
};
|
||||
|
||||
// tud_desc_set is required by tinyusb stack
|
||||
@@ -115,10 +151,10 @@ uint16_t const * const string_desc_arr [] =
|
||||
tud_desc_set_t tud_desc_set =
|
||||
{
|
||||
.device = &desc_device,
|
||||
.config = NULL,
|
||||
.config = desc_configuration,
|
||||
|
||||
.string_arr = (uint8_t const **) string_desc_arr,
|
||||
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
|
||||
|
||||
.hid_report = NULL,
|
||||
.hid_report = desc_hid_report,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user