summaryrefslogtreecommitdiff
path: root/examples/device/hid_generic_inout/src
diff options
context:
space:
mode:
authorhathach <[email protected]>2019-05-11 16:31:08 +0700
committerhathach <[email protected]>2019-05-11 16:31:52 +0700
commitde56a0ca8987c15c06112ece2589dab18f1d6666 (patch)
tree101bd3bf6cb994043e3347e2e473f252996fd215 /examples/device/hid_generic_inout/src
parentbfa073818c83266c2353ccf4fce580eef62e4a0c (diff)
add tud_descriptor_string_cb() for getting string descriptor from application
- remove tud_desc_set.string_arr/string_count
Diffstat (limited to 'examples/device/hid_generic_inout/src')
-rw-r--r--examples/device/hid_generic_inout/src/usb_descriptors.c61
1 files changed, 42 insertions, 19 deletions
diff --git a/examples/device/hid_generic_inout/src/usb_descriptors.c b/examples/device/hid_generic_inout/src/usb_descriptors.c
index 231eb4fa6..b25915ceb 100644
--- a/examples/device/hid_generic_inout/src/usb_descriptors.c
+++ b/examples/device/hid_generic_inout/src/usb_descriptors.c
@@ -88,30 +88,53 @@ uint8_t const desc_configuration[] =
TUD_HID_INOUT_DESCRIPTOR(ITF_NUM_HID, 0, HID_PROTOCOL_NONE, sizeof(desc_hid_report), 0x80 | EPNUM_HID, EPNUM_HID, 16, 10)
};
-//------------- String Descriptors -------------//
-// array of pointer to string descriptors
-uint16_t const * const string_desc_arr [] =
+
+// tud_desc_set is required by tinyusb stack
+tud_desc_set_t tud_desc_set =
{
- // 0: is supported language = English
- TUD_DESC_STRCONV(0x0409),
+ .device = &desc_device,
+ .config = desc_configuration,
- // 1: Manufacturer
- TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', '.', 'o', 'r', 'g'),
+#if CFG_TUD_HID
+ .hid_report = desc_hid_report,
+#endif
+};
- // 2: Product
- TUD_DESC_STRCONV('t', 'i', 'n', 'y', 'u', 's', 'b', ' ', 'd', 'e', 'v', 'i', 'c', 'e'),
+//------------- String Descriptors -------------//
- // 3: Serials, should use chip ID
- TUD_DESC_STRCONV('1', '2', '3', '4', '5', '6'),
+// array of pointer to string descriptors
+char const* string_desc_arr [] =
+{
+ (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ "123456", // 3: Serials, should use chip ID
};
-// tud_desc_set is required by tinyusb stack
-tud_desc_set_t tud_desc_set =
+// Invoked when received GET_STRING_DESC request
+// max_char is CFG_TUD_ENDOINT0_SIZE/2 -1, typically max_char = 31 if Endpoint0 size is 64
+// Return number of characters. Note usb string is in 16-bits unicode format
+uint8_t tud_descriptor_string_cb(uint8_t index, uint16_t* desc, uint8_t max_char)
{
- .device = &desc_device,
- .config = desc_configuration,
+ if ( index == 0)
+ {
+ memcpy(desc, string_desc_arr[0], 2);
+ return 1;
+ }else
+ {
+ if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return 0;
- .string_arr = (uint8_t const **) string_desc_arr,
- .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
- .hid_report = desc_hid_report,
-};
+ const char* str = string_desc_arr[index];
+
+ // Cap at max char
+ uint8_t count = strlen(str);
+ if ( count > max_char ) count = max_char;
+
+ for(uint8_t i=0; i<count; i++)
+ {
+ *desc++ = str[i];
+ }
+
+ return count;
+ }
+}