Buffer from Sam Grove changed to have static instead of dynamic memory allocation. Fixed size to 256B.

Dependents:   BufferedSerialStatic

Fork of Buffer by Sam Grove

Committer:
goranirnas
Date:
Mon May 28 14:34:02 2018 +0000
Revision:
7:e80960adb2ad
Parent:
6:89564915f2a7
Updated buffer to use static memory allocation. Buffer size 256B.

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
goranirnas 7:e80960adb2ad 24 /**
goranirnas 7:e80960adb2ad 25 * Modification of original code to implement buffer with static memory
goranirnas 7:e80960adb2ad 26 * allocation with size 256.
goranirnas 7:e80960adb2ad 27 */
goranirnas 7:e80960adb2ad 28
sam_grove 6:89564915f2a7 29 #ifndef MYBUFFER_H
sam_grove 6:89564915f2a7 30 #define MYBUFFER_H
sam_grove 6:89564915f2a7 31
sam_grove 6:89564915f2a7 32 #include <stdint.h>
sam_grove 6:89564915f2a7 33 #include <string.h>
sam_grove 6:89564915f2a7 34
sam_grove 6:89564915f2a7 35 template <typename T>
sam_grove 6:89564915f2a7 36 class MyBuffer
sam_grove 6:89564915f2a7 37 {
sam_grove 6:89564915f2a7 38 private:
goranirnas 7:e80960adb2ad 39 const static uint16_t BUFFER_SIZE = 256;
goranirnas 7:e80960adb2ad 40 T _buf[BUFFER_SIZE];
sam_grove 6:89564915f2a7 41 volatile uint32_t _wloc;
sam_grove 6:89564915f2a7 42 volatile uint32_t _rloc;
sam_grove 6:89564915f2a7 43 uint32_t _size;
sam_grove 6:89564915f2a7 44
sam_grove 6:89564915f2a7 45 public:
sam_grove 6:89564915f2a7 46 /** Create a Buffer and allocate memory for it
sam_grove 6:89564915f2a7 47 * @param size The size of the buffer
sam_grove 6:89564915f2a7 48 */
goranirnas 7:e80960adb2ad 49 MyBuffer();
sam_grove 6:89564915f2a7 50
sam_grove 6:89564915f2a7 51 /** Get the size of the ring buffer
sam_grove 6:89564915f2a7 52 * @return the size of the ring buffer
sam_grove 6:89564915f2a7 53 */
sam_grove 6:89564915f2a7 54 uint32_t getSize();
sam_grove 6:89564915f2a7 55
sam_grove 6:89564915f2a7 56 /** Destry a Buffer and release it's allocated memory
sam_grove 6:89564915f2a7 57 */
sam_grove 6:89564915f2a7 58 ~MyBuffer();
sam_grove 6:89564915f2a7 59
sam_grove 6:89564915f2a7 60 /** Add a data element into the buffer
sam_grove 6:89564915f2a7 61 * @param data Something to add to the buffer
sam_grove 6:89564915f2a7 62 */
sam_grove 6:89564915f2a7 63 void put(T data);
sam_grove 6:89564915f2a7 64
sam_grove 6:89564915f2a7 65 /** Remove a data element from the buffer
sam_grove 6:89564915f2a7 66 * @return Pull the oldest element from the buffer
sam_grove 6:89564915f2a7 67 */
sam_grove 6:89564915f2a7 68 T get(void);
sam_grove 6:89564915f2a7 69
sam_grove 6:89564915f2a7 70 /** Get the address to the head of the buffer
sam_grove 6:89564915f2a7 71 * @return The address of element 0 in the buffer
sam_grove 6:89564915f2a7 72 */
sam_grove 6:89564915f2a7 73 T *head(void);
sam_grove 6:89564915f2a7 74
sam_grove 6:89564915f2a7 75 /** Reset the buffer to 0. Useful if using head() to parse packeted data
sam_grove 6:89564915f2a7 76 */
sam_grove 6:89564915f2a7 77 void clear(void);
sam_grove 6:89564915f2a7 78
sam_grove 6:89564915f2a7 79 /** Determine if anything is readable in the buffer
sam_grove 6:89564915f2a7 80 * @return 1 if something can be read, 0 otherwise
sam_grove 6:89564915f2a7 81 */
sam_grove 6:89564915f2a7 82 uint32_t available(void);
sam_grove 6:89564915f2a7 83
sam_grove 6:89564915f2a7 84 /** Overloaded operator for writing to the buffer
sam_grove 6:89564915f2a7 85 * @param data Something to put in the buffer
sam_grove 6:89564915f2a7 86 * @return
sam_grove 6:89564915f2a7 87 */
sam_grove 6:89564915f2a7 88 MyBuffer &operator= (T data)
sam_grove 6:89564915f2a7 89 {
sam_grove 6:89564915f2a7 90 put(data);
sam_grove 6:89564915f2a7 91 return *this;
sam_grove 6:89564915f2a7 92 }
sam_grove 6:89564915f2a7 93
sam_grove 6:89564915f2a7 94 /** Overloaded operator for reading from the buffer
sam_grove 6:89564915f2a7 95 * @return Pull the oldest element from the buffer
sam_grove 6:89564915f2a7 96 */
sam_grove 6:89564915f2a7 97 operator int(void)
sam_grove 6:89564915f2a7 98 {
sam_grove 6:89564915f2a7 99 return get();
sam_grove 6:89564915f2a7 100 }
sam_grove 6:89564915f2a7 101
sam_grove 6:89564915f2a7 102 uint32_t peek(char c);
sam_grove 6:89564915f2a7 103
sam_grove 6:89564915f2a7 104 };
sam_grove 6:89564915f2a7 105
sam_grove 6:89564915f2a7 106 template <class T>
sam_grove 6:89564915f2a7 107 inline void MyBuffer<T>::put(T data)
sam_grove 6:89564915f2a7 108 {
sam_grove 6:89564915f2a7 109 _buf[_wloc++] = data;
sam_grove 6:89564915f2a7 110 _wloc %= (_size-1);
sam_grove 6:89564915f2a7 111
sam_grove 6:89564915f2a7 112 return;
sam_grove 6:89564915f2a7 113 }
sam_grove 6:89564915f2a7 114
sam_grove 6:89564915f2a7 115 template <class T>
sam_grove 6:89564915f2a7 116 inline T MyBuffer<T>::get(void)
sam_grove 6:89564915f2a7 117 {
sam_grove 6:89564915f2a7 118 T data_pos = _buf[_rloc++];
sam_grove 6:89564915f2a7 119 _rloc %= (_size-1);
sam_grove 6:89564915f2a7 120
sam_grove 6:89564915f2a7 121 return data_pos;
sam_grove 6:89564915f2a7 122 }
sam_grove 6:89564915f2a7 123
sam_grove 6:89564915f2a7 124 template <class T>
sam_grove 6:89564915f2a7 125 inline T *MyBuffer<T>::head(void)
sam_grove 6:89564915f2a7 126 {
sam_grove 6:89564915f2a7 127 T *data_pos = &_buf[0];
sam_grove 6:89564915f2a7 128
sam_grove 6:89564915f2a7 129 return data_pos;
sam_grove 6:89564915f2a7 130 }
sam_grove 6:89564915f2a7 131
sam_grove 6:89564915f2a7 132 template <class T>
sam_grove 6:89564915f2a7 133 inline uint32_t MyBuffer<T>::available(void)
sam_grove 6:89564915f2a7 134 {
sam_grove 6:89564915f2a7 135 return (_wloc == _rloc) ? 0 : 1;
sam_grove 6:89564915f2a7 136 }
sam_grove 6:89564915f2a7 137
sam_grove 6:89564915f2a7 138 #endif
sam_grove 6:89564915f2a7 139