lufa中的设备描述符...这是什么样的结构(内部)?我认为这没什么具体的,只是向 C/C++ 提出的问题。程序员

发布于 2024-10-18 13:37:31 字数 994 浏览 7 评论 0原文

我正在一个项目中使用 LUFA ,在阅读了一些示例后,我看到了其中一些构造。这些是宏吗?我知道 AVR 设备并且知道 PROGMEM 是其中之一?但什么是 .Header 以及为什么它以“.”开头。

有人可以向我解释如何创建这样的结构,或者告诉我在 LUFA 文档中哪里可以找到它们吗?

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

};

I am using LUFA for a project and after reading some of the examples I saw some of these constructs. Are these macros? 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

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何处潇湘 2024-10-25 13:37:31

这是命名结构体成员的 C99 方式,因此您可以按任意顺序给出值。我相信这个术语是“指定的初始化程序”。不是 C++ 的一部分。

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++.

贱人配狗天长地久 2024-10-25 13:37:31

作为 Bo Persson 说,这是初始化结构体的 C99 方式USB_Descriptor_Device_t 表示 Header 字段是 USB_Descriptor_Header_t

如果您要处理的话,您可能应该阅读一下指定的初始化程序 C99 代码。您可以将您的代码片段翻译为:

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 */

我认为在这种情况下显式初始化更容易阅读,但指定的初始化程序确实有其用途。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文