baseline build
Dependencies: FastPWM mbed-os mbed
DataContainer.cpp@0:8a420ac6394e, 2017-06-19 (annotated)
- Committer:
- jrhodes5150
- Date:
- Mon Jun 19 15:55:51 2017 +0000
- Revision:
- 0:8a420ac6394e
initial build - baseline;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jrhodes5150 | 0:8a420ac6394e | 1 | #include "DataContainer.h" |
jrhodes5150 | 0:8a420ac6394e | 2 | |
jrhodes5150 | 0:8a420ac6394e | 3 | // construction |
jrhodes5150 | 0:8a420ac6394e | 4 | DataContainer::DataContainer( int iSize, int iCount ) |
jrhodes5150 | 0:8a420ac6394e | 5 | { |
jrhodes5150 | 0:8a420ac6394e | 6 | // allocate space for the queue |
jrhodes5150 | 0:8a420ac6394e | 7 | if (( m_pnHead = ( unsigned char* )malloc( iSize * iCount )) != NULL ) |
jrhodes5150 | 0:8a420ac6394e | 8 | { |
jrhodes5150 | 0:8a420ac6394e | 9 | // set the tail/clear the indices/set the size and length |
jrhodes5150 | 0:8a420ac6394e | 10 | m_pnTail = m_pnHead + ( iSize * iCount ); |
jrhodes5150 | 0:8a420ac6394e | 11 | m_iSize = iSize; |
jrhodes5150 | 0:8a420ac6394e | 12 | m_iCount = iCount; |
jrhodes5150 | 0:8a420ac6394e | 13 | m_pnRdIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 14 | m_pnWrIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 15 | m_iLclCount = 0; |
jrhodes5150 | 0:8a420ac6394e | 16 | } |
jrhodes5150 | 0:8a420ac6394e | 17 | } |
jrhodes5150 | 0:8a420ac6394e | 18 | |
jrhodes5150 | 0:8a420ac6394e | 19 | // destruction |
jrhodes5150 | 0:8a420ac6394e | 20 | DataContainer::~DataContainer( ) |
jrhodes5150 | 0:8a420ac6394e | 21 | { |
jrhodes5150 | 0:8a420ac6394e | 22 | // free the memory |
jrhodes5150 | 0:8a420ac6394e | 23 | free( m_pnHead ); |
jrhodes5150 | 0:8a420ac6394e | 24 | } |
jrhodes5150 | 0:8a420ac6394e | 25 | |
jrhodes5150 | 0:8a420ac6394e | 26 | // put an item into the queue |
jrhodes5150 | 0:8a420ac6394e | 27 | bool DataContainer::Put( void* pvItem ) |
jrhodes5150 | 0:8a420ac6394e | 28 | { |
jrhodes5150 | 0:8a420ac6394e | 29 | bool bResult = false; |
jrhodes5150 | 0:8a420ac6394e | 30 | |
jrhodes5150 | 0:8a420ac6394e | 31 | // check for room |
jrhodes5150 | 0:8a420ac6394e | 32 | if ( m_iLclCount < m_iCount ) |
jrhodes5150 | 0:8a420ac6394e | 33 | { |
jrhodes5150 | 0:8a420ac6394e | 34 | __disable_irq( ); |
jrhodes5150 | 0:8a420ac6394e | 35 | // copy the item/adjust the pointer/check for overflow |
jrhodes5150 | 0:8a420ac6394e | 36 | memcpy( m_pnWrIndex, pvItem, m_iSize ); |
jrhodes5150 | 0:8a420ac6394e | 37 | m_pnWrIndex += m_iSize; |
jrhodes5150 | 0:8a420ac6394e | 38 | if ( m_pnWrIndex >= m_pnTail ) |
jrhodes5150 | 0:8a420ac6394e | 39 | m_pnWrIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 40 | |
jrhodes5150 | 0:8a420ac6394e | 41 | // increment the count |
jrhodes5150 | 0:8a420ac6394e | 42 | m_iLclCount++; |
jrhodes5150 | 0:8a420ac6394e | 43 | __enable_irq( ); |
jrhodes5150 | 0:8a420ac6394e | 44 | |
jrhodes5150 | 0:8a420ac6394e | 45 | // set the result to 0k |
jrhodes5150 | 0:8a420ac6394e | 46 | bResult = true; |
jrhodes5150 | 0:8a420ac6394e | 47 | } |
jrhodes5150 | 0:8a420ac6394e | 48 | |
jrhodes5150 | 0:8a420ac6394e | 49 | // return the status |
jrhodes5150 | 0:8a420ac6394e | 50 | return( bResult ); |
jrhodes5150 | 0:8a420ac6394e | 51 | } |
jrhodes5150 | 0:8a420ac6394e | 52 | |
jrhodes5150 | 0:8a420ac6394e | 53 | // get an item from the queue |
jrhodes5150 | 0:8a420ac6394e | 54 | bool DataContainer::Get( void* pvItem ) |
jrhodes5150 | 0:8a420ac6394e | 55 | { |
jrhodes5150 | 0:8a420ac6394e | 56 | bool bResult = false; |
jrhodes5150 | 0:8a420ac6394e | 57 | |
jrhodes5150 | 0:8a420ac6394e | 58 | // check for room |
jrhodes5150 | 0:8a420ac6394e | 59 | if ( m_iLclCount != 0 ) |
jrhodes5150 | 0:8a420ac6394e | 60 | { |
jrhodes5150 | 0:8a420ac6394e | 61 | __disable_irq( ); |
jrhodes5150 | 0:8a420ac6394e | 62 | // copy the item/adjust the pointer/check for overflow |
jrhodes5150 | 0:8a420ac6394e | 63 | memcpy( pvItem, m_pnRdIndex, m_iSize ); |
jrhodes5150 | 0:8a420ac6394e | 64 | m_pnRdIndex += m_iSize; |
jrhodes5150 | 0:8a420ac6394e | 65 | if ( m_pnRdIndex >= m_pnTail ) |
jrhodes5150 | 0:8a420ac6394e | 66 | m_pnRdIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 67 | |
jrhodes5150 | 0:8a420ac6394e | 68 | // decrement the count |
jrhodes5150 | 0:8a420ac6394e | 69 | m_iLclCount--; |
jrhodes5150 | 0:8a420ac6394e | 70 | __enable_irq( ); |
jrhodes5150 | 0:8a420ac6394e | 71 | // set the result to 0k |
jrhodes5150 | 0:8a420ac6394e | 72 | bResult = true; |
jrhodes5150 | 0:8a420ac6394e | 73 | } |
jrhodes5150 | 0:8a420ac6394e | 74 | |
jrhodes5150 | 0:8a420ac6394e | 75 | // return the status |
jrhodes5150 | 0:8a420ac6394e | 76 | return( bResult ); |
jrhodes5150 | 0:8a420ac6394e | 77 | } |
jrhodes5150 | 0:8a420ac6394e | 78 | |
jrhodes5150 | 0:8a420ac6394e | 79 | // put an item into the queue |
jrhodes5150 | 0:8a420ac6394e | 80 | bool DataContainer::PutIrq( void* pvItem ) |
jrhodes5150 | 0:8a420ac6394e | 81 | { |
jrhodes5150 | 0:8a420ac6394e | 82 | bool bResult = false; |
jrhodes5150 | 0:8a420ac6394e | 83 | |
jrhodes5150 | 0:8a420ac6394e | 84 | // check for room |
jrhodes5150 | 0:8a420ac6394e | 85 | if ( m_iLclCount < m_iCount ) |
jrhodes5150 | 0:8a420ac6394e | 86 | { |
jrhodes5150 | 0:8a420ac6394e | 87 | // copy the item/adjust the pointer/check for overflow |
jrhodes5150 | 0:8a420ac6394e | 88 | memcpy( m_pnWrIndex, pvItem, m_iSize ); |
jrhodes5150 | 0:8a420ac6394e | 89 | m_pnWrIndex += m_iSize; |
jrhodes5150 | 0:8a420ac6394e | 90 | if ( m_pnWrIndex >= m_pnTail ) |
jrhodes5150 | 0:8a420ac6394e | 91 | m_pnWrIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 92 | |
jrhodes5150 | 0:8a420ac6394e | 93 | // increment the count |
jrhodes5150 | 0:8a420ac6394e | 94 | m_iLclCount++; |
jrhodes5150 | 0:8a420ac6394e | 95 | |
jrhodes5150 | 0:8a420ac6394e | 96 | // set the result to 0k |
jrhodes5150 | 0:8a420ac6394e | 97 | bResult = true; |
jrhodes5150 | 0:8a420ac6394e | 98 | } |
jrhodes5150 | 0:8a420ac6394e | 99 | |
jrhodes5150 | 0:8a420ac6394e | 100 | // return the status |
jrhodes5150 | 0:8a420ac6394e | 101 | return( bResult ); |
jrhodes5150 | 0:8a420ac6394e | 102 | } |
jrhodes5150 | 0:8a420ac6394e | 103 | |
jrhodes5150 | 0:8a420ac6394e | 104 | // get an item from the queue |
jrhodes5150 | 0:8a420ac6394e | 105 | bool DataContainer::GetIrq( void* pvItem ) |
jrhodes5150 | 0:8a420ac6394e | 106 | { |
jrhodes5150 | 0:8a420ac6394e | 107 | bool bResult = false; |
jrhodes5150 | 0:8a420ac6394e | 108 | |
jrhodes5150 | 0:8a420ac6394e | 109 | // check for room |
jrhodes5150 | 0:8a420ac6394e | 110 | if ( m_iLclCount != 0 ) |
jrhodes5150 | 0:8a420ac6394e | 111 | { |
jrhodes5150 | 0:8a420ac6394e | 112 | // copy the item/adjust the pointer/check for overflow |
jrhodes5150 | 0:8a420ac6394e | 113 | memcpy( pvItem, m_pnRdIndex, m_iSize ); |
jrhodes5150 | 0:8a420ac6394e | 114 | m_pnRdIndex += m_iSize; |
jrhodes5150 | 0:8a420ac6394e | 115 | if ( m_pnRdIndex >= m_pnTail ) |
jrhodes5150 | 0:8a420ac6394e | 116 | m_pnRdIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 117 | |
jrhodes5150 | 0:8a420ac6394e | 118 | // decrement the count |
jrhodes5150 | 0:8a420ac6394e | 119 | m_iLclCount--; |
jrhodes5150 | 0:8a420ac6394e | 120 | |
jrhodes5150 | 0:8a420ac6394e | 121 | // set the result to 0k |
jrhodes5150 | 0:8a420ac6394e | 122 | bResult = true; |
jrhodes5150 | 0:8a420ac6394e | 123 | } |
jrhodes5150 | 0:8a420ac6394e | 124 | |
jrhodes5150 | 0:8a420ac6394e | 125 | // return the status |
jrhodes5150 | 0:8a420ac6394e | 126 | return( bResult ); |
jrhodes5150 | 0:8a420ac6394e | 127 | } |
jrhodes5150 | 0:8a420ac6394e | 128 | |
jrhodes5150 | 0:8a420ac6394e | 129 | // get the number of items in the queue |
jrhodes5150 | 0:8a420ac6394e | 130 | int DataContainer::GetNumberOfItems( void ) |
jrhodes5150 | 0:8a420ac6394e | 131 | { |
jrhodes5150 | 0:8a420ac6394e | 132 | // return the count |
jrhodes5150 | 0:8a420ac6394e | 133 | return( m_iLclCount ); |
jrhodes5150 | 0:8a420ac6394e | 134 | } |
jrhodes5150 | 0:8a420ac6394e | 135 | |
jrhodes5150 | 0:8a420ac6394e | 136 | // peeks at the item at the top of the queue |
jrhodes5150 | 0:8a420ac6394e | 137 | bool DataContainer::Peek( void* pvItem ) |
jrhodes5150 | 0:8a420ac6394e | 138 | { |
jrhodes5150 | 0:8a420ac6394e | 139 | bool bResult = false; |
jrhodes5150 | 0:8a420ac6394e | 140 | |
jrhodes5150 | 0:8a420ac6394e | 141 | // check for room |
jrhodes5150 | 0:8a420ac6394e | 142 | if ( m_iLclCount != 0 ) |
jrhodes5150 | 0:8a420ac6394e | 143 | { |
jrhodes5150 | 0:8a420ac6394e | 144 | // copy the item |
jrhodes5150 | 0:8a420ac6394e | 145 | memcpy( pvItem, m_pnRdIndex, m_iSize ); |
jrhodes5150 | 0:8a420ac6394e | 146 | |
jrhodes5150 | 0:8a420ac6394e | 147 | // set the result to 0k |
jrhodes5150 | 0:8a420ac6394e | 148 | bResult = true; |
jrhodes5150 | 0:8a420ac6394e | 149 | } |
jrhodes5150 | 0:8a420ac6394e | 150 | |
jrhodes5150 | 0:8a420ac6394e | 151 | // return the status |
jrhodes5150 | 0:8a420ac6394e | 152 | return( bResult ); |
jrhodes5150 | 0:8a420ac6394e | 153 | } |
jrhodes5150 | 0:8a420ac6394e | 154 | |
jrhodes5150 | 0:8a420ac6394e | 155 | // flush all items from the queue |
jrhodes5150 | 0:8a420ac6394e | 156 | void DataContainer::Flush( void ) |
jrhodes5150 | 0:8a420ac6394e | 157 | { |
jrhodes5150 | 0:8a420ac6394e | 158 | // reset the indices |
jrhodes5150 | 0:8a420ac6394e | 159 | m_iLclCount = 0; |
jrhodes5150 | 0:8a420ac6394e | 160 | m_pnRdIndex = m_pnWrIndex = m_pnHead; |
jrhodes5150 | 0:8a420ac6394e | 161 | } |
jrhodes5150 | 0:8a420ac6394e | 162 | |
jrhodes5150 | 0:8a420ac6394e | 163 |