summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorhathach <[email protected]>2021-02-09 16:06:38 +0700
committerhathach <[email protected]>2021-02-09 16:06:38 +0700
commit9c3a44b6a3bfa6951213b0f24476f5360c8b5fdf (patch)
tree5da590fa3c0e19d6172221579ab7d40fe56a3c5b /examples
parentd2b8e591f6f939f87c28aaa8b12f67a5072d2b06 (diff)
update hid_composite_freertos example
Diffstat (limited to 'examples')
-rw-r--r--examples/device/hid_composite/src/usb_descriptors.c2
-rw-r--r--examples/device/hid_composite_freertos/src/main.c136
-rw-r--r--examples/device/hid_composite_freertos/src/usb_descriptors.c9
-rw-r--r--examples/device/hid_composite_freertos/src/usb_descriptors.h4
4 files changed, 102 insertions, 49 deletions
diff --git a/examples/device/hid_composite/src/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c
index e5f1ea703..ec4976188 100644
--- a/examples/device/hid_composite/src/usb_descriptors.c
+++ b/examples/device/hid_composite/src/usb_descriptors.c
@@ -107,7 +107,7 @@ uint8_t const desc_configuration[] =
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 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, 2)
+ TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c
index 2a9da69f6..aa006fbea 100644
--- a/examples/device/hid_composite_freertos/src/main.c
+++ b/examples/device/hid_composite_freertos/src/main.c
@@ -163,45 +163,17 @@ void tud_resume_cb(void)
// USB HID
//--------------------------------------------------------------------+
-void hid_task(void* param)
+static void send_hid_report(uint8_t report_id, uint32_t btn)
{
- (void) param;
+ // skip if hid is not ready yet
+ if ( !tud_hid_ready() ) return;
- while(1)
+ switch(report_id)
{
- // Poll every 10ms
- vTaskDelay(pdMS_TO_TICKS(10));
-
- uint32_t const btn = board_button_read();
-
- // Remote wakeup
- if ( tud_suspended() && btn )
- {
- // Wake up host if we are in suspend mode
- // and REMOTE_WAKEUP feature is enabled by host
- tud_remote_wakeup();
- }
-
- /*------------- Mouse -------------*/
- if ( tud_hid_ready() )
- {
- if ( btn )
- {
- int8_t const delta = 5;
-
- // no button, right + down, no scroll pan
- tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
-
- // delay a bit before sending keyboard report
- vTaskDelay(pdMS_TO_TICKS(10));
- }
- }
-
- /*------------- Keyboard -------------*/
- if ( tud_hid_ready() )
+ case REPORT_ID_KEYBOARD:
{
// use to avoid send multiple consecutive zero report for keyboard
- static bool has_key = false;
+ static bool has_keyboard_key = false;
if ( btn )
{
@@ -209,21 +181,26 @@ void hid_task(void* param)
keycode[0] = HID_KEY_A;
tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
-
- has_key = true;
+ has_keyboard_key = true;
}else
{
// send empty key report if previously has key pressed
- if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
- has_key = false;
+ if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
+ has_keyboard_key = false;
}
+ }
+ break;
+
+ case REPORT_ID_MOUSE:
+ {
+ int8_t const delta = 5;
- // delay a bit before sending consumer report
- vTaskDelay(pdMS_TO_TICKS(10));
+ // no button, right + down, no scroll, no pan
+ tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
}
+ break;
- /*------------- Consume Control -------------*/
- if ( tud_hid_ready() )
+ case REPORT_ID_CONSUMER_CONTROL:
{
// use to avoid send multiple consecutive zero report
static bool has_consumer_key = false;
@@ -233,7 +210,6 @@ void hid_task(void* param)
// volume down
uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
-
has_consumer_key = true;
}else
{
@@ -243,9 +219,83 @@ void hid_task(void* param)
has_consumer_key = false;
}
}
+ break;
+
+ case REPORT_ID_GAMEPAD:
+ {
+ // 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;
+ report.buttons = GAMEPAD_BUTTON_A;
+ tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
+
+ has_gamepad_key = true;
+ }else
+ {
+ report.hat = GAMEPAD_HAT_CENTERED;
+ report.buttons = 0;
+ if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
+ has_gamepad_key = false;
+ }
+ }
+ break;
+
+ default: break;
}
}
+void hid_task(void* param)
+{
+ (void) param;
+
+ while(1)
+ {
+ // Poll every 10ms
+ vTaskDelay(pdMS_TO_TICKS(10));
+
+ uint32_t const btn = board_button_read();
+
+ // Remote wakeup
+ if ( tud_suspended() && btn )
+ {
+ // Wake up host if we are in suspend mode
+ // and REMOTE_WAKEUP feature is enabled by host
+ tud_remote_wakeup();
+ }
+ else
+ {
+ // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
+ send_hid_report(REPORT_ID_KEYBOARD, btn);
+ }
+ }
+}
+
+// Invoked when sent REPORT successfully to host
+// Application can use this to send the next report
+// Note: For composite reports, report[0] is report ID
+void tud_hid_report_complete_cb(uint8_t itf, uint8_t const* report, uint8_t len)
+{
+ (void) itf;
+ (void) len;
+
+ uint8_t next_report_id = report[0] + 1;
+
+ if (next_report_id < REPORT_ID_COUNT)
+ {
+ send_hid_report(next_report_id, board_button_read());
+ }
+}
+
+
// Invoked when received GET_REPORT control request
// Application must fill buffer report's content and return its length.
// Return zero will cause the stack to STALL request
diff --git a/examples/device/hid_composite_freertos/src/usb_descriptors.c b/examples/device/hid_composite_freertos/src/usb_descriptors.c
index ead1580da..ec4976188 100644
--- a/examples/device/hid_composite_freertos/src/usb_descriptors.c
+++ b/examples/device/hid_composite_freertos/src/usb_descriptors.c
@@ -73,9 +73,10 @@ uint8_t const * tud_descriptor_device_cb(void)
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_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_GAMEPAD ( HID_REPORT_ID(REPORT_ID_GAMEPAD ))
};
// Invoked when received GET HID REPORT DESCRIPTOR
@@ -106,7 +107,7 @@ uint8_t const desc_configuration[] =
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 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)
+ TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 5)
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
diff --git a/examples/device/hid_composite_freertos/src/usb_descriptors.h b/examples/device/hid_composite_freertos/src/usb_descriptors.h
index d604650e3..ca8925ad9 100644
--- a/examples/device/hid_composite_freertos/src/usb_descriptors.h
+++ b/examples/device/hid_composite_freertos/src/usb_descriptors.h
@@ -29,7 +29,9 @@ enum
{
REPORT_ID_KEYBOARD = 1,
REPORT_ID_MOUSE,
- REPORT_ID_CONSUMER_CONTROL
+ REPORT_ID_CONSUMER_CONTROL,
+ REPORT_ID_GAMEPAD,
+ REPORT_ID_COUNT
};
#endif /* USB_DESCRIPTORS_H_ */