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