noka @willow-micro / FIFO

Dependents:   AsyncSerial AsyncSerial AsyncSerial AsyncSerialh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FIFO.hpp Source File

FIFO.hpp

Go to the documentation of this file.
00001 // -*- coding: utf-8 -*-
00002 /**
00003  @file      FIFO.hpp
00004  @brief     Variable Length Ring Buffer
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 
00015  @see 
00016  Copyright (C) 2016-2017 T.Kawamura.
00017  Released under the MIT license.
00018  http://opensource.org/licenses/mit-license.php
00019  
00020 */
00021 
00022 #ifndef FIFO_H
00023 #define FIFO_H
00024 
00025 /**
00026      Disable all interuupts and save the status of interrupts.
00027      This macro is usable at ONLY Cortex-M Series. 
00028 */
00029 #define DISABLE_INTERRUPTS uint32_t primask = __get_PRIMASK();  __disable_irq()
00030 /**
00031      Enable all interuupts when the status of interrupts is ENABLED.
00032      This macro is usable at ONLY Cortex-M Series. 
00033 */
00034 #define RESTORE_INTERUUPTS if( !(primask & 1) ) __enable_irq()
00035 
00036 /**
00037     @class  FIFO
00038   @brief    Variable Length Software Ring Buffer
00039 */
00040 template <typename T>
00041 class FIFO{
00042  private:
00043     T *buffer;  // Buffer
00044   uint32_t size;    // Buffer Size
00045   volatile uint32_t getindex;   // Index for Getting Data
00046   volatile uint32_t putindex;   // Index for Putting Data
00047   volatile uint32_t count;  // Size of Data
00048     
00049  public:
00050     /**
00051         @brief  Create a new FIFO.
00052         @param  size    Buffer Size.
00053     */
00054     FIFO(uint32_t bufsize);
00055 
00056     /**
00057         @brief  Destroy the FIFO.
00058         @param  No Parameters.
00059     */
00060     virtual ~FIFO(void);
00061 
00062     /**
00063   @brief    Clear the buffer.
00064   @param    No Parameters.
00065     */
00066     virtual void clear(void);
00067 
00068     /**
00069   @brief    Get buffer size.
00070   @param    No Parameters.
00071   @return   Buffer size
00072     */
00073     virtual uint32_t getsize(void);
00074 
00075     /**
00076   @brief    Get byte from the buffer.
00077   @param    No Parameters.
00078   @retval   All Data
00079   @retval   0   Error.
00080     */
00081     virtual T get(void);
00082 
00083     /**
00084       @brief    Peek byte from the buffer
00085         Peek 1byte from the buffer.(Do not touch index)
00086       @param    No Parameters.
00087       @retval   All Data
00088       @retval   0   Error.
00089     */  
00090     virtual T peek(void);
00091     
00092     /**
00093   @brief    Put byte to the buffer
00094   @param    No Parameters.
00095   @param    putdata     The Data for Putting.
00096   @return   Nothing.
00097     */
00098     virtual void put(const T putdata);
00099 
00100     /**
00101   @brief    Get Size of Retained Data.
00102   @param    No Parameters.
00103   @return   Data size.
00104     */
00105     virtual uint32_t available(void);
00106 
00107     /**
00108      *  @brief  Overloaded operator for putting data to the FIFO.
00109      *  @param  Data to put
00110      */
00111     FIFO &operator= (T data){
00112         put(data);
00113         return *this;
00114     }
00115 
00116     /**
00117      *  @brief  Overloaded operator for getting data from the FIFO.
00118      *  @param  No Parameters.
00119      *  @return Oldest Data
00120      */
00121     operator int(void){
00122         return get();
00123     }
00124     
00125 };
00126 
00127 #endif
00128