在 C++ 中正确地从 C 库初始化 typedef 结构 [英] Initializing typedef struct from C library properly in C++
问题描述
我想在我的 C++ 项目中包含一个库(控制 Raspberry Pi 上的 RGB LED 灯条).导入库工作正常,但我在正确初始化某些结构时遇到了很大的问题.我什至不知道在哪里可以找到正确的语法,我做了很多谷歌搜索,但没有走多远.
I want to include a library in my C++ project (controls RGB LED strips on the Raspberry Pi). Importing the library is working fine but I have quite the issue with properly initializing some structs. I'm pretty lost where to even find the proper syntax, I did a lot of googling but didn't get very far.
我首先想要的是让库附带的示例应用程序运行.请参阅:https://github.com/richardghirst/rpi_ws281x/blob/master/main.c
What I want to at first is getting the sample application going that comes with the library. See: https://github.com/richardghirst/rpi_ws281x/blob/master/main.c
我的主要问题是这个.在C++方式下做的事情我该怎么做?
My main issue is this. How do I do what is done below the C++ way?
ws2811_t ledstring =
{
.freq = TARGET_FREQ,
.dmanum = DMA,
.channel =
{
[0] =
{
.gpionum = GPIO_PIN,
.count = LED_COUNT,
.invert = 0,
.brightness = 255,
},
[1] =
{
.gpionum = 0,
.count = 0,
.invert = 0,
.brightness = 0,
},
},
};
它的初始化方式是特定于 C 的,并且不会在任何当前的 C++ 标准中编译.请参阅:为什么 C++11 不支持指定初始化程序列为 C99?到目前为止,我只使用过自己的结构体,也从未使用过 typedef,所以我只是对此处定义结构体的方式感到困惑.
The way this is initialized is C specific and doesn't compile in any current C++ standard. See: Why does C++11 not support designated initializer list as C99? So far I only ever used my own structs and also never used typedef, so I'm just confused the way structs are defined here.
上面初始化的结构体就是这样定义的.请参阅:https://github.com/richardghirst/rpi_ws281x/blob/master/ws2811.h
The struct(s) that gets initialized above is defined in this way. See: https://github.com/richardghirst/rpi_ws281x/blob/master/ws2811.h
typedef struct
{
int gpionum; //< GPIO Pin with PWM alternate function
int invert; //< Invert output signal
int count; //< Number of LEDs, 0 if channel is unused
int brightness; //< Brightness value between 0 and 255
ws2811_led_t *leds; //< LED buffers, allocated by driver based on count
} ws2811_channel_t;
typedef struct
{
struct ws2811_device *device; //< Private data for driver use
uint32_t freq; //< Required output frequency
int dmanum; //< DMA number _not_ already in use
ws2811_channel_t channel[RPI_PWM_CHANNELS];
} ws2811_t;
我尝试的是这样的:
ws2811_led_t matrix[WIDTH][HEIGHT];
ws2811_channel_t channel0 = {GPIO_PIN,LED_COUNT,0,255,*matrix};
ws2811_t ledstring = {nullptr,TARGET_FREQ,DMA,channel0};
当我真正渲染"到 LED 灯条时,它可以编译但导致 malloc 错误:
That compiles but results in a malloc error when I come to actually "render" to the LED strip:
int x, y;
for (x = 0; x < WIDTH; x++)
{
for (y = 0; y < HEIGHT; y++)
{
cout << "LEDs size: " << (y * WIDTH) + x << endl;
ledstring.channel[0].leds[(y * WIDTH) + x] = matrix[x][y];
}
}
在循环构造完成后导致此错误消息:
Results in this error message after the loop construct finishes:
malloc(): memory corruption (fast): 0x021acaa8
推荐答案
您应该能够使用以下初始化程序:
You should be able to use use following initializer:
ws2811_t ledstring =
{
nullptr,
TARGET_FREQ,
DMA,
{
{ GPIO_PIN, 0, LED_COUNT, 255 },
{ 0 }
}
};
这篇关于在 C++ 中正确地从 C 库初始化 typedef 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!