Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MyBuffer.h
00001 00002 /** 00003 * @file Buffer.h 00004 * @brief Software Buffer - Templated Ring Buffer for most data types 00005 * @author sam grove 00006 * @version 1.0 00007 * @see 00008 * 00009 * Copyright (c) 2013 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 */ 00023 00024 #ifndef MYBUFFER_H 00025 #define MYBUFFER_H 00026 00027 #include <stdint.h> 00028 #include <string.h> 00029 00030 /** A templated software ring buffer 00031 * 00032 * Example: 00033 * @code 00034 * #include "mbed.h" 00035 * #include "MyBuffer.h" 00036 * 00037 * MyBuffer <char> buf; 00038 * 00039 * int main() 00040 * { 00041 * buf = 'a'; 00042 * buf.put('b'); 00043 * char *head = buf.head(); 00044 * puts(head); 00045 * 00046 * char whats_in_there[2] = {0}; 00047 * int pos = 0; 00048 * 00049 * while(buf.available()) 00050 * { 00051 * whats_in_there[pos++] = buf; 00052 * } 00053 * printf("%c %c\n", whats_in_there[0], whats_in_there[1]); 00054 * buf.clear(); 00055 * error("done\n\n\n"); 00056 * } 00057 * @endcode 00058 */ 00059 00060 template <typename T> 00061 class MyBuffer 00062 { 00063 private: 00064 T *_buf; 00065 volatile uint32_t _wloc; 00066 volatile uint32_t _rloc; 00067 uint32_t _size; 00068 00069 public: 00070 /** Create a Buffer and allocate memory for it 00071 * @param size The size of the buffer 00072 */ 00073 MyBuffer(uint32_t size = 0x100); 00074 00075 /** Get the size of the ring buffer 00076 * @return the size of the ring buffer 00077 */ 00078 uint32_t getSize(); 00079 00080 /** Destry a Buffer and release it's allocated memory 00081 */ 00082 ~MyBuffer(); 00083 00084 /** Add a data element into the buffer 00085 * @param data Something to add to the buffer 00086 */ 00087 void put(T data); 00088 00089 /** Remove a data element from the buffer 00090 * @return Pull the oldest element from the buffer 00091 */ 00092 T get(void); 00093 00094 /** Get the address to the head of the buffer 00095 * @return The address of element 0 in the buffer 00096 */ 00097 T *head(void); 00098 00099 /** Reset the buffer to 0. Useful if using head() to parse packeted data 00100 */ 00101 void clear(void); 00102 00103 /** Determine if anything is readable in the buffer 00104 * @return 1 if something can be read, 0 otherwise 00105 */ 00106 uint32_t available(void); 00107 00108 /** Overloaded operator for writing to the buffer 00109 * @param data Something to put in the buffer 00110 * @return 00111 */ 00112 MyBuffer &operator= (T data) 00113 { 00114 put(data); 00115 return *this; 00116 } 00117 00118 /** Overloaded operator for reading from the buffer 00119 * @return Pull the oldest element from the buffer 00120 */ 00121 operator int(void) 00122 { 00123 return get(); 00124 } 00125 00126 uint32_t peek(char c); 00127 00128 }; 00129 00130 template <class T> 00131 inline void MyBuffer<T>::put(T data) 00132 { 00133 _buf[_wloc++] = data; 00134 _wloc %= (_size-1); 00135 00136 return; 00137 } 00138 00139 template <class T> 00140 inline T MyBuffer<T>::get(void) 00141 { 00142 T data_pos = _buf[_rloc++]; 00143 _rloc %= (_size-1); 00144 00145 return data_pos; 00146 } 00147 00148 template <class T> 00149 inline T *MyBuffer<T>::head(void) 00150 { 00151 T *data_pos = &_buf[0]; 00152 00153 return data_pos; 00154 } 00155 00156 template <class T> 00157 inline uint32_t MyBuffer<T>::available(void) 00158 { 00159 return (_wloc == _rloc) ? 0 : 1; 00160 } 00161 00162 #endif 00163
Generated on Tue Jul 12 2022 15:14:29 by
