improve usb hid

This commit is contained in:
hathach 2018-07-31 21:06:24 +07:00
parent 27b2a9fe59
commit fd661c1d57
3 changed files with 55 additions and 24 deletions

View File

@ -36,11 +36,33 @@
extern "C" { extern "C" {
#endif #endif
#define USB_HID_NUM_DEVICES 2 // 1 to enable device, 0 to disable
#define USB_HID_DEVICE_KEYBOARD 1
#define USB_HID_DEVICE_MOUSE 1
#define USB_HID_DEVICE_CONSUMER 0
#define USB_HID_DEVICE_GAMEPAD 0
#define USB_HID_REPORT_ID_KEYBOARD 1 enum {
#define USB_HID_REPORT_ID_MOUSE 2 USB_HID_REPORT_ID_UNUSED = 0,
//#define USB_HID_REPORT_ID_CONSUMER 3
#if USB_HID_DEVICE_KEYBOARD
USB_HID_REPORT_ID_KEYBOARD,
#endif
#if USB_HID_DEVICE_MOUSE
USB_HID_REPORT_ID_MOUSE,
#endif
#if USB_HID_DEVICE_CONSUMER
USB_HID_REPORT_ID_CONSUMER,
#endif
#if USB_HID_DEVICE_GAMEPAD
USB_HID_REPORT_ID_GAMEPAD,
#endif
};
#define USB_HID_NUM_DEVICES (USB_HID_DEVICE_KEYBOARD + USB_HID_DEVICE_MOUSE + USB_HID_DEVICE_CONSUMER + USB_HID_DEVICE_GAMEPAD )
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;

View File

@ -31,53 +31,58 @@
#include "common-hal/usb_hid/Device.h" #include "common-hal/usb_hid/Device.h"
#include "shared-bindings/usb_hid/Device.h" #include "shared-bindings/usb_hid/Device.h"
#define USB_HID_REPORT_LENGTH_KEYBOARD 8 #define USB_HID_REPORT_LENGTH_KEYBOARD 8
#define USB_HID_REPORT_LENGTH_MOUSE 4 #define USB_HID_REPORT_LENGTH_MOUSE 4
#define USB_HID_REPORT_LENGTH_CONSUMER 2
// Buffers are report size + 1 to include the Report ID prefix byte if needed. #if USB_HID_DEVICE_KEYBOARD
#ifdef USB_HID_REPORT_ID_KEYBOARD static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD];
static uint8_t keyboard_report_buffer[USB_HID_REPORT_LENGTH_KEYBOARD + 1];
#endif #endif
#ifdef USB_HID_REPORT_ID_MOUSE
static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE + 1]; #if USB_HID_DEVICE_MOUSE
static uint8_t mouse_report_buffer[USB_HID_REPORT_LENGTH_MOUSE];
#endif #endif
#ifdef USB_HID_REPORT_ID_CONSUMER
static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER + 1]; #if USB_HID_DEVICE_CONSUMER
static uint8_t consumer_report_buffer[USB_HID_REPORT_LENGTH_CONSUMER];
#endif #endif
#ifdef USB_HID_REPORT_ID_SYS_CONTROL #ifdef USB_HID_REPORT_ID_SYS_CONTROL
static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL + 1]; static uint8_t sys_control_report_buffer[USB_HID_REPORT_LENGTH_SYS_CONTROL];
#endif #endif
#ifdef USB_HID_REPORT_ID_GAMEPAD #ifdef USB_HID_REPORT_ID_GAMEPAD
static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD + 1]; static uint8_t gamepad_report_buffer[USB_HID_REPORT_LENGTH_GAMEPAD];
#endif #endif
#ifdef USB_HID_REPORT_ID_DIGITIZER #ifdef USB_HID_REPORT_ID_DIGITIZER
static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER + 1]; static uint8_t digitizer_report_buffer[USB_HID_REPORT_LENGTH_DIGITIZER];
#endif #endif
usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = { usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = {
#ifdef USB_HID_REPORT_ID_KEYBOARD #if USB_HID_DEVICE_KEYBOARD
{ {
.base = { .type = &usb_hid_device_type }, .base = { .type = &usb_hid_device_type },
.report_buffer = keyboard_report_buffer, .report_buffer = keyboard_report_buffer,
// .endpoint = USB_HID_ENDPOINT_IN,
.report_id = USB_HID_REPORT_ID_KEYBOARD, .report_id = USB_HID_REPORT_ID_KEYBOARD,
.report_length = USB_HID_REPORT_LENGTH_KEYBOARD, .report_length = USB_HID_REPORT_LENGTH_KEYBOARD,
.usage_page = 0x01, .usage_page = 0x01,
.usage = 0x06, .usage = 0x06,
}, },
#endif #endif
#ifdef USB_HID_REPORT_ID_MOUSE
#if USB_HID_DEVICE_MOUSE
{ {
.base = { .type = &usb_hid_device_type }, .base = { .type = &usb_hid_device_type },
.report_buffer = mouse_report_buffer, .report_buffer = mouse_report_buffer,
// .endpoint = USB_HID_ENDPOINT_IN,
.report_id = USB_HID_REPORT_ID_MOUSE, .report_id = USB_HID_REPORT_ID_MOUSE,
.report_length = USB_HID_REPORT_LENGTH_MOUSE, .report_length = USB_HID_REPORT_LENGTH_MOUSE,
.usage_page = 0x01, .usage_page = 0x01,
.usage = 0x02, .usage = 0x02,
}, },
#endif #endif
#ifdef USB_HID_REPORT_ID_CONSUMER
#if USB_HID_DEVICE_CONSUMER
{ {
.base = { .type = &usb_hid_device_type }, .base = { .type = &usb_hid_device_type },
.report_buffer = consumer_report_buffer, .report_buffer = consumer_report_buffer,
@ -88,6 +93,7 @@ usb_hid_device_obj_t usb_hid_devices[USB_HID_NUM_DEVICES] = {
.usage = 0x01, .usage = 0x01,
}, },
#endif #endif
#ifdef USB_HID_REPORT_ID_SYS_CONTROL #ifdef USB_HID_REPORT_ID_SYS_CONTROL
{ {
.base = { .type = &usb_hid_device_type }, .base = { .type = &usb_hid_device_type },

View File

@ -141,14 +141,17 @@ tusb_desc_device_t const usb_desc_dev =
uint8_t const usb_desc_hid_generic_report[] = uint8_t const usb_desc_hid_generic_report[] =
{ {
#if USB_HID_REPORT_ID_KEYBOARD #if USB_HID_DEVICE_KEYBOARD
HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(USB_HID_REPORT_ID_KEYBOARD), ), HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(USB_HID_REPORT_ID_KEYBOARD), ),
#endif #endif
#if USB_HID_REPORT_ID_MOUSE #if USB_HID_DEVICE_MOUSE
HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_MOUSE), ) HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_MOUSE), ),
#endif #endif
#if USB_HID_DEVICE_CONSUMER
HID_REPORT_DESC_MOUSE( HID_REPORT_ID(USB_HID_REPORT_ID_CONSUMER), )
#endif
}; };