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