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