summaryrefslogtreecommitdiff
path: root/examples/device/hid_composite/src
diff options
context:
space:
mode:
authorHa Thach <[email protected]>2021-02-08 19:40:18 +0700
committerGitHub <[email protected]>2021-02-08 19:40:18 +0700
commitac4555b361ae75d09ab97535183adc7cbf96edd0 (patch)
treeb36872f041eaf75cda5dc4723745f01938fc729b /examples/device/hid_composite/src
parentf49d47a0aa6781e8c4f86fbb7725c23f6549b14e (diff)
parent72bcc0685c142c512f1d04ff18eaa13b99f62e93 (diff)
Merge pull request #638 from hathach/add-gamepad
Enhance HID Gamepad with DPad/Hat
Diffstat (limited to 'examples/device/hid_composite/src')
-rw-r--r--examples/device/hid_composite/src/main.c29
-rw-r--r--examples/device/hid_composite/src/usb_descriptors.c5
-rw-r--r--examples/device/hid_composite/src/usb_descriptors.h1
3 files changed, 33 insertions, 2 deletions
diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c
index 6a9b6445d..45b8ba674 100644
--- a/examples/device/hid_composite/src/main.c
+++ b/examples/device/hid_composite/src/main.c
@@ -183,6 +183,35 @@ void hid_task(void)
if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
has_consumer_key = false;
}
+
+ // delay a bit before sending next report
+ board_delay(10);
+ }
+
+ /*------------- Gamepad -------------*/
+ if ( tud_hid_ready() )
+ {
+ // use to avoid send multiple consecutive zero report for keyboard
+ static bool has_gamepad_key = false;
+
+ hid_gamepad_report_t report =
+ {
+ .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
+ .hat = 0, .buttons = 0
+ };
+
+ if ( btn )
+ {
+ report.hat = GAMEPAD_HAT_UP;
+ tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
+
+ has_gamepad_key = true;
+ }else
+ {
+ report.hat = GAMEPAD_HAT_CENTERED;
+ if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
+ has_gamepad_key = false;
+ }
}
}
diff --git a/examples/device/hid_composite/src/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c
index 879eff05e..fbe0e2550 100644
--- a/examples/device/hid_composite/src/usb_descriptors.c
+++ b/examples/device/hid_composite/src/usb_descriptors.c
@@ -75,7 +75,8 @@ uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(REPORT_ID_KEYBOARD )),
TUD_HID_REPORT_DESC_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE )),
- TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL ))
+ TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )),
+ TUD_HID_REPORT_DESC_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD ))
};
// Invoked when received GET HID REPORT DESCRIPTOR
@@ -105,7 +106,7 @@ uint8_t const desc_configuration[] =
// Config number, interface count, string index, total length, attribute, power in mA
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
- // Interface number, string index, protocol, report descriptor len, EP In & Out address, size & polling interval
+ // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval
TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
};
diff --git a/examples/device/hid_composite/src/usb_descriptors.h b/examples/device/hid_composite/src/usb_descriptors.h
index feda83dcd..7894719fd 100644
--- a/examples/device/hid_composite/src/usb_descriptors.h
+++ b/examples/device/hid_composite/src/usb_descriptors.h
@@ -30,6 +30,7 @@ enum
REPORT_ID_KEYBOARD = 1,
REPORT_ID_MOUSE,
REPORT_ID_CONSUMER_CONTROL,
+ REPORT_ID_GAMEPAD
};
#endif /* USB_DESCRIPTORS_H_ */