my customized lib

Committer:
DuyLionTran
Date:
Sun Nov 26 15:08:14 2017 +0000
Revision:
0:8094b249013c
Initial commit

Who changed what in which revision?

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