Axeda Ready Demo for Freescale FRDM-KL46Z as accident alert system
Dependencies: FRDM_MMA8451Q KL46Z-USBHost MAG3110 SocketModem TSI mbed FATFileSystem
Fork of AxedaGo-Freescal_FRDM-KL46Z revert by
SocketModem/utils/MTSCircularBuffer.h@1:5ad12c581db4, 2014-07-02 (annotated)
- Committer:
- AxedaCorp
- Date:
- Wed Jul 02 15:59:38 2014 +0000
- Revision:
- 1:5ad12c581db4
- Parent:
- 0:65004368569c
url ip switch
;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:65004368569c | 1 | /* Universal Socket Modem Interface Library |
AxedaCorp | 0:65004368569c | 2 | * Copyright (c) 2013 Multi-Tech Systems |
AxedaCorp | 0:65004368569c | 3 | * |
AxedaCorp | 0:65004368569c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AxedaCorp | 0:65004368569c | 5 | * you may not use this file except in compliance with the License. |
AxedaCorp | 0:65004368569c | 6 | * You may obtain a copy of the License at |
AxedaCorp | 0:65004368569c | 7 | * |
AxedaCorp | 0:65004368569c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AxedaCorp | 0:65004368569c | 9 | * |
AxedaCorp | 0:65004368569c | 10 | * Unless required by applicable law or agreed to in writing, software |
AxedaCorp | 0:65004368569c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AxedaCorp | 0:65004368569c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AxedaCorp | 0:65004368569c | 13 | * See the License for the specific language governing permissions and |
AxedaCorp | 0:65004368569c | 14 | * limitations under the License. |
AxedaCorp | 0:65004368569c | 15 | */ |
AxedaCorp | 0:65004368569c | 16 | |
AxedaCorp | 0:65004368569c | 17 | #ifndef MTSCIRCULARBUFFER_H |
AxedaCorp | 0:65004368569c | 18 | #define MTSCIRCULARBUFFER_H |
AxedaCorp | 0:65004368569c | 19 | |
AxedaCorp | 0:65004368569c | 20 | #include "mbed.h" |
AxedaCorp | 0:65004368569c | 21 | #include "Vars.h" |
AxedaCorp | 0:65004368569c | 22 | |
AxedaCorp | 0:65004368569c | 23 | namespace mts { |
AxedaCorp | 0:65004368569c | 24 | |
AxedaCorp | 0:65004368569c | 25 | /** This class provides a circular byte buffer meant for temporary storage |
AxedaCorp | 0:65004368569c | 26 | * during IO transactions. It contains many of the common methods you |
AxedaCorp | 0:65004368569c | 27 | * would expect from a circular buffer like read, write, and various |
AxedaCorp | 0:65004368569c | 28 | * methods for checking the size or status. It should be noted that |
AxedaCorp | 0:65004368569c | 29 | * this class does not include any special code for thread safety like |
AxedaCorp | 0:65004368569c | 30 | * a lock. In most cases this is not problematic, but is something |
AxedaCorp | 0:65004368569c | 31 | * to be aware of. |
AxedaCorp | 0:65004368569c | 32 | */ |
AxedaCorp | 0:65004368569c | 33 | class MTSCircularBuffer |
AxedaCorp | 0:65004368569c | 34 | { |
AxedaCorp | 0:65004368569c | 35 | public: |
AxedaCorp | 0:65004368569c | 36 | /** Creates an MTSCircularBuffer object with the specified static size. |
AxedaCorp | 0:65004368569c | 37 | * |
AxedaCorp | 0:65004368569c | 38 | * @prarm bufferSize size of the buffer in bytes. |
AxedaCorp | 0:65004368569c | 39 | */ |
AxedaCorp | 0:65004368569c | 40 | MTSCircularBuffer(int bufferSize); |
AxedaCorp | 0:65004368569c | 41 | |
AxedaCorp | 0:65004368569c | 42 | /** Destructs an MTSCircularBuffer object and frees all related resources. |
AxedaCorp | 0:65004368569c | 43 | */ |
AxedaCorp | 0:65004368569c | 44 | ~MTSCircularBuffer(); |
AxedaCorp | 0:65004368569c | 45 | |
AxedaCorp | 0:65004368569c | 46 | /** This method enables bulk reads from the buffer. If more data is |
AxedaCorp | 0:65004368569c | 47 | * requested then available it simply returns all remaining data within the |
AxedaCorp | 0:65004368569c | 48 | * buffer. |
AxedaCorp | 0:65004368569c | 49 | * |
AxedaCorp | 0:65004368569c | 50 | * @param data the buffer where data read will be added to. |
AxedaCorp | 0:65004368569c | 51 | * @param length the amount of data in bytes to be read into the buffer. |
AxedaCorp | 0:65004368569c | 52 | * @returns the total number of bytes that were read. |
AxedaCorp | 0:65004368569c | 53 | */ |
AxedaCorp | 0:65004368569c | 54 | int read(char* data, int length); |
AxedaCorp | 0:65004368569c | 55 | |
AxedaCorp | 0:65004368569c | 56 | /** This method reads a single byte from the buffer. |
AxedaCorp | 0:65004368569c | 57 | * |
AxedaCorp | 0:65004368569c | 58 | * @param data char where the read byte will be stored. |
AxedaCorp | 0:65004368569c | 59 | * @returns 1 if byte is read or 0 if no bytes available. |
AxedaCorp | 0:65004368569c | 60 | */ |
AxedaCorp | 0:65004368569c | 61 | int read(char& data); |
AxedaCorp | 0:65004368569c | 62 | |
AxedaCorp | 0:65004368569c | 63 | /** This method enables bulk writes to the buffer. If more data |
AxedaCorp | 0:65004368569c | 64 | * is requested to be written then space available the method writes |
AxedaCorp | 0:65004368569c | 65 | * as much data as possible and returns the actual amount written. |
AxedaCorp | 0:65004368569c | 66 | * |
AxedaCorp | 0:65004368569c | 67 | * @param data the byte array to be written. |
AxedaCorp | 0:65004368569c | 68 | * @param length the length of data to be written from the data paramter. |
AxedaCorp | 0:65004368569c | 69 | * @returns the number of bytes written to the buffer, which is 0 if |
AxedaCorp | 0:65004368569c | 70 | * the buffer is full. |
AxedaCorp | 0:65004368569c | 71 | */ |
AxedaCorp | 0:65004368569c | 72 | int write(const char* data, int length); |
AxedaCorp | 0:65004368569c | 73 | |
AxedaCorp | 0:65004368569c | 74 | /** This method writes a signle byte as a char to the buffer. |
AxedaCorp | 0:65004368569c | 75 | * |
AxedaCorp | 0:65004368569c | 76 | * @param data the byte to be written as a char. |
AxedaCorp | 0:65004368569c | 77 | * @returns 1 if the byte was written or 0 if the buffer was full. |
AxedaCorp | 0:65004368569c | 78 | */ |
AxedaCorp | 0:65004368569c | 79 | int write(char data); |
AxedaCorp | 0:65004368569c | 80 | |
AxedaCorp | 0:65004368569c | 81 | /** This method is used to setup a callback funtion when the buffer reaches |
AxedaCorp | 0:65004368569c | 82 | * a certain threshold. The threshold condition is checked after every read |
AxedaCorp | 0:65004368569c | 83 | * and write call is completed. The condition is made up of both a threshold |
AxedaCorp | 0:65004368569c | 84 | * value and operator. An example that would trigger a callback is if the |
AxedaCorp | 0:65004368569c | 85 | * threshold was 10, the operator GREATER, and there were 12 bytes added to an |
AxedaCorp | 0:65004368569c | 86 | * empty buffer. |
AxedaCorp | 0:65004368569c | 87 | * |
AxedaCorp | 0:65004368569c | 88 | * @param tptr a pointer to the object to be called when the condition is met. |
AxedaCorp | 0:65004368569c | 89 | * @param mptr a pointer to the function within the object to be called when |
AxedaCorp | 0:65004368569c | 90 | * the condition is met. |
AxedaCorp | 0:65004368569c | 91 | * @param threshold the value in bytes to be used as part of the condition. |
AxedaCorp | 0:65004368569c | 92 | * @param op the operator to be used in conjunction with the threshold |
AxedaCorp | 0:65004368569c | 93 | * as part of the condition. |
AxedaCorp | 0:65004368569c | 94 | */ |
AxedaCorp | 0:65004368569c | 95 | template<typename T> |
AxedaCorp | 0:65004368569c | 96 | void attach(T *tptr, void( T::*mptr)(void), int threshold, Vars::RelationalOperator op) |
AxedaCorp | 0:65004368569c | 97 | { |
AxedaCorp | 0:65004368569c | 98 | _threshold = threshold; |
AxedaCorp | 0:65004368569c | 99 | _op = op; |
AxedaCorp | 0:65004368569c | 100 | notify.attach(tptr, mptr); |
AxedaCorp | 0:65004368569c | 101 | } |
AxedaCorp | 0:65004368569c | 102 | |
AxedaCorp | 0:65004368569c | 103 | /** This method is used to setup a callback funtion when the buffer reaches |
AxedaCorp | 0:65004368569c | 104 | * a certain threshold. The threshold condition is checked after every read |
AxedaCorp | 0:65004368569c | 105 | * and write call is completed. The condition is made up of both a threshold |
AxedaCorp | 0:65004368569c | 106 | * value and operator. An example that would trigger a callback is if the |
AxedaCorp | 0:65004368569c | 107 | * threshold was 10, the operator GREATER, and there were 12 bytes added to an |
AxedaCorp | 0:65004368569c | 108 | * empty buffer. |
AxedaCorp | 0:65004368569c | 109 | * |
AxedaCorp | 0:65004368569c | 110 | * @param fptr a pointer to the static function to be called when the condition |
AxedaCorp | 0:65004368569c | 111 | * is met. |
AxedaCorp | 0:65004368569c | 112 | * @param threshold the value in bytes to be used as part of the condition. |
AxedaCorp | 0:65004368569c | 113 | * @param op the operator to be used in conjunction with the threshold |
AxedaCorp | 0:65004368569c | 114 | * as part of the condition. |
AxedaCorp | 0:65004368569c | 115 | */ |
AxedaCorp | 0:65004368569c | 116 | void attach(void(*fptr)(void), int threshold, Vars::RelationalOperator op) |
AxedaCorp | 0:65004368569c | 117 | { |
AxedaCorp | 0:65004368569c | 118 | _threshold = threshold; |
AxedaCorp | 0:65004368569c | 119 | _op = op; |
AxedaCorp | 0:65004368569c | 120 | notify.attach(fptr); |
AxedaCorp | 0:65004368569c | 121 | } |
AxedaCorp | 0:65004368569c | 122 | |
AxedaCorp | 0:65004368569c | 123 | /** This method returns the size of the storage space currently allocated for |
AxedaCorp | 0:65004368569c | 124 | * the buffer. This value is equivalent to the one passed into the constructor. |
AxedaCorp | 0:65004368569c | 125 | * This value is equal or greater than the size() of the buffer. |
AxedaCorp | 0:65004368569c | 126 | * |
AxedaCorp | 0:65004368569c | 127 | * @returns the allocated size of the buffer in bytes. |
AxedaCorp | 0:65004368569c | 128 | */ |
AxedaCorp | 0:65004368569c | 129 | int capacity(); |
AxedaCorp | 0:65004368569c | 130 | |
AxedaCorp | 0:65004368569c | 131 | /** This method returns the amount of space left for writing. |
AxedaCorp | 0:65004368569c | 132 | * |
AxedaCorp | 0:65004368569c | 133 | * @returns numbers of unused bytes in buffer. |
AxedaCorp | 0:65004368569c | 134 | */ |
AxedaCorp | 0:65004368569c | 135 | int remaining(); |
AxedaCorp | 0:65004368569c | 136 | |
AxedaCorp | 0:65004368569c | 137 | /** This method returns the number of bytes available for reading. |
AxedaCorp | 0:65004368569c | 138 | * |
AxedaCorp | 0:65004368569c | 139 | * @returns number of bytes currently in buffer. |
AxedaCorp | 0:65004368569c | 140 | */ |
AxedaCorp | 0:65004368569c | 141 | int size(); |
AxedaCorp | 0:65004368569c | 142 | |
AxedaCorp | 0:65004368569c | 143 | /** This method returns whether the buffer is empty. |
AxedaCorp | 0:65004368569c | 144 | * |
AxedaCorp | 0:65004368569c | 145 | * @returns true if empty, otherwise false. |
AxedaCorp | 0:65004368569c | 146 | */ |
AxedaCorp | 0:65004368569c | 147 | bool isEmpty(); |
AxedaCorp | 0:65004368569c | 148 | |
AxedaCorp | 0:65004368569c | 149 | /** This method returns whether the buffer is full. |
AxedaCorp | 0:65004368569c | 150 | * |
AxedaCorp | 0:65004368569c | 151 | * @returns true if full, otherwise false. |
AxedaCorp | 0:65004368569c | 152 | */ |
AxedaCorp | 0:65004368569c | 153 | bool isFull(); |
AxedaCorp | 0:65004368569c | 154 | |
AxedaCorp | 0:65004368569c | 155 | /** This method clears the buffer. This is done through |
AxedaCorp | 0:65004368569c | 156 | * setting the internal read and write indexes to the same |
AxedaCorp | 0:65004368569c | 157 | * value and is therefore not an expensive operation. |
AxedaCorp | 0:65004368569c | 158 | */ |
AxedaCorp | 0:65004368569c | 159 | void clear(); |
AxedaCorp | 0:65004368569c | 160 | |
AxedaCorp | 0:65004368569c | 161 | |
AxedaCorp | 0:65004368569c | 162 | private: |
AxedaCorp | 0:65004368569c | 163 | int bufferSize; // total size of the buffer |
AxedaCorp | 0:65004368569c | 164 | char* buffer; // internal byte buffer as a character buffer |
AxedaCorp | 0:65004368569c | 165 | int readIndex; // read index for circular buffer |
AxedaCorp | 0:65004368569c | 166 | int writeIndex; // write index for circular buffer |
AxedaCorp | 0:65004368569c | 167 | int bytes; // available data |
AxedaCorp | 0:65004368569c | 168 | FunctionPointer notify; // function pointer used for the internal callback notification |
AxedaCorp | 0:65004368569c | 169 | int _threshold; // threshold for the notification |
AxedaCorp | 0:65004368569c | 170 | Vars::RelationalOperator _op; // operator that determines the direction of the threshold |
AxedaCorp | 0:65004368569c | 171 | void checkThreshold(); // private function that checks thresholds and processes notifications |
AxedaCorp | 0:65004368569c | 172 | }; |
AxedaCorp | 0:65004368569c | 173 | |
AxedaCorp | 0:65004368569c | 174 | } |
AxedaCorp | 0:65004368569c | 175 | |
AxedaCorp | 0:65004368569c | 176 | #endif /* MTSCIRCULARBUFFER_H */ |