Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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