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