All Projects → Locoduino → RingBuffer

Locoduino / RingBuffer

Licence: GPL-2.0 license
A RingBuffer library for Arduino

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to RingBuffer

LiquidCrystal I2C
This is an Arduino library for HD44780 LCD display, operated in 4 bit mode over I2C bus with 8-bit I/O expander PCF8574
Stars: ✭ 20 (-41.18%)
Mutual labels:  arduino-library
Arduino-FrequencyDetector
Fast audio frequency detector without fft for plain Arduino and Attiny85. Whistle switch example included.
Stars: ✭ 22 (-35.29%)
Mutual labels:  arduino-library
hatrack
Fast, multi-reader, multi-writer, lockless data structures for parallel programming
Stars: ✭ 55 (+61.76%)
Mutual labels:  ring-buffer
FlexyStepper
Stepper motor control library for Arduino supporting in motion changes
Stars: ✭ 41 (+20.59%)
Mutual labels:  arduino-library
arduino-ble-gadget
Create your own Do-It-Yourself BLE enabled sensor gadget on the ESP32 platform.
Stars: ✭ 31 (-8.82%)
Mutual labels:  arduino-library
AutomationShield
Arduino library and MATLAB/Simulink API for the AutomationShield Arduino expansion boards for control engineering education.
Stars: ✭ 22 (-35.29%)
Mutual labels:  arduino-library
gfx demo
GFX Demo for Arduino and the ESP-IDF
Stars: ✭ 63 (+85.29%)
Mutual labels:  arduino-library
SparkFun MPU-9250 Breakout Arduino Library
Arduino sketch for MPU-9250 9DoF with AHRS sensor fusion
Stars: ✭ 68 (+100%)
Mutual labels:  arduino-library
Modbus-TCP
Modbus TCP client library to interact with Modbus servers such as PLCs.
Stars: ✭ 43 (+26.47%)
Mutual labels:  arduino-library
Pixie Chroma
Arduino library and documentation for Pixie Chroma displays!
Stars: ✭ 33 (-2.94%)
Mutual labels:  arduino-library
ArduinoGamepad
A GamePad HID library for Arduino Pro Micro/Leonardo (ATMega32u4)
Stars: ✭ 66 (+94.12%)
Mutual labels:  arduino-library
RingBuffer
Classic ringbuffer with optional Stream interface
Stars: ✭ 53 (+55.88%)
Mutual labels:  ring-buffer
DirectIO
Fast, simple I/O library for Arduino
Stars: ✭ 109 (+220.59%)
Mutual labels:  arduino-library
fifo-rs
A first-in-first-out for bytes, like kfifo in Linux.
Stars: ✭ 18 (-47.06%)
Mutual labels:  ring-buffer
arduino-pid-autotuner
Automated PID tuning using Ziegler-Nichols/relay method
Stars: ✭ 101 (+197.06%)
Mutual labels:  arduino-library
ESP32 I2C Slave
I2C slave library for ESP32
Stars: ✭ 70 (+105.88%)
Mutual labels:  arduino-library
SparkFun ADXL345 Arduino Library
Arduino Library for the ADXL345
Stars: ✭ 34 (+0%)
Mutual labels:  arduino-library
Arduino RT-Thread
RT-Thread library optimized for Arduino.
Stars: ✭ 38 (+11.76%)
Mutual labels:  arduino-library
kprobe-template
kprobes template
Stars: ✭ 30 (-11.76%)
Mutual labels:  ring-buffer
Loom
Arduino library for Internet of Things Rapid Prototyping in environmental sensing
Stars: ✭ 24 (-29.41%)
Mutual labels:  arduino-library

GitHub release GitHub commits Build Status

RingBuffer

A simple and easy to use ring buffer library for Arduino. Interrupt safe functions are provided too.

Changelog

  • 1.0.3 Changed the way templates are instanciated. Now, a size greater than 255 is allowed and leads to a uint16_t datatype used for size and index. In addition, wrong size are detected and a compilation error is emited. Added example BigBuffer with a size over 255.
  • 1.0.2 Changed the name of the template from RingBuffer to RingBuf in order to avoid a name conflict with and internal RingBuffer class used in the ARM version of the Arduino core.
  • 1.0.1 Fix a mistake in pop documentation
  • 1.0 Initial release.

