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