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