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

Committer:
babylonica
Date:
Sat Jun 17 09:13:12 2017 +0000
Revision:
2:fa27aa055777
Parent:
1:fe6b5355c973
Child:
3:137a7245f990
Edit briefs

Who changed what in which revision?

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