Circular_Buffer

yes, the queue itself worked, the return was wrong by one offset. essentially when deleting items from front or back, the head moves forward and tail moves backward, respectively. even though it does, the data is still there until it is overwritten. just the reading offset was off an indice

I figured it would be the indice not incrementing/decrementing properly.

I have noticed that when you want to pop old data out you can do it even if the buffer is empty. It's important to check if the buffer is empty before popping otherwise you risk getting data that is not relevant anymore.
 
wouldn't common sense not need a performance hit? i could sprinkle conditions all over but thats extra cpu :) size() and available() are there for reference
 
Last edited:
Not sure why this is not working for me, but can't figure it out.

Code:
#include "circular_buffer.h"
Circular_Buffer<uint16_t, 10> cb;

void setup() {
  Serial.begin(1);
  delay(2000);
  Serial.println("----------------------");

  cb.write(10);
  cb.list();
  cb.write(20);
  cb.list();
  cb.write(30);
  cb.list();
 
  int val1 = cb.pop_front();
  Serial.print("val1 = "); Serial.println(val1);
  int val2 = cb.pop_front();
  Serial.print("val2 = ");Serial.println(val2);
  int val3 = cb.pop_front();
  Serial.print("val3 = ");Serial.println(val3);
  cb.list();
 
}

void loop() {

while(1);
}

gets me this, so my value of 10 is replaced with 30?

Code:
----------------------

Circular Ring Buffer Queue Size: 1 / 10

Indice:  	[0]      	
Entries:	10		


Circular Ring Buffer Queue Size: 2 / 10

Indice:  	[0]      	[1]      	
Entries:	10		20		


Circular Ring Buffer Queue Size: 3 / 10

Indice:  	[0]      	[1]      	[0]      	
Entries:	30		20		30		

val1 = 30
val2 = 20
val3 = 30
There are no queues available...
 
What is the difference between the mean and average functions?

I need a running window giving me the average of the values currently inside the window. If I empty the buffer and start filling with new values again it should give me the correct average even if there is only one value inside. Will this work? Which should I use? Average or mean?
 
both are the same, they call same function, so it's user preference really. You can continue writing to the buffer it will overwrite older values when full, no need to empty it and wait for it to fill up. this way you can have a consistant average read over a period of time

Code:
T mean() { return average(); }
 
What's the best way to copy the last N values from the circular buffer into an array? Is there a built in function or just do a for loop and do a "peek()" for every element?

Edit: I think this method is what I'm looking for but I don't want to remove it, just copy it:
Code:
T read(T *buffer, uint16_t length) { return readBytes(buffer,length); } // read the front of the queue into a buffer and remove it
 
Back
Top