fix nrf51822 i2c & spi conflict
Dependencies: BLE_API eMPL_MPU6050 nRF51822
Fork of Seeed_Tiny_BLE_Flash by
mbed-src/api/CircularBuffer.h@5:b8c02645e6af, 2015-11-17 (annotated)
- Committer:
- yihui
- Date:
- Tue Nov 17 07:48:56 2015 +0000
- Revision:
- 5:b8c02645e6af
fix i2c & spi conflict
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 5:b8c02645e6af | 1 | /* mbed Microcontroller Library |
yihui | 5:b8c02645e6af | 2 | * Copyright (c) 2015 ARM Limited |
yihui | 5:b8c02645e6af | 3 | * |
yihui | 5:b8c02645e6af | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
yihui | 5:b8c02645e6af | 5 | * you may not use this file except in compliance with the License. |
yihui | 5:b8c02645e6af | 6 | * You may obtain a copy of the License at |
yihui | 5:b8c02645e6af | 7 | * |
yihui | 5:b8c02645e6af | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
yihui | 5:b8c02645e6af | 9 | * |
yihui | 5:b8c02645e6af | 10 | * Unless required by applicable law or agreed to in writing, software |
yihui | 5:b8c02645e6af | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
yihui | 5:b8c02645e6af | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
yihui | 5:b8c02645e6af | 13 | * See the License for the specific language governing permissions and |
yihui | 5:b8c02645e6af | 14 | * limitations under the License. |
yihui | 5:b8c02645e6af | 15 | */ |
yihui | 5:b8c02645e6af | 16 | #ifndef MBED_CIRCULARBUFFER_H |
yihui | 5:b8c02645e6af | 17 | #define MBED_CIRCULARBUFFER_H |
yihui | 5:b8c02645e6af | 18 | |
yihui | 5:b8c02645e6af | 19 | namespace mbed { |
yihui | 5:b8c02645e6af | 20 | |
yihui | 5:b8c02645e6af | 21 | /** Templated Circular buffer class |
yihui | 5:b8c02645e6af | 22 | */ |
yihui | 5:b8c02645e6af | 23 | template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
yihui | 5:b8c02645e6af | 24 | class CircularBuffer { |
yihui | 5:b8c02645e6af | 25 | public: |
yihui | 5:b8c02645e6af | 26 | CircularBuffer() : _head(0), _tail(0), _full(false) { |
yihui | 5:b8c02645e6af | 27 | } |
yihui | 5:b8c02645e6af | 28 | |
yihui | 5:b8c02645e6af | 29 | ~CircularBuffer() { |
yihui | 5:b8c02645e6af | 30 | } |
yihui | 5:b8c02645e6af | 31 | |
yihui | 5:b8c02645e6af | 32 | /** Push the transaction to the buffer. This overwrites the buffer if it's |
yihui | 5:b8c02645e6af | 33 | * full |
yihui | 5:b8c02645e6af | 34 | * |
yihui | 5:b8c02645e6af | 35 | * @param data Data to be pushed to the buffer |
yihui | 5:b8c02645e6af | 36 | */ |
yihui | 5:b8c02645e6af | 37 | void push(const T& data) { |
yihui | 5:b8c02645e6af | 38 | if (full()) { |
yihui | 5:b8c02645e6af | 39 | _tail++; |
yihui | 5:b8c02645e6af | 40 | _tail %= BufferSize; |
yihui | 5:b8c02645e6af | 41 | } |
yihui | 5:b8c02645e6af | 42 | _pool[_head++] = data; |
yihui | 5:b8c02645e6af | 43 | _head %= BufferSize; |
yihui | 5:b8c02645e6af | 44 | if (_head == _tail) { |
yihui | 5:b8c02645e6af | 45 | _full = true; |
yihui | 5:b8c02645e6af | 46 | } |
yihui | 5:b8c02645e6af | 47 | } |
yihui | 5:b8c02645e6af | 48 | |
yihui | 5:b8c02645e6af | 49 | /** Pop the transaction from the buffer |
yihui | 5:b8c02645e6af | 50 | * |
yihui | 5:b8c02645e6af | 51 | * @param data Data to be pushed to the buffer |
yihui | 5:b8c02645e6af | 52 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
yihui | 5:b8c02645e6af | 53 | */ |
yihui | 5:b8c02645e6af | 54 | bool pop(T& data) { |
yihui | 5:b8c02645e6af | 55 | if (!empty()) { |
yihui | 5:b8c02645e6af | 56 | data = _pool[_tail++]; |
yihui | 5:b8c02645e6af | 57 | _tail %= BufferSize; |
yihui | 5:b8c02645e6af | 58 | _full = false; |
yihui | 5:b8c02645e6af | 59 | return true; |
yihui | 5:b8c02645e6af | 60 | } |
yihui | 5:b8c02645e6af | 61 | return false; |
yihui | 5:b8c02645e6af | 62 | } |
yihui | 5:b8c02645e6af | 63 | |
yihui | 5:b8c02645e6af | 64 | /** Check if the buffer is empty |
yihui | 5:b8c02645e6af | 65 | * |
yihui | 5:b8c02645e6af | 66 | * @return True if the buffer is empty, false if not |
yihui | 5:b8c02645e6af | 67 | */ |
yihui | 5:b8c02645e6af | 68 | bool empty() { |
yihui | 5:b8c02645e6af | 69 | return (_head == _tail) && !_full; |
yihui | 5:b8c02645e6af | 70 | } |
yihui | 5:b8c02645e6af | 71 | |
yihui | 5:b8c02645e6af | 72 | /** Check if the buffer is full |
yihui | 5:b8c02645e6af | 73 | * |
yihui | 5:b8c02645e6af | 74 | * @return True if the buffer is full, false if not |
yihui | 5:b8c02645e6af | 75 | */ |
yihui | 5:b8c02645e6af | 76 | bool full() { |
yihui | 5:b8c02645e6af | 77 | return _full; |
yihui | 5:b8c02645e6af | 78 | } |
yihui | 5:b8c02645e6af | 79 | |
yihui | 5:b8c02645e6af | 80 | /** Reset the buffer |
yihui | 5:b8c02645e6af | 81 | * |
yihui | 5:b8c02645e6af | 82 | */ |
yihui | 5:b8c02645e6af | 83 | void reset() { |
yihui | 5:b8c02645e6af | 84 | _head = 0; |
yihui | 5:b8c02645e6af | 85 | _tail = 0; |
yihui | 5:b8c02645e6af | 86 | _full = false; |
yihui | 5:b8c02645e6af | 87 | } |
yihui | 5:b8c02645e6af | 88 | |
yihui | 5:b8c02645e6af | 89 | private: |
yihui | 5:b8c02645e6af | 90 | T _pool[BufferSize]; |
yihui | 5:b8c02645e6af | 91 | volatile CounterType _head; |
yihui | 5:b8c02645e6af | 92 | volatile CounterType _tail; |
yihui | 5:b8c02645e6af | 93 | volatile bool _full; |
yihui | 5:b8c02645e6af | 94 | }; |
yihui | 5:b8c02645e6af | 95 | |
yihui | 5:b8c02645e6af | 96 | } |
yihui | 5:b8c02645e6af | 97 | |
yihui | 5:b8c02645e6af | 98 | #endif |