A metronome using the FRDM K64F board

Committer:
ram54288
Date:
Sun May 14 18:40:18 2017 +0000
Revision:
0:a7a43371b306
Initial commit

Who changed what in which revision?

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