test test test
snake-test/mbed/platform/CircularBuffer.h@0:e4c5e6ec922e, 2020-05-25 (annotated)
- Committer:
- mohamedmoawya
- Date:
- Mon May 25 19:06:11 2020 +0000
- Revision:
- 0:e4c5e6ec922e
snake game tteest
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mohamedmoawya | 0:e4c5e6ec922e | 1 | /* mbed Microcontroller Library |
mohamedmoawya | 0:e4c5e6ec922e | 2 | * Copyright (c) 2015 ARM Limited |
mohamedmoawya | 0:e4c5e6ec922e | 3 | * |
mohamedmoawya | 0:e4c5e6ec922e | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mohamedmoawya | 0:e4c5e6ec922e | 5 | * you may not use this file except in compliance with the License. |
mohamedmoawya | 0:e4c5e6ec922e | 6 | * You may obtain a copy of the License at |
mohamedmoawya | 0:e4c5e6ec922e | 7 | * |
mohamedmoawya | 0:e4c5e6ec922e | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mohamedmoawya | 0:e4c5e6ec922e | 9 | * |
mohamedmoawya | 0:e4c5e6ec922e | 10 | * Unless required by applicable law or agreed to in writing, software |
mohamedmoawya | 0:e4c5e6ec922e | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mohamedmoawya | 0:e4c5e6ec922e | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mohamedmoawya | 0:e4c5e6ec922e | 13 | * See the License for the specific language governing permissions and |
mohamedmoawya | 0:e4c5e6ec922e | 14 | * limitations under the License. |
mohamedmoawya | 0:e4c5e6ec922e | 15 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 16 | #ifndef MBED_CIRCULARBUFFER_H |
mohamedmoawya | 0:e4c5e6ec922e | 17 | #define MBED_CIRCULARBUFFER_H |
mohamedmoawya | 0:e4c5e6ec922e | 18 | |
mohamedmoawya | 0:e4c5e6ec922e | 19 | #include "platform/mbed_critical.h" |
mohamedmoawya | 0:e4c5e6ec922e | 20 | #include "platform/mbed_assert.h" |
mohamedmoawya | 0:e4c5e6ec922e | 21 | |
mohamedmoawya | 0:e4c5e6ec922e | 22 | namespace mbed { |
mohamedmoawya | 0:e4c5e6ec922e | 23 | |
mohamedmoawya | 0:e4c5e6ec922e | 24 | namespace internal { |
mohamedmoawya | 0:e4c5e6ec922e | 25 | /* Detect if CounterType of the Circular buffer is of unsigned type. */ |
mohamedmoawya | 0:e4c5e6ec922e | 26 | template<typename T> |
mohamedmoawya | 0:e4c5e6ec922e | 27 | struct is_unsigned { |
mohamedmoawya | 0:e4c5e6ec922e | 28 | static const bool value = false; |
mohamedmoawya | 0:e4c5e6ec922e | 29 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 30 | template<> |
mohamedmoawya | 0:e4c5e6ec922e | 31 | struct is_unsigned<unsigned char> { |
mohamedmoawya | 0:e4c5e6ec922e | 32 | static const bool value = true; |
mohamedmoawya | 0:e4c5e6ec922e | 33 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 34 | template<> |
mohamedmoawya | 0:e4c5e6ec922e | 35 | struct is_unsigned<unsigned short> { |
mohamedmoawya | 0:e4c5e6ec922e | 36 | static const bool value = true; |
mohamedmoawya | 0:e4c5e6ec922e | 37 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 38 | template<> |
mohamedmoawya | 0:e4c5e6ec922e | 39 | struct is_unsigned<unsigned int> { |
mohamedmoawya | 0:e4c5e6ec922e | 40 | static const bool value = true; |
mohamedmoawya | 0:e4c5e6ec922e | 41 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 42 | template<> |
mohamedmoawya | 0:e4c5e6ec922e | 43 | struct is_unsigned<unsigned long> { |
mohamedmoawya | 0:e4c5e6ec922e | 44 | static const bool value = true; |
mohamedmoawya | 0:e4c5e6ec922e | 45 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 46 | template<> |
mohamedmoawya | 0:e4c5e6ec922e | 47 | struct is_unsigned<unsigned long long> { |
mohamedmoawya | 0:e4c5e6ec922e | 48 | static const bool value = true; |
mohamedmoawya | 0:e4c5e6ec922e | 49 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 50 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 51 | |
mohamedmoawya | 0:e4c5e6ec922e | 52 | /** \addtogroup platform */ |
mohamedmoawya | 0:e4c5e6ec922e | 53 | /** @{*/ |
mohamedmoawya | 0:e4c5e6ec922e | 54 | /** |
mohamedmoawya | 0:e4c5e6ec922e | 55 | * \defgroup platform_CircularBuffer CircularBuffer functions |
mohamedmoawya | 0:e4c5e6ec922e | 56 | * @{ |
mohamedmoawya | 0:e4c5e6ec922e | 57 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 58 | |
mohamedmoawya | 0:e4c5e6ec922e | 59 | /** Templated Circular buffer class |
mohamedmoawya | 0:e4c5e6ec922e | 60 | * |
mohamedmoawya | 0:e4c5e6ec922e | 61 | * @note Synchronization level: Interrupt safe |
mohamedmoawya | 0:e4c5e6ec922e | 62 | * @note CounterType must be unsigned and consistent with BufferSize |
mohamedmoawya | 0:e4c5e6ec922e | 63 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 64 | template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
mohamedmoawya | 0:e4c5e6ec922e | 65 | class CircularBuffer { |
mohamedmoawya | 0:e4c5e6ec922e | 66 | public: |
mohamedmoawya | 0:e4c5e6ec922e | 67 | CircularBuffer() : _head(0), _tail(0), _full(false) |
mohamedmoawya | 0:e4c5e6ec922e | 68 | { |
mohamedmoawya | 0:e4c5e6ec922e | 69 | MBED_STATIC_ASSERT( |
mohamedmoawya | 0:e4c5e6ec922e | 70 | internal::is_unsigned<CounterType>::value, |
mohamedmoawya | 0:e4c5e6ec922e | 71 | "CounterType must be unsigned" |
mohamedmoawya | 0:e4c5e6ec922e | 72 | ); |
mohamedmoawya | 0:e4c5e6ec922e | 73 | |
mohamedmoawya | 0:e4c5e6ec922e | 74 | MBED_STATIC_ASSERT( |
mohamedmoawya | 0:e4c5e6ec922e | 75 | (sizeof(CounterType) >= sizeof(uint32_t)) || |
mohamedmoawya | 0:e4c5e6ec922e | 76 | (BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))), |
mohamedmoawya | 0:e4c5e6ec922e | 77 | "Invalid BufferSize for the CounterType" |
mohamedmoawya | 0:e4c5e6ec922e | 78 | ); |
mohamedmoawya | 0:e4c5e6ec922e | 79 | } |
mohamedmoawya | 0:e4c5e6ec922e | 80 | |
mohamedmoawya | 0:e4c5e6ec922e | 81 | ~CircularBuffer() |
mohamedmoawya | 0:e4c5e6ec922e | 82 | { |
mohamedmoawya | 0:e4c5e6ec922e | 83 | } |
mohamedmoawya | 0:e4c5e6ec922e | 84 | |
mohamedmoawya | 0:e4c5e6ec922e | 85 | /** Push the transaction to the buffer. This overwrites the buffer if it's |
mohamedmoawya | 0:e4c5e6ec922e | 86 | * full |
mohamedmoawya | 0:e4c5e6ec922e | 87 | * |
mohamedmoawya | 0:e4c5e6ec922e | 88 | * @param data Data to be pushed to the buffer |
mohamedmoawya | 0:e4c5e6ec922e | 89 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 90 | void push(const T &data) |
mohamedmoawya | 0:e4c5e6ec922e | 91 | { |
mohamedmoawya | 0:e4c5e6ec922e | 92 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 93 | if (full()) { |
mohamedmoawya | 0:e4c5e6ec922e | 94 | _tail++; |
mohamedmoawya | 0:e4c5e6ec922e | 95 | if (_tail == BufferSize) { |
mohamedmoawya | 0:e4c5e6ec922e | 96 | _tail = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 97 | } |
mohamedmoawya | 0:e4c5e6ec922e | 98 | } |
mohamedmoawya | 0:e4c5e6ec922e | 99 | _pool[_head++] = data; |
mohamedmoawya | 0:e4c5e6ec922e | 100 | if (_head == BufferSize) { |
mohamedmoawya | 0:e4c5e6ec922e | 101 | _head = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 102 | } |
mohamedmoawya | 0:e4c5e6ec922e | 103 | if (_head == _tail) { |
mohamedmoawya | 0:e4c5e6ec922e | 104 | _full = true; |
mohamedmoawya | 0:e4c5e6ec922e | 105 | } |
mohamedmoawya | 0:e4c5e6ec922e | 106 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 107 | } |
mohamedmoawya | 0:e4c5e6ec922e | 108 | |
mohamedmoawya | 0:e4c5e6ec922e | 109 | /** Pop the transaction from the buffer |
mohamedmoawya | 0:e4c5e6ec922e | 110 | * |
mohamedmoawya | 0:e4c5e6ec922e | 111 | * @param data Data to be popped from the buffer |
mohamedmoawya | 0:e4c5e6ec922e | 112 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
mohamedmoawya | 0:e4c5e6ec922e | 113 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 114 | bool pop(T &data) |
mohamedmoawya | 0:e4c5e6ec922e | 115 | { |
mohamedmoawya | 0:e4c5e6ec922e | 116 | bool data_popped = false; |
mohamedmoawya | 0:e4c5e6ec922e | 117 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 118 | if (!empty()) { |
mohamedmoawya | 0:e4c5e6ec922e | 119 | data = _pool[_tail++]; |
mohamedmoawya | 0:e4c5e6ec922e | 120 | if (_tail == BufferSize) { |
mohamedmoawya | 0:e4c5e6ec922e | 121 | _tail = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 122 | } |
mohamedmoawya | 0:e4c5e6ec922e | 123 | _full = false; |
mohamedmoawya | 0:e4c5e6ec922e | 124 | data_popped = true; |
mohamedmoawya | 0:e4c5e6ec922e | 125 | } |
mohamedmoawya | 0:e4c5e6ec922e | 126 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 127 | return data_popped; |
mohamedmoawya | 0:e4c5e6ec922e | 128 | } |
mohamedmoawya | 0:e4c5e6ec922e | 129 | |
mohamedmoawya | 0:e4c5e6ec922e | 130 | /** Check if the buffer is empty |
mohamedmoawya | 0:e4c5e6ec922e | 131 | * |
mohamedmoawya | 0:e4c5e6ec922e | 132 | * @return True if the buffer is empty, false if not |
mohamedmoawya | 0:e4c5e6ec922e | 133 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 134 | bool empty() const |
mohamedmoawya | 0:e4c5e6ec922e | 135 | { |
mohamedmoawya | 0:e4c5e6ec922e | 136 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 137 | bool is_empty = (_head == _tail) && !_full; |
mohamedmoawya | 0:e4c5e6ec922e | 138 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 139 | return is_empty; |
mohamedmoawya | 0:e4c5e6ec922e | 140 | } |
mohamedmoawya | 0:e4c5e6ec922e | 141 | |
mohamedmoawya | 0:e4c5e6ec922e | 142 | /** Check if the buffer is full |
mohamedmoawya | 0:e4c5e6ec922e | 143 | * |
mohamedmoawya | 0:e4c5e6ec922e | 144 | * @return True if the buffer is full, false if not |
mohamedmoawya | 0:e4c5e6ec922e | 145 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 146 | bool full() const |
mohamedmoawya | 0:e4c5e6ec922e | 147 | { |
mohamedmoawya | 0:e4c5e6ec922e | 148 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 149 | bool full = _full; |
mohamedmoawya | 0:e4c5e6ec922e | 150 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 151 | return full; |
mohamedmoawya | 0:e4c5e6ec922e | 152 | } |
mohamedmoawya | 0:e4c5e6ec922e | 153 | |
mohamedmoawya | 0:e4c5e6ec922e | 154 | /** Reset the buffer |
mohamedmoawya | 0:e4c5e6ec922e | 155 | * |
mohamedmoawya | 0:e4c5e6ec922e | 156 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 157 | void reset() |
mohamedmoawya | 0:e4c5e6ec922e | 158 | { |
mohamedmoawya | 0:e4c5e6ec922e | 159 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 160 | _head = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 161 | _tail = 0; |
mohamedmoawya | 0:e4c5e6ec922e | 162 | _full = false; |
mohamedmoawya | 0:e4c5e6ec922e | 163 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 164 | } |
mohamedmoawya | 0:e4c5e6ec922e | 165 | |
mohamedmoawya | 0:e4c5e6ec922e | 166 | /** Get the number of elements currently stored in the circular_buffer */ |
mohamedmoawya | 0:e4c5e6ec922e | 167 | CounterType size() const |
mohamedmoawya | 0:e4c5e6ec922e | 168 | { |
mohamedmoawya | 0:e4c5e6ec922e | 169 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 170 | CounterType elements; |
mohamedmoawya | 0:e4c5e6ec922e | 171 | if (!_full) { |
mohamedmoawya | 0:e4c5e6ec922e | 172 | if (_head < _tail) { |
mohamedmoawya | 0:e4c5e6ec922e | 173 | elements = BufferSize + _head - _tail; |
mohamedmoawya | 0:e4c5e6ec922e | 174 | } else { |
mohamedmoawya | 0:e4c5e6ec922e | 175 | elements = _head - _tail; |
mohamedmoawya | 0:e4c5e6ec922e | 176 | } |
mohamedmoawya | 0:e4c5e6ec922e | 177 | } else { |
mohamedmoawya | 0:e4c5e6ec922e | 178 | elements = BufferSize; |
mohamedmoawya | 0:e4c5e6ec922e | 179 | } |
mohamedmoawya | 0:e4c5e6ec922e | 180 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 181 | return elements; |
mohamedmoawya | 0:e4c5e6ec922e | 182 | } |
mohamedmoawya | 0:e4c5e6ec922e | 183 | |
mohamedmoawya | 0:e4c5e6ec922e | 184 | /** Peek into circular buffer without popping |
mohamedmoawya | 0:e4c5e6ec922e | 185 | * |
mohamedmoawya | 0:e4c5e6ec922e | 186 | * @param data Data to be peeked from the buffer |
mohamedmoawya | 0:e4c5e6ec922e | 187 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
mohamedmoawya | 0:e4c5e6ec922e | 188 | */ |
mohamedmoawya | 0:e4c5e6ec922e | 189 | bool peek(T &data) const |
mohamedmoawya | 0:e4c5e6ec922e | 190 | { |
mohamedmoawya | 0:e4c5e6ec922e | 191 | bool data_updated = false; |
mohamedmoawya | 0:e4c5e6ec922e | 192 | core_util_critical_section_enter(); |
mohamedmoawya | 0:e4c5e6ec922e | 193 | if (!empty()) { |
mohamedmoawya | 0:e4c5e6ec922e | 194 | data = _pool[_tail]; |
mohamedmoawya | 0:e4c5e6ec922e | 195 | data_updated = true; |
mohamedmoawya | 0:e4c5e6ec922e | 196 | } |
mohamedmoawya | 0:e4c5e6ec922e | 197 | core_util_critical_section_exit(); |
mohamedmoawya | 0:e4c5e6ec922e | 198 | return data_updated; |
mohamedmoawya | 0:e4c5e6ec922e | 199 | } |
mohamedmoawya | 0:e4c5e6ec922e | 200 | |
mohamedmoawya | 0:e4c5e6ec922e | 201 | private: |
mohamedmoawya | 0:e4c5e6ec922e | 202 | T _pool[BufferSize]; |
mohamedmoawya | 0:e4c5e6ec922e | 203 | CounterType _head; |
mohamedmoawya | 0:e4c5e6ec922e | 204 | CounterType _tail; |
mohamedmoawya | 0:e4c5e6ec922e | 205 | bool _full; |
mohamedmoawya | 0:e4c5e6ec922e | 206 | }; |
mohamedmoawya | 0:e4c5e6ec922e | 207 | |
mohamedmoawya | 0:e4c5e6ec922e | 208 | /**@}*/ |
mohamedmoawya | 0:e4c5e6ec922e | 209 | |
mohamedmoawya | 0:e4c5e6ec922e | 210 | /**@}*/ |
mohamedmoawya | 0:e4c5e6ec922e | 211 | |
mohamedmoawya | 0:e4c5e6ec922e | 212 | } |
mohamedmoawya | 0:e4c5e6ec922e | 213 | |
mohamedmoawya | 0:e4c5e6ec922e | 214 | #endif |