All Projects → Bambofy → EmbeddedRingBuffer

Bambofy / EmbeddedRingBuffer

Licence: MIT license
A ring buffer designed to work with embedded devices, does not use heap allocations.

Programming Languages

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

Projects that are alternatives of or similar to EmbeddedRingBuffer

WebServer
High-performance multi-threaded tcp network server in c++11
Stars: ✭ 58 (+23.4%)
Mutual labels:  buffer
vivid.ex
Vivid is a simple 2D rendering library written in Elixir.
Stars: ✭ 27 (-42.55%)
Mutual labels:  buffer
typedarray-to-buffer
Convert a typed array to a Buffer without a copy.
Stars: ✭ 64 (+36.17%)
Mutual labels:  buffer
RingBuffer
模仿 kfifo 实现的环形缓冲区
Stars: ✭ 64 (+36.17%)
Mutual labels:  buffer
codec
Encode keys, values and range options, with built-in or custom encodings.
Stars: ✭ 27 (-42.55%)
Mutual labels:  buffer
zlib
Pure javascript implementation of Zlib nodejs core module.The zlib module provides compression functionality implemented using Gzip and Deflate/Inflate.
Stars: ✭ 14 (-70.21%)
Mutual labels:  buffer
NALib
General purpose C sourcecode collection
Stars: ✭ 16 (-65.96%)
Mutual labels:  buffer
JABS.nvim
Just Another Buffer Switcher for Neovim
Stars: ✭ 150 (+219.15%)
Mutual labels:  buffer
bipbuffer
a C implementation of Simon Cooke's bipbuffer
Stars: ✭ 15 (-68.09%)
Mutual labels:  buffer
as-string-sink
An efficient dynamically sized string buffer (aka String Builder) for AssemblyScript
Stars: ✭ 23 (-51.06%)
Mutual labels:  buffer
biguint-format
Node.js module to format big uint numbers from a byte array or a Buffer
Stars: ✭ 16 (-65.96%)
Mutual labels:  buffer
go-disk-buffer
This package helps to work with huge amount of data, which cannot be stored in RAM
Stars: ✭ 39 (-17.02%)
Mutual labels:  buffer
ZLToolKit
一个基于C++11的轻量级网络框架,基于线程池技术可以实现大并发网络IO
Stars: ✭ 1,302 (+2670.21%)
Mutual labels:  ringbuffer
ember-validated-form-buffer
A validated form buffer that wraps Ember Data models for use in forms.
Stars: ✭ 46 (-2.13%)
Mutual labels:  buffer
RingBuffer
Classic ringbuffer with optional Stream interface
Stars: ✭ 53 (+12.77%)
Mutual labels:  buffer
overflow
A command-line tool for exploiting stack-based buffer overflow vulnerabilities.
Stars: ✭ 66 (+40.43%)
Mutual labels:  buffer
node-pg-large-object
Large object support for PostgreSQL clients using the node-postgres library.
Stars: ✭ 31 (-34.04%)
Mutual labels:  buffer
CuVec
Unifying Python/C++/CUDA memory: Python buffered array ↔️ `std::vector` ↔️ CUDA managed memory
Stars: ✭ 73 (+55.32%)
Mutual labels:  buffer
sirdez
Glorious Binary Serialization and Deserialization for TypeScript.
Stars: ✭ 20 (-57.45%)
Mutual labels:  buffer
to-ico
Convert PNG to ICO in memory
Stars: ✭ 115 (+144.68%)
Mutual labels:  buffer

Embedded Ring Buffer

This is a header only ring buffer that is designed to work on embedded devices, it is able to handle non-blocking ISR spooling.

There are two objects, RingBuffer and Block. The block represents a set of items in the ringbuffer. You can write single items to the ring buffer and read many at once. It was designed to handle ISR routines and non-blocking devices therefore bytes can be wrote in an interrupt handler and read in large chunks to output to slower medium like SD cards.

It does not use malloc/free since the size is defined as a template parameter and is written in plain C++98 with no special features used.

Sponsors


How to use it.

Once a block has been used you must Skip() the buffer the number of reads you made, this is so that writing to the buffer does not corrupt the blocks data as it is being used.

Without ISR

int main()
{
    RingBuffer<100, int> buffer;
    Block<int> block;

    /* Write 100 ints */
    for (int i = 0; i < buffer.Length(); i++)
    {
        buffer.Append(i);
    }

    /* Read a block */
    block = buffer.Read(100);
    buffer.Skip(block.Length());

    /* Print out the block */
    for (int i = 0; i < block.Length(); i++)
    {
        std::cout << block.At(i) << std::endl;
    }

    /* Read another block */
    block = buffer.Read(1000);
    buffer.Skip(block.Length());

    /* Print out the block */
    for (int i = 0; i < block.Length(); i++)
    {
        std::cout << block.At(i) << std::endl;
    }


    return 0;
}

With ISR.

RingBuffer<100, int> buffer;
Block<int> block;
bool writing = false;

void SD_WriteCompleted()
{
    buffer.Skip(block.Length()); /* Manually skip the buffer to prevent corruption */
    
    writing = false;
}

void ISR()
{
    /* Perform an action like sample temperature sensor. */

    buffer.Append(0);
}

int main()
{
    while (true)
    {
        if (writing) continue;
        
        /* Read a block */
        block = buffer.Read(100, false); /* NOTE: here the second parameter disables the automatic skipping */

        writing = true;
        
        SD.write(block.Start(), block.Length()); /* non blocking mode */
    }


    return 0;
}

Please reference the doxygen comments for more details.

Issues

Blocks cannot span the 0th index of the buffer.

Since the ring buffer is just an array, to return a block that overlaps the 0th index would mean having 2 regions of the array to process, however the sd card writing (DMA) only works on single arrays at a time. This means that if you have a ringbuffer of 100 items at your reading from position 98, the maximum number of elements a block can return is 2. This may sound unrealistic but this was designed for SD cards that write extremely large blocks at a time (>30KB) using buffers that are large enough to store a considerable number of elements before requiring writing to the sd card. In practice the ring buffers write position will never exceed far from the read position since the SD card is emptying it constantly. You must calculate the timing of your input data and output data carefully for the buffer.

References

Alternatives

Todo

  • Test the buffer thoroughly to debug.
  • Benchmark it.
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].