Circular buffer library

Dependents:   WizziDebug

Committer:
Jeej
Date:
Wed Apr 26 10:33:30 2017 +0000
Revision:
0:64d6dbdbc339
CBuffer as standalone library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jeej 0:64d6dbdbc339 1 #include "mbed.h"
Jeej 0:64d6dbdbc339 2 #include "WizziDebug.h"
Jeej 0:64d6dbdbc339 3
Jeej 0:64d6dbdbc339 4 #define LocalCircularBuffer CircularBuffer<T,BufferSize,CounterType>
Jeej 0:64d6dbdbc339 5
Jeej 0:64d6dbdbc339 6 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
Jeej 0:64d6dbdbc339 7 class CBuffer : private LocalCircularBuffer
Jeej 0:64d6dbdbc339 8 {
Jeej 0:64d6dbdbc339 9 public:
Jeej 0:64d6dbdbc339 10
Jeej 0:64d6dbdbc339 11 CBuffer() : _buffered_data_size(0) {}
Jeej 0:64d6dbdbc339 12
Jeej 0:64d6dbdbc339 13 ~CBuffer() {}
Jeej 0:64d6dbdbc339 14
Jeej 0:64d6dbdbc339 15 CounterType get_size(void)
Jeej 0:64d6dbdbc339 16 {
Jeej 0:64d6dbdbc339 17 return BufferSize;
Jeej 0:64d6dbdbc339 18 }
Jeej 0:64d6dbdbc339 19
Jeej 0:64d6dbdbc339 20 CounterType available_data(void)
Jeej 0:64d6dbdbc339 21 {
Jeej 0:64d6dbdbc339 22 return _buffered_data_size;
Jeej 0:64d6dbdbc339 23 }
Jeej 0:64d6dbdbc339 24
Jeej 0:64d6dbdbc339 25 CounterType remaining_space(void)
Jeej 0:64d6dbdbc339 26 {
Jeej 0:64d6dbdbc339 27 return BufferSize - _buffered_data_size;
Jeej 0:64d6dbdbc339 28 }
Jeej 0:64d6dbdbc339 29
Jeej 0:64d6dbdbc339 30 bool full(void)
Jeej 0:64d6dbdbc339 31 {
Jeej 0:64d6dbdbc339 32 return this->LocalCircularBuffer::full();
Jeej 0:64d6dbdbc339 33 }
Jeej 0:64d6dbdbc339 34
Jeej 0:64d6dbdbc339 35 bool empty(void)
Jeej 0:64d6dbdbc339 36 {
Jeej 0:64d6dbdbc339 37 return this->LocalCircularBuffer::empty();
Jeej 0:64d6dbdbc339 38 }
Jeej 0:64d6dbdbc339 39
Jeej 0:64d6dbdbc339 40 void reset(void)
Jeej 0:64d6dbdbc339 41 {
Jeej 0:64d6dbdbc339 42 _buffered_data_size = 0;
Jeej 0:64d6dbdbc339 43 this->LocalCircularBuffer::reset();
Jeej 0:64d6dbdbc339 44 }
Jeej 0:64d6dbdbc339 45
Jeej 0:64d6dbdbc339 46 T pop(void)
Jeej 0:64d6dbdbc339 47 {
Jeej 0:64d6dbdbc339 48 T data;
Jeej 0:64d6dbdbc339 49
Jeej 0:64d6dbdbc339 50 ASSERT(this->LocalCircularBuffer::pop(data), "CBuffer: No data in buffer to pop\r\n");
Jeej 0:64d6dbdbc339 51 _buffered_data_size--;
Jeej 0:64d6dbdbc339 52
Jeej 0:64d6dbdbc339 53 return data;
Jeej 0:64d6dbdbc339 54 }
Jeej 0:64d6dbdbc339 55
Jeej 0:64d6dbdbc339 56 void push(T data)
Jeej 0:64d6dbdbc339 57 {
Jeej 0:64d6dbdbc339 58 ASSERT(!this->LocalCircularBuffer::full(), "CBuffer: Buffer is full, can't push\r\n");
Jeej 0:64d6dbdbc339 59
Jeej 0:64d6dbdbc339 60 this->LocalCircularBuffer::push(data);
Jeej 0:64d6dbdbc339 61 _buffered_data_size++;
Jeej 0:64d6dbdbc339 62 }
Jeej 0:64d6dbdbc339 63
Jeej 0:64d6dbdbc339 64 void get(T* buf, CounterType size)
Jeej 0:64d6dbdbc339 65 {
Jeej 0:64d6dbdbc339 66 ASSERT(size, "CBuffer: Can't copy length 0\r\n");
Jeej 0:64d6dbdbc339 67 ASSERT(size <= _buffered_data_size, "CBuffer: Size too long (%d) for available data (%d)\r\n", size, _buffered_data_size);
Jeej 0:64d6dbdbc339 68
Jeej 0:64d6dbdbc339 69 CounterType len = 0;
Jeej 0:64d6dbdbc339 70
Jeej 0:64d6dbdbc339 71 while (len < size)
Jeej 0:64d6dbdbc339 72 {
Jeej 0:64d6dbdbc339 73 if (buf != NULL)
Jeej 0:64d6dbdbc339 74 {
Jeej 0:64d6dbdbc339 75 this->LocalCircularBuffer::pop(buf[len]);
Jeej 0:64d6dbdbc339 76 }
Jeej 0:64d6dbdbc339 77 else
Jeej 0:64d6dbdbc339 78 {
Jeej 0:64d6dbdbc339 79 // Pop data in dummy buffer
Jeej 0:64d6dbdbc339 80 T dummy;
Jeej 0:64d6dbdbc339 81 this->LocalCircularBuffer::pop(dummy);
Jeej 0:64d6dbdbc339 82 }
Jeej 0:64d6dbdbc339 83 len++;
Jeej 0:64d6dbdbc339 84 }
Jeej 0:64d6dbdbc339 85
Jeej 0:64d6dbdbc339 86 _buffered_data_size -= size;
Jeej 0:64d6dbdbc339 87 }
Jeej 0:64d6dbdbc339 88
Jeej 0:64d6dbdbc339 89 void add(T* buf, CounterType size)
Jeej 0:64d6dbdbc339 90 {
Jeej 0:64d6dbdbc339 91 ASSERT(size, "CBuffer: Can't add 0 bytes\r\n");
Jeej 0:64d6dbdbc339 92 ASSERT(size <= this->remaining_space(), "CBuffer: Size too long (%d) for available space (%d)\r\n", size, BufferSize - _buffered_data_size);
Jeej 0:64d6dbdbc339 93 ASSERT(buf != NULL, "CBuffer: Can't copy from NULL buffer\r\n");
Jeej 0:64d6dbdbc339 94
Jeej 0:64d6dbdbc339 95 CounterType len = 0;
Jeej 0:64d6dbdbc339 96
Jeej 0:64d6dbdbc339 97 while (len < size)
Jeej 0:64d6dbdbc339 98 {
Jeej 0:64d6dbdbc339 99 this->LocalCircularBuffer::push(buf[len++]);
Jeej 0:64d6dbdbc339 100 }
Jeej 0:64d6dbdbc339 101
Jeej 0:64d6dbdbc339 102 _buffered_data_size += size;
Jeej 0:64d6dbdbc339 103 }
Jeej 0:64d6dbdbc339 104
Jeej 0:64d6dbdbc339 105 private:
Jeej 0:64d6dbdbc339 106 volatile CounterType _buffered_data_size;
Jeej 0:64d6dbdbc339 107 };