This library provides variable length ring buffering with software FIFO(First-in, First-out). This library overwrites when the buffer is full.
Dependents: AsyncSerial AsyncSerial AsyncSerial AsyncSerialh
FIFO.cpp@0:f3bdfd31af88, 2017-03-29 (annotated)
- Committer:
- babylonica
- Date:
- Wed Mar 29 16:08:55 2017 +0000
- Revision:
- 0:f3bdfd31af88
- Child:
- 1:fe6b5355c973
Add template.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
babylonica | 0:f3bdfd31af88 | 1 | // -*- coding: utf-8 -*- |
babylonica | 0:f3bdfd31af88 | 2 | /** |
babylonica | 0:f3bdfd31af88 | 3 | @file FIFO.cpp |
babylonica | 0:f3bdfd31af88 | 4 | @brief 1Byte Software FIFO(First-in, First-out) |
babylonica | 0:f3bdfd31af88 | 5 | |
babylonica | 0:f3bdfd31af88 | 6 | This library provides variable length ring buffering with software FIFO(First-in, First-out). |
babylonica | 0:f3bdfd31af88 | 7 | This library DO NOT overwrite when the buffer is full. |
babylonica | 0:f3bdfd31af88 | 8 | |
babylonica | 0:f3bdfd31af88 | 9 | @author T.Kawamura |
babylonica | 0:f3bdfd31af88 | 10 | @version 2.1 |
babylonica | 0:f3bdfd31af88 | 11 | @date 2016-11-20 T.Kawamura Written for AVR. |
babylonica | 0:f3bdfd31af88 | 12 | @date 2017-03-28 T.Kawamura Fork for mbed/C++. |
babylonica | 0:f3bdfd31af88 | 13 | @date 2017-03-29 T.Kawamura Add template. |
babylonica | 0:f3bdfd31af88 | 14 | |
babylonica | 0:f3bdfd31af88 | 15 | @see |
babylonica | 0:f3bdfd31af88 | 16 | Copyright (C) 2016-2017 T.Kawamura. |
babylonica | 0:f3bdfd31af88 | 17 | Released under the MIT license. |
babylonica | 0:f3bdfd31af88 | 18 | http://opensource.org/licenses/mit-license.php |
babylonica | 0:f3bdfd31af88 | 19 | |
babylonica | 0:f3bdfd31af88 | 20 | */ |
babylonica | 0:f3bdfd31af88 | 21 | |
babylonica | 0:f3bdfd31af88 | 22 | #include <stdint.h> |
babylonica | 0:f3bdfd31af88 | 23 | #include "mbed.h" // for cmsis |
babylonica | 0:f3bdfd31af88 | 24 | #include "FIFO.hpp" |
babylonica | 0:f3bdfd31af88 | 25 | |
babylonica | 0:f3bdfd31af88 | 26 | template <class T> |
babylonica | 0:f3bdfd31af88 | 27 | FIFO<T>::FIFO(uint32_t bufsize){ |
babylonica | 0:f3bdfd31af88 | 28 | buffer = new T[bufsize]; |
babylonica | 0:f3bdfd31af88 | 29 | size = bufsize; |
babylonica | 0:f3bdfd31af88 | 30 | getindex = 0; |
babylonica | 0:f3bdfd31af88 | 31 | putindex = 0; |
babylonica | 0:f3bdfd31af88 | 32 | count = 0; |
babylonica | 0:f3bdfd31af88 | 33 | return; |
babylonica | 0:f3bdfd31af88 | 34 | } |
babylonica | 0:f3bdfd31af88 | 35 | |
babylonica | 0:f3bdfd31af88 | 36 | template <class T> |
babylonica | 0:f3bdfd31af88 | 37 | FIFO<T>::~FIFO(void){ |
babylonica | 0:f3bdfd31af88 | 38 | delete[] buffer; |
babylonica | 0:f3bdfd31af88 | 39 | return; |
babylonica | 0:f3bdfd31af88 | 40 | } |
babylonica | 0:f3bdfd31af88 | 41 | |
babylonica | 0:f3bdfd31af88 | 42 | template <class T> |
babylonica | 0:f3bdfd31af88 | 43 | void FIFO<T>::clear(void){ |
babylonica | 0:f3bdfd31af88 | 44 | DISABLE_INTERRUPTS; |
babylonica | 0:f3bdfd31af88 | 45 | |
babylonica | 0:f3bdfd31af88 | 46 | count = 0; |
babylonica | 0:f3bdfd31af88 | 47 | getindex = 0; |
babylonica | 0:f3bdfd31af88 | 48 | putindex = 0; |
babylonica | 0:f3bdfd31af88 | 49 | |
babylonica | 0:f3bdfd31af88 | 50 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 51 | |
babylonica | 0:f3bdfd31af88 | 52 | return; |
babylonica | 0:f3bdfd31af88 | 53 | } |
babylonica | 0:f3bdfd31af88 | 54 | |
babylonica | 0:f3bdfd31af88 | 55 | template <class T> |
babylonica | 0:f3bdfd31af88 | 56 | uint32_t FIFO<T>::getsize(void){ |
babylonica | 0:f3bdfd31af88 | 57 | return size; |
babylonica | 0:f3bdfd31af88 | 58 | } |
babylonica | 0:f3bdfd31af88 | 59 | |
babylonica | 0:f3bdfd31af88 | 60 | template <class T> |
babylonica | 0:f3bdfd31af88 | 61 | T FIFO<T>::get(void){ |
babylonica | 0:f3bdfd31af88 | 62 | T getdata; |
babylonica | 0:f3bdfd31af88 | 63 | |
babylonica | 0:f3bdfd31af88 | 64 | DISABLE_INTERRUPTS; |
babylonica | 0:f3bdfd31af88 | 65 | |
babylonica | 0:f3bdfd31af88 | 66 | if ( count <= 0 ){ |
babylonica | 0:f3bdfd31af88 | 67 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 68 | return 0; |
babylonica | 0:f3bdfd31af88 | 69 | } |
babylonica | 0:f3bdfd31af88 | 70 | |
babylonica | 0:f3bdfd31af88 | 71 | getdata = buffer[getindex]; |
babylonica | 0:f3bdfd31af88 | 72 | getindex++; |
babylonica | 0:f3bdfd31af88 | 73 | if ( getindex >= size ){ //When the index is in the terminus of the buffer |
babylonica | 0:f3bdfd31af88 | 74 | getindex = 0; |
babylonica | 0:f3bdfd31af88 | 75 | } |
babylonica | 0:f3bdfd31af88 | 76 | count--; |
babylonica | 0:f3bdfd31af88 | 77 | |
babylonica | 0:f3bdfd31af88 | 78 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 79 | |
babylonica | 0:f3bdfd31af88 | 80 | return getdata; |
babylonica | 0:f3bdfd31af88 | 81 | } |
babylonica | 0:f3bdfd31af88 | 82 | |
babylonica | 0:f3bdfd31af88 | 83 | template <class T> |
babylonica | 0:f3bdfd31af88 | 84 | T FIFO<T>::peek(void){ |
babylonica | 0:f3bdfd31af88 | 85 | T getdata; |
babylonica | 0:f3bdfd31af88 | 86 | |
babylonica | 0:f3bdfd31af88 | 87 | DISABLE_INTERRUPTS; |
babylonica | 0:f3bdfd31af88 | 88 | |
babylonica | 0:f3bdfd31af88 | 89 | if ( count <= 0 ){ //When the buffer is empty |
babylonica | 0:f3bdfd31af88 | 90 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 91 | return -1; |
babylonica | 0:f3bdfd31af88 | 92 | } |
babylonica | 0:f3bdfd31af88 | 93 | getdata = buffer[getindex]; |
babylonica | 0:f3bdfd31af88 | 94 | |
babylonica | 0:f3bdfd31af88 | 95 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 96 | return getdata; |
babylonica | 0:f3bdfd31af88 | 97 | } |
babylonica | 0:f3bdfd31af88 | 98 | |
babylonica | 0:f3bdfd31af88 | 99 | template <class T> |
babylonica | 0:f3bdfd31af88 | 100 | uint8_t FIFO<T>::put(const T putdata){ |
babylonica | 0:f3bdfd31af88 | 101 | DISABLE_INTERRUPTS; |
babylonica | 0:f3bdfd31af88 | 102 | |
babylonica | 0:f3bdfd31af88 | 103 | if ( count >= size ){ //When the buffer is full |
babylonica | 0:f3bdfd31af88 | 104 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 105 | return 0; |
babylonica | 0:f3bdfd31af88 | 106 | } |
babylonica | 0:f3bdfd31af88 | 107 | buffer[putindex] = putdata; |
babylonica | 0:f3bdfd31af88 | 108 | putindex++; |
babylonica | 0:f3bdfd31af88 | 109 | if ( putindex >= size ){ // When the index is in the terminus of the buffer |
babylonica | 0:f3bdfd31af88 | 110 | putindex = 0; |
babylonica | 0:f3bdfd31af88 | 111 | } |
babylonica | 0:f3bdfd31af88 | 112 | count++; |
babylonica | 0:f3bdfd31af88 | 113 | |
babylonica | 0:f3bdfd31af88 | 114 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 115 | |
babylonica | 0:f3bdfd31af88 | 116 | return 1; |
babylonica | 0:f3bdfd31af88 | 117 | } |
babylonica | 0:f3bdfd31af88 | 118 | |
babylonica | 0:f3bdfd31af88 | 119 | template <class T> |
babylonica | 0:f3bdfd31af88 | 120 | uint32_t FIFO<T>::available(void){ |
babylonica | 0:f3bdfd31af88 | 121 | uint32_t c = 0; |
babylonica | 0:f3bdfd31af88 | 122 | |
babylonica | 0:f3bdfd31af88 | 123 | DISABLE_INTERRUPTS; |
babylonica | 0:f3bdfd31af88 | 124 | |
babylonica | 0:f3bdfd31af88 | 125 | c = count; |
babylonica | 0:f3bdfd31af88 | 126 | |
babylonica | 0:f3bdfd31af88 | 127 | RESTORE_INTERUUPTS; |
babylonica | 0:f3bdfd31af88 | 128 | |
babylonica | 0:f3bdfd31af88 | 129 | return c; |
babylonica | 0:f3bdfd31af88 | 130 | } |
babylonica | 0:f3bdfd31af88 | 131 | |
babylonica | 0:f3bdfd31af88 | 132 | template class FIFO<uint8_t>; |
babylonica | 0:f3bdfd31af88 | 133 | template class FIFO<int8_t>; |
babylonica | 0:f3bdfd31af88 | 134 | template class FIFO<uint16_t>; |
babylonica | 0:f3bdfd31af88 | 135 | template class FIFO<int16_t>; |
babylonica | 0:f3bdfd31af88 | 136 | template class FIFO<uint32_t>; |
babylonica | 0:f3bdfd31af88 | 137 | template class FIFO<int32_t>; |
babylonica | 0:f3bdfd31af88 | 138 | template class FIFO<uint64_t>; |
babylonica | 0:f3bdfd31af88 | 139 | template class FIFO<int64_t>; |
babylonica | 0:f3bdfd31af88 | 140 | template class FIFO<char>; |
babylonica | 0:f3bdfd31af88 | 141 | template class FIFO<wchar_t>; |
babylonica | 0:f3bdfd31af88 | 142 |