Library for Modtronix NZ32 STM32 boards, like the NZ32-SC151, NZ32-SB072, NZ32-SE411 and others

Committer:
modtronix
Date:
Sun Aug 30 09:29:08 2015 +1000
Revision:
4:43abdd8eda40
Child:
7:709130701ac7
Added buffer and debug files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 4:43abdd8eda40 1 /**
modtronix 4:43abdd8eda40 2 * File: mx_circular_buffer.h
modtronix 4:43abdd8eda40 3 *
modtronix 4:43abdd8eda40 4 * Author: Modtronix Engineering - www.modtronix.com
modtronix 4:43abdd8eda40 5 *
modtronix 4:43abdd8eda40 6 * Description:
modtronix 4:43abdd8eda40 7 *
modtronix 4:43abdd8eda40 8 * Software License Agreement:
modtronix 4:43abdd8eda40 9 * This software has been written or modified by Modtronix Engineering. The code
modtronix 4:43abdd8eda40 10 * may be modified and can be used free of charge for commercial and non commercial
modtronix 4:43abdd8eda40 11 * applications. If this is modified software, any license conditions from original
modtronix 4:43abdd8eda40 12 * software also apply. Any redistribution must include reference to 'Modtronix
modtronix 4:43abdd8eda40 13 * Engineering' and web link(www.modtronix.com) in the file header.
modtronix 4:43abdd8eda40 14 *
modtronix 4:43abdd8eda40 15 * THIS SOFTWARE IS PROVIDED IN AN 'AS IS' CONDITION. NO WARRANTIES, WHETHER EXPRESS,
modtronix 4:43abdd8eda40 16 * IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
modtronix 4:43abdd8eda40 17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE
modtronix 4:43abdd8eda40 18 * COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
modtronix 4:43abdd8eda40 19 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
modtronix 4:43abdd8eda40 20 */
modtronix 4:43abdd8eda40 21 #ifndef SRC_MX_CIRCULAR_BUFFER_H_
modtronix 4:43abdd8eda40 22 #define SRC_MX_CIRCULAR_BUFFER_H_
modtronix 4:43abdd8eda40 23
modtronix 4:43abdd8eda40 24 #include "mx_buffer_base.h"
modtronix 4:43abdd8eda40 25
modtronix 4:43abdd8eda40 26 /** Templated Circular buffer class
modtronix 4:43abdd8eda40 27 */
modtronix 4:43abdd8eda40 28 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
modtronix 4:43abdd8eda40 29 class MxCircularBuffer : public MxBuffer {
modtronix 4:43abdd8eda40 30 public:
modtronix 4:43abdd8eda40 31 MxCircularBuffer() : _head(0), _tail(0), _full(false) {
modtronix 4:43abdd8eda40 32 }
modtronix 4:43abdd8eda40 33
modtronix 4:43abdd8eda40 34 ~MxCircularBuffer() {
modtronix 4:43abdd8eda40 35 }
modtronix 4:43abdd8eda40 36
modtronix 4:43abdd8eda40 37
modtronix 4:43abdd8eda40 38 /** Adds an object to the buffer. If no space in buffer, this function does nothing.
modtronix 4:43abdd8eda40 39 * do anything!
modtronix 4:43abdd8eda40 40 * Use getFree() function prior to this function to ensure buffer has enough free space!
modtronix 4:43abdd8eda40 41 *
modtronix 4:43abdd8eda40 42 * @param data Data to be pushed to the buffer
modtronix 4:43abdd8eda40 43 *
modtronix 4:43abdd8eda40 44 * @return Returns true if character added to buffer, else false
modtronix 4:43abdd8eda40 45 */
modtronix 4:43abdd8eda40 46 bool put(const T& data) {
modtronix 4:43abdd8eda40 47 if (isFull()) {
modtronix 4:43abdd8eda40 48 return false;
modtronix 4:43abdd8eda40 49 }
modtronix 4:43abdd8eda40 50 _pool[_head++] = data;
modtronix 4:43abdd8eda40 51 _head %= BufferSize;
modtronix 4:43abdd8eda40 52 if (_head == _tail) {
modtronix 4:43abdd8eda40 53 _full = true;
modtronix 4:43abdd8eda40 54 }
modtronix 4:43abdd8eda40 55 return true;
modtronix 4:43abdd8eda40 56 }
modtronix 4:43abdd8eda40 57
modtronix 4:43abdd8eda40 58
modtronix 4:43abdd8eda40 59 /** Adds given array to the buffer. This function checks if buffer has enough space.
modtronix 4:43abdd8eda40 60 * If not enough space available, nothing is added, and this function returns 0.
modtronix 4:43abdd8eda40 61 *
modtronix 4:43abdd8eda40 62 * @param buf Source buffer containing array to add to buffer
modtronix 4:43abdd8eda40 63 * @param bufSize Size of array to add to buffer
modtronix 4:43abdd8eda40 64 *
modtronix 4:43abdd8eda40 65 * @return Returns true if character added to buffer, else false
modtronix 4:43abdd8eda40 66 */
modtronix 4:43abdd8eda40 67 bool putArray(uint8_t* buf, uint16_t bufSize) {
modtronix 4:43abdd8eda40 68 bool retVal = false;
modtronix 4:43abdd8eda40 69 int i;
modtronix 4:43abdd8eda40 70
modtronix 4:43abdd8eda40 71 if (getFree() < bufSize) {
modtronix 4:43abdd8eda40 72 return false;
modtronix 4:43abdd8eda40 73 }
modtronix 4:43abdd8eda40 74
modtronix 4:43abdd8eda40 75 for(i=0; i<bufSize; i++) {
modtronix 4:43abdd8eda40 76 retVal = retVal | put(buf[i]);
modtronix 4:43abdd8eda40 77 }
modtronix 4:43abdd8eda40 78 return retVal;
modtronix 4:43abdd8eda40 79 }
modtronix 4:43abdd8eda40 80
modtronix 4:43abdd8eda40 81
modtronix 4:43abdd8eda40 82 /** Gets and remove and object from the buffer. Ensure buffer is NOT empty before calling this function!
modtronix 4:43abdd8eda40 83 *
modtronix 4:43abdd8eda40 84 * @return Read data
modtronix 4:43abdd8eda40 85 */
modtronix 4:43abdd8eda40 86 T get() {
modtronix 4:43abdd8eda40 87 if (!isEmpty()) {
modtronix 4:43abdd8eda40 88 T retData;
modtronix 4:43abdd8eda40 89 retData = _pool[_tail++];
modtronix 4:43abdd8eda40 90 _tail %= BufferSize;
modtronix 4:43abdd8eda40 91 _full = false;
modtronix 4:43abdd8eda40 92 return retData;
modtronix 4:43abdd8eda40 93 }
modtronix 4:43abdd8eda40 94 return 0;
modtronix 4:43abdd8eda40 95 }
modtronix 4:43abdd8eda40 96
modtronix 4:43abdd8eda40 97
modtronix 4:43abdd8eda40 98 /** Gets and object from the buffer, but do NOT remove it. Ensure buffer is NOT empty before calling
modtronix 4:43abdd8eda40 99 * this function! If buffer is empty, will return an undefined value.
modtronix 4:43abdd8eda40 100 *
modtronix 4:43abdd8eda40 101 * @return Read data
modtronix 4:43abdd8eda40 102 */
modtronix 4:43abdd8eda40 103 T peek() {
modtronix 4:43abdd8eda40 104 return _pool[_tail];
modtronix 4:43abdd8eda40 105 }
modtronix 4:43abdd8eda40 106
modtronix 4:43abdd8eda40 107
modtronix 4:43abdd8eda40 108 /** Gets an object from the buffer at given offset, but do NOT remove it. Given offset is a value from
modtronix 4:43abdd8eda40 109 * 0 to n. Ensure buffer has as many objects as the offset requested! For example, if buffer has 5 objects
modtronix 4:43abdd8eda40 110 * available, given offset can be a value from 0 to 4.
modtronix 4:43abdd8eda40 111 *
modtronix 4:43abdd8eda40 112 * @param offset Offset of requested object. A value from 0-n, where (n+1) = available objects = getAvailable()
modtronix 4:43abdd8eda40 113 * @return Object at given offset
modtronix 4:43abdd8eda40 114 */
modtronix 4:43abdd8eda40 115 T peekAt(CounterType offset) {
modtronix 4:43abdd8eda40 116 return _pool[(offset+_tail)%BufferSize];
modtronix 4:43abdd8eda40 117 }
modtronix 4:43abdd8eda40 118
modtronix 4:43abdd8eda40 119
modtronix 4:43abdd8eda40 120 /** Gets the last object added to the buffer, but do NOT remove it.
modtronix 4:43abdd8eda40 121 *
modtronix 4:43abdd8eda40 122 * @return Object at given offset
modtronix 4:43abdd8eda40 123 */
modtronix 4:43abdd8eda40 124 T peekLastAdded() {
modtronix 4:43abdd8eda40 125 return _pool[(_head-1)%BufferSize];
modtronix 4:43abdd8eda40 126 }
modtronix 4:43abdd8eda40 127
modtronix 4:43abdd8eda40 128
modtronix 4:43abdd8eda40 129 /** Gets and object from the buffer. Returns true if OK, else false
modtronix 4:43abdd8eda40 130 *
modtronix 4:43abdd8eda40 131 * @param data Variable to put read data into
modtronix 4:43abdd8eda40 132 * @return True if the buffer is not empty and data contains a transaction, false otherwise
modtronix 4:43abdd8eda40 133 */
modtronix 4:43abdd8eda40 134 bool getAndCheck(T& data) {
modtronix 4:43abdd8eda40 135 if (!isEmpty()) {
modtronix 4:43abdd8eda40 136 data = _pool[_tail++];
modtronix 4:43abdd8eda40 137 _tail %= BufferSize;
modtronix 4:43abdd8eda40 138 _full = false;
modtronix 4:43abdd8eda40 139 return true;
modtronix 4:43abdd8eda40 140 }
modtronix 4:43abdd8eda40 141 return false;
modtronix 4:43abdd8eda40 142 }
modtronix 4:43abdd8eda40 143
modtronix 4:43abdd8eda40 144
modtronix 4:43abdd8eda40 145 /** Check if the buffer is empty
modtronix 4:43abdd8eda40 146 *
modtronix 4:43abdd8eda40 147 * @return True if the buffer is empty, false if not
modtronix 4:43abdd8eda40 148 */
modtronix 4:43abdd8eda40 149 bool isEmpty() {
modtronix 4:43abdd8eda40 150 return (_head == _tail) && !_full;
modtronix 4:43abdd8eda40 151 }
modtronix 4:43abdd8eda40 152
modtronix 4:43abdd8eda40 153
modtronix 4:43abdd8eda40 154 /** Check if the buffer is full
modtronix 4:43abdd8eda40 155 *
modtronix 4:43abdd8eda40 156 * @return True if the buffer is full, false if not
modtronix 4:43abdd8eda40 157 */
modtronix 4:43abdd8eda40 158 bool isFull() {
modtronix 4:43abdd8eda40 159 return _full;
modtronix 4:43abdd8eda40 160 }
modtronix 4:43abdd8eda40 161
modtronix 4:43abdd8eda40 162
modtronix 4:43abdd8eda40 163 /** Get number of available bytes in buffer
modtronix 4:43abdd8eda40 164 * @return Number of available bytes in buffer
modtronix 4:43abdd8eda40 165 */
modtronix 4:43abdd8eda40 166 CounterType getAvailable() {
modtronix 4:43abdd8eda40 167 CounterType avail;
modtronix 4:43abdd8eda40 168 if (isEmpty()) {
modtronix 4:43abdd8eda40 169 return 0;
modtronix 4:43abdd8eda40 170 }
modtronix 4:43abdd8eda40 171 avail = _head - _tail;
modtronix 4:43abdd8eda40 172 avail %= BufferSize;
modtronix 4:43abdd8eda40 173 return avail;
modtronix 4:43abdd8eda40 174 }
modtronix 4:43abdd8eda40 175
modtronix 4:43abdd8eda40 176
modtronix 4:43abdd8eda40 177 /** Get number of free bytes in buffer available for writing data to.
modtronix 4:43abdd8eda40 178 * @return Number of free bytes in buffer available for writing data to.
modtronix 4:43abdd8eda40 179 */
modtronix 4:43abdd8eda40 180 CounterType getFree() {
modtronix 4:43abdd8eda40 181 CounterType free;
modtronix 4:43abdd8eda40 182 //Full
modtronix 4:43abdd8eda40 183 if (_full==true) {
modtronix 4:43abdd8eda40 184 return 0;
modtronix 4:43abdd8eda40 185 }
modtronix 4:43abdd8eda40 186 //Empty
modtronix 4:43abdd8eda40 187 if(_head == _tail) {
modtronix 4:43abdd8eda40 188 return BufferSize;
modtronix 4:43abdd8eda40 189 }
modtronix 4:43abdd8eda40 190 free = _tail - _head;
modtronix 4:43abdd8eda40 191 free %= BufferSize;
modtronix 4:43abdd8eda40 192 return free;
modtronix 4:43abdd8eda40 193 }
modtronix 4:43abdd8eda40 194
modtronix 4:43abdd8eda40 195
modtronix 4:43abdd8eda40 196 /** Reset the buffer
modtronix 4:43abdd8eda40 197 */
modtronix 4:43abdd8eda40 198 void reset() {
modtronix 4:43abdd8eda40 199 _head = 0;
modtronix 4:43abdd8eda40 200 _tail = 0;
modtronix 4:43abdd8eda40 201 _full = false;
modtronix 4:43abdd8eda40 202 }
modtronix 4:43abdd8eda40 203
modtronix 4:43abdd8eda40 204
modtronix 4:43abdd8eda40 205
modtronix 4:43abdd8eda40 206 private:
modtronix 4:43abdd8eda40 207 T _pool[BufferSize];
modtronix 4:43abdd8eda40 208 volatile CounterType _head;
modtronix 4:43abdd8eda40 209 volatile CounterType _tail;
modtronix 4:43abdd8eda40 210 volatile bool _full;
modtronix 4:43abdd8eda40 211 };
modtronix 4:43abdd8eda40 212
modtronix 4:43abdd8eda40 213 #endif /* SRC_MX_CIRCULAR_BUFFER_H_ */