Checking if a specific item is in the Queue

Hi,
I went through the API and could not find any function to check if a specific item has been inserted in the queue. Is that correct?

If so I assume I have to create a loop that goes through the queue using xQueuePeek() and do the comparison myself. Is that correct?

Thank you :slight_smile:
Rick

There is no way of looking through a queue without reading the items out. Peeking will only look at the head of the queue. Can you describe why you need to do this?

I have a queue that receives messages from multiple sources (buttons, SPI, UART etc.). Then according to which message it receives, it sets the hardware into a specific state (say Msg_MotorRunTest_Start). Such state in the software is nothing more than a loop inside a Switch-Case which can only be exited when a specific command (like Msg_MotorRunTest_Stop) is received.

I can exit it by sending a notification but I think the more correct/elegant way is to us the same messaging system/channel (i.e. the same Queue).

Maybe the queue is not the ideal solution to use?

Thank you

you can optionally insert an item to the head instead of the tail of a queue. That way, you can implement some kind of priority item.

Thank you @RAc, didn’t think of that. :slight_smile:

What about a task notification where you set some bits, and then use simple logical AND operations to test if desired bits are set, and process them in the order that you like?

Quick and dirty example:

enum {
    ACTION_BUTTONS = 0x00000001,
    ACTION_SPI = 0x00000002,
    ACTION_UART = 0x00000004
};

void buttons_pressed() {
    xTaskNotify(task_handle, ACTION_BUTTONS, eSetBits);
}

void processing_task() {
    uint32_t events;

    while (true) {
        xTaskNotifyWait(0, 0xFFFFFFFF, &events, portMAX_DELAY);

        if (events & ACTION_BUTTONS) {
            process_buttons();
        }
    }
}

Thank you @tomstorey :slight_smile:
I am using MPLAB which until the next release supports only an older version of freeRTOS which has only 1 notification queue and have already used most of the 32 notification bits and want to keep few available is I need to add some functionality in the future. Also I already implemented a queue and have a lot of code already based on that functionality.

But thank you for that suggestion, I am always interested in suggestions and finding out how experienced programmers think and implement solutions.

Thank you
Rick

1 Like