minor code reformatting

This commit is contained in:
King Kévin 2022-10-24 14:44:26 +02:00
parent d80d4877f5
commit b25024e71f
1 changed files with 10 additions and 14 deletions

View File

@ -136,35 +136,31 @@ uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid)
size_t chr_count; size_t chr_count;
if ( index == 0) if (index == 0) { // get language (not a string)
{
memcpy(&_desc_str[1], string_desc_arr[0], 2); memcpy(&_desc_str[1], string_desc_arr[0], 2);
chr_count = 1; chr_count = 1;
} } else { // get other description (strings)
else
{
// Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; // check if requested descriptor exists
const char* str = string_desc_arr[index]; // load requested string
const char* str = string_desc_arr[index]; // check string length
// Cap at max char
chr_count = strlen(str); chr_count = strlen(str);
if ( chr_count > 31 ) { if ( chr_count > 31 ) { // cap at max char
chr_count = 31; chr_count = 31;
} }
// Convert ASCII string into UTF-16 // convert ASCII string into UTF-16
for(uint8_t i=0; i<chr_count; i++) for(uint8_t i = 0; i < chr_count; i++)
{ {
_desc_str[1+i] = str[i]; _desc_str[1 + i] = str[i];
} }
} }
// first byte is length (including header), second byte is string type // first byte is length (including header), second byte is string type
_desc_str[0] = (uint16_t)((((uint16_t)TUSB_DESC_STRING) << 8 ) | (2u*chr_count + 2u)); _desc_str[0] = (uint16_t)((((uint16_t)TUSB_DESC_STRING) << 8 ) | (2u * chr_count + 2u));
return _desc_str; return _desc_str;
} }