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