From 3d95835f2238f581669a1ff129a20ce8237649e1 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 1 Feb 2021 14:47:39 +0700 Subject: [PATCH] add consumer control to hid_composite examples both no OS and freeRTOS --- examples/device/hid_composite/src/main.c | 28 +++++++++++++++++-- .../hid_composite/src/usb_descriptors.c | 5 ++-- .../hid_composite/src/usb_descriptors.h | 3 +- .../device/hid_composite_freertos/src/main.c | 27 +++++++++++++++++- .../src/usb_descriptors.c | 3 +- .../src/usb_descriptors.h | 3 +- examples/rules.mk | 6 ++-- 7 files changed, 65 insertions(+), 10 deletions(-) diff --git a/examples/device/hid_composite/src/main.c b/examples/device/hid_composite/src/main.c index 7207e4d4c..6a9b6445d 100644 --- a/examples/device/hid_composite/src/main.c +++ b/examples/device/hid_composite/src/main.c @@ -133,7 +133,7 @@ void hid_task(void) // 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 + // delay a bit before sending keyboard report board_delay(10); } } @@ -158,10 +158,34 @@ void hid_task(void) if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); has_key = false; } + + // delay a bit before sending consumer report + board_delay(10); + } + + /*------------- Consume Control -------------*/ + if ( tud_hid_ready() ) + { + // 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; + } } } - // 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/usb_descriptors.c b/examples/device/hid_composite/src/usb_descriptors.c index 5052a436f..879eff05e 100644 --- a/examples/device/hid_composite/src/usb_descriptors.c +++ b/examples/device/hid_composite/src/usb_descriptors.c @@ -73,8 +73,9 @@ 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 )) }; // Invoked when received GET HID REPORT DESCRIPTOR diff --git a/examples/device/hid_composite/src/usb_descriptors.h b/examples/device/hid_composite/src/usb_descriptors.h index 6992d3349..feda83dcd 100644 --- a/examples/device/hid_composite/src/usb_descriptors.h +++ b/examples/device/hid_composite/src/usb_descriptors.h @@ -28,7 +28,8 @@ enum { REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE + REPORT_ID_MOUSE, + REPORT_ID_CONSUMER_CONTROL, }; #endif /* USB_DESCRIPTORS_H_ */ diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c index 83c68b515..2a9da69f6 100644 --- a/examples/device/hid_composite_freertos/src/main.c +++ b/examples/device/hid_composite_freertos/src/main.c @@ -192,7 +192,7 @@ void hid_task(void* param) // 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 + // delay a bit before sending keyboard report vTaskDelay(pdMS_TO_TICKS(10)); } } @@ -217,6 +217,31 @@ void hid_task(void* param) if (has_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL); has_key = false; } + + // delay a bit before sending consumer report + vTaskDelay(pdMS_TO_TICKS(10)); + } + + /*------------- Consume Control -------------*/ + if ( tud_hid_ready() ) + { + // 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; + } } } } diff --git a/examples/device/hid_composite_freertos/src/usb_descriptors.c b/examples/device/hid_composite_freertos/src/usb_descriptors.c index 5052a436f..a169ee2c7 100644 --- a/examples/device/hid_composite_freertos/src/usb_descriptors.c +++ b/examples/device/hid_composite_freertos/src/usb_descriptors.c @@ -74,7 +74,8 @@ 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_MOUSE ( HID_REPORT_ID(REPORT_ID_MOUSE) ), + TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(REPORT_ID_CONSUMER_CONTROL )) }; // Invoked when received GET HID REPORT DESCRIPTOR diff --git a/examples/device/hid_composite_freertos/src/usb_descriptors.h b/examples/device/hid_composite_freertos/src/usb_descriptors.h index 6992d3349..d604650e3 100644 --- a/examples/device/hid_composite_freertos/src/usb_descriptors.h +++ b/examples/device/hid_composite_freertos/src/usb_descriptors.h @@ -28,7 +28,8 @@ enum { REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE + REPORT_ID_MOUSE, + REPORT_ID_CONSUMER_CONTROL }; #endif /* USB_DESCRIPTORS_H_ */ diff --git a/examples/rules.mk b/examples/rules.mk index 5dded759c..66194dfcc 100644 --- a/examples/rules.mk +++ b/examples/rules.mk @@ -36,10 +36,12 @@ erase: monitor: idf.py -B$(BUILD) -DFAMILY=$(FAMILY) -DBOARD=$(BOARD) $(CMAKE_DEFSYM) monitor +uf2: $(BUILD)/$(PROJECT).uf2 + UF2_FAMILY_ID = 0xbfdd4eee -$(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).hex +$(BUILD)/$(PROJECT).uf2: $(BUILD)/$(PROJECT).bin @echo CREATE $@ - $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -c -o $@ $^ + $(PYTHON) $(TOP)/tools/uf2/utils/uf2conv.py -f $(UF2_FAMILY_ID) -b 0x0 -c -o $@ $^ else ifeq ($(FAMILY),rp2040)