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