xbee communication for UWB quadcopter project
queue/queue.cpp@0:26a151d2c6db, 2014-01-08 (annotated)
- Committer:
- oprospero
- Date:
- Wed Jan 08 07:32:30 2014 +0000
- Revision:
- 0:26a151d2c6db
- Child:
- 7:951c71c71b2d
Create two serial instance for Rx and Tx
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
oprospero | 0:26a151d2c6db | 1 | /*************************** queue.cpp ***************************************/ |
oprospero | 0:26a151d2c6db | 2 | /* */ |
oprospero | 0:26a151d2c6db | 3 | /* Authers: Oanh Tran, Ling Lei, Sihao Xie, Greg Abdo. */ |
oprospero | 0:26a151d2c6db | 4 | /* Date: February 23, 2013 */ |
oprospero | 0:26a151d2c6db | 5 | /* Version: 1.0 */ |
oprospero | 0:26a151d2c6db | 6 | /* */ |
oprospero | 0:26a151d2c6db | 7 | /* The queue is used to stack StructureItem in order with a FILO */ |
oprospero | 0:26a151d2c6db | 8 | /*arrangement. */ |
oprospero | 0:26a151d2c6db | 9 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 10 | |
oprospero | 0:26a151d2c6db | 11 | #include "queue.h" |
oprospero | 0:26a151d2c6db | 12 | |
oprospero | 0:26a151d2c6db | 13 | /***************************** constructor ***********************************/ |
oprospero | 0:26a151d2c6db | 14 | /* Description: */ |
oprospero | 0:26a151d2c6db | 15 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 16 | |
oprospero | 0:26a151d2c6db | 17 | queue::queue() |
oprospero | 0:26a151d2c6db | 18 | { |
oprospero | 0:26a151d2c6db | 19 | front = NULL; // Set front to NULL at the start. |
oprospero | 0:26a151d2c6db | 20 | } |
oprospero | 0:26a151d2c6db | 21 | |
oprospero | 0:26a151d2c6db | 22 | /******************************* distructor **********************************/ |
oprospero | 0:26a151d2c6db | 23 | /* Description: */ |
oprospero | 0:26a151d2c6db | 24 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 25 | |
oprospero | 0:26a151d2c6db | 26 | queue::~queue() |
oprospero | 0:26a151d2c6db | 27 | { |
oprospero | 0:26a151d2c6db | 28 | clear(); // Clear the entire queue. |
oprospero | 0:26a151d2c6db | 29 | } |
oprospero | 0:26a151d2c6db | 30 | |
oprospero | 0:26a151d2c6db | 31 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 32 | /* Description: */ |
oprospero | 0:26a151d2c6db | 33 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 34 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 35 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 36 | |
oprospero | 0:26a151d2c6db | 37 | bool queue::isEmpty() |
oprospero | 0:26a151d2c6db | 38 | { |
oprospero | 0:26a151d2c6db | 39 | // Is the queue empty? |
oprospero | 0:26a151d2c6db | 40 | if( front == NULL ) // Check the front pointer. |
oprospero | 0:26a151d2c6db | 41 | return true; // Queue is empty, return true. |
oprospero | 0:26a151d2c6db | 42 | |
oprospero | 0:26a151d2c6db | 43 | return false; // There is atleast one item, not empty. |
oprospero | 0:26a151d2c6db | 44 | } |
oprospero | 0:26a151d2c6db | 45 | |
oprospero | 0:26a151d2c6db | 46 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 47 | /* Description: */ |
oprospero | 0:26a151d2c6db | 48 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 49 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 50 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 51 | |
oprospero | 0:26a151d2c6db | 52 | void queue::clear() |
oprospero | 0:26a151d2c6db | 53 | { |
oprospero | 0:26a151d2c6db | 54 | // Is the list already empty? |
oprospero | 0:26a151d2c6db | 55 | if( isEmpty() ) // Check for an empty list. |
oprospero | 0:26a151d2c6db | 56 | return; // List is empty, don't need to do anything. |
oprospero | 0:26a151d2c6db | 57 | |
oprospero | 0:26a151d2c6db | 58 | queueNode * current = front; // Create node to help step through list. |
oprospero | 0:26a151d2c6db | 59 | queueNode * placeHold = front; // Create node to keep place of delete. |
oprospero | 0:26a151d2c6db | 60 | |
oprospero | 0:26a151d2c6db | 61 | // As long as were not at the end, keep stepping to the next node. |
oprospero | 0:26a151d2c6db | 62 | while( current != NULL) |
oprospero | 0:26a151d2c6db | 63 | { |
oprospero | 0:26a151d2c6db | 64 | placeHold = current->next; // Hold where we have to go. |
oprospero | 0:26a151d2c6db | 65 | delete current; // Delete the node. |
oprospero | 0:26a151d2c6db | 66 | current = placeHold; // Set current to the next node. |
oprospero | 0:26a151d2c6db | 67 | } |
oprospero | 0:26a151d2c6db | 68 | |
oprospero | 0:26a151d2c6db | 69 | front = NULL; // Reset the front to NULL; |
oprospero | 0:26a151d2c6db | 70 | length = 0; |
oprospero | 0:26a151d2c6db | 71 | } |
oprospero | 0:26a151d2c6db | 72 | |
oprospero | 0:26a151d2c6db | 73 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 74 | /* Description: */ |
oprospero | 0:26a151d2c6db | 75 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 76 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 77 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 78 | |
oprospero | 0:26a151d2c6db | 79 | void queue::add( short * item ) |
oprospero | 0:26a151d2c6db | 80 | { |
oprospero | 0:26a151d2c6db | 81 | // Were we passed an invalid object somehow? |
oprospero | 0:26a151d2c6db | 82 | if( item == NULL ) // Check for NULL |
oprospero | 0:26a151d2c6db | 83 | return; // If so, return. |
oprospero | 0:26a151d2c6db | 84 | |
oprospero | 0:26a151d2c6db | 85 | if( queueLength() > MAXQUEUELENGTH ) |
oprospero | 0:26a151d2c6db | 86 | clear(); |
oprospero | 0:26a151d2c6db | 87 | |
oprospero | 0:26a151d2c6db | 88 | queueNode * newNode = new queueNode( item ); // Create the new node. |
oprospero | 0:26a151d2c6db | 89 | |
oprospero | 0:26a151d2c6db | 90 | if( isEmpty() ) |
oprospero | 0:26a151d2c6db | 91 | front = newNode; // Set front to the new node. |
oprospero | 0:26a151d2c6db | 92 | |
oprospero | 0:26a151d2c6db | 93 | else |
oprospero | 0:26a151d2c6db | 94 | { |
oprospero | 0:26a151d2c6db | 95 | queueNode *temp = front; |
oprospero | 0:26a151d2c6db | 96 | while( temp->next != NULL ) |
oprospero | 0:26a151d2c6db | 97 | temp = temp->next; |
oprospero | 0:26a151d2c6db | 98 | |
oprospero | 0:26a151d2c6db | 99 | temp->next = newNode; |
oprospero | 0:26a151d2c6db | 100 | } |
oprospero | 0:26a151d2c6db | 101 | length++; |
oprospero | 0:26a151d2c6db | 102 | } |
oprospero | 0:26a151d2c6db | 103 | |
oprospero | 0:26a151d2c6db | 104 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 105 | /* Description: */ |
oprospero | 0:26a151d2c6db | 106 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 107 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 108 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 109 | |
oprospero | 0:26a151d2c6db | 110 | short * queue::pop() |
oprospero | 0:26a151d2c6db | 111 | { |
oprospero | 0:26a151d2c6db | 112 | // Is the list already empty? |
oprospero | 0:26a151d2c6db | 113 | if( isEmpty() ) // Check for an empty list. |
oprospero | 0:26a151d2c6db | 114 | return NULL; // List is empty, don't need to do anything. |
oprospero | 0:26a151d2c6db | 115 | |
oprospero | 0:26a151d2c6db | 116 | short* dataHold = front->data; // Keep track of what were returning. |
oprospero | 0:26a151d2c6db | 117 | queueNode * oldNode = front; // Save the old node to be deleted |
oprospero | 0:26a151d2c6db | 118 | front = front->next; // Set front to next object. |
oprospero | 0:26a151d2c6db | 119 | |
oprospero | 0:26a151d2c6db | 120 | delete oldNode; // Delete the front node. |
oprospero | 0:26a151d2c6db | 121 | length--; // Remove one from the length. |
oprospero | 0:26a151d2c6db | 122 | return dataHold; // return the stuctureItem. |
oprospero | 0:26a151d2c6db | 123 | } |
oprospero | 0:26a151d2c6db | 124 | |
oprospero | 0:26a151d2c6db | 125 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 126 | /* Description: */ |
oprospero | 0:26a151d2c6db | 127 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 128 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 129 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 130 | |
oprospero | 0:26a151d2c6db | 131 | short * queue::peek() |
oprospero | 0:26a151d2c6db | 132 | { |
oprospero | 0:26a151d2c6db | 133 | if( front != NULL ) |
oprospero | 0:26a151d2c6db | 134 | return front->data; |
oprospero | 0:26a151d2c6db | 135 | |
oprospero | 0:26a151d2c6db | 136 | return NULL; |
oprospero | 0:26a151d2c6db | 137 | } |
oprospero | 0:26a151d2c6db | 138 | |
oprospero | 0:26a151d2c6db | 139 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 140 | /* Description: */ |
oprospero | 0:26a151d2c6db | 141 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 142 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 143 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 144 | |
oprospero | 0:26a151d2c6db | 145 | short queue::queueLength() |
oprospero | 0:26a151d2c6db | 146 | { |
oprospero | 0:26a151d2c6db | 147 | return length; |
oprospero | 0:26a151d2c6db | 148 | } |