Easily add all supported connectivity methods to your mbed OS project

Dependencies:   type-yd-driver

Committer:
MACRUM
Date:
Wed Jul 12 10:52:58 2017 +0000
Revision:
0:615f90842ce8
Initial commit

Who changed what in which revision?

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