Queue Handler

Dependents:   BLE_ECG kragl kragl_v2 4180_final_project_v2 ... more

Committer:
wbasser
Date:
Fri Oct 22 22:07:25 2010 +0000
Revision:
0:a03810d46457
Version 01_00_00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wbasser 0:a03810d46457 1 /* mbed queue Library
wbasser 0:a03810d46457 2 * Copyright (c) 2010 William Basser ( wbasser [at] gmail [dot] com )
wbasser 0:a03810d46457 3 *
wbasser 0:a03810d46457 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
wbasser 0:a03810d46457 5 * of this software and associated documentation files (the "Software"), to deal
wbasser 0:a03810d46457 6 * in the Software without restriction, including without limitation the rights
wbasser 0:a03810d46457 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wbasser 0:a03810d46457 8 * copies of the Software, and to permit persons to whom the Software is
wbasser 0:a03810d46457 9 * furnished to do so, subject to the following conditions:
wbasser 0:a03810d46457 10 *
wbasser 0:a03810d46457 11 * The above copyright notice and this permission notice shall be included in
wbasser 0:a03810d46457 12 * all copies or substantial portions of the Software.
wbasser 0:a03810d46457 13 *
wbasser 0:a03810d46457 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wbasser 0:a03810d46457 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wbasser 0:a03810d46457 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wbasser 0:a03810d46457 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wbasser 0:a03810d46457 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wbasser 0:a03810d46457 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wbasser 0:a03810d46457 20 * THE SOFTWARE.
wbasser 0:a03810d46457 21 */
wbasser 0:a03810d46457 22
wbasser 0:a03810d46457 23 // inlcude the defs
wbasser 0:a03810d46457 24 #include "queue.h"
wbasser 0:a03810d46457 25
wbasser 0:a03810d46457 26 // construction
wbasser 0:a03810d46457 27 Queue::Queue( int iSize, int iCount )
wbasser 0:a03810d46457 28 {
wbasser 0:a03810d46457 29 // allocate space for the queue
wbasser 0:a03810d46457 30 if (( m_pnHead = ( unsigned char* )malloc( iSize * iCount )) != NULL )
wbasser 0:a03810d46457 31 {
wbasser 0:a03810d46457 32 // set the tail/clear the indices/set the size and length
wbasser 0:a03810d46457 33 m_pnTail = m_pnHead + ( iSize * iCount );
wbasser 0:a03810d46457 34 m_iSize = iSize;
wbasser 0:a03810d46457 35 m_iCount = iCount;
wbasser 0:a03810d46457 36 m_pnRdIndex = m_pnHead;
wbasser 0:a03810d46457 37 m_pnWrIndex = m_pnHead;
wbasser 0:a03810d46457 38 m_iLclCount = 0;
wbasser 0:a03810d46457 39 }
wbasser 0:a03810d46457 40 }
wbasser 0:a03810d46457 41
wbasser 0:a03810d46457 42 // destruction
wbasser 0:a03810d46457 43 Queue::~Queue( )
wbasser 0:a03810d46457 44 {
wbasser 0:a03810d46457 45 // free the memory
wbasser 0:a03810d46457 46 free( m_pnHead );
wbasser 0:a03810d46457 47 }
wbasser 0:a03810d46457 48
wbasser 0:a03810d46457 49 // put an item into the queue
wbasser 0:a03810d46457 50 bool Queue::Put( void* pvItem )
wbasser 0:a03810d46457 51 {
wbasser 0:a03810d46457 52 bool bResult = false;
wbasser 0:a03810d46457 53
wbasser 0:a03810d46457 54 // check for room
wbasser 0:a03810d46457 55 if ( m_iLclCount < m_iCount )
wbasser 0:a03810d46457 56 {
wbasser 0:a03810d46457 57 __disable_irq( );
wbasser 0:a03810d46457 58 // copy the item/adjust the pointer/check for overflow
wbasser 0:a03810d46457 59 memcpy( m_pnWrIndex, pvItem, m_iSize );
wbasser 0:a03810d46457 60 m_pnWrIndex += m_iSize;
wbasser 0:a03810d46457 61 if ( m_pnWrIndex >= m_pnTail )
wbasser 0:a03810d46457 62 m_pnWrIndex = m_pnHead;
wbasser 0:a03810d46457 63
wbasser 0:a03810d46457 64 // increment the count
wbasser 0:a03810d46457 65 m_iLclCount++;
wbasser 0:a03810d46457 66 __enable_irq( );
wbasser 0:a03810d46457 67
wbasser 0:a03810d46457 68 // set the result to 0k
wbasser 0:a03810d46457 69 bResult = true;
wbasser 0:a03810d46457 70 }
wbasser 0:a03810d46457 71
wbasser 0:a03810d46457 72 // return the status
wbasser 0:a03810d46457 73 return( bResult );
wbasser 0:a03810d46457 74 }
wbasser 0:a03810d46457 75
wbasser 0:a03810d46457 76 // get an item from the queue
wbasser 0:a03810d46457 77 bool Queue::Get( void* pvItem )
wbasser 0:a03810d46457 78 {
wbasser 0:a03810d46457 79 bool bResult = false;
wbasser 0:a03810d46457 80
wbasser 0:a03810d46457 81 // check for room
wbasser 0:a03810d46457 82 if ( m_iLclCount != 0 )
wbasser 0:a03810d46457 83 {
wbasser 0:a03810d46457 84 __disable_irq( );
wbasser 0:a03810d46457 85 // copy the item/adjust the pointer/check for overflow
wbasser 0:a03810d46457 86 memcpy( pvItem, m_pnRdIndex, m_iSize );
wbasser 0:a03810d46457 87 m_pnRdIndex += m_iSize;
wbasser 0:a03810d46457 88 if ( m_pnRdIndex >= m_pnTail )
wbasser 0:a03810d46457 89 m_pnRdIndex = m_pnHead;
wbasser 0:a03810d46457 90
wbasser 0:a03810d46457 91 // decrement the count
wbasser 0:a03810d46457 92 m_iLclCount--;
wbasser 0:a03810d46457 93 __enable_irq( );
wbasser 0:a03810d46457 94 // set the result to 0k
wbasser 0:a03810d46457 95 bResult = true;
wbasser 0:a03810d46457 96 }
wbasser 0:a03810d46457 97
wbasser 0:a03810d46457 98 // return the status
wbasser 0:a03810d46457 99 return( bResult );
wbasser 0:a03810d46457 100 }
wbasser 0:a03810d46457 101
wbasser 0:a03810d46457 102 // put an item into the queue
wbasser 0:a03810d46457 103 bool Queue::PutIrq( void* pvItem )
wbasser 0:a03810d46457 104 {
wbasser 0:a03810d46457 105 bool bResult = false;
wbasser 0:a03810d46457 106
wbasser 0:a03810d46457 107 // check for room
wbasser 0:a03810d46457 108 if ( m_iLclCount < m_iCount )
wbasser 0:a03810d46457 109 {
wbasser 0:a03810d46457 110 // copy the item/adjust the pointer/check for overflow
wbasser 0:a03810d46457 111 memcpy( m_pnWrIndex, pvItem, m_iSize );
wbasser 0:a03810d46457 112 m_pnWrIndex += m_iSize;
wbasser 0:a03810d46457 113 if ( m_pnWrIndex >= m_pnTail )
wbasser 0:a03810d46457 114 m_pnWrIndex = m_pnHead;
wbasser 0:a03810d46457 115
wbasser 0:a03810d46457 116 // increment the count
wbasser 0:a03810d46457 117 m_iLclCount++;
wbasser 0:a03810d46457 118
wbasser 0:a03810d46457 119 // set the result to 0k
wbasser 0:a03810d46457 120 bResult = true;
wbasser 0:a03810d46457 121 }
wbasser 0:a03810d46457 122
wbasser 0:a03810d46457 123 // return the status
wbasser 0:a03810d46457 124 return( bResult );
wbasser 0:a03810d46457 125 }
wbasser 0:a03810d46457 126
wbasser 0:a03810d46457 127 // get an item from the queue
wbasser 0:a03810d46457 128 bool Queue::GetIrq( void* pvItem )
wbasser 0:a03810d46457 129 {
wbasser 0:a03810d46457 130 bool bResult = false;
wbasser 0:a03810d46457 131
wbasser 0:a03810d46457 132 // check for room
wbasser 0:a03810d46457 133 if ( m_iLclCount != 0 )
wbasser 0:a03810d46457 134 {
wbasser 0:a03810d46457 135 // copy the item/adjust the pointer/check for overflow
wbasser 0:a03810d46457 136 memcpy( pvItem, m_pnRdIndex, m_iSize );
wbasser 0:a03810d46457 137 m_pnRdIndex += m_iSize;
wbasser 0:a03810d46457 138 if ( m_pnRdIndex >= m_pnTail )
wbasser 0:a03810d46457 139 m_pnRdIndex = m_pnHead;
wbasser 0:a03810d46457 140
wbasser 0:a03810d46457 141 // decrement the count
wbasser 0:a03810d46457 142 m_iLclCount--;
wbasser 0:a03810d46457 143
wbasser 0:a03810d46457 144 // set the result to 0k
wbasser 0:a03810d46457 145 bResult = true;
wbasser 0:a03810d46457 146 }
wbasser 0:a03810d46457 147
wbasser 0:a03810d46457 148 // return the status
wbasser 0:a03810d46457 149 return( bResult );
wbasser 0:a03810d46457 150 }
wbasser 0:a03810d46457 151
wbasser 0:a03810d46457 152 // get the number of items in the queue
wbasser 0:a03810d46457 153 int Queue::GetNumberOfItems( void )
wbasser 0:a03810d46457 154 {
wbasser 0:a03810d46457 155 // return the count
wbasser 0:a03810d46457 156 return( m_iLclCount );
wbasser 0:a03810d46457 157 }
wbasser 0:a03810d46457 158
wbasser 0:a03810d46457 159 // peeks at the item at the top of the queue
wbasser 0:a03810d46457 160 bool Queue::Peek( void* pvItem )
wbasser 0:a03810d46457 161 {
wbasser 0:a03810d46457 162 bool bResult = false;
wbasser 0:a03810d46457 163
wbasser 0:a03810d46457 164 // check for room
wbasser 0:a03810d46457 165 if ( m_iLclCount != 0 )
wbasser 0:a03810d46457 166 {
wbasser 0:a03810d46457 167 // copy the item
wbasser 0:a03810d46457 168 memcpy( pvItem, m_pnRdIndex, m_iSize );
wbasser 0:a03810d46457 169
wbasser 0:a03810d46457 170 // set the result to 0k
wbasser 0:a03810d46457 171 bResult = true;
wbasser 0:a03810d46457 172 }
wbasser 0:a03810d46457 173
wbasser 0:a03810d46457 174 // return the status
wbasser 0:a03810d46457 175 return( bResult );
wbasser 0:a03810d46457 176 }
wbasser 0:a03810d46457 177
wbasser 0:a03810d46457 178 // flush all items from the queue
wbasser 0:a03810d46457 179 void Queue::Flush( void )
wbasser 0:a03810d46457 180 {
wbasser 0:a03810d46457 181 // reset the indices
wbasser 0:a03810d46457 182 m_iLclCount = 0;
wbasser 0:a03810d46457 183 m_pnRdIndex = m_pnWrIndex = m_pnHead;
wbasser 0:a03810d46457 184 }
wbasser 0:a03810d46457 185