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