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
Diff: utils/MTSCircularBuffer.h
- Revision:
- 0:563b70517320
- Child:
- 2:8d3ea0dfce39
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils/MTSCircularBuffer.h Mon Dec 09 15:29:35 2013 +0000 @@ -0,0 +1,120 @@ +#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 the byte or -1 if no byte was available. + */ + int read(); + + /** 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 capacity in the buffer or -1 if the write failed + * because the buffer was full. + */ + int write(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 capacity in the buffer 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); + + void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op); + + /** 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 */ \ No newline at end of file