结构分配错误
Wrong assignment to struct
我有这样的结构:
typedef struct ws2811_channel_t
{
int gpionum; //< GPIO Pin with PWM alternate function, 0 if unused
int invert; //< Invert output signal
int count; //< Number of LEDs, 0 if channel is unused
int strip_type; //< Strip color layout -- one of WS2811_STRIP_xxx constants
ws2811_led_t *leds; //< LED buffers, allocated by driver based on count
uint8_t brightness; //< Brightness value between 0 and 255
uint8_t wshift; //< White shift value
uint8_t rshift; //< Red shift value
uint8_t gshift; //< Green shift value
uint8_t bshift; //< Blue shift value
uint8_t *gamma; //< Gamma correction table
} ws2811_channel_t;
typedef struct ws2811_t
{
uint64_t render_wait_time; //< time in µs before the next render can run
struct ws2811_device *device; //< Private data for driver use
const rpi_hw_t *rpi_hw; //< RPI Hardware Information
uint32_t freq; //< Required output frequency
int dmanum; //< DMA number _not_ already in use
ws2811_channel_t channel[RPI_PWM_CHANNELS];
} ws2811_t;
和我的工作示例:
ws2811_channel_t channel = {
.gpionum = LED_PIN,
.invert = LED_INVERT,
.count = LED_COUNT,
.strip_type = WS2811_STRIP_RGB,
.brightness = LED_BRIGHTNESS,
};
ws2811_t test = {
.freq = LED_FREQ_HZ,
.dmanum = LED_DMA,
channel,
};
LOG_INFO("LEDController: Init - " << ws2811_get_return_t_str(ws2811_init(&test)));
for (int i = 0; i < LED_COUNT; i++)
{
test.channel[0].leds[i] = (0 << 16) | (0 << 8) | 0;
}
ws2811_render(&test);
我想在 class 中为私有 属性 进行适当的分配,所以当我这样声明时:
ws2811_channel_t channel;
channel.gpionum = LED_PIN;
channel.invert = LED_INVERT;
channel.count = LED_COUNT;
channel.strip_type = WS2811_STRIP_RGB;
channel.brightness = LED_BRIGHTNESS;
ws2811_t test;
test.freq = LED_FREQ_HZ;
test.dmanum = LED_DMA;
test.channel[0] = channel;
LOG_INFO("LEDController: Init - " << ws2811_get_return_t_str(ws2811_init(&test)));
for (int i = 0; i < LED_COUNT; i++)
{
test.channel[0].leds[i] = (0 << 16) | (0 << 8) | 0;
}
ws2811_render(&test);
ws2811_render(&test);
挂起并冻结,在某些时候我得到了指向该方法的错误。
我假设这是因为它以某种方式有错误的分配。但是为什么?
渲染的定义是
ws2811_return_t ws2811_render(ws2811_t *ws2811)
从这里开始:https://github.com/jgarff/rpi_ws281x/blob/master/ws2811.c#L1138
我做错了什么?
在您的第二个示例中,尝试 ws2811_channel_t channel{};
和 ws2811_t test{};
- 例如添加 {}
.
如果您这样做,那么您将零 初始化所有成员。您的第一个(工作)版本和第二个(损坏的)版本之间的主要区别在于,第一个版本会将 gamma
之类的内容归零,而第二个版本则不会。添加 {}
会将所有内容归零,然后再覆盖您想要的项目。
如果您这样做 type a = {...}
,那么 ...
和 未提及或编入索引的任何内容都将 归零.如果你这样做 type b; b.field=value;
这里没有任何东西可以将任何未初始化的字段清零。
我有这样的结构:
typedef struct ws2811_channel_t
{
int gpionum; //< GPIO Pin with PWM alternate function, 0 if unused
int invert; //< Invert output signal
int count; //< Number of LEDs, 0 if channel is unused
int strip_type; //< Strip color layout -- one of WS2811_STRIP_xxx constants
ws2811_led_t *leds; //< LED buffers, allocated by driver based on count
uint8_t brightness; //< Brightness value between 0 and 255
uint8_t wshift; //< White shift value
uint8_t rshift; //< Red shift value
uint8_t gshift; //< Green shift value
uint8_t bshift; //< Blue shift value
uint8_t *gamma; //< Gamma correction table
} ws2811_channel_t;
typedef struct ws2811_t
{
uint64_t render_wait_time; //< time in µs before the next render can run
struct ws2811_device *device; //< Private data for driver use
const rpi_hw_t *rpi_hw; //< RPI Hardware Information
uint32_t freq; //< Required output frequency
int dmanum; //< DMA number _not_ already in use
ws2811_channel_t channel[RPI_PWM_CHANNELS];
} ws2811_t;
和我的工作示例:
ws2811_channel_t channel = {
.gpionum = LED_PIN,
.invert = LED_INVERT,
.count = LED_COUNT,
.strip_type = WS2811_STRIP_RGB,
.brightness = LED_BRIGHTNESS,
};
ws2811_t test = {
.freq = LED_FREQ_HZ,
.dmanum = LED_DMA,
channel,
};
LOG_INFO("LEDController: Init - " << ws2811_get_return_t_str(ws2811_init(&test)));
for (int i = 0; i < LED_COUNT; i++)
{
test.channel[0].leds[i] = (0 << 16) | (0 << 8) | 0;
}
ws2811_render(&test);
我想在 class 中为私有 属性 进行适当的分配,所以当我这样声明时:
ws2811_channel_t channel;
channel.gpionum = LED_PIN;
channel.invert = LED_INVERT;
channel.count = LED_COUNT;
channel.strip_type = WS2811_STRIP_RGB;
channel.brightness = LED_BRIGHTNESS;
ws2811_t test;
test.freq = LED_FREQ_HZ;
test.dmanum = LED_DMA;
test.channel[0] = channel;
LOG_INFO("LEDController: Init - " << ws2811_get_return_t_str(ws2811_init(&test)));
for (int i = 0; i < LED_COUNT; i++)
{
test.channel[0].leds[i] = (0 << 16) | (0 << 8) | 0;
}
ws2811_render(&test);
ws2811_render(&test);
挂起并冻结,在某些时候我得到了指向该方法的错误。
我假设这是因为它以某种方式有错误的分配。但是为什么?
渲染的定义是
ws2811_return_t ws2811_render(ws2811_t *ws2811)
从这里开始:https://github.com/jgarff/rpi_ws281x/blob/master/ws2811.c#L1138
我做错了什么?
在您的第二个示例中,尝试 ws2811_channel_t channel{};
和 ws2811_t test{};
- 例如添加 {}
.
如果您这样做,那么您将零 初始化所有成员。您的第一个(工作)版本和第二个(损坏的)版本之间的主要区别在于,第一个版本会将 gamma
之类的内容归零,而第二个版本则不会。添加 {}
会将所有内容归零,然后再覆盖您想要的项目。
如果您这样做 type a = {...}
,那么 ...
和 未提及或编入索引的任何内容都将 归零.如果你这样做 type b; b.field=value;
这里没有任何东西可以将任何未初始化的字段清零。