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.cpp
- Committer:
- Nurchu
- Date:
- 2018-04-23
- Revision:
- 31:39d04aedc3e5
- Parent:
- 29:0c6f3c0c992a
- Child:
- 32:c40d581f50af
File content as of revision 31:39d04aedc3e5:
#include "CircularBuf.h" #include "stdio.h" template <typename T> CircularBuf<T>::CircularBuf(unsigned int size) : _size(size), _head(size), _tail(size) { _data = (T*)malloc(sizeof(T) * size); } template <typename T> CircularBuf<T>::~CircularBuf() { free(_data); } template <typename T> unsigned int CircularBuf<T>::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 <typename T> unsigned int CircularBuf<T>::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 <typename T> unsigned int CircularBuf<T>::size() { int s = _head - _tail; // If buffer overlaps end if (s < 0) s += _size; return s; } template <typename T> void CircularBuf<T>::clear() { _head = 0; _tail = 0; }