Irfan Tito Kurniawan / ros_lib
Committer:
irfantitok
Date:
Wed Sep 02 13:51:31 2020 +0000
Revision:
0:8f3710bfd298
Resolved round not found

Who changed what in which revision?

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