This is a simple queue handler implemented in C, designed to be reused for simple tasks like queues for embedded interfaces such as SPI or UART. The module uses a circular buffer to store data, providing efficient enqueue and dequeue operations.
- Circular buffer implementation
- Enqueue and dequeue operations
- Error handling for full and empty queues
- Initialization with custom buffer, size, and element size
Include the queue.h
header file in your source code to use the queue functions.
Initialize the queue with a buffer, size, and element size using the queue_init
function. This function returns a status code indicating the result of the initialization.
queue_t queue;
uint8_t buffer[10];
queue_status_t status = queue_init(&queue, buffer, 10, sizeof(uint8_t));
To add data to the queue, use the queue_enqueue
function. This function takes the queue pointer and the data to be enqueued as arguments.
uint8_t data = 1;
queue_status_t status = queue_enqueue(&queue, &data);
To remove data from the queue, use the queue_dequeue
function. This function takes the queue pointer and a pointer to the variable where the dequeued data will be stored.
uint8_t data;
queue_status_t status = queue_dequeue(&queue, &data);
The queue_enqueue
and queue_dequeue
functions return an error code to indicate if the operation was successful. The error codes are defined in the queue.h
header file.
queue_status_t status = queue_enqueue(&queue, &data);
if (status == QUEUE_FULL) {
// Handle full queue error
}
status = queue_dequeue(&queue, &data);
if (status == QUEUE_EMPTY) {
// Handle empty queue error
}
You can also use the queue with a struct data type. Here is an example:
typedef struct {
uint8_t id;
uint16_t value;
uint8_t padding; // Padding to align the struct size
} data_t;
queue_t queue;
data_t buffer[10];
queue_init(&queue, buffer, 10, sizeof(data_t));
data_t data = {1, 100, 0};
queue_enqueue(&queue, &data);
data_t dequeued_data;
queue_dequeue(&queue, &dequeued_data);
Unit tests for the queue module are written using GoogleTest. To run the tests, follow these steps:
- Install GoogleTest.
- Create a
test
directory and add thetest_queue.cpp
file with the test cases. - Update your
CMakeLists.txt
to include the test directory and link GoogleTest. - Build and run the tests using CMake.
cmake .
make
ctest