Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Queue by
queue.h@1:4770d82994dd, 2016-07-04 (annotated)
- Committer:
- Maor_T
- Date:
- Mon Jul 04 15:22:46 2016 +0000
- Revision:
- 1:4770d82994dd
- Parent:
- 0:a03810d46457
d
Who changed what in which revision?
| User | Revision | Line number | New 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 | #ifndef MBED_QUEUE_H |
| wbasser | 0:a03810d46457 | 24 | #define MBED_QUEUE_H |
| wbasser | 0:a03810d46457 | 25 | |
| wbasser | 0:a03810d46457 | 26 | #include "mbed.h" |
| wbasser | 0:a03810d46457 | 27 | |
| wbasser | 0:a03810d46457 | 28 | /** queue modle |
| wbasser | 0:a03810d46457 | 29 | * |
| wbasser | 0:a03810d46457 | 30 | * Example: |
| wbasser | 0:a03810d46457 | 31 | * @code |
| wbasser | 0:a03810d46457 | 32 | * |
| wbasser | 0:a03810d46457 | 33 | * #include "mbed.h" |
| wbasser | 0:a03810d46457 | 34 | * |
| wbasser | 0:a03810d46457 | 35 | * // include the queue library |
| wbasser | 0:a03810d46457 | 36 | * #include "queue.h" |
| wbasser | 0:a03810d46457 | 37 | * |
| wbasser | 0:a03810d46457 | 38 | * Serial myPc( USBTX, USBRX ); |
| wbasser | 0:a03810d46457 | 39 | * Queue myQueue( 1, 5 ); |
| wbasser | 0:a03810d46457 | 40 | * |
| wbasser | 0:a03810d46457 | 41 | * int main() |
| wbasser | 0:a03810d46457 | 42 | * { |
| wbasser | 0:a03810d46457 | 43 | * unsigned char nTemp; |
| wbasser | 0:a03810d46457 | 44 | * |
| wbasser | 0:a03810d46457 | 45 | * // display the number of items in the queue |
| wbasser | 0:a03810d46457 | 46 | * myPc.printf( "Items in queue: %d\r", myQueue.GetNumberOfItems( )); |
| wbasser | 0:a03810d46457 | 47 | * |
| wbasser | 0:a03810d46457 | 48 | * // add item to queue |
| wbasser | 0:a03810d46457 | 49 | * nTemp = 5; |
| wbasser | 0:a03810d46457 | 50 | * myQueue.Put( &nTemp ); |
| wbasser | 0:a03810d46457 | 51 | * nTemp = 6; |
| wbasser | 0:a03810d46457 | 52 | * myQueue.Put( &nTemp ); |
| wbasser | 0:a03810d46457 | 53 | * nTemp = 7; |
| wbasser | 0:a03810d46457 | 54 | * myQueue.Put( &nTemp ); |
| wbasser | 0:a03810d46457 | 55 | * |
| wbasser | 0:a03810d46457 | 56 | * // display the number of items in the queue |
| wbasser | 0:a03810d46457 | 57 | * myPc.printf( "Items in queue: %d\r", myQueue.GetNumberOfItems( )); |
| wbasser | 0:a03810d46457 | 58 | * |
| wbasser | 0:a03810d46457 | 59 | * // peek at item at the top of the queue |
| wbasser | 0:a03810d46457 | 60 | * myQueue.Peek( &nTemp ); |
| wbasser | 0:a03810d46457 | 61 | * myPc.printf( "Peek: %d\r", nTemp ); |
| wbasser | 0:a03810d46457 | 62 | * |
| wbasser | 0:a03810d46457 | 63 | * // get an item from queue |
| wbasser | 0:a03810d46457 | 64 | * myQueue.Get( &nTemp ); |
| wbasser | 0:a03810d46457 | 65 | * myPc.printf( "Item 0 = %d\r", nTemp ); |
| wbasser | 0:a03810d46457 | 66 | * myQueue.Get( &nTemp ); |
| wbasser | 0:a03810d46457 | 67 | * myPc.printf( "Item 1 = %d\r", nTemp ); |
| wbasser | 0:a03810d46457 | 68 | * |
| wbasser | 0:a03810d46457 | 69 | * // queue should be empty |
| wbasser | 0:a03810d46457 | 70 | * if ( !myQueue.Get( &nTemp )) |
| wbasser | 0:a03810d46457 | 71 | * { |
| wbasser | 0:a03810d46457 | 72 | * // queue is empty |
| wbasser | 0:a03810d46457 | 73 | * myPc.printf( "Queue empty!\r" ); |
| wbasser | 0:a03810d46457 | 74 | * } |
| wbasser | 0:a03810d46457 | 75 | * } |
| wbasser | 0:a03810d46457 | 76 | * |
| wbasser | 0:a03810d46457 | 77 | * @endcode |
| wbasser | 0:a03810d46457 | 78 | */ |
| wbasser | 0:a03810d46457 | 79 | |
| wbasser | 0:a03810d46457 | 80 | class Queue { |
| wbasser | 0:a03810d46457 | 81 | public: |
| wbasser | 0:a03810d46457 | 82 | /** Create a Queue object |
| wbasser | 0:a03810d46457 | 83 | * |
| wbasser | 0:a03810d46457 | 84 | * @param iSize size of the object in queue |
| wbasser | 0:a03810d46457 | 85 | * @param iCount number of items in the queue |
| wbasser | 0:a03810d46457 | 86 | */ |
| wbasser | 0:a03810d46457 | 87 | Queue( int iSize, int iCount ); |
| wbasser | 0:a03810d46457 | 88 | |
| wbasser | 0:a03810d46457 | 89 | /** destruction |
| wbasser | 0:a03810d46457 | 90 | * |
| wbasser | 0:a03810d46457 | 91 | */ |
| wbasser | 0:a03810d46457 | 92 | virtual ~Queue( void ); |
| wbasser | 0:a03810d46457 | 93 | |
| wbasser | 0:a03810d46457 | 94 | /** Add item to queue |
| wbasser | 0:a03810d46457 | 95 | * |
| wbasser | 0:a03810d46457 | 96 | * @param pvItem item to add |
| wbasser | 0:a03810d46457 | 97 | * @returns true if item added, false if queue full |
| wbasser | 0:a03810d46457 | 98 | */ |
| wbasser | 0:a03810d46457 | 99 | bool Put( void* pvItem ); |
| wbasser | 0:a03810d46457 | 100 | |
| wbasser | 0:a03810d46457 | 101 | /** get an item from the queue in an IRQ handler |
| wbasser | 0:a03810d46457 | 102 | * |
| wbasser | 0:a03810d46457 | 103 | * @param pvItem pointer to the tiem to retrieve |
| wbasser | 0:a03810d46457 | 104 | * @returns true if item fetched, false if queue is empty |
| wbasser | 0:a03810d46457 | 105 | */ |
| wbasser | 0:a03810d46457 | 106 | bool GetIrq( void* pvItem ); |
| wbasser | 0:a03810d46457 | 107 | |
| wbasser | 0:a03810d46457 | 108 | /** Add item to queue from an IRQ handler |
| wbasser | 0:a03810d46457 | 109 | * |
| wbasser | 0:a03810d46457 | 110 | * @param pvItem item to add |
| wbasser | 0:a03810d46457 | 111 | * @returns true if item added, false if queue full |
| wbasser | 0:a03810d46457 | 112 | */ |
| wbasser | 0:a03810d46457 | 113 | bool PutIrq( void* pvItem ); |
| wbasser | 0:a03810d46457 | 114 | |
| wbasser | 0:a03810d46457 | 115 | /** get an item from the queue |
| wbasser | 0:a03810d46457 | 116 | * |
| wbasser | 0:a03810d46457 | 117 | * @param pvItem pointer to the tiem to retrieve |
| wbasser | 0:a03810d46457 | 118 | * @returns true if item fetched, false if queue is empty |
| wbasser | 0:a03810d46457 | 119 | */ |
| wbasser | 0:a03810d46457 | 120 | bool Get( void* pvItem ); |
| wbasser | 0:a03810d46457 | 121 | |
| wbasser | 0:a03810d46457 | 122 | /** get the number of items in the queue |
| wbasser | 0:a03810d46457 | 123 | * |
| wbasser | 0:a03810d46457 | 124 | * @returns the number of items in the queue |
| wbasser | 0:a03810d46457 | 125 | */ |
| wbasser | 0:a03810d46457 | 126 | int GetNumberOfItems( void ); |
| wbasser | 0:a03810d46457 | 127 | |
| wbasser | 0:a03810d46457 | 128 | /** peek at the entry at the top of the queue |
| wbasser | 0:a03810d46457 | 129 | * |
| wbasser | 0:a03810d46457 | 130 | * @returns the entry at the top of the queue |
| wbasser | 0:a03810d46457 | 131 | */ |
| wbasser | 0:a03810d46457 | 132 | bool Peek( void* pvItem ); |
| wbasser | 0:a03810d46457 | 133 | |
| wbasser | 0:a03810d46457 | 134 | /** flush the queue |
| wbasser | 0:a03810d46457 | 135 | * |
| wbasser | 0:a03810d46457 | 136 | */ |
| wbasser | 0:a03810d46457 | 137 | void Flush( void ); |
| wbasser | 0:a03810d46457 | 138 | |
| wbasser | 0:a03810d46457 | 139 | |
| wbasser | 0:a03810d46457 | 140 | protected: |
| wbasser | 0:a03810d46457 | 141 | int m_iSize; // size of each item in queue |
| wbasser | 0:a03810d46457 | 142 | int m_iCount; // number of items in the queue |
| wbasser | 0:a03810d46457 | 143 | unsigned char* m_pnHead; // pointer to the head of the queue |
| wbasser | 0:a03810d46457 | 144 | unsigned char* m_pnTail; // pointer to the tail of the queue |
| wbasser | 0:a03810d46457 | 145 | unsigned char* m_pnRdIndex; // read index |
| wbasser | 0:a03810d46457 | 146 | unsigned char* m_pnWrIndex; // write index |
| wbasser | 0:a03810d46457 | 147 | int m_iLclCount; // number of items in queue |
| wbasser | 0:a03810d46457 | 148 | }; |
| wbasser | 0:a03810d46457 | 149 | #endif |
