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
Diff: CircularBuf.cpp
- Revision:
- 17:604f9c4bd6d3
- Parent:
- 16:d0d3bb2fc3ce
- Child:
- 20:e068469ffb89
--- a/CircularBuf.cpp Fri Apr 20 15:23:53 2018 +0000 +++ b/CircularBuf.cpp Fri Apr 20 16:07:47 2018 +0000 @@ -1,10 +1,70 @@ #include "CircularBuf.h" template <class T> -CircularBuf::CircularBuf(unsigned int size) {} +CircularBuf::CircularBuf(unsigned int size) : _size(size) { + _data = new T[size]; +} + +template <class T> +CircularBuf::~CircularBuf() { + delete _data; +} + +template <class T> +unsigned int CircularBuf::push(T* data, unsigned int size) { + unsigned int cnt = 0; + + for (int i = 0; i < size; i++) { + unsigned int next = _head + 1; + + if (next >= _size) + next = 0; + + if (next == _tail) + return cnt; + + _data[next] = data[cnt]; + _head = next; + cnt++; + } + + return cnt; +} template <class T> -CircularBuf::~CircularBuf() {} +unsigned int CircularBuf::pop(T* data, unsigned int size) { + unsigned int cnt = 0; + + for (int i = 0; i < size; i++) { + unsigned int next = _tail + 1; + + if (next >= size) + next = 0; + + if (next == _head) + return cnt; + + data[cnt] = _data[next]; + _tail = next; + cnt++; + } + + return cnt; +} template <class T> -void CircularBuf::push() \ No newline at end of file +unsigned int CircularBuf::size() { + int s = _head - _tail; + + // If buffer overlaps end + if (s < 0) + s += _size; + + return s; +} + +template <class T> +void CircularBuf::clear() { + _head = 0; + _tail = 0; +} \ No newline at end of file