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