|
| 1 | +/* |
| 2 | + Copyright (c) 2020 Arduino. All right reserved. |
| 3 | +
|
| 4 | + This library is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either |
| 7 | + version 2.1 of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + This library is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + See the GNU Lesser General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with this library; if not, write to the Free Software |
| 16 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | +*/ |
| 18 | + |
| 19 | +#ifdef __cplusplus |
| 20 | + |
| 21 | +#ifndef _FASTER_SAFE_RING_BUFFER_ |
| 22 | +#define _FASTER_SAFE_RING_BUFFER_ |
| 23 | +#include "sync.h" |
| 24 | + |
| 25 | + |
| 26 | +// Note: Below is a modified version of the RingBuffer.h, that removes the |
| 27 | +// count member variable, such that the producer and consumer don't need to |
| 28 | +// modify the same members. Allows one or both of them to run without needing |
| 29 | +// to disable interrupts for reading and writing members. |
| 30 | + |
| 31 | +// Define constants and variables for buffering incoming serial data. We're |
| 32 | +// using a ring buffer (I think), in which head is the index of the location |
| 33 | +// to which to write the next incoming character and tail is the index of the |
| 34 | +// location from which to read. |
| 35 | +namespace arduino { |
| 36 | + |
| 37 | +#define SERIAL_BUFFER_SIZE 64 |
| 38 | + |
| 39 | +template <int N> |
| 40 | +class SaferRingBufferN |
| 41 | +{ |
| 42 | + public: |
| 43 | + uint8_t _aucBuffer[N] ; |
| 44 | + volatile int _iHead ; |
| 45 | + volatile int _iTail ; |
| 46 | + |
| 47 | + public: |
| 48 | + SaferRingBufferN( void ) ; |
| 49 | + void store_char( uint8_t c ) ; |
| 50 | + void clear(); |
| 51 | + int read_char(); |
| 52 | + int available(); |
| 53 | + int availableForStore(); |
| 54 | + int peek(); |
| 55 | + bool isFull(); |
| 56 | + |
| 57 | + private: |
| 58 | + int nextIndex(int index); |
| 59 | + inline bool isEmpty() const { return (_iHead == _iTail); } |
| 60 | +}; |
| 61 | + |
| 62 | +typedef SaferRingBufferN<SERIAL_BUFFER_SIZE> SaferRingBuffer; |
| 63 | + |
| 64 | + |
| 65 | +template <int N> |
| 66 | +SaferRingBufferN<N>::SaferRingBufferN( void ) |
| 67 | +{ |
| 68 | + memset( _aucBuffer, 0, N ) ; |
| 69 | + clear(); |
| 70 | +} |
| 71 | + |
| 72 | +template <int N> |
| 73 | +void SaferRingBufferN<N>::store_char( uint8_t c ) |
| 74 | +{ |
| 75 | + // if we should be storing the received character into the location |
| 76 | + // just before the tail (meaning that the head would advance to the |
| 77 | + // current location of the tail), we're about to overflow the buffer |
| 78 | + // and so we don't write the character or advance the head. |
| 79 | + int newhead = nextIndex(_iHead); |
| 80 | + if (newhead != _iTail) |
| 81 | + { |
| 82 | + _aucBuffer[_iHead] = c ; |
| 83 | + _iHead = newhead; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +template <int N> |
| 88 | +void SaferRingBufferN<N>::clear() |
| 89 | +{ |
| 90 | + _iHead = 0; |
| 91 | + _iTail = 0; |
| 92 | +} |
| 93 | + |
| 94 | +template <int N> |
| 95 | +int SaferRingBufferN<N>::read_char() |
| 96 | +{ |
| 97 | + if (_iHead == _iTail) |
| 98 | + return -1; |
| 99 | + |
| 100 | + uint8_t value = _aucBuffer[_iTail]; |
| 101 | + _iTail = nextIndex(_iTail); |
| 102 | + |
| 103 | + return value; |
| 104 | +} |
| 105 | + |
| 106 | +template <int N> |
| 107 | +int SaferRingBufferN<N>::available() |
| 108 | +{ |
| 109 | + // grab state of head and tail, to keep result consistent |
| 110 | + int head = _iHead; |
| 111 | + int tail = _iTail; |
| 112 | + |
| 113 | + if (head >= tail) |
| 114 | + return head - tail; |
| 115 | + return N + head - tail; |
| 116 | +} |
| 117 | + |
| 118 | +template <int N> |
| 119 | +int SaferRingBufferN<N>::availableForStore() |
| 120 | +{ |
| 121 | + // grab state of head and tail, to keep result consistent |
| 122 | + int head = _iHead; |
| 123 | + int tail = _iTail; |
| 124 | + if (head >= tail) return N - 1 - head + tail; |
| 125 | + return tail - head - 1; |
| 126 | +} |
| 127 | + |
| 128 | +template <int N> |
| 129 | +int SaferRingBufferN<N>::peek() |
| 130 | +{ |
| 131 | + if (isEmpty()) |
| 132 | + return -1; |
| 133 | + |
| 134 | + return _aucBuffer[_iTail]; |
| 135 | +} |
| 136 | + |
| 137 | +template <int N> |
| 138 | +int SaferRingBufferN<N>::nextIndex(int index) |
| 139 | +{ |
| 140 | + return (uint32_t)(index + 1) % N; |
| 141 | +} |
| 142 | + |
| 143 | +template <int N> |
| 144 | +bool SaferRingBufferN<N>::isFull() |
| 145 | +{ |
| 146 | + int newhead = nextIndex(_iHead); |
| 147 | + return (newhead == _iTail); |
| 148 | +} |
| 149 | + |
| 150 | +/////////////////////////////////// |
| 151 | + |
| 152 | +// Protect writes for potential multiple writers |
| 153 | +template <int N> |
| 154 | +class FasterSafeWriteRingBufferN : public SaferRingBufferN<N> |
| 155 | +{ |
| 156 | + public: |
| 157 | + void store_char( uint8_t c ) ; |
| 158 | +}; |
| 159 | + |
| 160 | +typedef FasterSafeWriteRingBufferN<SERIAL_BUFFER_SIZE> FasterSafeWriteRingBuffer; |
| 161 | + |
| 162 | + |
| 163 | +template <int N> |
| 164 | +void FasterSafeWriteRingBufferN<N>::store_char(uint8_t c) { |
| 165 | + synchronized { |
| 166 | + SaferRingBufferN<N>::store_char(c); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +template <int N> |
| 171 | +class FasterSafeReadRingBufferN : public SaferRingBufferN<N> |
| 172 | +{ |
| 173 | + public: |
| 174 | + int read_char(); |
| 175 | +}; |
| 176 | + |
| 177 | +typedef FasterSafeReadRingBufferN<SERIAL_BUFFER_SIZE> FasterSafeReadRingBuffer; |
| 178 | + |
| 179 | +template <int N> |
| 180 | +int FasterSafeReadRingBufferN<N>::read_char() { |
| 181 | + synchronized { |
| 182 | + return SaferRingBufferN<N>::read_char(); |
| 183 | + } |
| 184 | + |
| 185 | + // We should never reached this line because the synchronized {} block gets |
| 186 | + // executed at least once. However the compiler gets confused and prints a |
| 187 | + // warning about control reaching the end of a non-void function. This |
| 188 | + // silences that warning. |
| 189 | + return -1; |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +} |
| 194 | + |
| 195 | +#endif /* _SAFE_RING_BUFFER_ */ |
| 196 | +#endif /* __cplusplus */ |
0 commit comments