Queue Handler

Dependents:   BLE_ECG kragl kragl_v2 4180_final_project_v2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers queue.cpp Source File

queue.cpp

00001 /* mbed queue Library
00002  * Copyright (c) 2010 William Basser ( wbasser [at] gmail [dot] com )
00003  * 
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 // inlcude the defs
00024 #include "queue.h"
00025 
00026 // construction 
00027 Queue::Queue( int iSize, int iCount )
00028 {
00029     // allocate space for the queue
00030     if (( m_pnHead = ( unsigned char* )malloc( iSize * iCount )) != NULL )
00031     {
00032         // set the tail/clear the indices/set the size and length
00033         m_pnTail     = m_pnHead + ( iSize * iCount );
00034         m_iSize     = iSize;
00035         m_iCount     = iCount;
00036         m_pnRdIndex    = m_pnHead;
00037         m_pnWrIndex    = m_pnHead;
00038         m_iLclCount    = 0;
00039     }
00040 }
00041 
00042 // destruction
00043 Queue::~Queue( )
00044 {
00045     // free the memory
00046     free( m_pnHead );
00047 }
00048 
00049 // put an item into the queue
00050 bool Queue::Put( void* pvItem )
00051 {
00052     bool bResult = false;
00053     
00054     // check for room
00055     if ( m_iLclCount < m_iCount )
00056     {
00057         __disable_irq( );
00058         // copy the item/adjust the pointer/check for overflow
00059         memcpy( m_pnWrIndex, pvItem, m_iSize );
00060         m_pnWrIndex += m_iSize;
00061         if ( m_pnWrIndex >= m_pnTail )
00062             m_pnWrIndex = m_pnHead;
00063 
00064         // increment the count
00065         m_iLclCount++;
00066         __enable_irq( );
00067 
00068         // set the result to 0k
00069         bResult = true;
00070     }
00071 
00072     // return the status
00073     return( bResult );
00074 }
00075 
00076 // get an item from the queue
00077 bool Queue::Get( void* pvItem )
00078 {
00079     bool bResult = false;
00080     
00081     // check for room
00082     if ( m_iLclCount != 0 )
00083     {
00084         __disable_irq( );
00085         // copy the item/adjust the pointer/check for overflow
00086         memcpy( pvItem, m_pnRdIndex, m_iSize );
00087         m_pnRdIndex += m_iSize;
00088         if ( m_pnRdIndex >= m_pnTail )
00089             m_pnRdIndex = m_pnHead;
00090 
00091         // decrement the count
00092         m_iLclCount--;
00093         __enable_irq( );
00094         // set the result to 0k
00095         bResult = true;
00096     }
00097 
00098     // return the status
00099     return( bResult );
00100 }
00101 
00102 // put an item into the queue
00103 bool Queue::PutIrq( void* pvItem )
00104 {
00105     bool bResult = false;
00106     
00107     // check for room
00108     if ( m_iLclCount < m_iCount )
00109     {
00110         // copy the item/adjust the pointer/check for overflow
00111         memcpy( m_pnWrIndex, pvItem, m_iSize );
00112         m_pnWrIndex += m_iSize;
00113         if ( m_pnWrIndex >= m_pnTail )
00114             m_pnWrIndex = m_pnHead;
00115 
00116         // increment the count
00117         m_iLclCount++;
00118 
00119         // set the result to 0k
00120         bResult = true;
00121     }
00122 
00123     // return the status
00124     return( bResult );
00125 }
00126 
00127 // get an item from the queue
00128 bool Queue::GetIrq( void* pvItem )
00129 {
00130     bool bResult = false;
00131     
00132     // check for room
00133     if ( m_iLclCount != 0 )
00134     {
00135         // copy the item/adjust the pointer/check for overflow
00136         memcpy( pvItem, m_pnRdIndex, m_iSize );
00137         m_pnRdIndex += m_iSize;
00138         if ( m_pnRdIndex >= m_pnTail )
00139             m_pnRdIndex = m_pnHead;
00140 
00141         // decrement the count
00142         m_iLclCount--;
00143 
00144         // set the result to 0k
00145         bResult = true;
00146     }
00147 
00148     // return the status
00149     return( bResult );
00150 }
00151 
00152 // get the number of items in the queue
00153 int Queue::GetNumberOfItems( void )
00154 {
00155     // return the count
00156     return( m_iLclCount );
00157 }
00158 
00159 // peeks at the item at the top of the queue
00160 bool Queue::Peek( void* pvItem )
00161 {
00162     bool bResult = false;
00163     
00164     // check for room
00165     if ( m_iLclCount != 0 )
00166     {
00167         // copy the item
00168         memcpy( pvItem, m_pnRdIndex, m_iSize );
00169 
00170         // set the result to 0k
00171         bResult = true;
00172     }
00173 
00174     // return the status
00175     return( bResult );
00176 }
00177 
00178 // flush all items from the queue
00179 void Queue::Flush( void )
00180 {
00181     // reset the indices
00182     m_iLclCount = 0;
00183     m_pnRdIndex = m_pnWrIndex = m_pnHead;
00184 }
00185