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:
Wed Jun 26 15:24:27 2013 +0000
Revision:
4:cd0a1f4c623f
Parent:
2:d13a72146516
Parent:
3:c2de0ddfe65b
Child:
5:7b754354b99c
merged

Who changed what in which revision?

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