Git Product home page Git Product logo

embedded_ringbuf_cpp's Introduction

Embedded RingBufCPP

This is a simple ring (FIFO) buffer queuing library for embedded platforms, such as Arduino's. This library is based on a previous one I wrote, only now in C++ instead of C. It uses C++'s templating, so no more weird pointer casting, and deep copies of objects are supported. It has concurrency protection built in, so feel free to do operations on the buffer inside of ISR's. All memory is statically allocated at compile time, so no heap memory is used. It can buffer any fixed size object (ints, floats, structs, objects, etc...).

FAQ's

I only have a C compiler for my platform
No worries, try the vanilla C version of the library.

Use Cases

A ring buffer is used when passing asynchronous io between two threads. In the case of the Arduino, it is very useful for buffering data in an interrupt routine that is later processed in your void loop().

Supported Platforms

The library currently supports:

  • AVR
  • ESP8266
  • Any other platform (just implement the RB_ATOMIC_START and RB_ATOMIC_END macros)

Install

This library is now available in the Arduino Library Manager, directly in the IDE. Go to Sketch > Include Library > Manage Libraries and search for RingBufCPP. Then #include <RingBufCPP.h> in your sketch.

To manually install this library, download this file as a zip, and extract the resulting folder into your Arduino Libraries folder. Installing an Arduino Library.

Examples

Look at the examples folder for several examples.

Contributing

If you find this Arduino library helpful, click the Star button, and you will make my day.

Feel free to improve this library. Fork it, make your changes, then submit a pull request!

API

Constructor

RingBufCPP<typename Type, size_t MaxElements>();

Creates a new RingBuf object that can buffer up to MaxElements of type Type.

Methods

add()

bool add(const Type &obj, bool overwrite=false)

Append an element to the buffer. If there is already MaxElements in the buffer, the oldest element will either be overwritten (when overwrite is true) or this add will have no effect (when overwrite is false). Return false on a full buffer.

peek()

Type *peek(uint16_t num);

Peek at the num'th element in the buffer. Returns a pointer to the location of the num'th element. If num is out of bounds or the num'th element is empty, a NULL pointer is returned. Note that this gives you direct memory access to the location of the num'th element in the buffer, allowing you to directly edit elements in the buffer. Note that while all of RingBuf's public methods are atomic (including this one), directly using the pointer returned from this method is not safe. If there is a possibility an interrupt could fire and remove/modify the item pointed to by the returned pointer, disable interrupts first with noInterrupts(), do whatever you need to do with the pointer, then you can re-enable interrupts by calling interrupts().

pull()

bool pull(Type *dest);

Pull the first element out of the buffer. The first element is copied into the location pointed to by dest. Returns false if the buffer is empty, otherwise returns true on success.

numElements()

size_t numElements();

Returns number of elements in the buffer.

isFull()

bool isFull();

Returns true if buffer is full, otherwise false.

isEmpty()

bool isEmpty();

Returns true if buffer is empty, false otherwise.

License

This library is open-source, and licensed under the MIT license. Do whatever you like with it, but contributions are appreciated.

embedded_ringbuf_cpp's People

Contributors

dasdgw avatar descampsa avatar kirberich avatar per1234 avatar scls19fr avatar slav-at-attachix avatar wizard97 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

embedded_ringbuf_cpp's Issues

Dev version is working on Arduino STM32

I've tested with a Mapple Mini clone (using the ArduinoSTM32 platform), seems to work as fine as it does on ATmega328.

I'm pretty sure you can at least experimental compatibility to these micros and platform ( ARDUINO_ARCH_STM32 ).

Not actually a ring buffer

So I was looking for a simple ring buffer to save me some time and I stumbled upon this project. My main issue is that the name is a misnomer: a ring buffer should be a fifo with a max size (good so far) but one that discards old elements if it's full. The current implementation is a simple FIFO, not an actual ring buffer.

Library not found in Arduino Library Manager

I can not find this library in the Arduino Library Manager, only the C RingBuffer library.
Not a problem for me, but you say in your readme that it is available in the Manager.

By the way, the fact that both C and C++ libraries have the same header filename make them impossible to install simultaneously. Maybe this is the cause of the problem, if the library manager detect the conflict?

Tag 1.2.0

@wizard97 could you please bump the master library version and tag it?
The add() API changed, but the change was not released.

volatile declaration

I think I may have found one issue here:

size_t _head;
size_t _numElements;

Do these variables need to be declared "volatile"? When I compiled a sketch without the volatile declaration, the isEmpty() member function always came back false.

Does this suggest _buf should be declared volatile as well?

Warning: invalid conversion from 'const CustomClass1*' to 'CustomClass1*' [-fpermissive]

I get this warning whenever use a class in the buffer. Things still work, not sure if it's something to look into. Code to reproduce the issue:

class CustomClass1
{
public:
	CustomClass1() {}
	uint8_t GetId() { return 0; }
};

RingBufCPP<CustomClass1, 10> TestCustomBuffer;

void DoSomethingWithBuffer()
{
    uint8_t Id = TestCustomBuffer.peek(0)->GetId();
}

Breaks inside RB_ATOMIC code block

The current RB_ATOMIC_* definitions for ESP8266 look like this

#define RB_ATOMIC_START do { uint32_t _savedIS = xt_rsil(15) ;
#define RB_ATOMIC_END xt_wsr_ps(_savedIS); } while(0);

If I have a code like this one:

do {

RB_ATOMIC_START 
{
     // .. do something
     if(condition) {
                break;
     }
     // .. do something
}
RB_ATOMIC_END

}
while(0)

The break inside the RB_ATOMIC code will be "silenced" and the code will misbehave. Isn't it better to have a definition without do { and } while(9) ?

Related to NicoHood/IRLremote#20

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.