简体   繁体   English

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

[英]Sending pointer to a struct through a queue in FreeRTOS

I can't seem to figure out how to send a pointer to a struct using a queue in FreeRTOS.我似乎无法弄清楚如何使用 FreeRTOS 中的队列发送指向结构的指针。 I've tried all I could think of, yet I always get a pointer to some random region of memory.我已经尝试了所有我能想到的,但我总是得到一个指向 memory 的某个随机区域的指针。

I'm trying to send a pointer to a button struct to another task, where it will then be drawn on the screen.我正在尝试将指向按钮结构的指针发送到另一个任务,然后它将在屏幕上绘制。 I tried sending the whole object and it worked, but since there's a lot of data in the struct (data of two icons) I don't really want to do that.我尝试发送整个 object 并且它有效,但由于结构中有很多数据(两个图标的数据)我真的不想这样做。

The code is being run in Atmel SAME70 Xplained.该代码正在 Atmel SAME70 Xplained 中运行。

Here is a simpler version of the code I'm working on:这是我正在处理的代码的更简单版本:

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

Any help is very much appreciated.很感谢任何形式的帮助。

It appears from the API that the xQueueSend does a copy via the pointer passed so if you want to pass a pointer on the queue you need to pass the address of a pointer that is pointing at your structure.从 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.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM