GithubHelp home page GithubHelp logo

ximtech / ringbuffer Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 1.0 44 KB

A ring buffer is a data structure that uses a fixed-size buffer as if it were connected end-to-end

License: MIT License

CMake 1.25% C 98.75%
stm32 c embedded library ringbuffer

ringbuffer's Introduction

RingBuffer

tests codecov Codacy Badge

STM32 lightweight implementation of Ring buffer.
Simple data structure that implements the FIFO (First-In-First-Out) ordering. This simply means that the first item added to your queue is the first one out.
More info about, see wiki

Applications

  • SPI buffering data stream
  • Scheduling pool
  • Event pool

Features

  • Specifically designed for embedded applications
  • Single on init memory allocation, no memory leaks
  • Simple implementation
  • Constant time operations O1
  • Fast array cleanup

Trade-offs

  • No data structure enlargement after initialization

Add as CPM project dependency

How to add CPM to the project, check the link

CPMAddPackage(
        NAME RingBuffer
        GITHUB_REPOSITORY ximtech/RingBuffer
        GIT_TAG origin/main)

target_link_libraries(${PROJECT_NAME} RingBuffer)
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})
# For Clion STM32 plugin generated Cmake use 
target_link_libraries(${PROJECT_NAME}.elf RingBuffer)

Usage

Example of usage:

RingBuffer ringBuffer = getRingBufferInstance(4);
ringBufferAdd(ringBuffer, 1);
ringBufferAdd(ringBuffer, 2);
ringBufferAdd(ringBuffer, 3);

printf("Size: %d\n", getRingBufferSize(ringBuffer)); // 3
printf("Is not full: %s\n", isRingBufferNotFull(ringBuffer) ? "Yes" : "No"); // Yes
printf("Is empty: %s\n", isRingBufferEmpty(ringBuffer) ? "Yes" : "No"); // No
printf("Is not empty: %s\n", isRingBufferNotEmpty(ringBuffer) ? "Yes" : "No"); // Yes

while (isRingBufferNotEmpty(ringBuffer)) {
    printf("%d ", ringBufferGet(ringBuffer)); // 1 2 3
}
printf("\n");

printf("Is empty: %s\n", isRingBufferEmpty(ringBuffer) ? "Yes" : "No"); // Yes

// add more elements than buffer size
ringBufferAdd(ringBuffer, 1);
ringBufferAdd(ringBuffer, 2);
ringBufferAdd(ringBuffer, 3);
ringBufferAdd(ringBuffer, 4);
ringBufferAdd(ringBuffer, 5);

while (isRingBufferNotEmpty(ringBuffer)) {
    printf("%d ", ringBufferGet(ringBuffer)); // 2 3 4 5
}

ringBufferDelete(ringBuffer);

ringbuffer's People

Contributors

ximtech avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

pinkyparadise

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.