Murata Type YD Wi-Fi driver
Dependents: easy-connect-type-yd
SNICInterface/SNIC/CBuffer.h@0:35a2186cf186, 2017-07-12 (annotated)
- Committer:
- MACRUM
- Date:
- Wed Jul 12 10:49:10 2017 +0000
- Revision:
- 0:35a2186cf186
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:35a2186cf186 | 1 | /* Copyright (C) 2012 mbed.org, MIT License |
MACRUM | 0:35a2186cf186 | 2 | * |
MACRUM | 0:35a2186cf186 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
MACRUM | 0:35a2186cf186 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
MACRUM | 0:35a2186cf186 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
MACRUM | 0:35a2186cf186 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
MACRUM | 0:35a2186cf186 | 7 | * furnished to do so, subject to the following conditions: |
MACRUM | 0:35a2186cf186 | 8 | * |
MACRUM | 0:35a2186cf186 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
MACRUM | 0:35a2186cf186 | 10 | * substantial portions of the Software. |
MACRUM | 0:35a2186cf186 | 11 | * |
MACRUM | 0:35a2186cf186 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
MACRUM | 0:35a2186cf186 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
MACRUM | 0:35a2186cf186 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
MACRUM | 0:35a2186cf186 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:35a2186cf186 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MACRUM | 0:35a2186cf186 | 17 | */ |
MACRUM | 0:35a2186cf186 | 18 | |
MACRUM | 0:35a2186cf186 | 19 | #ifndef CIRCBUFFER_H_ |
MACRUM | 0:35a2186cf186 | 20 | #define CIRCBUFFER_H_ |
MACRUM | 0:35a2186cf186 | 21 | |
MACRUM | 0:35a2186cf186 | 22 | template <class T> |
MACRUM | 0:35a2186cf186 | 23 | class CircBuffer { |
MACRUM | 0:35a2186cf186 | 24 | public: |
MACRUM | 0:35a2186cf186 | 25 | CircBuffer(int length) { |
MACRUM | 0:35a2186cf186 | 26 | write = 0; |
MACRUM | 0:35a2186cf186 | 27 | read = 0; |
MACRUM | 0:35a2186cf186 | 28 | size = length + 1; |
MACRUM | 0:35a2186cf186 | 29 | buf = (T *)malloc(size * sizeof(T)); |
MACRUM | 0:35a2186cf186 | 30 | }; |
MACRUM | 0:35a2186cf186 | 31 | |
MACRUM | 0:35a2186cf186 | 32 | bool isFull() { |
MACRUM | 0:35a2186cf186 | 33 | return (((write + 1) % size) == read); |
MACRUM | 0:35a2186cf186 | 34 | }; |
MACRUM | 0:35a2186cf186 | 35 | |
MACRUM | 0:35a2186cf186 | 36 | bool isEmpty() { |
MACRUM | 0:35a2186cf186 | 37 | return (read == write); |
MACRUM | 0:35a2186cf186 | 38 | }; |
MACRUM | 0:35a2186cf186 | 39 | |
MACRUM | 0:35a2186cf186 | 40 | void queue(T k) { |
MACRUM | 0:35a2186cf186 | 41 | |
MACRUM | 0:35a2186cf186 | 42 | if (isFull()) { |
MACRUM | 0:35a2186cf186 | 43 | read++; |
MACRUM | 0:35a2186cf186 | 44 | read %= size; |
MACRUM | 0:35a2186cf186 | 45 | } |
MACRUM | 0:35a2186cf186 | 46 | buf[write++] = k; |
MACRUM | 0:35a2186cf186 | 47 | write %= size; |
MACRUM | 0:35a2186cf186 | 48 | } |
MACRUM | 0:35a2186cf186 | 49 | |
MACRUM | 0:35a2186cf186 | 50 | void flush() { |
MACRUM | 0:35a2186cf186 | 51 | read = 0; |
MACRUM | 0:35a2186cf186 | 52 | write = 0; |
MACRUM | 0:35a2186cf186 | 53 | } |
MACRUM | 0:35a2186cf186 | 54 | |
MACRUM | 0:35a2186cf186 | 55 | |
MACRUM | 0:35a2186cf186 | 56 | uint32_t available() { |
MACRUM | 0:35a2186cf186 | 57 | return (write >= read) ? write - read : size - read + write; |
MACRUM | 0:35a2186cf186 | 58 | }; |
MACRUM | 0:35a2186cf186 | 59 | |
MACRUM | 0:35a2186cf186 | 60 | bool dequeue(T * c) { |
MACRUM | 0:35a2186cf186 | 61 | bool empty = isEmpty(); |
MACRUM | 0:35a2186cf186 | 62 | if (!empty) { |
MACRUM | 0:35a2186cf186 | 63 | *c = buf[read++]; |
MACRUM | 0:35a2186cf186 | 64 | read %= size; |
MACRUM | 0:35a2186cf186 | 65 | } |
MACRUM | 0:35a2186cf186 | 66 | return(!empty); |
MACRUM | 0:35a2186cf186 | 67 | }; |
MACRUM | 0:35a2186cf186 | 68 | |
MACRUM | 0:35a2186cf186 | 69 | private: |
MACRUM | 0:35a2186cf186 | 70 | volatile uint32_t write; |
MACRUM | 0:35a2186cf186 | 71 | volatile uint32_t read; |
MACRUM | 0:35a2186cf186 | 72 | uint32_t size; |
MACRUM | 0:35a2186cf186 | 73 | T * buf; |
MACRUM | 0:35a2186cf186 | 74 | }; |
MACRUM | 0:35a2186cf186 | 75 | |
MACRUM | 0:35a2186cf186 | 76 | #endif |
MACRUM | 0:35a2186cf186 | 77 |