在 FreeRTOS 中通过队列发送指向结构的指针

Sending pointer to a struct through a queue in FreeRTOS

我似乎无法弄清楚如何在 FreeRTOS 中使用队列发送指向结构的指针。我已经尝试了所有我能想到的方法,但我总是得到一个指向某个随机内存区域的指针。

我正在尝试将指向按钮结构的指针发送到另一个任务,然后它将在屏幕上绘制。我尝试发送整个对象并且成功了,但是由于结构中有很多数据(两个图标的数据)我真的不想这样做。

代码正在 运行 Atmel SAME70 Xplained 中。

这是我正在处理的代码的更简单版本:

typedef  struct {
    uint32_t width;
    uint32_t height;
    uint32_t x;
    uint32_t y;
    uint8_t status;
    void (*callback)(t_but);
    tImage iconOn;
    tImage iconOff;
} t_but;
void task_lcd(void) {
    xQueueButtons = xQueueCreate(6, sizeof(struct t_but *));
    t_but *button;

    configure_lcd();
    draw_screen();

    while (1) {
        if (xQueueReceive(xQueueButtons, &(button), (TickType_t)500 / portTICK_PERIOD_MS)) {
            // This always prints some random numbers.
            printf("Button X: %" PRIu32 "\r\n", button->x);
        }
    }
}
void  task_buttons(void) {
    t_but butPlay = {.width = 64,
                     .height = 64,
                     .x = 60,
                     .y = 445,
                     .status = 0,
                     .callback = &ButPlayCallback,
                     .iconOn = play_red,
                     .iconOff = play_black};  

    xQueueSend(xQueueButtons, &butPlay, 0);

    while (1) {
        // Some other code.
    }
}

非常感谢任何帮助。

从 API 看来,xQueueSend 通过传递的指针进行复制,所以如果你想传递队列上的指针,你需要传递指向你的结构的指针的地址.

void  task_buttons(void) {
    t_but butPlay = {.width = 64,
                     .height = 64,
                     .x = 60,
                     .y = 445,
                     .status = 0,
                     .callback = &ButPlayCallback,
                     .iconOn = play_red,
                     .iconOff = play_black}; 
    t_but * const p_but = &butPlay; 

    xQueueSend(xQueueButtons, &p_but, 0);

    while (1) {
        // Some other code.
    }
}