Skip to content

Message Queue

Derek Jamison edited this page Apr 29, 2023 · 12 revisions

The FuriMessageQueue is typically used in an application for communicating events.

Key concepts

Create an enumeration of event types that you want to be able to queue.

typedef enum {
    MyEventTypeKey,
} MyEventType;

Create a structure with the event type and the associated data.

typedef struct {
    MyEventType type; // The reason for this event.
    InputEvent input; // This data is specific to keypress data.
    // TODO: Add additional properties that are helpful for your events.
} MyEvent;

Allocate a queue.

FuriMessageQueue* queue = furi_message_queue_alloc(8, sizeof(MyEvent));

In one of your callback routines, queue a message.

// For example, an input_callback has "InputEvent* input_event" as the first parameter and 
// a context as the second parameter. Typically, from the context you are able to access 
// the queue object.  This is accomplished by having FuriMessageQueue as the callback context, 
// or storing the queue as a property in your context.
MyEvent event = {.type = MyEventTypeKey, .input = *input_event};
furi_message_queue_put(queue, &event, FuriWaitForever);

Dequeue messages in a loop until you get done event.

MyEvent event;
bool keep_processing = true;
while (keep_processing) {
   if(furi_message_queue_get(queue, &event, FuriWaitForever) == FuriStatusOk) {
      // Process the event, set keep_processing to false if this is the done event.
   }
}

Free the queue when your application is exiting.

furi_message_queue_free(queue);
Clone this wiki locally