Library for Bluetooth Low Energy Module ble 4.0 HM-11

Committer:
igbt6
Date:
Thu Feb 25 02:44:30 2016 +0000
Revision:
7:aa4675590203
Parent:
5:9a00e7bb0275
more functions implemented

Who changed what in which revision?

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