Experiment of serial command protocol

Dependencies:   RingBuffer SerialInterfaceProtocol duinotech_16x2_LCD mbed

You can edit this area

Committer:
rba90
Date:
Sat Jun 04 12:08:49 2016 +0000
Revision:
0:2ba6a9f316b6
init

Who changed what in which revision?

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