Hi,
I'm using the nrf_queue library and I need to peek more than one element at a time.
How to do?
Hi,
I'm using the nrf_queue library and I need to peek more than one element at a time.
How to do?
I requested a feature for this like a year ago. It's very easy to implement, you just have to add a "peek" argument to some functions and adjust the definitions here and there. The gist of the thing is this (I just added the just_peek boolean and some if conditions):
/**@brief Read elements from the queue. This function assumes that there are enough elements * in the queue to read and that this process will not be interrupted. * * @param[in] p_queue Pointer to the nrf_queue_t instance. * @param[out] p_data Pointer to the buffer where elements will be copied. * @param[in] element_count Number of elements to read. * @param[in] just_peek Should not advance the front of the queue. */ static void queue_read(nrf_queue_t const * p_queue, void * p_data, uint32_t element_count, bool just_peek) { size_t front = p_queue->p_cb->front; size_t back = p_queue->p_cb->back; size_t continuous = (front <= back) ? (back - front) : (p_queue->size + 1 - front); void const * p_read_ptr = (void const *)((size_t)p_queue->p_buffer + front * p_queue->element_size); if (element_count <= continuous) { memcpy(p_data, p_read_ptr, element_count * p_queue->element_size); if (!just_peek) { p_queue->p_cb->front = ((front + element_count) <= p_queue->size) ? (front + element_count) : 0; } } else { size_t first_read_length = continuous * p_queue->element_size; memcpy(p_data, p_read_ptr, first_read_length); size_t elements_left = element_count - continuous; memcpy((void *)((size_t)p_data + first_read_length), p_queue->p_buffer, elements_left * p_queue->element_size); if (!just_peek) { p_queue->p_cb->front = elements_left; } } }