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:
Thu May 23 19:26:31 2013 +0000
Revision:
1:490224f41c09
Parent:
0:5e4bca1bd5f7
Child:
2:d13a72146516
Child:
3:c2de0ddfe65b
Added documentation

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 enum BUFFER_SIZE{SMALL = 0x100, MEDIUM = 0x400, LARGE = 0x1000};
sam_grove 0:5e4bca1bd5f7 70
sam_grove 0:5e4bca1bd5f7 71 public:
sam_grove 1:490224f41c09 72 /** Create a Buffer and allocate memory for it
sam_grove 1:490224f41c09 73 * @param size An enum from BUFFER_SIZE.
sam_grove 1:490224f41c09 74 */
sam_grove 0:5e4bca1bd5f7 75 Buffer(BUFFER_SIZE size = Buffer::SMALL);
sam_grove 1:490224f41c09 76
sam_grove 1:490224f41c09 77 /** Destry a Buffer and release it's allocated memory
sam_grove 1:490224f41c09 78 */
sam_grove 0:5e4bca1bd5f7 79 ~Buffer();
sam_grove 0:5e4bca1bd5f7 80
sam_grove 1:490224f41c09 81 /** Add a data element into the buffer
sam_grove 1:490224f41c09 82 * @param data Something to add to the buffer
sam_grove 1:490224f41c09 83 */
sam_grove 0:5e4bca1bd5f7 84 void put(T data);
sam_grove 1:490224f41c09 85
sam_grove 1:490224f41c09 86 /** Remove a data element from the buffer
sam_grove 1:490224f41c09 87 * @return Pull the oldest element from the buffer
sam_grove 1:490224f41c09 88 */
sam_grove 0:5e4bca1bd5f7 89 T get(void);
sam_grove 1:490224f41c09 90
sam_grove 1:490224f41c09 91 /** Get the address to the head of the buffer
sam_grove 1:490224f41c09 92 * @return The address of element 0 in the buffer
sam_grove 1:490224f41c09 93 */
sam_grove 0:5e4bca1bd5f7 94 T *head(void);
sam_grove 1:490224f41c09 95
sam_grove 1:490224f41c09 96 /** Reset the buffer to 0. Useful if using head() to parse packeted data
sam_grove 1:490224f41c09 97 */
sam_grove 0:5e4bca1bd5f7 98 void clear(void);
sam_grove 1:490224f41c09 99
sam_grove 1:490224f41c09 100 /** Determine if anything is readable in the buffer
sam_grove 1:490224f41c09 101 * @return 1 if something can be read, 0 otherwise
sam_grove 1:490224f41c09 102 */
sam_grove 0:5e4bca1bd5f7 103 uint32_t available(void);
sam_grove 0:5e4bca1bd5f7 104
sam_grove 1:490224f41c09 105 /** Overloaded operator for writing to the buffer
sam_grove 1:490224f41c09 106 * @param data Something to put in the buffer
sam_grove 1:490224f41c09 107 * @return
sam_grove 1:490224f41c09 108 */
sam_grove 1:490224f41c09 109 Buffer &operator= (T data)
sam_grove 1:490224f41c09 110 {
sam_grove 1:490224f41c09 111 put(data);
sam_grove 1:490224f41c09 112 return *this;
sam_grove 1:490224f41c09 113 }
sam_grove 1:490224f41c09 114
sam_grove 1:490224f41c09 115 /** Overloaded operator for reading from the buffer
sam_grove 1:490224f41c09 116 * @return Pull the oldest element from the buffer
sam_grove 1:490224f41c09 117 */
sam_grove 1:490224f41c09 118 operator int(void)
sam_grove 1:490224f41c09 119 {
sam_grove 1:490224f41c09 120 return get();
sam_grove 1:490224f41c09 121 }
sam_grove 1:490224f41c09 122
sam_grove 0:5e4bca1bd5f7 123 };
sam_grove 0:5e4bca1bd5f7 124
sam_grove 0:5e4bca1bd5f7 125 #endif
sam_grove 0:5e4bca1bd5f7 126