The queue library provides interrupt secure implementation of the circular buffer to store predefined objects.
Key features include:
- Protected enqueuing
- Protected dequeuing
- Two modes of overflow handling
Creating a queue
To create a queue instance, use the NRF_QUEUE_DEF macro and provide the stored type, the identifier, the queue size, and the working mode. The macro allocates memory for the queue and the internals of the instance.
The following example code allocates memory for a queue instance and a queue that can store up to 10 bytes:
NRF_QUEUE_DEF
(uint8_t, m_byte_queue, 10,
NRF_QUEUE_MODE_NO_OVERFLOW
);
After the queue is defined, you can define its interface (this step is optional):
NRF_QUEUE_INTERFACE_DEC
(uint8_t, byte_queue);
NRF_QUEUE_INTERFACE_DEF
(uint8_t, byte_queue, &m_byte_queue)
Using the queue
This section contains sample code that lets you perform actions on the queue instance:
- Putting an item into the queue:
-
Alternatively, putting an item into the queue if the interface has not been defined:
uint8_t data = 0x01;ret_code_t err_code = nrf_queue_push (&m_byte_queue, &data);APP_ERROR_CHECK (err_code);
-
Atomically putting more items into the queue:
If NRF_QUEUE_MODE_NO_OVERFLOW mode is selected and there is not enough space, NRF_ERROR_NO_MEM will be returned.uint8_t data[] = {0x01, 0x02, 0x03};ret_code_t err_code = byte_queue_write(data, sizeof (data));APP_ERROR_CHECK (err_code);
- Getting an item from the queue:
- Getting an item without removing it from the queue: If the queue is empty, NRF_ERROR_NOT_FOUND will be returned.
-
Getting more items from the queue:
uint8_t data[5];ret_code_t err_code = byte_queue_read(data, sizeof (data));APP_ERROR_CHECK (err_code);