A template for ring buffer implementation

Dependents:   AwsomeStation LoRaBaseStation LoRaTerminal

Committer:
rba90
Date:
Thu Sep 01 04:08:34 2016 +0000
Revision:
4:e996c0644148
Parent:
3:21ee07b29eb7
Child:
5:021f688bfdaa
remove debug text

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rba90 1:5f641cf190da 1 #include "RingBuffer.h"
rba90 3:21ee07b29eb7 2 #include <stdio.h>
rba90 3:21ee07b29eb7 3 #include <stdlib.h>
rba90 0:36b372831d9e 4
rba90 0:36b372831d9e 5 template <typename T>
rba90 3:21ee07b29eb7 6 CircularBuffer<T>::CircularBuffer(const size_t size)
rba90 0:36b372831d9e 7 :buffer_size(size)
rba90 0:36b372831d9e 8 {
rba90 0:36b372831d9e 9 read_ptr = 0;
rba90 0:36b372831d9e 10 write_ptr = 0;
rba90 0:36b372831d9e 11 count = 0;
rba90 0:36b372831d9e 12
rba90 0:36b372831d9e 13 // mutex lock
rba90 0:36b372831d9e 14 mux = false;
rba90 0:36b372831d9e 15
rba90 0:36b372831d9e 16 // overflow
rba90 0:36b372831d9e 17 is_over_flow = false;
rba90 0:36b372831d9e 18
rba90 0:36b372831d9e 19 // container
rba90 3:21ee07b29eb7 20 // data = new T[buffer_size];
rba90 3:21ee07b29eb7 21
rba90 3:21ee07b29eb7 22 // use c styled memory allocation instead
rba90 3:21ee07b29eb7 23 data = (T *)malloc(buffer_size * sizeof(T));
rba90 0:36b372831d9e 24 }
rba90 0:36b372831d9e 25
rba90 0:36b372831d9e 26 template <typename T>
rba90 0:36b372831d9e 27 CircularBuffer<T>::~CircularBuffer()
rba90 0:36b372831d9e 28 {
rba90 3:21ee07b29eb7 29 // delete[] data;
rba90 3:21ee07b29eb7 30 free(data);
rba90 0:36b372831d9e 31 }
rba90 0:36b372831d9e 32
rba90 0:36b372831d9e 33 template <typename T>
rba90 0:36b372831d9e 34 bool CircularBuffer<T>::isLocked()
rba90 0:36b372831d9e 35 {
rba90 0:36b372831d9e 36 return mux;
rba90 0:36b372831d9e 37 }
rba90 0:36b372831d9e 38
rba90 0:36b372831d9e 39 template <typename T>
rba90 0:36b372831d9e 40 void CircularBuffer<T>::lock()
rba90 0:36b372831d9e 41 {
rba90 0:36b372831d9e 42 mux = true;
rba90 0:36b372831d9e 43 }
rba90 0:36b372831d9e 44
rba90 0:36b372831d9e 45 template <typename T>
rba90 0:36b372831d9e 46 void CircularBuffer<T>::unlock()
rba90 0:36b372831d9e 47 {
rba90 0:36b372831d9e 48 mux = false;
rba90 0:36b372831d9e 49 }
rba90 0:36b372831d9e 50
rba90 0:36b372831d9e 51 template <typename T>
rba90 0:36b372831d9e 52 void CircularBuffer<T>::enqueue(T in)
rba90 0:36b372831d9e 53 {
rba90 0:36b372831d9e 54 data[write_ptr++] = in;
rba90 0:36b372831d9e 55 write_ptr %= buffer_size;
rba90 0:36b372831d9e 56
rba90 0:36b372831d9e 57 count++;
rba90 0:36b372831d9e 58 }
rba90 0:36b372831d9e 59
rba90 0:36b372831d9e 60 template <typename T>
rba90 0:36b372831d9e 61 T CircularBuffer<T>::dequeue()
rba90 0:36b372831d9e 62 {
rba90 0:36b372831d9e 63 T temp = data[read_ptr++];
rba90 0:36b372831d9e 64 read_ptr %= buffer_size;
rba90 0:36b372831d9e 65
rba90 0:36b372831d9e 66 count--;
rba90 0:36b372831d9e 67 return temp;
rba90 0:36b372831d9e 68 }
rba90 0:36b372831d9e 69
rba90 0:36b372831d9e 70 template <typename T>
rba90 3:21ee07b29eb7 71 size_t CircularBuffer<T>::getReadPtr()
rba90 0:36b372831d9e 72 {
rba90 0:36b372831d9e 73 return read_ptr;
rba90 0:36b372831d9e 74 }
rba90 0:36b372831d9e 75
rba90 0:36b372831d9e 76 template <typename T>
rba90 3:21ee07b29eb7 77 size_t CircularBuffer<T>::getWritePtr()
rba90 0:36b372831d9e 78 {
rba90 0:36b372831d9e 79 return write_ptr;
rba90 0:36b372831d9e 80 }
rba90 0:36b372831d9e 81
rba90 0:36b372831d9e 82 template <typename T>
rba90 3:21ee07b29eb7 83 size_t CircularBuffer<T>::getCounter()
rba90 0:36b372831d9e 84 {
rba90 0:36b372831d9e 85 return count;
rba90 0:36b372831d9e 86 }
rba90 0:36b372831d9e 87
rba90 0:36b372831d9e 88 template <typename T>
rba90 0:36b372831d9e 89 bool CircularBuffer<T>::getOverFlow()
rba90 0:36b372831d9e 90 {
rba90 0:36b372831d9e 91 return is_over_flow;
rba90 0:36b372831d9e 92 }
rba90 0:36b372831d9e 93
rba90 0:36b372831d9e 94 template <typename T>
rba90 0:36b372831d9e 95 void CircularBuffer<T>::clearOverFlow()
rba90 0:36b372831d9e 96 {
rba90 0:36b372831d9e 97 is_over_flow = false;
rba90 0:36b372831d9e 98 }
rba90 0:36b372831d9e 99
rba90 0:36b372831d9e 100 template <typename T>
rba90 0:36b372831d9e 101 T CircularBuffer<T>::first()
rba90 0:36b372831d9e 102 {
rba90 0:36b372831d9e 103 if (read_ptr > 0)
rba90 0:36b372831d9e 104 {
rba90 0:36b372831d9e 105 return data[read_ptr - 1];
rba90 0:36b372831d9e 106 }
rba90 0:36b372831d9e 107 else
rba90 0:36b372831d9e 108 {
rba90 0:36b372831d9e 109 return data[read_ptr];
rba90 0:36b372831d9e 110 }
rba90 0:36b372831d9e 111 }
rba90 0:36b372831d9e 112
rba90 0:36b372831d9e 113 template <typename T>
rba90 0:36b372831d9e 114 T CircularBuffer<T>::last()
rba90 0:36b372831d9e 115 {
rba90 0:36b372831d9e 116 if (write_ptr > 0)
rba90 0:36b372831d9e 117 {
rba90 0:36b372831d9e 118 return data[write_ptr - 1];
rba90 0:36b372831d9e 119 }
rba90 0:36b372831d9e 120 else
rba90 0:36b372831d9e 121 {
rba90 0:36b372831d9e 122 return data[write_ptr];
rba90 0:36b372831d9e 123 }
rba90 0:36b372831d9e 124 }
rba90 0:36b372831d9e 125
rba90 0:36b372831d9e 126 template <typename T>
rba90 3:21ee07b29eb7 127 T CircularBuffer<T>::operator[](size_t idx)
rba90 0:36b372831d9e 128 {
rba90 0:36b372831d9e 129 return data[idx];
rba90 0:36b372831d9e 130 }
rba90 0:36b372831d9e 131
rba90 0:36b372831d9e 132 // force compiler to create code for template
rba90 3:21ee07b29eb7 133 template class CircularBuffer<uint8_t>;