xbee communication for UWB quadcopter project Originally by Greg Abdo Forking to reduce impact of interrupt by moving packetbuilder out of the interrupt and letting be handled in the main loop
Fork of com by
queue/queue.cpp@22:0e8e22f161ff, 2014-11-02 (annotated)
- Committer:
- oprospero
- Date:
- Sun Nov 02 19:18:11 2014 +0000
- Revision:
- 22:0e8e22f161ff
- Parent:
- 16:89695823d407
Clean up
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 | return false; // There is atleast one item, not empty. |
oprospero | 0:26a151d2c6db | 43 | } |
oprospero | 0:26a151d2c6db | 44 | |
oprospero | 0:26a151d2c6db | 45 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 46 | /* Description: */ |
oprospero | 0:26a151d2c6db | 47 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 48 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 49 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 50 | |
oprospero | 0:26a151d2c6db | 51 | void queue::clear() |
oprospero | 0:26a151d2c6db | 52 | { |
oprospero | 0:26a151d2c6db | 53 | // Is the list already empty? |
oprospero | 0:26a151d2c6db | 54 | if( isEmpty() ) // Check for an empty list. |
oprospero | 0:26a151d2c6db | 55 | return; // List is empty, don't need to do anything. |
oprospero | 0:26a151d2c6db | 56 | |
oprospero | 0:26a151d2c6db | 57 | queueNode * current = front; // Create node to help step through list. |
oprospero | 0:26a151d2c6db | 58 | queueNode * placeHold = front; // Create node to keep place of delete. |
oprospero | 0:26a151d2c6db | 59 | |
oprospero | 0:26a151d2c6db | 60 | // As long as were not at the end, keep stepping to the next node. |
oprospero | 0:26a151d2c6db | 61 | while( current != NULL) |
oprospero | 0:26a151d2c6db | 62 | { |
oprospero | 0:26a151d2c6db | 63 | placeHold = current->next; // Hold where we have to go. |
oprospero | 0:26a151d2c6db | 64 | delete current; // Delete the node. |
oprospero | 0:26a151d2c6db | 65 | current = placeHold; // Set current to the next node. |
oprospero | 0:26a151d2c6db | 66 | } |
oprospero | 0:26a151d2c6db | 67 | |
oprospero | 0:26a151d2c6db | 68 | front = NULL; // Reset the front to NULL; |
oprospero | 0:26a151d2c6db | 69 | length = 0; |
oprospero | 0:26a151d2c6db | 70 | } |
oprospero | 0:26a151d2c6db | 71 | |
oprospero | 0:26a151d2c6db | 72 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 73 | /* Description: */ |
oprospero | 0:26a151d2c6db | 74 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 75 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 76 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 77 | |
oprospero | 0:26a151d2c6db | 78 | void queue::add( short * item ) |
oprospero | 0:26a151d2c6db | 79 | { |
oprospero | 0:26a151d2c6db | 80 | // Were we passed an invalid object somehow? |
oprospero | 0:26a151d2c6db | 81 | if( item == NULL ) // Check for NULL |
oprospero | 0:26a151d2c6db | 82 | return; // If so, return. |
oprospero | 0:26a151d2c6db | 83 | |
oprospero | 0:26a151d2c6db | 84 | if( queueLength() > MAXQUEUELENGTH ) |
oprospero | 8:951c71c71b2d | 85 | delete pop(); |
oprospero | 0:26a151d2c6db | 86 | |
oprospero | 0:26a151d2c6db | 87 | queueNode * newNode = new queueNode( item ); // Create the new node. |
oprospero | 0:26a151d2c6db | 88 | |
oprospero | 0:26a151d2c6db | 89 | if( isEmpty() ) |
oprospero | 0:26a151d2c6db | 90 | front = newNode; // Set front to the new node. |
oprospero | 0:26a151d2c6db | 91 | else |
oprospero | 0:26a151d2c6db | 92 | { |
oprospero | 0:26a151d2c6db | 93 | queueNode *temp = front; |
oprospero | 0:26a151d2c6db | 94 | while( temp->next != NULL ) |
oprospero | 0:26a151d2c6db | 95 | temp = temp->next; |
oprospero | 0:26a151d2c6db | 96 | |
oprospero | 0:26a151d2c6db | 97 | temp->next = newNode; |
oprospero | 0:26a151d2c6db | 98 | } |
oprospero | 0:26a151d2c6db | 99 | length++; |
oprospero | 0:26a151d2c6db | 100 | } |
oprospero | 0:26a151d2c6db | 101 | |
oprospero | 0:26a151d2c6db | 102 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 103 | /* Description: */ |
oprospero | 0:26a151d2c6db | 104 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 105 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 106 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 107 | |
oprospero | 0:26a151d2c6db | 108 | short * queue::pop() |
oprospero | 0:26a151d2c6db | 109 | { |
oprospero | 0:26a151d2c6db | 110 | // Is the list already empty? |
oprospero | 0:26a151d2c6db | 111 | if( isEmpty() ) // Check for an empty list. |
oprospero | 0:26a151d2c6db | 112 | return NULL; // List is empty, don't need to do anything. |
oprospero | 0:26a151d2c6db | 113 | |
oprospero | 0:26a151d2c6db | 114 | short* dataHold = front->data; // Keep track of what were returning. |
oprospero | 0:26a151d2c6db | 115 | queueNode * oldNode = front; // Save the old node to be deleted |
oprospero | 0:26a151d2c6db | 116 | front = front->next; // Set front to next object. |
oprospero | 16:89695823d407 | 117 | |
oprospero | 0:26a151d2c6db | 118 | delete oldNode; // Delete the front node. |
oprospero | 0:26a151d2c6db | 119 | length--; // Remove one from the length. |
oprospero | 0:26a151d2c6db | 120 | return dataHold; // return the stuctureItem. |
oprospero | 0:26a151d2c6db | 121 | } |
oprospero | 0:26a151d2c6db | 122 | |
oprospero | 0:26a151d2c6db | 123 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 124 | /* Description: */ |
oprospero | 0:26a151d2c6db | 125 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 126 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 127 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 128 | |
oprospero | 0:26a151d2c6db | 129 | short * queue::peek() |
oprospero | 0:26a151d2c6db | 130 | { |
oprospero | 0:26a151d2c6db | 131 | if( front != NULL ) |
oprospero | 0:26a151d2c6db | 132 | return front->data; |
oprospero | 0:26a151d2c6db | 133 | |
oprospero | 0:26a151d2c6db | 134 | return NULL; |
oprospero | 0:26a151d2c6db | 135 | } |
oprospero | 0:26a151d2c6db | 136 | |
oprospero | 0:26a151d2c6db | 137 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 138 | /* Description: */ |
oprospero | 0:26a151d2c6db | 139 | /* Accepts: */ |
oprospero | 0:26a151d2c6db | 140 | /* Returns: */ |
oprospero | 0:26a151d2c6db | 141 | /*****************************************************************************/ |
oprospero | 0:26a151d2c6db | 142 | |
oprospero | 0:26a151d2c6db | 143 | short queue::queueLength() |
oprospero | 0:26a151d2c6db | 144 | { |
oprospero | 0:26a151d2c6db | 145 | return length; |
oprospero | 0:26a151d2c6db | 146 | } |