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