noka @willow-micro / FIFO

Dependents:   AsyncSerial AsyncSerial AsyncSerial AsyncSerialh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FIFO.cpp Source File

FIFO.cpp

Go to the documentation of this file.
00001 // -*- coding: utf-8 -*-
00002 /**
00003  @file      FIFO.cpp
00004  @brief     1Byte Software FIFO(First-in, First-out)
00005  
00006  This library provides variable length ring buffering with software FIFO(First-in, First-out).
00007  This library overwrites when the buffer is full.
00008  
00009  @author    T.Kawamura
00010  @version   2.1
00011  @date      2016-11-20  T.Kawamura  Written for AVR.
00012  @date      2017-03-28  T.Kawamura  Fork for mbed/C++.
00013  @date      2017-03-29  T.Kawamura  Add template.
00014  @date      2017-06-17  T.Kawamura  Chanege to overwrite.
00015  
00016  @see 
00017  Copyright (C) 2016-2017 T.Kawamura.
00018  Released under the MIT license.
00019  http://opensource.org/licenses/mit-license.php
00020  
00021 */
00022 
00023 #include <stdint.h>
00024 #include "mbed.h"       // for cmsis
00025 #include "FIFO.hpp"
00026 
00027 template <class T>
00028 FIFO<T>::FIFO(uint32_t bufsize){
00029     buffer = new T[bufsize];
00030     size = bufsize;
00031     getindex = 0;
00032     putindex = 0;
00033     count = 0;
00034     return;
00035 }
00036 
00037 template <class T>
00038 FIFO<T>::~FIFO(void){
00039     delete[] buffer;
00040     return;
00041 }
00042 
00043 template <class T>
00044 void FIFO<T>::clear(void){
00045     DISABLE_INTERRUPTS;
00046   
00047   count = 0;
00048   getindex = 0;
00049   putindex = 0;
00050 
00051   RESTORE_INTERUUPTS;
00052 
00053     return;
00054 }
00055 
00056 template <class T>
00057 uint32_t FIFO<T>::getsize(void){
00058   return size;
00059 }
00060 
00061 template <class T>
00062 T FIFO<T>::get(void){
00063   T getdata;
00064 
00065     DISABLE_INTERRUPTS;
00066 
00067   if ( count <= 0 ){
00068     RESTORE_INTERUUPTS;
00069     return 0;
00070   }
00071   
00072   getdata = buffer[getindex];
00073   getindex++;
00074   if ( getindex >= size ){  //When the index is in the terminus of the buffer
00075     getindex = 0;
00076   }
00077   count--;
00078 
00079     RESTORE_INTERUUPTS;
00080 
00081   return getdata;
00082 }
00083 
00084 template <class T>
00085 T FIFO<T>::peek(void){
00086   T getdata;
00087   
00088   DISABLE_INTERRUPTS;
00089 
00090   if ( count <= 0 ){    //When the buffer is empty
00091         RESTORE_INTERUUPTS;
00092     return 0;
00093   }
00094   getdata = buffer[getindex];
00095 
00096     RESTORE_INTERUUPTS;
00097   return getdata;
00098 }
00099 
00100 template <class T>
00101 void FIFO<T>::put(const T putdata){
00102   DISABLE_INTERRUPTS;
00103   
00104   buffer[putindex] = putdata;
00105   putindex++;
00106   if ( putindex >= size ){  // When the index is in the terminus of the buffer
00107     putindex = 0;
00108   }
00109   count++;
00110 
00111   RESTORE_INTERUUPTS;
00112 
00113   return;
00114 }
00115 
00116 template <class T>
00117 uint32_t FIFO<T>::available(void){
00118   uint32_t c = 0;
00119   
00120   DISABLE_INTERRUPTS;
00121 
00122   c = count;
00123 
00124   RESTORE_INTERUUPTS;
00125   
00126   return c;
00127 }
00128 
00129 template class FIFO<uint8_t>;
00130 template class FIFO<int8_t>;
00131 template class FIFO<uint16_t>;
00132 template class FIFO<int16_t>;
00133 template class FIFO<uint32_t>;
00134 template class FIFO<int32_t>;
00135 template class FIFO<uint64_t>;
00136 template class FIFO<int64_t>;
00137 template class FIFO<char>;
00138 template class FIFO<wchar_t>;
00139