Buffer for general purpose use. Templated for most datatypes

Dependents:   BufferedSoftSerial 09_PT1000 10_PT1000 11_PT1000 ... more

Example

 #include "mbed.h"
 #include "Buffer.h"

 Buffer <char> buf;

 int main()
 {
     buf = 'a';
     buf.put('b');
     char *head = buf.head();
     puts(head);

     char whats_in_there[2] = {0};
     int pos = 0;

     while(buf.available())
     {   
         whats_in_there[pos++] = buf;
     }
     printf("%c %c\n", whats_in_there[0], whats_in_there[1]);
     buf.clear();
     error("done\n\n\n");
 }
Committer:
sam_grove
Date:
Mon Mar 07 21:10:13 2016 +0000
Revision:
6:89564915f2a7
Update class name to MyBuffer to avoid conflicting names with a class in the mbed library

Who changed what in which revision?

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