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@36:c3cac3494e9b, 2018-04-29 (annotated)
- Committer:
- drechtmann3
- Date:
- Sun Apr 29 15:23:56 2018 +0000
- Revision:
- 36:c3cac3494e9b
- Parent:
- 35:99aa54a25a9e
- Child:
- 37:b146a0a0b894
Nothing
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: |
drechtmann3 | 35:99aa54a25a9e | 16 | // Argument: |
drechtmann3 | 35:99aa54a25a9e | 17 | // |
drechtmann3 | 35:99aa54a25a9e | 18 | /** |
drechtmann3 | 35:99aa54a25a9e | 19 | * @param size Size of the buffer. |
drechtmann3 | 36:c3cac3494e9b | 20 | * Assigns a circular buffer of the diven size. |
drechtmann3 | 35:99aa54a25a9e | 21 | */ |
Nurchu | 20:e068469ffb89 | 22 | CircularBuf(unsigned int size); |
Nurchu | 20:e068469ffb89 | 23 | ~CircularBuf(); |
drechtmann3 | 35:99aa54a25a9e | 24 | /** |
drechtmann3 | 35:99aa54a25a9e | 25 | Pushes data onto the buffer |
drechtmann3 | 35:99aa54a25a9e | 26 | (Adds data to the buffer) |
Nurchu | 14:4637a9f02919 | 27 | // Arguement: |
drechtmann3 | 35:99aa54a25a9e | 28 | * @param data The array of data to add to the buffer. |
drechtmann3 | 35:99aa54a25a9e | 29 | * @param size The size of the array "data" |
drechtmann3 | 36:c3cac3494e9b | 30 | * data, The array of data to add |
drechtmann3 | 36:c3cac3494e9b | 31 | * size, The amount of data in the array |
drechtmann3 | 36:c3cac3494e9b | 32 | * Return: |
drechtmann3 | 36:c3cac3494e9b | 33 | * Amount of data actually written |
drechtmann3 | 36:c3cac3494e9b | 34 | * |
drechtmann3 | 36:c3cac3494e9b | 35 | * Example Code: |
drechtmann3 | 36:c3cac3494e9b | 36 | * |
drechtmann3 | 36:c3cac3494e9b | 37 | * int dataToAdd[2]; |
drechtmann3 | 36:c3cac3494e9b | 38 | * dataToAdd[0] = 15; |
drechtmann3 | 36:c3cac3494e9b | 39 | * dataToAdd[1] = 23; |
drechtmann3 | 36:c3cac3494e9b | 40 | * buffer.push(dataToAdd, 2); |
drechtmann3 | 36:c3cac3494e9b | 41 | * |
drechtmann3 | 36:c3cac3494e9b | 42 | * buffer now contains [15, 23] |
drechtmann3 | 35:99aa54a25a9e | 43 | */ |
Nurchu | 17:604f9c4bd6d3 | 44 | unsigned int push(T* data, unsigned int size); |
Nurchu | 14:4637a9f02919 | 45 | |
drechtmann3 | 35:99aa54a25a9e | 46 | /** |
Nurchu | 14:4637a9f02919 | 47 | // Pops data from the buffer |
Nurchu | 14:4637a9f02919 | 48 | // Arguement: |
Nurchu | 14:4637a9f02919 | 49 | // data, The array of data popped |
Nurchu | 14:4637a9f02919 | 50 | // size, The amount of data to pop |
Nurchu | 14:4637a9f02919 | 51 | // Return: |
Nurchu | 14:4637a9f02919 | 52 | // Amount of data actually popped |
Nurchu | 20:e068469ffb89 | 53 | // |
Nurchu | 20:e068469ffb89 | 54 | // Example Code: |
Nurchu | 20:e068469ffb89 | 55 | // |
Nurchu | 20:e068469ffb89 | 56 | // int dataToRemove[2]; |
Nurchu | 20:e068469ffb89 | 57 | // buffer.pop(dataToRemove, 2); |
Nurchu | 20:e068469ffb89 | 58 | // |
Nurchu | 20:e068469ffb89 | 59 | // dataToRemove now contains the 2 oldest values that were in buffer |
drechtmann3 | 35:99aa54a25a9e | 60 | */ |
Nurchu | 14:4637a9f02919 | 61 | unsigned int pop(T* data, unsigned int size); |
Nurchu | 14:4637a9f02919 | 62 | |
Nurchu | 14:4637a9f02919 | 63 | // Amount of data in the buffer |
Nurchu | 14:4637a9f02919 | 64 | unsigned int size(); |
Nurchu | 14:4637a9f02919 | 65 | |
Nurchu | 14:4637a9f02919 | 66 | // Clears the buffer completely |
Nurchu | 14:4637a9f02919 | 67 | void clear(); |
Nurchu | 14:4637a9f02919 | 68 | |
Nurchu | 14:4637a9f02919 | 69 | T* _data; |
Nurchu | 17:604f9c4bd6d3 | 70 | // Size of the array |
Nurchu | 14:4637a9f02919 | 71 | unsigned int _size; |
Nurchu | 17:604f9c4bd6d3 | 72 | // Start pointer |
Nurchu | 17:604f9c4bd6d3 | 73 | unsigned int _head; |
Nurchu | 17:604f9c4bd6d3 | 74 | // End pointer |
Nurchu | 17:604f9c4bd6d3 | 75 | unsigned int _tail; |
Nurchu | 14:4637a9f02919 | 76 | }; |