I am programming STM32 using STM32CubeIDE.
As you can see from the code I am writing, all elements of the "ADC_buffer" are replaced with the next position element, except the last one .. which will be filled/replaced with the new sample read from the ADC.
// Shift the buffer and store the last ADC data
for(int i = 0; i < BUFFER_SIZE-1; i++){
ADC_buffer[i] = ADC_buffer[i+1];
}
ADC_buffer[BUFFER_SIZE-1] = ADC_Value;
1) Is this the implementation of a circular buffer?
2) If the answer is yes, what are the differences between these three lines of code I wrote and what a circular buffer set by STM32CubeMX would do? (the image is only descriptive .. I'm not using DMA)
Is this the implementation of a circular buffer?
No. The key characteristic that makes a buffer "circular" is that, logically, it wraps around from the end back to the beginning. That is, if one is writing into the buffer and reaches the last position, one continues at the first position (supposing that position is free, which it can be). Similarly if one is reading. A sense is always maintained of the range of positions that contain valid data, which frequently does not start at index 0.
One of the key advantages of a circular buffer, that justifies the extra metadata and more complicated implementation, is that one never has to move the data within the buffer. That is, shifting the data down in the array, as your code does, is among the main things that a circular buffer avoids.