Bluetooth SPI MBED

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_FIFO.cpp Source File

Adafruit_FIFO.cpp

Go to the documentation of this file.
00001 /**************************************************************************/
00002 /*!
00003     @file     Adafruit_FIFO.cpp
00004     @author   hathach
00005 
00006     @section LICENSE
00007 
00008     Software License Agreement (BSD License)
00009 
00010     Copyright (c) 2015, Adafruit Industries (adafruit.com)
00011     All rights reserved.
00012 
00013     Redistribution and use in source and binary forms, with or without
00014     modification, are permitted provided that the following conditions are met:
00015     1. Redistributions of source code must retain the above copyright
00016     notice, this list of conditions and the following disclaimer.
00017     2. Redistributions in binary form must reproduce the above copyright
00018     notice, this list of conditions and the following disclaimer in the
00019     documentation and/or other materials provided with the distribution.
00020     3. Neither the name of the copyright holders nor the
00021     names of its contributors may be used to endorse or promote products
00022     derived from this software without specific prior written permission.
00023 
00024     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
00025     EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00026     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00028     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00031     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00032     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00033     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034 */
00035 /**************************************************************************/
00036 
00037 #include "Adafruit_FIFO.h "
00038 #include <string.h>
00039 
00040 /******************************************************************************/
00041 /*!
00042     @brief  Constructor
00043 
00044     @param[in] buffer
00045                Memory location to store data
00046     @param[in] depth
00047                Maximum number of items can be hold in buffer
00048     @param[in] item_size
00049                Number of bytes of each item
00050     @param[in] overwrite
00051                Should the buffer is overwitten to the first item when it is full
00052 */
00053 /******************************************************************************/
00054 Adafruit_FIFO::Adafruit_FIFO(void* buffer, uint16_t depth, uint8_t item_size, bool overwrite)
00055 {
00056   m_buffer       = (uint8_t*) buffer;
00057   m_depth        = depth;
00058   m_item_size    = item_size;
00059   m_overwritable = overwrite;
00060 
00061   m_count = m_wr_idx = m_rd_idx = 0;
00062 }
00063 
00064 /******************************************************************************/
00065 /*!
00066     @brief  Clear the FIFO
00067 */
00068 /******************************************************************************/
00069 void Adafruit_FIFO::clear(void)
00070 {
00071   m_rd_idx = m_wr_idx = m_count = 0;
00072 }
00073 
00074 /******************************************************************************/
00075 /*!
00076     @brief  Write an item to the FIFO
00077 
00078     @param[in] item
00079                Memory address of the item
00080 */
00081 /******************************************************************************/
00082 bool Adafruit_FIFO::write(void const* item)
00083 {
00084   if ( full() && !m_overwritable ) return false;
00085 
00086   memcpy( m_buffer + (m_wr_idx * m_item_size),
00087           item,
00088           m_item_size);
00089 
00090   m_wr_idx = (m_wr_idx + 1) % m_depth;
00091 
00092   if ( full() )
00093   {
00094     m_rd_idx = m_wr_idx; // keep the full state (rd == wr && len = size)
00095   }
00096   else
00097   {
00098     m_count++;
00099   }
00100 
00101   return true;
00102 }
00103 
00104 /******************************************************************************/
00105 /*!
00106     @brief  Write array of items to the FIFO
00107 
00108     @param[in] data
00109                Memory address of the item's array
00110     @param[in] n
00111                Number of items to write
00112 
00113     @return    Number of written items
00114 */
00115 /******************************************************************************/
00116 uint16_t Adafruit_FIFO::write_n(void const * data, uint16_t n)
00117 {
00118   if ( n == 0 ) return 0;
00119 
00120   uint8_t* buf = (uint8_t*) data;
00121 
00122   uint16_t len = 0;
00123   while( (len < n) && write(buf) )
00124   {
00125     len++;
00126     buf += m_item_size;
00127   }
00128 
00129   return len;
00130 }
00131 
00132 /******************************************************************************/
00133 /*!
00134     @brief  Read an item from FIFO
00135 
00136     @param[in] buffer
00137                Memory address to store item
00138 */
00139 /******************************************************************************/
00140 bool Adafruit_FIFO::read(void* buffer)
00141 {
00142   if( empty() ) return false;
00143 
00144   memcpy(buffer,
00145          m_buffer + (m_rd_idx * m_item_size),
00146          m_item_size);
00147   m_rd_idx = (m_rd_idx + 1) % m_depth;
00148   m_count--;
00149 
00150   return true;
00151 }
00152 
00153 /******************************************************************************/
00154 /*!
00155     @brief  Read multiple items to an array
00156 
00157     @param[in] buffer
00158                Memory address of the item's array
00159     @param[in] n
00160                Number of items to read
00161 
00162     @return    Number of read items
00163 */
00164 /******************************************************************************/
00165 
00166 uint16_t Adafruit_FIFO::read_n (void * buffer, uint16_t n)
00167 {
00168   if( n == 0 ) return 0;
00169 
00170   uint8_t* buf = (uint8_t*) buffer;
00171 
00172   uint16_t len = 0;
00173   while( (len < n) && read(buf) )
00174   {
00175     len++;
00176     buf += m_item_size;
00177   }
00178 
00179   return len;
00180 }
00181 
00182 /******************************************************************************/
00183 /*!
00184     @brief  Read an item without removing it from the FIFO
00185 
00186     @param[in] buffer
00187                Memory address to store item
00188 */
00189 /******************************************************************************/
00190 bool Adafruit_FIFO::peek(void* buffer)
00191 {
00192   if( empty() ) return false;
00193 
00194   memcpy(buffer,
00195          m_buffer + (m_rd_idx * m_item_size),
00196          m_item_size);
00197 
00198   return true;
00199 }
00200 
00201 
00202 /******************************************************************************/
00203 /*!
00204     @brief  Read an item without removing it from the FIFO at the specific index
00205 
00206     @param[in] position
00207                Position to read from in the FIFO buffer
00208 
00209     @param[in] buffer
00210                Memory address to store item
00211 */
00212 /******************************************************************************/
00213 bool Adafruit_FIFO::peekAt(uint16_t position, void * p_buffer)
00214 {
00215   if( empty() || (position >= m_count) ) return false;
00216 
00217   uint16_t index = (m_rd_idx + position) % m_depth; // rd_idx is position=0
00218   memcpy(p_buffer,
00219          m_buffer + (index * m_item_size),
00220          m_item_size);
00221 
00222   return true;
00223 }
00224