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