summaryrefslogtreecommitdiff
path: root/examples/device/hid_composite
diff options
context:
space:
mode:
authorReinhard Panhuber <[email protected]>2021-02-12 18:05:20 +0100
committerReinhard Panhuber <[email protected]>2021-02-12 18:05:20 +0100
commitcdf600048f717f187243b8118eac43b793c3feec (patch)
tree00d88a8e39560c2e1a35f6c9feb97062733d9d8b /examples/device/hid_composite
parent185414721ff549058a199ade40966015e6260996 (diff)
parent09868434cd9f53394350cce682333ada97f796c3 (diff)
Merge remote-tracking branch 'upstream/master' into edpt_ISO_xfer
Diffstat (limited to 'examples/device/hid_composite')
-rw-r--r--examples/device/hid_composite/CMakeLists.txt41
-rw-r--r--examples/device/hid_composite/src/main.c140
-rw-r--r--examples/device/hid_composite/src/tusb_config.h3
-rw-r--r--examples/device/hid_composite/src/usb_descriptors.c10
-rw-r--r--examples/device/hid_composite/src/usb_descriptors.h5
5 files changed, 160 insertions, 39 deletions
diff --git a/examples/device/hid_composite/CMakeLists.txt b/examples/device/hid_composite/CMakeLists.txt
new file mode 100644
index 000000000..f4bf75c26
--- /dev/null
+++ b/examples/device/hid_composite/CMakeLists.txt
@@ -0,0 +1,41 @@
+# use directory name for project id
+get_filename_component(PROJECT ${CMAKE_CURRENT_SOURCE_DIR} NAME)
+set(PROJECT ${BOARD}-${PROJECT})
+
+# TOP is absolute path to root directory of TinyUSB git repo
+set(TOP "../../..")
+get_filename_component(TOP "${TOP}" REALPATH)
+
+# Check for -DFAMILY=
+if(FAMILY STREQUAL "rp2040")
+ cmake_minimum_required(VERSION 3.12)
+ set(PICO_SDK_PATH ${TOP}/hw/mcu/raspberrypi/pico-sdk)
+ include(${PICO_SDK_PATH}/pico_sdk_init.cmake)
+ project(${PROJECT})
+ pico_sdk_init()
+ add_executable(${PROJECT})
+
+ include(${TOP}/hw/bsp/${FAMILY}/family.cmake)
+
+ # Example source
+ target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+ # Example include
+ target_include_directories(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+ # Example defines
+ target_compile_definitions(${PROJECT} PUBLIC
+ CFG_TUSB_OS=OPT_OS_PICO
+ )
+
+ target_link_libraries(${PROJECT} pico_stdlib pico_fix_rp2040_usb_device_enumeration)
+ pico_add_extra_outputs(${PROJECT})
+
+else()
+ message(FATAL_ERROR "Invalid FAMILY specified")
+endif()
diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c
index 7207e4d4c..9fb2de88d 100644
--- a/examples/device/hid_composite/src/main.c
+++ b/examples/device/hid_composite/src/main.c
@@ -104,6 +104,98 @@ void tud_resume_cb(void)
// USB HID
//--------------------------------------------------------------------+
+static void send_hid_report(uint8_t report_id, uint32_t btn)
+{
+ // skip if hid is not ready yet
+ if ( !tud_hid_ready() ) return;
+
+ switch(report_id)
+ {
+ case REPORT_ID_KEYBOARD:
+ {
+ // use to avoid send multiple consecutive zero report for keyboard
+ static bool has_keyboard_key = false;
+
+ if ( btn )
+ {
+ uint8_t keycode[6] = { 0 };
+ keycode[0] = HID_KEY_A;
+
+ tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
+ has_keyboard_key = true;
+ }else
+ {
+ // send empty key report if previously has key pressed
+ 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;
+
+ // no button, right + down, no scroll, no pan
+ tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta, delta, 0, 0);
+ }
+ break;
+
+ case REPORT_ID_CONSUMER_CONTROL:
+ {
+ // use to avoid send multiple consecutive zero report
+ static bool has_consumer_key = false;
+
+ if ( btn )
+ {
+ // 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
+ {
+ // send empty key report (release key) if previously has key pressed
+ uint16_t empty_key = 0;
+ if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
+ 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;
+ }
+}
+
+// Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
+// tud_hid_report_complete_cb() is used to send the next report after previous one is complete
void hid_task(void)
{
// Poll every 10ms
@@ -121,47 +213,29 @@ void hid_task(void)
// 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() )
+ }else
{
- 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 attempt to send keyboard report
- board_delay(10);
- }
+ // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
+ send_hid_report(REPORT_ID_KEYBOARD, btn);
}
+}
- /*------------- 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;
+// 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;
- tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
+ uint8_t next_report_id = report[0] + 1;
- has_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 (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/src/tusb_config.h b/examples/device/hid_composite/src/tusb_config.h
index d8e6b73e8..3e608ed37 100644
--- a/examples/device/hid_composite/src/tusb_config.h
+++ b/examples/device/hid_composite/src/tusb_config.h
@@ -64,8 +64,9 @@
#error "Incorrect RHPort configuration"
#endif
-// This example doesn't use an RTOS
+#ifndef CFG_TUSB_OS
#define CFG_TUSB_OS OPT_OS_NONE
+#endif
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
// #define CFG_TUSB_DEBUG 0
diff --git a/examples/device/hid_composite/src/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c
index 5052a436f..ec4976188 100644
--- a/examples/device/hid_composite/src/usb_descriptors.c
+++ b/examples/device/hid_composite/src/usb_descriptors.c
@@ -73,8 +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_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
@@ -104,8 +106,8 @@ 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
- TUD_HID_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), EPNUM_HID, CFG_TUD_HID_EP_BUFSIZE, 10)
+ // 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, 5)
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
diff --git a/examples/device/hid_composite/src/usb_descriptors.h b/examples/device/hid_composite/src/usb_descriptors.h
index 6992d3349..ca8925ad9 100644
--- a/examples/device/hid_composite/src/usb_descriptors.h
+++ b/examples/device/hid_composite/src/usb_descriptors.h
@@ -28,7 +28,10 @@
enum
{
REPORT_ID_KEYBOARD = 1,
- REPORT_ID_MOUSE
+ REPORT_ID_MOUSE,
+ REPORT_ID_CONSUMER_CONTROL,
+ REPORT_ID_GAMEPAD,
+ REPORT_ID_COUNT
};
#endif /* USB_DESCRIPTORS_H_ */