Limitation

The size of the ring buffer is limited to 65535 elements. The compiler will emit an error is the buffer size is 0 or if the buffer size is greated than 65535.

Using the library

First include the header in your sketch

#include <RingBuf.h>

Instantiate a ring buffer by using the following syntax:

RingBuf<type, size> myRingBuffer;

type is the type name of each element of the ring buffer. size is the size, from 1 to 65535, of the ring buffer. For instance the declaration shown below instantiate a ring buffer where each element is a byte with a size of 20.

RingBuf<byte, 20> aBuffer;

Functions

The following functions are available to manage the ring buffer.

isEmpty()

isEmpty() returns a bool which is true if the buffer is empty and false otherwise.

isFull()

isFull() returns a bool which is true if the buffer is full and false otherwise.

maxSize()

maxSize() returns an uint8_t for buffers of size lower or equal to 255 and an uint16_t for buffers of size in the [256, 65535] interval. It is the value set when the ring buffer has been instantiated.

size()

size() returns an uint8_t for buffers of size lower or equal to 255 and an uint16_t for buffers of size in the [256, 65535] interval, which is the current size of the ring buffer. It is between 0 and maxSize().

clear()

clear() empties the ring buffer resetting its size to 0.

push(data)

push(data) pushes data at the end of the ring buffer if there is room available. data should be of the type declared when the ring buffer has been instanciated. If the data has been successfully added to the ring buffer true is returned and false otherwise. A second form exists where the argument is a pointer to the data. The use of this second form allows to reduce the number of copies. Using argument passing by value or pointer is left to your discretion

lockedPush(data)

lockedPush(data) works as push(data) except interrupts are disabled during the update of the ring buffer. You should use this function in your main program if the ring buffer is shared between the main program and an interrupt handler and data are pushed by the main program and popped by the interrupt handler.

pop(data)

If the buffer is not empty, pop(data) pops a data from the beginning of the ring buffer, puts it in data and return true. If the buffer is empty data is unchanged and false is returned.

lockedPop(data)

lockedPop(data) works as pop(data). In addition interrupts are disabled during the update of the ring buffer. You should use this function in your main program if the ring buffer is shared between the main program and an interrupt handler and data are pushed by the interrupt handler and popped by the main program.

operator[]

The standard array element access syntax allows for direct access of elements of the ring buffer. For instance, if a buffer is declared like that:

RingBuffer<uint32_t, 10> aBuffer;

one can write:

uint32_t v = aBuffer[3];

to get the element at index 3 in the buffer.

index 0 corresponds to the first element in the buffer and index size() - 1 to the last element.

If the index provided is greater than or equal to size() the element at index 0 is returned even if it does not exist. This avoids making an access outside the buffer. It is therefore up to you to verify that the index is valid.

Note: this operator is not interrupt safe. If you need to access a circular buffer in your main program while the buffer is being manipulated by an interrupt handler, it is up to you to inhibit and restore interrupts before and after access.

Some examples

Add an element with error handling

RingBuffer<uint8_t, 10> myBuffer;

void setup()
{
  if (! myBuffer.push(32)) {
    // oups error, push failed because the buffer is full
    someErrorProcessing();
  }
}

Process an element if available

RingBuffer<uint8_t, 10> myBuffer;

void setup()
{
  uint8_t data;
  if (myBuffer.pop(data)) {
    // pop put something in data
    Serial.println(data);
  }
}

Fill a buffer with values from 10 to 1 then print all the elements

#include <RingBuffer.h>

RingBuffer<uint8_t, 10> myBuffer;

void setup()
{
  uint8_t i = 10;
  while(myBuffer.push(i--));
  Serial.begin(115200);
  for (uint8_t j = 0; j < myBuffer.size(); j++) {
    Serial.println(myBuffer[j]);
  }
}

void loop()
{
}

Note about Travis CI

The RingBuffer Library examples are built on Travis CI for the following boards:

  • Arduino Leonardo
  • Arduino Uno
  • Arduino Mega 2560
  • Arduino Zero
  • Arduino Due
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].