Queue Handler

Dependents:   BLE_ECG kragl kragl_v2 4180_final_project_v2 ... more

Files at this revision

API Documentation at this revision

Comitter:
wbasser
Date:
Fri Oct 22 22:07:25 2010 +0000
Commit message:
Version 01_00_00

Changed in this revision

queue.cpp Show annotated file Show diff for this revision Revisions of this file
queue.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r a03810d46457 queue.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/queue.cpp	Fri Oct 22 22:07:25 2010 +0000
@@ -0,0 +1,185 @@
+/* mbed queue Library
+ * Copyright (c) 2010 William Basser ( wbasser [at] gmail [dot] com )
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// inlcude the defs
+#include "queue.h"
+
+// construction 
+Queue::Queue( int iSize, int iCount )
+{
+    // allocate space for the queue
+    if (( m_pnHead = ( unsigned char* )malloc( iSize * iCount )) != NULL )
+    {
+        // set the tail/clear the indices/set the size and length
+        m_pnTail     = m_pnHead + ( iSize * iCount );
+        m_iSize     = iSize;
+        m_iCount     = iCount;
+        m_pnRdIndex    = m_pnHead;
+        m_pnWrIndex    = m_pnHead;
+        m_iLclCount    = 0;
+    }
+}
+
+// destruction
+Queue::~Queue( )
+{
+    // free the memory
+    free( m_pnHead );
+}
+
+// put an item into the queue
+bool Queue::Put( void* pvItem )
+{
+    bool bResult = false;
+    
+    // check for room
+    if ( m_iLclCount < m_iCount )
+    {
+        __disable_irq( );
+        // copy the item/adjust the pointer/check for overflow
+        memcpy( m_pnWrIndex, pvItem, m_iSize );
+        m_pnWrIndex += m_iSize;
+        if ( m_pnWrIndex >= m_pnTail )
+            m_pnWrIndex = m_pnHead;
+
+        // increment the count
+        m_iLclCount++;
+        __enable_irq( );
+
+        // set the result to 0k
+        bResult = true;
+    }
+
+    // return the status
+    return( bResult );
+}
+
+// get an item from the queue
+bool Queue::Get( void* pvItem )
+{
+    bool bResult = false;
+    
+    // check for room
+    if ( m_iLclCount != 0 )
+    {
+        __disable_irq( );
+        // copy the item/adjust the pointer/check for overflow
+        memcpy( pvItem, m_pnRdIndex, m_iSize );
+        m_pnRdIndex += m_iSize;
+        if ( m_pnRdIndex >= m_pnTail )
+            m_pnRdIndex = m_pnHead;
+
+        // decrement the count
+        m_iLclCount--;
+        __enable_irq( );
+        // set the result to 0k
+        bResult = true;
+    }
+
+    // return the status
+    return( bResult );
+}
+
+// put an item into the queue
+bool Queue::PutIrq( void* pvItem )
+{
+    bool bResult = false;
+    
+    // check for room
+    if ( m_iLclCount < m_iCount )
+    {
+        // copy the item/adjust the pointer/check for overflow
+        memcpy( m_pnWrIndex, pvItem, m_iSize );
+        m_pnWrIndex += m_iSize;
+        if ( m_pnWrIndex >= m_pnTail )
+            m_pnWrIndex = m_pnHead;
+
+        // increment the count
+        m_iLclCount++;
+
+        // set the result to 0k
+        bResult = true;
+    }
+
+    // return the status
+    return( bResult );
+}
+
+// get an item from the queue
+bool Queue::GetIrq( void* pvItem )
+{
+    bool bResult = false;
+    
+    // check for room
+    if ( m_iLclCount != 0 )
+    {
+        // copy the item/adjust the pointer/check for overflow
+        memcpy( pvItem, m_pnRdIndex, m_iSize );
+        m_pnRdIndex += m_iSize;
+        if ( m_pnRdIndex >= m_pnTail )
+            m_pnRdIndex = m_pnHead;
+
+        // decrement the count
+        m_iLclCount--;
+
+        // set the result to 0k
+        bResult = true;
+    }
+
+    // return the status
+    return( bResult );
+}
+
+// get the number of items in the queue
+int Queue::GetNumberOfItems( void )
+{
+    // return the count
+    return( m_iLclCount );
+}
+
+// peeks at the item at the top of the queue
+bool Queue::Peek( void* pvItem )
+{
+    bool bResult = false;
+    
+    // check for room
+    if ( m_iLclCount != 0 )
+    {
+        // copy the item
+        memcpy( pvItem, m_pnRdIndex, m_iSize );
+
+        // set the result to 0k
+        bResult = true;
+    }
+
+    // return the status
+    return( bResult );
+}
+
+// flush all items from the queue
+void Queue::Flush( void )
+{
+    // reset the indices
+    m_iLclCount = 0;
+    m_pnRdIndex = m_pnWrIndex = m_pnHead;
+}
+
diff -r 000000000000 -r a03810d46457 queue.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/queue.h	Fri Oct 22 22:07:25 2010 +0000
@@ -0,0 +1,149 @@
+/* mbed Queue Library
+ * Copyright (c) 2010 William Basser ( wbasser [at] gmail [dot] com )
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MBED_QUEUE_H
+#define MBED_QUEUE_H
+
+#include "mbed.h"
+
+/** queue modle
+ *
+ * Example:
+ * @code
+ *
+ * #include "mbed.h"
+ *
+ * // include the queue library
+ * #include "queue.h"
+ *
+ * Serial myPc( USBTX, USBRX );
+ * Queue myQueue( 1, 5 );
+ *
+ * int main() 
+ * {
+ *    unsigned char   nTemp;
+ *   
+ *    // display the number of items in the queue
+ *    myPc.printf( "Items in queue: %d\r", myQueue.GetNumberOfItems( ));
+ *    
+ *    // add item to queue
+ *    nTemp = 5;
+ *    myQueue.Put( &nTemp );
+ *    nTemp = 6;
+ *    myQueue.Put( &nTemp );
+ *    nTemp = 7;
+ *    myQueue.Put( &nTemp );
+ *    
+ *    // display the number of items in the queue
+ *    myPc.printf( "Items in queue: %d\r", myQueue.GetNumberOfItems( ));
+ *    
+ *    // peek at item at the top of the queue
+ *    myQueue.Peek( &nTemp );
+ *    myPc.printf( "Peek: %d\r", nTemp );
+ *    
+ *    // get an item from queue
+ *    myQueue.Get( &nTemp );
+ *    myPc.printf( "Item 0 = %d\r", nTemp );
+ *    myQueue.Get( &nTemp );
+ *    myPc.printf( "Item 1 = %d\r", nTemp );
+ *    
+ *    // queue should be empty
+ *    if ( !myQueue.Get( &nTemp ))
+ *    {
+ *        // queue is empty
+ *        myPc.printf( "Queue empty!\r" );
+ *    }
+ * }
+ *
+ * @endcode
+ */
+
+class Queue {
+public:
+    /** Create a Queue object
+     *
+     * @param iSize size of the object in queue
+     * @param iCount number of items in the queue
+     */
+    Queue( int iSize, int iCount );
+
+    /** destruction
+     *
+     */
+    virtual ~Queue( void );
+
+    /** Add item to queue
+     *
+     * @param pvItem item to add
+     * @returns true if item added, false if queue full
+     */
+    bool Put( void* pvItem );
+
+    /** get an item from the queue in an IRQ handler
+     *
+     * @param pvItem pointer to the tiem to retrieve
+     * @returns true if item fetched, false if queue is empty
+     */
+    bool GetIrq( void* pvItem );
+    
+    /** Add item to queue from an IRQ handler
+     *
+     * @param pvItem item to add
+     * @returns true if item added, false if queue full
+     */
+    bool PutIrq( void* pvItem );
+
+    /** get an item from the queue
+     *
+     * @param pvItem pointer to the tiem to retrieve
+     * @returns true if item fetched, false if queue is empty
+     */
+    bool Get( void* pvItem );
+    
+    /** get the number of items in the queue
+     * 
+     * @returns the number of items in the queue
+     */
+    int GetNumberOfItems( void );
+    
+    /** peek at the entry at the top of the queue
+     * 
+     * @returns the entry at the top of the queue
+     */
+    bool Peek( void* pvItem );    
+
+    /** flush the queue
+     *
+     */
+    void Flush( void );
+
+
+protected:
+    int              m_iSize;        // size of each item in queue
+    int              m_iCount;        // number of items in the queue
+    unsigned char*   m_pnHead;        // pointer to the head of the queue
+    unsigned char*   m_pnTail;        // pointer to the tail of the queue
+    unsigned char*   m_pnRdIndex;    // read index
+    unsigned char*   m_pnWrIndex;    // write index
+    int              m_iLclCount;    // number of items in queue
+};
+#endif