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