test

Dependencies:   mbed Watchdog stm32-sensor-base2

Committer:
ruslanbredun
Date:
Fri Nov 05 14:39:08 2021 +0000
Revision:
19:7935ca386e13
Parent:
11:32eeb052cda5
LastFirmWare05/11/21;

Who changed what in which revision?

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