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