Simple and lightweight FIFO\LIFO buffer library for the Arduino and ESP boards.
FIFObuf<TYPE> fifo(BUFFER_SIZE); // create FIFO buffer
LIFObuf<TYPE> lifo(BUFFER_SIZE); // create LIFO buffervoidpush(TYPE data); // Add data to bufferTYPEpop(); // Get and delete data from bufferTYPEat(unsignedint index); // Read data without deleting at index positionsize_tsize(); // Get elements quantity in the buffervoidclear(); // Clear buffer#include<Arduino.h>// include FIFObuf.h for FIFO buffer, or LIFObuf.h for LIFO buffer
#include<FIFObuf.h>
#include<LIFObuf.h>// define buffer size
#defineBUF_SIZE10// FIFO buffer for int
FIFObuf<int> int_fifo(BUF_SIZE);
// LIFO buffer for strings
LIFObuf<String> string_lifo(BUF_SIZE);
voidsetup()
{
Serial.begin(115200);
delay(300);
// LIFO example part// Add num to FIFO bufferfor (int i = 0; i < BUF_SIZE; ++i) {
int_fifo.push(i);
}
// If we try to add element to full buffer, it returns falsebool status = int_fifo.push(100);
Serial.printf("Try to push to full buffer. Push status: %d\n", status);
// Look element at index 4
Serial.printf("Elem value at forth position: %d\n", int_fifo.at(4));
// Pop elements from FIFO buffer
Serial.printf("Pops from FIFO int buffer:\n");
while (int_fifo.size() > 0) {
Serial.printf("%d ", int_fifo.pop());
}
Serial.printf("\n");
// If we try to pop element from empty buf, it returns 0int n = int_fifo.pop();
Serial.printf("Pop element from empty buffer. \nBuf size: %d Val: %d\n\n", int_fifo.size(), n);
// LIFO example part// Push strings to LIFO
string_lifo.push("LIFO");
string_lifo.push("Buffer");
string_lifo.push("Strings");
// Pop elements from LIFO buffer
Serial.printf("Pops from LIFO string buffer:\n");
while (string_lifo.size() > 0) {
Serial.printf("%s ", string_lifo.pop().c_str());
}
Serial.printf("\n");
// If we try to pop element from empty buf, it returns nothing
String s = string_lifo.pop().c_str();
s = string_lifo.pop().c_str();
Serial.printf("Pop element from empty buffer: \nBuf size: %d Val: %s\n", string_lifo.size(), s);
// Add some new value to buffer
status = string_lifo.push("NewElement");
Serial.printf("Push status: %d\n", status);
s = string_lifo.pop().c_str();
Serial.printf("Pop element from string buffer: \nBuf size: %d Val: %s\n", string_lifo.size(), s);
}
voidloop(){}