Language: EN

libreria-arduino-circular-buffer

Arduino Circular Buffer Library

The Circular Buffer library implements a Circular Buffer in Arduino. The Circular Buffer class uses templates to allow it to work with different types (int, long, float, …).

For more information, see the entry Implementing a circular buffer in Arduino.

User Manual

Constructor

The Circular Buffer is instantiated through its constructor, which receives the buffer size as the only parameter.

CircularBuffer<int> circularBuffer(capacity);

Get elements

// Get element N
circularBuffer[N];

// Get the first element
circularBuffer.First();

// Get the first N elements
circularBuffer.First(numItems);

// Get the last element
circularBuffer.Last();

// Get the last N elements
circularBuffer.Last(numItems);

Add elements

// Add an element to the end of the buffer
circularBuffer.Add(T item);

// Insert an element at the beginning of the buffer
circularBuffer.Insert(T item);

Remove elements

// Remove the first element from the buffer
circularBuffer.RemoveFirst();

// Remove the last element from the buffer
circularBuffer.RemoveLast();

Extract elements

Get and remove an element, useful for implementing queues and stacks easily.

// Remove the first element, and return the previous value
circularBuffer.ExtractFirst();

// Remove the last element, and return the previous value
circularBuffer.ExtractLast();

Replace elements

// Replace the first element
circularBuffer.ReplaceFirst(item);

// Replace the last element
circularBuffer.ReplaceLast(item);

// Replace the element at the specified index
circularBuffer.ReplaceAt(index, item);

Conversion

// Return the buffer as an Array
circularBuffer.ToArray();

// Copy the buffer's contents to an Array
circularBuffer.CopyTo(items);

// Fill the buffer from the contents of an Array
circularBuffer.FromArray(items, numItems);

Examples

The Circular Buffer library includes the following examples to illustrate its use.

  • CircularBuffer: Example of use for integer variables.

Installation

  • Download the latest version from this link GitHub
  • Unzip the file
  • Copy to your libraries folder (usually My Documents\Arduino\libraries)
  • Restart the Arduino IDE

github-full