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