iyaowu / microcontroller-circular-buffer Goto Github PK
View Code? Open in Web Editor NEWThis project forked from cmlburnett/microcontroller-circular-buffer
A circular buffer implementation for a microcontroller
This project forked from cmlburnett/microcontroller-circular-buffer
A circular buffer implementation for a microcontroller
Microcontroller Circular Buffer I looked everywhere for circular buffer implemenation suitable for a microcontroller. The key limitation is the absence of a malloc so the circular buffer implementation would have to work with a statically declared memory space. Additionally, it would have to work with bytes and nothing else. I wrote this from scratch for my needs. Included is cbufftest.c that does some simple test cases. You can also use it to see how to use it. Taken from cbufftest.c: --------------------------------------------------------------------- long ret; unsigned char buff[12]; unsigned char hey[21]; cbuff_t c; cbuff_init(&c, buff, 12); cbuff_enqueue(&c, (unsigned char*)"Hello", 5); ret = cbuff_dequeue1(&c); ret = cbuff_dequeue(&c, hey, 20); --------------------------------------------------------------------- Note that the size of the actual space declared need not be the same size initialized. You can certainly call cbuff_init with a size less than that of the space it manages. Really, there's nothing stopping you from doing the following: --------------------------------------------------------------------- unsigned char buff[20]; cbuff_t c, d; cbuff_init(&c, buff, 10); cbuff_init(&d, buff+10, 10); --------------------------------------------------------------------- A quick note about the philosophy behind the cbuff_t struct. typedef struct { unsigned char *buff; int start; int end; int cnt; int remain; int size; } cbuff_t; The absolute minimum that is needed to manage a circular buffer is the start and end index, but this leaves the ambiguity of full and empty. (See the Wikipedia article for further explanation and a nice set of SVG graphics I made to demonstrate the circular buffer.) So you usually then need a flag indicating that state. Instead of a flag, I opted to put in the counter directly. Additionally, I put in the remain counter to avoid repeatedly computing how many bytes are free. Absolutely this is redundant information but I see it as an extreme of the space-time tradeoff: I am fine giving up the few extra bytes of RAM to avoid repeated calculations. Finally, a note about the optimization I have done. This was originally written for the ARM7TDMI architecture for the LPC 2138 chip by NXP/Philips. I reviewed several iterations of objdump(1) trying to tweak some performance into the functions. This optimization may be unnecessary but it was fun doing it. Further more, it may totally flop optimally on anything but the ARM7TDMI and may not even be the most optimal.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.