A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
utils/MTSCircularBuffer.h
- Committer:
- sgodinez
- Date:
- 2013-12-19
- Revision:
- 41:81d035fb0b6a
- Parent:
- 17:2d7c4ea7491b
- Child:
- 43:3cacf019ed7d
File content as of revision 41:81d035fb0b6a:
#ifndef MTSCIRCULARBUFFER_H #define MTSCIRCULARBUFFER_H #include "mbed.h" #include "Vars.h" /** This class provides a circular byte buffer meant for temporary storage * during IO transactions. It contains many of the common methods you * would expect from a circular buffer like read, write, and various * methods for checking the size or status. It should be noted that * this class does not include any special code for thread safety like * a lock. In most cases this is not problematic, but is something * to be aware of. */ class MTSCircularBuffer { public: /** Creates an MTSCircularBuffer object with the specified static size. * * @prarm bufferSize size of the buffer in bytes. */ MTSCircularBuffer(int bufferSize); /** Destructs an MTSCircularBuffer object and frees all related resources. */ ~MTSCircularBuffer(); /** This method enables bulk reads from the buffer. If more data is * requested then available it simply returns all remaining data within the * buffer. * * @param data the buffer where data read will be added to. * @param length the amount of data in bytes to be read into the buffer. * @returns the total number of bytes that were read. */ int read(char* data, int length); /** This method reads a single byte from the buffer. * * @returns length read or -1 if error. */ int read(char& data); /** This method enables bulk writes to the buffer. If more data * is requested to be written then space available no data is written * and an error value returned. * * @param data the byte array to be written. * @param length the length of data to be written from the data paramter. * @returns the number of bytes written in the buffer or -1 if the write failed * because the buffer was full. */ int write(const char* data, int length); /** This method writes a signle byte as a char to the buffer. * * @param data the byte to be written as a char. * @returns the number of bytes written or -1 if the write failed * because the buffer was full. */ int write(char data); /** * */ template<typename T> void attach(T *tptr, void( T::*mptr)(void), int threshold, Vars::RelationalOperator op) { _threshold = threshold; _op = op; notify.attach(tptr, mptr); } void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op) { _threshold = threshold; _op = op; notify.attach(fptr); } /** This method returns the static size of the buffer. This value * is equivalent to the one passed into the constructor. * * @returns the static size of the buffer in bytes. */ int getSize(); /** This method returns the amount of space left for writing. * * @returns numbers of unused bytes in buffer. */ int capacity(); /** This method returns the number of bytes available for reading. * * @returns number of bytes currently in buffer. */ int available(); /** This method returns whether the buffer is empty. * * @returns true if empty, otherwise false. */ bool isEmpty(); /** This method returns whether the buffer is full. * * @returns true if full, otherwise false. */ bool isFull(); /** This method clears the buffer. This is done through * setting the internal read and write indexes to the same * value and is therefore not an expensive operation. */ void clear(); private: int bufferSize; // total size of the buffer char* buffer; // internal byte buffer as a character buffer int readIndex; // read index for circular buffer int writeIndex; // write index for circular buffer FunctionPointer notify; // function pointer used for the internal callback notification int _threshold; // threshold for the notification Vars::RelationalOperator _op; // operator that determines the direction of the threshold void checkThreshold(); // private function that checks thresholds and processes notifications }; #endif /* MTSCIRCULARBUFFER_H */