开发者

Device descriptor in lufa... What kind of structure is this (inside)? I think it is nothing specific, and only a question to C/C++ programmers

I am using LUFA for a project and after reading some of the examples I saw some of these constructs. Are these m开发者_JAVA技巧acros? I know AVR devices and know that PROGMEM is one? But what is .Header and why is it starting with a ".".

Can someone explain to me how to create contructs like these or show me where I will find them in the LUFA documentation?

USB_Descriptor_Device_t PROGMEM DeviceDescriptor =

{
    .Header                 = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},

    .USBSpecification       = VERSION_BCD(01.10),
    .Class                  = USB_CSCP_NoDeviceClass,
    .SubClass               = USB_CSCP_NoDeviceSubclass,
    .Protocol               = USB_CSCP_NoDeviceProtocol,

    .Endpoint0Size          = FIXED_CONTROL_ENDPOINT_SIZE,

    .VendorID               = 0x03EB,
    .ProductID              = 0x2045,
    .ReleaseNumber          = VERSION_BCD(00.01),

    .ManufacturerStrIndex   = 0x01,
    .ProductStrIndex        = 0x02,
    .SerialNumStrIndex      = USE_INTERNAL_SERIAL,

    .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS

};


That is a C99 way of naming members of a struct, so you can give the values in an arbitrary order. I believe the term is "designated initializers". Not part of C++.


As Bo Persson said, this is the C99 way of initializing a struct. The LUFA documentation for USB_Descriptor_Device_t says that the Header field is a USB_Descriptor_Header_t.

You should probably read up on designated initializers a little if you are going to be dealing with C99 code. You can translate your snippet into:

USB_Descriptor_Device_t PROGMEM DeviceDescriptor;
memset(&DeviceDescriptor, 0, sizeof(DeviceDescriptor));
DeviceDescriptor.Header.Size = sizeof(USB_Descriptor_Device_t);
DeviceDescriptor.Header.Type = DTYPE_Device;
DeviceDescriptor.USBSpecification = VERSION_BCD(01.10); /* beware of leading zeros! */
DeviceDescriptor.Class = USB_CSCP_NoDeviceClass;
DeviceDescriptor.SubClass = USB_CSCP_NoDeviceSubClass;
DeviceDescriptor.Protocol = USB_CSCP_NoDeviceProtocol;
/* etc etc etc */

I think that explicit initialization is easier to read in this case, but designated initializers do have their uses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