Buffer for general purpose use. Templated for most datatypes
Dependents: BufferedSoftSerial 09_PT1000 10_PT1000 11_PT1000 ... more
Example
#include "mbed.h" #include "Buffer.h" Buffer <char> buf; int main() { buf = 'a'; buf.put('b'); char *head = buf.head(); puts(head); char whats_in_there[2] = {0}; int pos = 0; while(buf.available()) { whats_in_there[pos++] = buf; } printf("%c %c\n", whats_in_there[0], whats_in_there[1]); buf.clear(); error("done\n\n\n"); }
Buffer.cpp@2:d13a72146516, 2013-05-23 (annotated)
- Committer:
- sam_grove
- Date:
- Thu May 23 23:48:36 2013 +0000
- Revision:
- 2:d13a72146516
- Parent:
- 1:490224f41c09
- Child:
- 4:cd0a1f4c623f
Optimized the use of _size and created _sizemask instead. Faster during normal operation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:5e4bca1bd5f7 | 1 | |
sam_grove | 1:490224f41c09 | 2 | /** |
sam_grove | 1:490224f41c09 | 3 | * @file Buffer.cpp |
sam_grove | 1:490224f41c09 | 4 | * @brief Software Buffer - Templated Ring Buffer for most data types |
sam_grove | 1:490224f41c09 | 5 | * @author sam grove |
sam_grove | 1:490224f41c09 | 6 | * @version 1.0 |
sam_grove | 1:490224f41c09 | 7 | * @see |
sam_grove | 1:490224f41c09 | 8 | * |
sam_grove | 1:490224f41c09 | 9 | * Copyright (c) 2013 |
sam_grove | 1:490224f41c09 | 10 | * |
sam_grove | 1:490224f41c09 | 11 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 1:490224f41c09 | 12 | * you may not use this file except in compliance with the License. |
sam_grove | 1:490224f41c09 | 13 | * You may obtain a copy of the License at |
sam_grove | 1:490224f41c09 | 14 | * |
sam_grove | 1:490224f41c09 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 1:490224f41c09 | 16 | * |
sam_grove | 1:490224f41c09 | 17 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 1:490224f41c09 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 1:490224f41c09 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 1:490224f41c09 | 20 | * See the License for the specific language governing permissions and |
sam_grove | 1:490224f41c09 | 21 | * limitations under the License. |
sam_grove | 1:490224f41c09 | 22 | */ |
sam_grove | 1:490224f41c09 | 23 | |
sam_grove | 0:5e4bca1bd5f7 | 24 | #include "Buffer.h" |
sam_grove | 0:5e4bca1bd5f7 | 25 | |
sam_grove | 0:5e4bca1bd5f7 | 26 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 27 | Buffer<T>::Buffer(BUFFER_SIZE size) |
sam_grove | 0:5e4bca1bd5f7 | 28 | { |
sam_grove | 0:5e4bca1bd5f7 | 29 | _buf = new T [size]; |
sam_grove | 2:d13a72146516 | 30 | _sizemask = size-1; |
sam_grove | 0:5e4bca1bd5f7 | 31 | clear(); |
sam_grove | 0:5e4bca1bd5f7 | 32 | |
sam_grove | 0:5e4bca1bd5f7 | 33 | return; |
sam_grove | 0:5e4bca1bd5f7 | 34 | } |
sam_grove | 0:5e4bca1bd5f7 | 35 | |
sam_grove | 0:5e4bca1bd5f7 | 36 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 37 | Buffer<T>::~Buffer() |
sam_grove | 0:5e4bca1bd5f7 | 38 | { |
sam_grove | 0:5e4bca1bd5f7 | 39 | delete [] _buf; |
sam_grove | 0:5e4bca1bd5f7 | 40 | |
sam_grove | 0:5e4bca1bd5f7 | 41 | return; |
sam_grove | 0:5e4bca1bd5f7 | 42 | } |
sam_grove | 0:5e4bca1bd5f7 | 43 | |
sam_grove | 0:5e4bca1bd5f7 | 44 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 45 | void Buffer<T>::put(T data) |
sam_grove | 0:5e4bca1bd5f7 | 46 | { |
sam_grove | 0:5e4bca1bd5f7 | 47 | _buf[_wloc++] = data; |
sam_grove | 2:d13a72146516 | 48 | _wloc &= _sizemask; |
sam_grove | 0:5e4bca1bd5f7 | 49 | |
sam_grove | 0:5e4bca1bd5f7 | 50 | return; |
sam_grove | 0:5e4bca1bd5f7 | 51 | } |
sam_grove | 0:5e4bca1bd5f7 | 52 | |
sam_grove | 0:5e4bca1bd5f7 | 53 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 54 | T Buffer<T>::get(void) |
sam_grove | 0:5e4bca1bd5f7 | 55 | { |
sam_grove | 0:5e4bca1bd5f7 | 56 | T data_pos = _buf[_rloc++]; |
sam_grove | 2:d13a72146516 | 57 | _rloc &= _sizemask; |
sam_grove | 0:5e4bca1bd5f7 | 58 | |
sam_grove | 0:5e4bca1bd5f7 | 59 | return data_pos; |
sam_grove | 0:5e4bca1bd5f7 | 60 | } |
sam_grove | 0:5e4bca1bd5f7 | 61 | |
sam_grove | 0:5e4bca1bd5f7 | 62 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 63 | T *Buffer<T>::head(void) |
sam_grove | 0:5e4bca1bd5f7 | 64 | { |
sam_grove | 0:5e4bca1bd5f7 | 65 | T *data_pos = &_buf[0]; |
sam_grove | 0:5e4bca1bd5f7 | 66 | |
sam_grove | 0:5e4bca1bd5f7 | 67 | return data_pos; |
sam_grove | 0:5e4bca1bd5f7 | 68 | } |
sam_grove | 0:5e4bca1bd5f7 | 69 | |
sam_grove | 0:5e4bca1bd5f7 | 70 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 71 | void Buffer<T>::clear(void) |
sam_grove | 0:5e4bca1bd5f7 | 72 | { |
sam_grove | 0:5e4bca1bd5f7 | 73 | _wloc = 0; |
sam_grove | 0:5e4bca1bd5f7 | 74 | _rloc = 0; |
sam_grove | 2:d13a72146516 | 75 | memset(_buf, 0, _sizemask+1); |
sam_grove | 0:5e4bca1bd5f7 | 76 | |
sam_grove | 0:5e4bca1bd5f7 | 77 | return; |
sam_grove | 0:5e4bca1bd5f7 | 78 | } |
sam_grove | 0:5e4bca1bd5f7 | 79 | |
sam_grove | 0:5e4bca1bd5f7 | 80 | template <class T> |
sam_grove | 0:5e4bca1bd5f7 | 81 | uint32_t Buffer<T>::available(void) |
sam_grove | 0:5e4bca1bd5f7 | 82 | { |
sam_grove | 0:5e4bca1bd5f7 | 83 | return (_wloc == _rloc) ? 0 : 1; |
sam_grove | 0:5e4bca1bd5f7 | 84 | } |
sam_grove | 0:5e4bca1bd5f7 | 85 | |
sam_grove | 2:d13a72146516 | 86 | template <class T> |
sam_grove | 2:d13a72146516 | 87 | uint32_t Buffer<T>::peek(char c) |
sam_grove | 2:d13a72146516 | 88 | { |
sam_grove | 2:d13a72146516 | 89 | return 1; |
sam_grove | 2:d13a72146516 | 90 | } |
sam_grove | 2:d13a72146516 | 91 | |
sam_grove | 2:d13a72146516 | 92 | |
sam_grove | 2:d13a72146516 | 93 | |
sam_grove | 1:490224f41c09 | 94 | // make the linker aware of some possible types |
sam_grove | 0:5e4bca1bd5f7 | 95 | template class Buffer<uint8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 96 | template class Buffer<int8_t>; |
sam_grove | 0:5e4bca1bd5f7 | 97 | template class Buffer<uint16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 98 | template class Buffer<int16_t>; |
sam_grove | 0:5e4bca1bd5f7 | 99 | template class Buffer<uint32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 100 | template class Buffer<int32_t>; |
sam_grove | 0:5e4bca1bd5f7 | 101 | template class Buffer<uint64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 102 | template class Buffer<int64_t>; |
sam_grove | 0:5e4bca1bd5f7 | 103 | template class Buffer<char>; |
sam_grove | 0:5e4bca1bd5f7 | 104 | template class Buffer<wchar_t>; |
sam_grove | 0:5e4bca1bd5f7 | 105 |