Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P
CircularBuf.h@29:0c6f3c0c992a, 2018-04-23 (annotated)
- Committer:
- Nurchu
- Date:
- Mon Apr 23 15:03:21 2018 +0000
- Revision:
- 29:0c6f3c0c992a
- Parent:
- 20:e068469ffb89
- Child:
- 35:99aa54a25a9e
Debugging CircularBuf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nurchu | 20:e068469ffb89 | 1 | #pragma once |
Nurchu | 20:e068469ffb89 | 2 | // Simple "queue" type data structure |
Nurchu | 20:e068469ffb89 | 3 | // Data is pushed onto the queue to add it to the FIFO buffer |
Nurchu | 20:e068469ffb89 | 4 | // It is then popped off the queue to remove it from the FIFO buffer |
Nurchu | 20:e068469ffb89 | 5 | // (It really should be enqueue and dequeue, but it's too late now) |
Nurchu | 20:e068469ffb89 | 6 | // |
Nurchu | 20:e068469ffb89 | 7 | // The circular buffer uses an array as the base and acts just like the name |
Nurchu | 20:e068469ffb89 | 8 | // suggests |
Nurchu | 20:e068469ffb89 | 9 | // You can push data on one side and remove from the other |
Nurchu | 20:e068469ffb89 | 10 | // As you add and remove data, the location of the data "moves" around the circle |
Nurchu | 20:e068469ffb89 | 11 | // https://en.wikipedia.org/wiki/Circular_buffer |
Nurchu | 20:e068469ffb89 | 12 | |
Nurchu | 20:e068469ffb89 | 13 | template <typename T> |
Nurchu | 20:e068469ffb89 | 14 | class CircularBuf { |
Nurchu | 14:4637a9f02919 | 15 | public: |
Nurchu | 16:d0d3bb2fc3ce | 16 | // Arguement: |
Nurchu | 16:d0d3bb2fc3ce | 17 | // size, The size of the underlying array to use |
Nurchu | 20:e068469ffb89 | 18 | CircularBuf(unsigned int size); |
Nurchu | 20:e068469ffb89 | 19 | ~CircularBuf(); |
Nurchu | 14:4637a9f02919 | 20 | |
Nurchu | 14:4637a9f02919 | 21 | // Pushes data onto the buffer |
Nurchu | 20:e068469ffb89 | 22 | // (Adds data to the buffer) |
Nurchu | 14:4637a9f02919 | 23 | // Arguement: |
Nurchu | 20:e068469ffb89 | 24 | // data, The array of data to add |
Nurchu | 14:4637a9f02919 | 25 | // size, The amount of data in the array |
Nurchu | 17:604f9c4bd6d3 | 26 | // Return: |
Nurchu | 17:604f9c4bd6d3 | 27 | // Amount of data actually written |
Nurchu | 20:e068469ffb89 | 28 | // |
Nurchu | 20:e068469ffb89 | 29 | // Example Code: |
Nurchu | 20:e068469ffb89 | 30 | // |
Nurchu | 20:e068469ffb89 | 31 | // int dataToAdd[2]; |
Nurchu | 20:e068469ffb89 | 32 | // dataToAdd[0] = 15; |
Nurchu | 20:e068469ffb89 | 33 | // dataToAdd[1] = 23; |
Nurchu | 20:e068469ffb89 | 34 | // buffer.push(dataToAdd, 2); |
Nurchu | 20:e068469ffb89 | 35 | // |
Nurchu | 20:e068469ffb89 | 36 | // buffer now contains [15, 23] |
Nurchu | 17:604f9c4bd6d3 | 37 | unsigned int push(T* data, unsigned int size); |
Nurchu | 14:4637a9f02919 | 38 | |
Nurchu | 14:4637a9f02919 | 39 | |
Nurchu | 14:4637a9f02919 | 40 | // Pops data from the buffer |
Nurchu | 14:4637a9f02919 | 41 | // Arguement: |
Nurchu | 14:4637a9f02919 | 42 | // data, The array of data popped |
Nurchu | 14:4637a9f02919 | 43 | // size, The amount of data to pop |
Nurchu | 14:4637a9f02919 | 44 | // Return: |
Nurchu | 14:4637a9f02919 | 45 | // Amount of data actually popped |
Nurchu | 20:e068469ffb89 | 46 | // |
Nurchu | 20:e068469ffb89 | 47 | // Example Code: |
Nurchu | 20:e068469ffb89 | 48 | // |
Nurchu | 20:e068469ffb89 | 49 | // int dataToRemove[2]; |
Nurchu | 20:e068469ffb89 | 50 | // buffer.pop(dataToRemove, 2); |
Nurchu | 20:e068469ffb89 | 51 | // |
Nurchu | 20:e068469ffb89 | 52 | // dataToRemove now contains the 2 oldest values that were in buffer |
Nurchu | 14:4637a9f02919 | 53 | unsigned int pop(T* data, unsigned int size); |
Nurchu | 14:4637a9f02919 | 54 | |
Nurchu | 14:4637a9f02919 | 55 | // Amount of data in the buffer |
Nurchu | 14:4637a9f02919 | 56 | unsigned int size(); |
Nurchu | 14:4637a9f02919 | 57 | |
Nurchu | 14:4637a9f02919 | 58 | // Clears the buffer completely |
Nurchu | 14:4637a9f02919 | 59 | void clear(); |
Nurchu | 14:4637a9f02919 | 60 | |
Nurchu | 14:4637a9f02919 | 61 | T* _data; |
Nurchu | 17:604f9c4bd6d3 | 62 | // Size of the array |
Nurchu | 14:4637a9f02919 | 63 | unsigned int _size; |
Nurchu | 17:604f9c4bd6d3 | 64 | // Start pointer |
Nurchu | 17:604f9c4bd6d3 | 65 | unsigned int _head; |
Nurchu | 17:604f9c4bd6d3 | 66 | // End pointer |
Nurchu | 17:604f9c4bd6d3 | 67 | unsigned int _tail; |
Nurchu | 14:4637a9f02919 | 68 | }; |