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

Committer:
jengbrecht
Date:
Mon Dec 09 15:29:35 2013 +0000
Revision:
0:563b70517320
Child:
2:8d3ea0dfce39
Initial Commit Library!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jengbrecht 0:563b70517320 1 #ifndef MTSCIRCULARBUFFER_H
jengbrecht 0:563b70517320 2 #define MTSCIRCULARBUFFER_H
jengbrecht 0:563b70517320 3
jengbrecht 0:563b70517320 4 #include "mbed.h"
jengbrecht 0:563b70517320 5 #include "Vars.h"
jengbrecht 0:563b70517320 6
jengbrecht 0:563b70517320 7 /** This class provides a circular byte buffer meant for temporary storage
jengbrecht 0:563b70517320 8 * during IO transactions. It contains many of the common methods you
jengbrecht 0:563b70517320 9 * would expect from a circular buffer like read, write, and various
jengbrecht 0:563b70517320 10 * methods for checking the size or status. It should be noted that
jengbrecht 0:563b70517320 11 * this class does not include any special code for thread safety like
jengbrecht 0:563b70517320 12 * a lock. In most cases this is not problematic, but is something
jengbrecht 0:563b70517320 13 * to be aware of.
jengbrecht 0:563b70517320 14 */
jengbrecht 0:563b70517320 15 class MTSCircularBuffer
jengbrecht 0:563b70517320 16 {
jengbrecht 0:563b70517320 17 public:
jengbrecht 0:563b70517320 18 /** Creates an MTSCircularBuffer object with the specified static size.
jengbrecht 0:563b70517320 19 *
jengbrecht 0:563b70517320 20 * @prarm bufferSize size of the buffer in bytes.
jengbrecht 0:563b70517320 21 */
jengbrecht 0:563b70517320 22 MTSCircularBuffer(int bufferSize);
jengbrecht 0:563b70517320 23
jengbrecht 0:563b70517320 24 /** Destructs an MTSCircularBuffer object and frees all related resources.
jengbrecht 0:563b70517320 25 */
jengbrecht 0:563b70517320 26 ~MTSCircularBuffer();
jengbrecht 0:563b70517320 27
jengbrecht 0:563b70517320 28 /** This method enables bulk reads from the buffer. If more data is
jengbrecht 0:563b70517320 29 * requested then available it simply returns all remaining data within the
jengbrecht 0:563b70517320 30 * buffer.
jengbrecht 0:563b70517320 31 *
jengbrecht 0:563b70517320 32 * @param data the buffer where data read will be added to.
jengbrecht 0:563b70517320 33 * @param length the amount of data in bytes to be read into the buffer.
jengbrecht 0:563b70517320 34 * @returns the total number of bytes that were read.
jengbrecht 0:563b70517320 35 */
jengbrecht 0:563b70517320 36 int read(char* data, int length);
jengbrecht 0:563b70517320 37
jengbrecht 0:563b70517320 38 /** This method reads a single byte from the buffer.
jengbrecht 0:563b70517320 39 *
jengbrecht 0:563b70517320 40 * @returns the byte or -1 if no byte was available.
jengbrecht 0:563b70517320 41 */
jengbrecht 0:563b70517320 42 int read();
jengbrecht 0:563b70517320 43
jengbrecht 0:563b70517320 44 /** This method enables bulk writes to the buffer. If more data
jengbrecht 0:563b70517320 45 * is requested to be written then space available no data is written
jengbrecht 0:563b70517320 46 * and an error value returned.
jengbrecht 0:563b70517320 47 *
jengbrecht 0:563b70517320 48 * @param data the byte array to be written.
jengbrecht 0:563b70517320 49 * @param length the length of data to be written from the data paramter.
jengbrecht 0:563b70517320 50 * @returns the capacity in the buffer or -1 if the write failed
jengbrecht 0:563b70517320 51 * because the buffer was full.
jengbrecht 0:563b70517320 52 */
jengbrecht 0:563b70517320 53 int write(char* data, int length);
jengbrecht 0:563b70517320 54
jengbrecht 0:563b70517320 55 /** This method writes a signle byte as a char to the buffer.
jengbrecht 0:563b70517320 56 *
jengbrecht 0:563b70517320 57 * @param data the byte to be written as a char.
jengbrecht 0:563b70517320 58 * @returns the capacity in the buffer or -1 if the write failed
jengbrecht 0:563b70517320 59 * because the buffer was full.
jengbrecht 0:563b70517320 60 */
jengbrecht 0:563b70517320 61 int write(char data);
jengbrecht 0:563b70517320 62
jengbrecht 0:563b70517320 63 /**
jengbrecht 0:563b70517320 64 *
jengbrecht 0:563b70517320 65 */
jengbrecht 0:563b70517320 66 template<typename T>
jengbrecht 0:563b70517320 67 void attach(T *tptr, void(T::*mptr)(void), int threshold, Vars::RelationalOperator op);
jengbrecht 0:563b70517320 68
jengbrecht 0:563b70517320 69 void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op);
jengbrecht 0:563b70517320 70
jengbrecht 0:563b70517320 71 /** This method returns the static size of the buffer. This value
jengbrecht 0:563b70517320 72 * is equivalent to the one passed into the constructor.
jengbrecht 0:563b70517320 73 *
jengbrecht 0:563b70517320 74 * @returns the static size of the buffer in bytes.
jengbrecht 0:563b70517320 75 */
jengbrecht 0:563b70517320 76 int getSize();
jengbrecht 0:563b70517320 77
jengbrecht 0:563b70517320 78 /** This method returns the amount of space left for writing.
jengbrecht 0:563b70517320 79 *
jengbrecht 0:563b70517320 80 * @returns numbers of unused bytes in buffer.
jengbrecht 0:563b70517320 81 */
jengbrecht 0:563b70517320 82 int capacity();
jengbrecht 0:563b70517320 83
jengbrecht 0:563b70517320 84 /** This method returns the number of bytes available for reading.
jengbrecht 0:563b70517320 85 *
jengbrecht 0:563b70517320 86 * @returns number of bytes currently in buffer.
jengbrecht 0:563b70517320 87 */
jengbrecht 0:563b70517320 88 int available();
jengbrecht 0:563b70517320 89
jengbrecht 0:563b70517320 90 /** This method returns whether the buffer is empty.
jengbrecht 0:563b70517320 91 *
jengbrecht 0:563b70517320 92 * @returns true if empty, otherwise false.
jengbrecht 0:563b70517320 93 */
jengbrecht 0:563b70517320 94 bool isEmpty();
jengbrecht 0:563b70517320 95
jengbrecht 0:563b70517320 96 /** This method returns whether the buffer is full.
jengbrecht 0:563b70517320 97 *
jengbrecht 0:563b70517320 98 * @returns true if full, otherwise false.
jengbrecht 0:563b70517320 99 */
jengbrecht 0:563b70517320 100 bool isFull();
jengbrecht 0:563b70517320 101
jengbrecht 0:563b70517320 102 /** This method clears the buffer. This is done through
jengbrecht 0:563b70517320 103 * setting the internal read and write indexes to the same
jengbrecht 0:563b70517320 104 * value and is therefore not an expensive operation.
jengbrecht 0:563b70517320 105 */
jengbrecht 0:563b70517320 106 void clear();
jengbrecht 0:563b70517320 107
jengbrecht 0:563b70517320 108
jengbrecht 0:563b70517320 109 private:
jengbrecht 0:563b70517320 110 int bufferSize; // total size of the buffer
jengbrecht 0:563b70517320 111 char* buffer; // internal byte buffer as a character buffer
jengbrecht 0:563b70517320 112 int readIndex; // read index for circular buffer
jengbrecht 0:563b70517320 113 int writeIndex; // write index for circular buffer
jengbrecht 0:563b70517320 114 FunctionPointer notify; // function pointer used for the internal callback notification
jengbrecht 0:563b70517320 115 int threshold; // threshold for the notification
jengbrecht 0:563b70517320 116 Vars::RelationalOperator op; // operator that determines the direction of the threshold
jengbrecht 0:563b70517320 117 void checkThreshold(); // private function that checks thresholds and processes notifications
jengbrecht 0:563b70517320 118 };
jengbrecht 0:563b70517320 119
jengbrecht 0:563b70517320 120 #endif /* MTSCIRCULARBUFFER_H */