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