<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>read elements</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/57665/read-elements</link><description>Hi, 
 I&amp;#39;m using the nrf_queue library and I need to peek more than one element at a time. 
 How to do?</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 11 Feb 2020 10:53:01 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/57665/read-elements" /><item><title>RE: Peek no of elements at a time in nrf queue</title><link>https://devzone.nordicsemi.com/thread/233689?ContentTypeID=1</link><pubDate>Tue, 11 Feb 2020 10:53:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e0a0260c-605f-493b-8cd6-b6abec33d847</guid><dc:creator>Andy</dc:creator><description>&lt;p&gt;I requested a feature for this like a year ago. It&amp;#39;s very easy to implement, you just have to add a &amp;quot;peek&amp;quot; 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):&lt;br /&gt;&lt;br /&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**@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-&amp;gt;p_cb-&amp;gt;front;
    size_t back         = p_queue-&amp;gt;p_cb-&amp;gt;back;
    size_t continuous   = (front &amp;lt;= back) ? (back - front) : (p_queue-&amp;gt;size + 1 - front);
    void const * p_read_ptr = (void const *)((size_t)p_queue-&amp;gt;p_buffer
                                           + front * p_queue-&amp;gt;element_size);

    if (element_count &amp;lt;= continuous)
    {
        memcpy(p_data,
               p_read_ptr,
               element_count * p_queue-&amp;gt;element_size);

        if (!just_peek)
        {
			p_queue-&amp;gt;p_cb-&amp;gt;front = ((front + element_count) &amp;lt;= p_queue-&amp;gt;size)
								 ? (front + element_count)
								 : 0;
        }
    }
    else
    {
        size_t first_read_length = continuous * p_queue-&amp;gt;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-&amp;gt;p_buffer,
               elements_left * p_queue-&amp;gt;element_size);

        if (!just_peek)
        {
        	p_queue-&amp;gt;p_cb-&amp;gt;front = elements_left;
        }
    }
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>