ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P

CircularBuf.h

Committer:
drechtmann3
Date:
2018-04-29
Revision:
35:99aa54a25a9e
Parent:
29:0c6f3c0c992a
Child:
36:c3cac3494e9b

File content as of revision 35:99aa54a25a9e:

#pragma once
// Simple "queue" type data structure
// Data is pushed onto the queue to add it to the FIFO buffer
// It is then popped off the queue to remove it from the FIFO buffer
// (It really should be enqueue and dequeue, but it's too late now)
//
// The circular buffer uses an array as the base and acts just like the name
// suggests
// You can push data on one side and remove from the other
// As you add and remove data, the location of the data "moves" around the circle
// https://en.wikipedia.org/wiki/Circular_buffer

template <typename T>
class CircularBuf {
public:
    // Argument:
    // 
    /**
    * @param size Size of the buffer.
    *Assigns a circular buffer of the diven size.
    */
    CircularBuf(unsigned int size);
    ~CircularBuf();
    /**
    Pushes data onto the buffer
    (Adds data to the buffer)
    // Arguement:
    * @param data The array of data to add to the buffer.
    * @param size The size of the array "data"
    //          data, The array of data to add
    //          size, The amount of data in the array
    // Return:
    //          Amount of data actually written
    //
    // Example Code:
    //
    // int dataToAdd[2];
    // dataToAdd[0] = 15;
    // dataToAdd[1] = 23;
    // buffer.push(dataToAdd, 2);
    //
    // buffer now contains [15, 23]
    */
    unsigned int push(T* data, unsigned int size);
    
    /**
    // Pops data from the buffer
    // Arguement:
    //          data, The array of data popped
    //          size, The amount of data to pop
    // Return:
    //          Amount of data actually popped
    //
    // Example Code:
    //
    // int dataToRemove[2];
    // buffer.pop(dataToRemove, 2);
    //
    // dataToRemove now contains the 2 oldest values that were in buffer
    */
    unsigned int pop(T* data, unsigned int size);
    
    // Amount of data in the buffer
    unsigned int size();
    
    // Clears the buffer completely
    void clear();
    
    T* _data;
    // Size of the array
    unsigned int _size;
    // Start pointer
    unsigned int _head;
    // End pointer
    unsigned int _tail;
};