Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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