ATParser for EMW3162 wifi module
BufferedSerial/Buffer/MyBuffer.h@0:1b9f0bcf0b2b, 2016-11-02 (annotated)
- Committer:
- Maggie17
- Date:
- Wed Nov 02 03:38:46 2016 +0000
- Revision:
- 0:1b9f0bcf0b2b
ATParse for EMW3162 wifi module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Maggie17 | 0:1b9f0bcf0b2b | 1 | |
Maggie17 | 0:1b9f0bcf0b2b | 2 | /** |
Maggie17 | 0:1b9f0bcf0b2b | 3 | * @file Buffer.h |
Maggie17 | 0:1b9f0bcf0b2b | 4 | * @brief Software Buffer - Templated Ring Buffer for most data types |
Maggie17 | 0:1b9f0bcf0b2b | 5 | * @author sam grove |
Maggie17 | 0:1b9f0bcf0b2b | 6 | * @version 1.0 |
Maggie17 | 0:1b9f0bcf0b2b | 7 | * @see |
Maggie17 | 0:1b9f0bcf0b2b | 8 | * |
Maggie17 | 0:1b9f0bcf0b2b | 9 | * Copyright (c) 2013 |
Maggie17 | 0:1b9f0bcf0b2b | 10 | * |
Maggie17 | 0:1b9f0bcf0b2b | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Maggie17 | 0:1b9f0bcf0b2b | 12 | * you may not use this file except in compliance with the License. |
Maggie17 | 0:1b9f0bcf0b2b | 13 | * You may obtain a copy of the License at |
Maggie17 | 0:1b9f0bcf0b2b | 14 | * |
Maggie17 | 0:1b9f0bcf0b2b | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
Maggie17 | 0:1b9f0bcf0b2b | 16 | * |
Maggie17 | 0:1b9f0bcf0b2b | 17 | * Unless required by applicable law or agreed to in writing, software |
Maggie17 | 0:1b9f0bcf0b2b | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
Maggie17 | 0:1b9f0bcf0b2b | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Maggie17 | 0:1b9f0bcf0b2b | 20 | * See the License for the specific language governing permissions and |
Maggie17 | 0:1b9f0bcf0b2b | 21 | * limitations under the License. |
Maggie17 | 0:1b9f0bcf0b2b | 22 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 23 | |
Maggie17 | 0:1b9f0bcf0b2b | 24 | #ifndef MYBUFFER_H |
Maggie17 | 0:1b9f0bcf0b2b | 25 | #define MYBUFFER_H |
Maggie17 | 0:1b9f0bcf0b2b | 26 | |
Maggie17 | 0:1b9f0bcf0b2b | 27 | #include <stdint.h> |
Maggie17 | 0:1b9f0bcf0b2b | 28 | #include <string.h> |
Maggie17 | 0:1b9f0bcf0b2b | 29 | |
Maggie17 | 0:1b9f0bcf0b2b | 30 | /** A templated software ring buffer |
Maggie17 | 0:1b9f0bcf0b2b | 31 | * |
Maggie17 | 0:1b9f0bcf0b2b | 32 | * Example: |
Maggie17 | 0:1b9f0bcf0b2b | 33 | * @code |
Maggie17 | 0:1b9f0bcf0b2b | 34 | * #include "mbed.h" |
Maggie17 | 0:1b9f0bcf0b2b | 35 | * #include "MyBuffer.h" |
Maggie17 | 0:1b9f0bcf0b2b | 36 | * |
Maggie17 | 0:1b9f0bcf0b2b | 37 | * MyBuffer <char> buf; |
Maggie17 | 0:1b9f0bcf0b2b | 38 | * |
Maggie17 | 0:1b9f0bcf0b2b | 39 | * int main() |
Maggie17 | 0:1b9f0bcf0b2b | 40 | * { |
Maggie17 | 0:1b9f0bcf0b2b | 41 | * buf = 'a'; |
Maggie17 | 0:1b9f0bcf0b2b | 42 | * buf.put('b'); |
Maggie17 | 0:1b9f0bcf0b2b | 43 | * char *head = buf.head(); |
Maggie17 | 0:1b9f0bcf0b2b | 44 | * puts(head); |
Maggie17 | 0:1b9f0bcf0b2b | 45 | * |
Maggie17 | 0:1b9f0bcf0b2b | 46 | * char whats_in_there[2] = {0}; |
Maggie17 | 0:1b9f0bcf0b2b | 47 | * int pos = 0; |
Maggie17 | 0:1b9f0bcf0b2b | 48 | * |
Maggie17 | 0:1b9f0bcf0b2b | 49 | * while(buf.available()) |
Maggie17 | 0:1b9f0bcf0b2b | 50 | * { |
Maggie17 | 0:1b9f0bcf0b2b | 51 | * whats_in_there[pos++] = buf; |
Maggie17 | 0:1b9f0bcf0b2b | 52 | * } |
Maggie17 | 0:1b9f0bcf0b2b | 53 | * printf("%c %c\n", whats_in_there[0], whats_in_there[1]); |
Maggie17 | 0:1b9f0bcf0b2b | 54 | * buf.clear(); |
Maggie17 | 0:1b9f0bcf0b2b | 55 | * error("done\n\n\n"); |
Maggie17 | 0:1b9f0bcf0b2b | 56 | * } |
Maggie17 | 0:1b9f0bcf0b2b | 57 | * @endcode |
Maggie17 | 0:1b9f0bcf0b2b | 58 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 59 | |
Maggie17 | 0:1b9f0bcf0b2b | 60 | template <typename T> |
Maggie17 | 0:1b9f0bcf0b2b | 61 | class MyBuffer |
Maggie17 | 0:1b9f0bcf0b2b | 62 | { |
Maggie17 | 0:1b9f0bcf0b2b | 63 | private: |
Maggie17 | 0:1b9f0bcf0b2b | 64 | T *_buf; |
Maggie17 | 0:1b9f0bcf0b2b | 65 | volatile uint32_t _wloc; |
Maggie17 | 0:1b9f0bcf0b2b | 66 | volatile uint32_t _rloc; |
Maggie17 | 0:1b9f0bcf0b2b | 67 | uint32_t _size; |
Maggie17 | 0:1b9f0bcf0b2b | 68 | |
Maggie17 | 0:1b9f0bcf0b2b | 69 | public: |
Maggie17 | 0:1b9f0bcf0b2b | 70 | /** Create a Buffer and allocate memory for it |
Maggie17 | 0:1b9f0bcf0b2b | 71 | * @param size The size of the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 72 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 73 | MyBuffer(uint32_t size = 0x100); |
Maggie17 | 0:1b9f0bcf0b2b | 74 | |
Maggie17 | 0:1b9f0bcf0b2b | 75 | /** Get the size of the ring buffer |
Maggie17 | 0:1b9f0bcf0b2b | 76 | * @return the size of the ring buffer |
Maggie17 | 0:1b9f0bcf0b2b | 77 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 78 | uint32_t getSize(); |
Maggie17 | 0:1b9f0bcf0b2b | 79 | |
Maggie17 | 0:1b9f0bcf0b2b | 80 | /** Destry a Buffer and release it's allocated memory |
Maggie17 | 0:1b9f0bcf0b2b | 81 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 82 | ~MyBuffer(); |
Maggie17 | 0:1b9f0bcf0b2b | 83 | |
Maggie17 | 0:1b9f0bcf0b2b | 84 | /** Add a data element into the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 85 | * @param data Something to add to the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 86 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 87 | void put(T data); |
Maggie17 | 0:1b9f0bcf0b2b | 88 | |
Maggie17 | 0:1b9f0bcf0b2b | 89 | /** Remove a data element from the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 90 | * @return Pull the oldest element from the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 91 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 92 | T get(void); |
Maggie17 | 0:1b9f0bcf0b2b | 93 | |
Maggie17 | 0:1b9f0bcf0b2b | 94 | /** Get the address to the head of the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 95 | * @return The address of element 0 in the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 96 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 97 | T *head(void); |
Maggie17 | 0:1b9f0bcf0b2b | 98 | |
Maggie17 | 0:1b9f0bcf0b2b | 99 | /** Reset the buffer to 0. Useful if using head() to parse packeted data |
Maggie17 | 0:1b9f0bcf0b2b | 100 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 101 | void clear(void); |
Maggie17 | 0:1b9f0bcf0b2b | 102 | |
Maggie17 | 0:1b9f0bcf0b2b | 103 | /** Determine if anything is readable in the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 104 | * @return 1 if something can be read, 0 otherwise |
Maggie17 | 0:1b9f0bcf0b2b | 105 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 106 | uint32_t available(void); |
Maggie17 | 0:1b9f0bcf0b2b | 107 | |
Maggie17 | 0:1b9f0bcf0b2b | 108 | /** Overloaded operator for writing to the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 109 | * @param data Something to put in the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 110 | * @return |
Maggie17 | 0:1b9f0bcf0b2b | 111 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 112 | MyBuffer &operator= (T data) |
Maggie17 | 0:1b9f0bcf0b2b | 113 | { |
Maggie17 | 0:1b9f0bcf0b2b | 114 | put(data); |
Maggie17 | 0:1b9f0bcf0b2b | 115 | return *this; |
Maggie17 | 0:1b9f0bcf0b2b | 116 | } |
Maggie17 | 0:1b9f0bcf0b2b | 117 | |
Maggie17 | 0:1b9f0bcf0b2b | 118 | /** Overloaded operator for reading from the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 119 | * @return Pull the oldest element from the buffer |
Maggie17 | 0:1b9f0bcf0b2b | 120 | */ |
Maggie17 | 0:1b9f0bcf0b2b | 121 | operator int(void) |
Maggie17 | 0:1b9f0bcf0b2b | 122 | { |
Maggie17 | 0:1b9f0bcf0b2b | 123 | return get(); |
Maggie17 | 0:1b9f0bcf0b2b | 124 | } |
Maggie17 | 0:1b9f0bcf0b2b | 125 | |
Maggie17 | 0:1b9f0bcf0b2b | 126 | uint32_t peek(char c); |
Maggie17 | 0:1b9f0bcf0b2b | 127 | |
Maggie17 | 0:1b9f0bcf0b2b | 128 | }; |
Maggie17 | 0:1b9f0bcf0b2b | 129 | |
Maggie17 | 0:1b9f0bcf0b2b | 130 | template <class T> |
Maggie17 | 0:1b9f0bcf0b2b | 131 | inline void MyBuffer<T>::put(T data) |
Maggie17 | 0:1b9f0bcf0b2b | 132 | { |
Maggie17 | 0:1b9f0bcf0b2b | 133 | _buf[_wloc++] = data; |
Maggie17 | 0:1b9f0bcf0b2b | 134 | _wloc %= (_size-1); |
Maggie17 | 0:1b9f0bcf0b2b | 135 | |
Maggie17 | 0:1b9f0bcf0b2b | 136 | return; |
Maggie17 | 0:1b9f0bcf0b2b | 137 | } |
Maggie17 | 0:1b9f0bcf0b2b | 138 | |
Maggie17 | 0:1b9f0bcf0b2b | 139 | template <class T> |
Maggie17 | 0:1b9f0bcf0b2b | 140 | inline T MyBuffer<T>::get(void) |
Maggie17 | 0:1b9f0bcf0b2b | 141 | { |
Maggie17 | 0:1b9f0bcf0b2b | 142 | T data_pos = _buf[_rloc++]; |
Maggie17 | 0:1b9f0bcf0b2b | 143 | _rloc %= (_size-1); |
Maggie17 | 0:1b9f0bcf0b2b | 144 | |
Maggie17 | 0:1b9f0bcf0b2b | 145 | return data_pos; |
Maggie17 | 0:1b9f0bcf0b2b | 146 | } |
Maggie17 | 0:1b9f0bcf0b2b | 147 | |
Maggie17 | 0:1b9f0bcf0b2b | 148 | template <class T> |
Maggie17 | 0:1b9f0bcf0b2b | 149 | inline T *MyBuffer<T>::head(void) |
Maggie17 | 0:1b9f0bcf0b2b | 150 | { |
Maggie17 | 0:1b9f0bcf0b2b | 151 | T *data_pos = &_buf[0]; |
Maggie17 | 0:1b9f0bcf0b2b | 152 | |
Maggie17 | 0:1b9f0bcf0b2b | 153 | return data_pos; |
Maggie17 | 0:1b9f0bcf0b2b | 154 | } |
Maggie17 | 0:1b9f0bcf0b2b | 155 | |
Maggie17 | 0:1b9f0bcf0b2b | 156 | template <class T> |
Maggie17 | 0:1b9f0bcf0b2b | 157 | inline uint32_t MyBuffer<T>::available(void) |
Maggie17 | 0:1b9f0bcf0b2b | 158 | { |
Maggie17 | 0:1b9f0bcf0b2b | 159 | return (_wloc == _rloc) ? 0 : 1; |
Maggie17 | 0:1b9f0bcf0b2b | 160 | } |
Maggie17 | 0:1b9f0bcf0b2b | 161 | |
Maggie17 | 0:1b9f0bcf0b2b | 162 | #endif |
Maggie17 | 0:1b9f0bcf0b2b | 163 |