Arduino 循环缓冲库
Please, before submitting a support request read carefully this README and check if an answer already exists among previously answered questions: do not abuse of the Github issue tracker.
CircularBuffer The library itself has an implicit memory consumption of about 0.5Kb: 580 bytes (max) of code and 8 bytes of memory, according to my calculations. That does not consider the space used to store the items themselves, obviously.
When declaring your buffer you should specify the data type it must handle and the buffer capacity: those two parameters will influence the memory consumed by the buffer.
#include
CircularBuffer bytes; // uses 538 bytes
CircularBuffer ints; // uses 638 bytes
CircularBuffer longs; // uses 838 bytes
CircularBuffer floats; // uses 988 bytes
CircularBuffer doubles; // uses 988 bytes
CircularBuffer chars; // uses 538 bytes
CircularBuffer pointers; // uses 638 bytesPlease note: the memory usage reported above includes the program memory used by the library code, the heap memory is much less and is comparable to an array of the same size and type of the buffer.
Let's start making things clear: the library doesn't support inserting data in the middle of the buffer.
You can add data to the buffer either before the first element via an unshift() operation or after the last element via a push() operation.
You can keep adding data beyond the buffer maximum capacity, but you'll lose the least significant information:
unshift() adds to the head, adding beyond capacity causes the element at tail to be overwritten and lostpush() adds to the tail, adding beyond capacity causes the element at head to be overwritten and lostBoth unshift() and push() return true if the addition didn't cause any information loss, false if an overwrite occurred:
CircularBuffer buffer; // buffer capacity is 5
// all of the following return true
buffer.unshift(1); // [1]
buffer.unshift(2); // [2,1]
buffer.unshift(3); // [3,2,1]
buffer.push(0); // [3,2,1,0]
buffer.push(5); // [3,2,1,0,5]
// buffer is now at full capacity, from now on any addition returns false
buffer.unshift(2); // [2,3,2,1,0] returns false
buffer.unshift(10); // [10,2,3,2,1] returns false
buffer.push(-5); // [2,3,2,1,-5] returns falseSimilarly to data addition, data retrieval can be performed at tail via a pop() operation or from head via a shift() operation: both cause the element being read to be removed from the buffer.
⚠ Reading data beyond the actual buffer size has an undefined behaviour and is user's responsibility to prevent such boundary violations using the additional operations listed in the next section. The library will behave differently depending on the data type and allocation method, but you can safely assume your program will crash if you don't watch your steps.
Non-destructive read operations are also available:
first() returns the element at headlast() returns the element at tail[] operator…isEmpty() returns true only if no data is stored in the bufferisFull() returns true if no data can be further added to the buffer without causing overwrites/data losssize() returns the number of elements currently stored in the buffer; it should be used in conjunction with the [] operator to avoid boundary violations: the first element index is always 0 (if buffer is not empty), the last element index is always size() - 1available() returns the number of elements that can be added before saturating the buffercapacity() returns the number of elements the buffer can store, for completeness only as it's user-defined and never changes REMOVED from 1.3.0 replaced by the read-only member variable capacityclear() resets the whole buffer to its initial state (pay attention though, if you had dynamically allocated objects in your buffer, memory used by such object is not released: iterate over the buffer contents and release objects accordingly to their allocation method)copyToArray(array) copies the contents of the buffer to a standard array array. The array must be large enough to hold all the elements currently in the buffer.copyToArray(conversionFn, array) copies the contents of the buffer to a standard array array executing a function on each element, usually a type conversion. The array must be large enough to hold all the elements currently in the buffer.Starting from version 1.3.0 the library is capable to automatically detecting which data type should be used for the index based on the buffer capacity:
65535 then your index is going to be an unsigned longunsigned int for buffers with a declared capacity greater than 255byte is going to sufficeIn addition, you can mix in the same code buffers with small index and buffers with normal index: previously this was not possible.
CircularBuffer optimizedBuffer; // reduced memory footprint, index type is uint8_t (a.k.a. byte)
CircularBuffer normalBuffer; // standard memory footprint, index type is unit16_t (a.k.a. unsigned int)
CircularBuffer hugeBuffer; // extended memory footprint, index type is unit32_t (a.k.a. unsigned long)To obtain the maximum advantage of the optimization above, anytime you need to refer to the buffer index you should use the most appropriate type: this can be easily achieved using the decltype specifier, like in the following example:
…Please note this does NOT make the library interrupt safe, but it does help its usage in interrupt driven firmwares.
Multiple examples are available in the examples folder of the library:
copyToArray() functions.If you use this library to store dynamically allocated objects, refrain from using the clear() method as that will not perform memory deallocation: you need to iterate over your buffer content and release memory accordingly to the allocation method used, either via delete (if you had used new) or free (in case of malloc):
while (!buffer.isEmpty()) {
// pick the correct one
delete buffer.pop();
free(buffer.pop());
}The very same applies for the pop() and shift() operations as any dynamically allocated object is only detached from the buffer, but the memory it uses is not automagically released (see the Object.ino example)
Record* record = new Record(millis(), sample); // a dynamically allocated object
buffer.push(record);
// somewhere else
if (!buffer.isEmpty()) {
Record* current = buffer.pop();
Serial.println(current.value());
delete current; // if you don't do this the object memory is lost!!!
}copyToArray(array) and copyToArray(array, convertFn).h header extension to .hppshift() and pop() operations misuse to mess up the buffer[] operatorabort() is AVR-specificMost of the major improvements below have been contributed by Erlkoenig90: thank you Niklas!
capacity() in favor of the constant instance attribute capacityEventLogging and Interrupts examplesCIRCULAT_BUFFER_XS macro switch in favor of automatic index type identificationUINT32_MAX)CIRCULAR_BUFFER_INT_SAFEmemset when clearingclear() functionpop() functionpop() and shift() implementationscapacity() functiondebug() function, disabled by pre-processor by default暂无开放 Issues,或尚未同步最近议题。