This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SD16 nRF52840 - Queue library

Hi everyone,

I am experimenting with the queue library 

typedef struct
{
  int16_t records[16];
} my_sensor_data_t;

my_sensor_data_t my_sensor_data;

NRF_QUEUE_DEF(my_sensor_data_t, m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW);

void my_sensors_buffer() {

  ret_code_t err_code;
  my_sensor_data.records[0] = 1;
  my_sensor_data.records[1] = 2;
  my_sensor_data.records[2] = 3;
  my_sensor_data.records[3] = 4;
  my_sensor_data.records[4] = 5;
  my_sensor_data.records[5] = 6;
  err_code = nrf_queue_push(&m_sensor_data_queue, &my_sensor_data); // buffer some data
  APP_ERROR_CHECK(err_code);


}

After pushing some data by calling nrf_queue_push how do I print the data stored in the buffer?

  • Hi,

    To get values out of the queue, you can use nrf_queue_pop(), which will remove the item from the queue and copy the contents to the location pointed to by _p_element. See Using the queue from the Queue library documentation for more information and examples how to use it and other API functions for interacting with the queue.

    Regards,
    Terje

  • Thank you for your prompt responce

    I am a bit confused. When I call nrf_queue_pop(), how do I define the lenght of data to pop from the queue? 

  • I tried to push and pop some data but it didn't work. When I pop data and read the buffer I read just zeros..

    int16_t records_push[16];
    int16_t records_pop[16];
    int16_t received_data[16];
    
    NRF_QUEUE_DEF(int16_t, m_sensor_data_queue, 200, NRF_QUEUE_MODE_OVERFLOW); // define a queue instance
    
    uint8_t records_size = sizeof(records_push) / sizeof(records_push[0]);
    
    void my_sensors_buffer() {
    
    //Populate the buffer
      ret_code_t err_code;
      for (uint8_t i = 0; i < records_size; i++) {
        records_push[i] = i;
      }
    
      err_code = nrf_queue_push(&m_sensor_data_queue, records_push); // queue the buffer
      APP_ERROR_CHECK(err_code);
    
      err_code = nrf_queue_pop(&m_sensor_data_queue, records_pop);
      APP_ERROR_CHECK(err_code);
    
      //memcpy(received_data, records_pop, records_size);
    
      for (uint8_t i = 0; i < records_size; i++) {
        NRF_LOG_INFO("Data_pop %d = %d",i, records_pop[i]);
      }
    }

  • Hi,

    What you actually push and pop is one element; the first value in the arrays, which has the value 0.

    You can use two approaches:

    Either use your old approach with the my_sensor_data_t, have a queue with elements of that type, and use pointers to that type for pushing and popping to/from the queue...

    ...or use your new approach with single elements in the queue, but then you have to manually loop through the records_push and records_pop arrays, and push/pop each element.

    The confusion is probably coming from the queue handling API using pointers, combined with the fact that c arrays are stored as pointers as well. This means a funciton taking a pointer as argument can also take an array as argument, but that is only because an array in essence is a pointer to its first element. The result of passing an array to such a function is that the function operates on the first element of the array only, and it only works because the full array and the first element both have the same address and are of compatible types.

    Regards,
    Terje

  • Hi ,


    Thank you for your assistance. I used the second approach to buffer data, but I have some questions regarding the functionality.
    My function so far:

    #define QUEUE_BUFFER_INTERVAL APP_TIMER_TICKS(1000) // timer for queuing data
    #define QUEUE_SIZE 20                           
    
    NRF_QUEUE_DEF(int16_t, m_sensor_data_queue, QUEUE_SIZE, NRF_QUEUE_MODE_OVERFLOW); // define a queue instance
    APP_TIMER_DEF(queue_buffer_timer_id);
    
    int16_t data_in[] = {1024, 3000, -10000, -18000, -10000, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 15000};
    int16_t data_out[20];
    uint8_t data_in_size = sizeof(data_in) / sizeof(data_in[0]);
    
    void my_sensors_buffer() {
      ret_code_t err_code;
    
      NRF_LOG_INFO("Queue some data");
      NRF_LOG_INFO("Byte size of array: %d", sizeof(data_in));
      NRF_LOG_INFO("Index size of array: %d", data_in_size);
    
      if (sizeof(data_in) > QUEUE_SIZE) {
        NRF_LOG_INFO("Warning: data input > queue size")
      }
    
      uint16_t queue_available_space = nrf_queue_available_get(&m_sensor_data_queue);
      NRF_LOG_INFO("Available queue space: %d", queue_available_space);
    
      for (uint16_t i = 0; i < data_in_size; i++) {
        err_code = nrf_queue_push(&m_sensor_data_queue, data_in + i); // queue the buffer
        APP_ERROR_CHECK(err_code);
        uint16_t queue_available_space = nrf_queue_available_get(&m_sensor_data_queue);
        NRF_LOG_INFO("Push[%d] - Available queue space: %d", i, queue_available_space);
      }
    
      for (uint16_t i = 0; i < data_in_size; i++) {
        err_code = nrf_queue_pop(&m_sensor_data_queue, data_out + i); // get data from the buffer
        APP_ERROR_CHECK(err_code);
      }
    
      for (uint16_t i = 0; i < data_in_size; i++) {
        NRF_LOG_INFO("Pop[%d] %d", i, *(data_out + i));
      }
    }

    1. What is the 3rd parameter in NRF_QUEUE_DEF() function (QUEUE_SIZE in my case)? Is this the number of queue entries? For example, in my case, the size of the queue is 20 x 2bytes = 40bytes?

    2. The nrf_queue_available_get() returns the available (unqueued) entries of the buffer right? When I run the code, the nrf_queue_available_get() returns a value reduced by one after each push call:

    <info> app: Queue some data
    <info> app: Byte size of array: 40
    <info> app: Index size of array: 20
    <info> app: Warning: data input > queue size
    <info> app: Available queue space: 20
    <info> app: Push[0] - Available queue space: 19
    <info> app: Push[1] - Available queue space: 18
    <info> app: Push[2] - Available queue space: 17
    <info> app: Push[3] - Available queue space: 16
    <info> app: Push[4] - Available queue space: 15
    <info> app: Push[5] - Available queue space: 14
    <info> app: Push[6] - Available queue space: 13
    <info> app: Push[7] - Available queue space: 12
    <info> app: Push[8] - Available queue space: 11
    <info> app: Push[9] - Available queue space: 10
    <info> app: Push[10] - Available queue space: 9
    <info> app: Push[11] - Available queue space: 8
    <info> app: Push[12] - Available queue space: 7
    <info> app: Push[13] - Available queue space: 6
    <info> app: Push[14] - Available queue space: 5
    <info> app: Push[15] - Available queue space: 4
    <info> app: Push[16] - Available queue space: 3
    <info> app: Push[17] - Available queue space: 2
    <info> app: Push[18] - Available queue space: 1
    <info> app: Push[19] - Available queue space: 0
    <info> app: Pop[0] 1024
    <info> app: Pop[1] 3000
    <info> app: Pop[2] -10000
    <info> app: Pop[3] -18000
    <info> app: Pop[4] -10000
    <info> app: Pop[5] 5
    <info> app: Pop[6] 6
    <info> app: Pop[7] 7
    <info> app: Pop[8] 8
    <info> app: Pop[9] 9
    <info> app: Pop[10] 10
    <info> app: Pop[11] 11
    <info> app: Pop[12] 12
    <info> app: Pop[13] 13
    <info> app: Pop[14] 14
    <info> app: Pop[15] 15
    <info> app: Pop[16] 16
    <info> app: Pop[17] 17
    <info> app: Pop[18] 18
    <info> app: Pop[19] 19
    

    3. I use the NRF_QUEUE_MODE_OVERFLOW mode. When the buffer is getting full, I expect to start overwriting starting from the 1st element of the queue. However, this does not happen. If I reduce the QUEUE_SIZE to 19 (instead of 20), maintaining the same payload, I got the error NRF_ERROR_NOT_FOUND

Related