mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
drivers/internal/ByteBuffer.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 1:9db0e321a9f4 | 1 | /* |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2018-2019, Arm Limited and affiliates. |
kenjiArai | 1:9db0e321a9f4 | 3 | * SPDX-License-Identifier: Apache-2.0 |
kenjiArai | 1:9db0e321a9f4 | 4 | * |
kenjiArai | 1:9db0e321a9f4 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 1:9db0e321a9f4 | 6 | * you may not use this file except in compliance with the License. |
kenjiArai | 1:9db0e321a9f4 | 7 | * You may obtain a copy of the License at |
kenjiArai | 1:9db0e321a9f4 | 8 | * |
kenjiArai | 1:9db0e321a9f4 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 1:9db0e321a9f4 | 10 | * |
kenjiArai | 1:9db0e321a9f4 | 11 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 1:9db0e321a9f4 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 1:9db0e321a9f4 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 1:9db0e321a9f4 | 14 | * See the License for the specific language governing permissions and |
kenjiArai | 1:9db0e321a9f4 | 15 | * limitations under the License. |
kenjiArai | 1:9db0e321a9f4 | 16 | */ |
kenjiArai | 1:9db0e321a9f4 | 17 | |
kenjiArai | 1:9db0e321a9f4 | 18 | #ifndef BYTE_BUFFER_H |
kenjiArai | 1:9db0e321a9f4 | 19 | #define BYTE_BUFFER_H |
kenjiArai | 1:9db0e321a9f4 | 20 | |
kenjiArai | 1:9db0e321a9f4 | 21 | #include <stdint.h> |
kenjiArai | 1:9db0e321a9f4 | 22 | |
kenjiArai | 1:9db0e321a9f4 | 23 | /** |
kenjiArai | 1:9db0e321a9f4 | 24 | * \defgroup drivers_ByteBuffer ByteBuffer class |
kenjiArai | 1:9db0e321a9f4 | 25 | * \ingroup drivers-internal-api-usb |
kenjiArai | 1:9db0e321a9f4 | 26 | * @{ |
kenjiArai | 1:9db0e321a9f4 | 27 | */ |
kenjiArai | 1:9db0e321a9f4 | 28 | class ByteBuffer { |
kenjiArai | 1:9db0e321a9f4 | 29 | public: |
kenjiArai | 1:9db0e321a9f4 | 30 | |
kenjiArai | 1:9db0e321a9f4 | 31 | /** |
kenjiArai | 1:9db0e321a9f4 | 32 | * Create a byte buffer of the given size |
kenjiArai | 1:9db0e321a9f4 | 33 | * |
kenjiArai | 1:9db0e321a9f4 | 34 | * @param size Number of bytes this buffer can hold |
kenjiArai | 1:9db0e321a9f4 | 35 | */ |
kenjiArai | 1:9db0e321a9f4 | 36 | ByteBuffer(uint32_t size = 0); |
kenjiArai | 1:9db0e321a9f4 | 37 | |
kenjiArai | 1:9db0e321a9f4 | 38 | /** |
kenjiArai | 1:9db0e321a9f4 | 39 | * Delete this byte buffer |
kenjiArai | 1:9db0e321a9f4 | 40 | */ |
kenjiArai | 1:9db0e321a9f4 | 41 | ~ByteBuffer(); |
kenjiArai | 1:9db0e321a9f4 | 42 | |
kenjiArai | 1:9db0e321a9f4 | 43 | /** |
kenjiArai | 1:9db0e321a9f4 | 44 | * Set the size of the buffer |
kenjiArai | 1:9db0e321a9f4 | 45 | * |
kenjiArai | 1:9db0e321a9f4 | 46 | * Buffer contents are reset. |
kenjiArai | 1:9db0e321a9f4 | 47 | * |
kenjiArai | 1:9db0e321a9f4 | 48 | * @param size New buffer size |
kenjiArai | 1:9db0e321a9f4 | 49 | */ |
kenjiArai | 1:9db0e321a9f4 | 50 | void resize(uint32_t size); |
kenjiArai | 1:9db0e321a9f4 | 51 | |
kenjiArai | 1:9db0e321a9f4 | 52 | /** |
kenjiArai | 1:9db0e321a9f4 | 53 | * Add a single byte to this buffer |
kenjiArai | 1:9db0e321a9f4 | 54 | * |
kenjiArai | 1:9db0e321a9f4 | 55 | * There must be enough space in the buffer or the behavior is undefined. |
kenjiArai | 1:9db0e321a9f4 | 56 | * |
kenjiArai | 1:9db0e321a9f4 | 57 | * @param data byte to add |
kenjiArai | 1:9db0e321a9f4 | 58 | */ |
kenjiArai | 1:9db0e321a9f4 | 59 | void push(uint8_t data); |
kenjiArai | 1:9db0e321a9f4 | 60 | |
kenjiArai | 1:9db0e321a9f4 | 61 | /** |
kenjiArai | 1:9db0e321a9f4 | 62 | * Write a block of data to this ByteBuffer |
kenjiArai | 1:9db0e321a9f4 | 63 | * |
kenjiArai | 1:9db0e321a9f4 | 64 | * There must be enough space in the ByteBuffer or the behavior is undefined. |
kenjiArai | 1:9db0e321a9f4 | 65 | * |
kenjiArai | 1:9db0e321a9f4 | 66 | * @param data Block of data to write |
kenjiArai | 1:9db0e321a9f4 | 67 | * @param size Size of data to write |
kenjiArai | 1:9db0e321a9f4 | 68 | */ |
kenjiArai | 1:9db0e321a9f4 | 69 | void write(uint8_t *data, uint32_t size); |
kenjiArai | 1:9db0e321a9f4 | 70 | |
kenjiArai | 1:9db0e321a9f4 | 71 | /** |
kenjiArai | 1:9db0e321a9f4 | 72 | * Remove a byte from this buffer |
kenjiArai | 1:9db0e321a9f4 | 73 | * |
kenjiArai | 1:9db0e321a9f4 | 74 | * @return data byte |
kenjiArai | 1:9db0e321a9f4 | 75 | */ |
kenjiArai | 1:9db0e321a9f4 | 76 | uint8_t pop(); |
kenjiArai | 1:9db0e321a9f4 | 77 | |
kenjiArai | 1:9db0e321a9f4 | 78 | /** |
kenjiArai | 1:9db0e321a9f4 | 79 | * Read a block of data from this ByteBuffer into a buffer pointed by 'data' |
kenjiArai | 1:9db0e321a9f4 | 80 | * |
kenjiArai | 1:9db0e321a9f4 | 81 | * There must be enough data in the ByteBuffer or the behavior is undefined. |
kenjiArai | 1:9db0e321a9f4 | 82 | * |
kenjiArai | 1:9db0e321a9f4 | 83 | * @param data Block of data to read |
kenjiArai | 1:9db0e321a9f4 | 84 | * @param size Size of data to read |
kenjiArai | 1:9db0e321a9f4 | 85 | */ |
kenjiArai | 1:9db0e321a9f4 | 86 | void read(uint8_t *data, uint32_t size); |
kenjiArai | 1:9db0e321a9f4 | 87 | |
kenjiArai | 1:9db0e321a9f4 | 88 | /** |
kenjiArai | 1:9db0e321a9f4 | 89 | * Return the number bytes in this byte buffer |
kenjiArai | 1:9db0e321a9f4 | 90 | * |
kenjiArai | 1:9db0e321a9f4 | 91 | * @return Number of used bytes |
kenjiArai | 1:9db0e321a9f4 | 92 | */ |
kenjiArai | 1:9db0e321a9f4 | 93 | uint32_t size(); |
kenjiArai | 1:9db0e321a9f4 | 94 | |
kenjiArai | 1:9db0e321a9f4 | 95 | /** |
kenjiArai | 1:9db0e321a9f4 | 96 | * Return the number of additional bytes this buffer can hold |
kenjiArai | 1:9db0e321a9f4 | 97 | * |
kenjiArai | 1:9db0e321a9f4 | 98 | * @return Number of free bytes |
kenjiArai | 1:9db0e321a9f4 | 99 | */ |
kenjiArai | 1:9db0e321a9f4 | 100 | uint32_t free(); |
kenjiArai | 1:9db0e321a9f4 | 101 | |
kenjiArai | 1:9db0e321a9f4 | 102 | /** |
kenjiArai | 1:9db0e321a9f4 | 103 | * Check if this byte buffer is full |
kenjiArai | 1:9db0e321a9f4 | 104 | * |
kenjiArai | 1:9db0e321a9f4 | 105 | * @return true if full, false otherwise |
kenjiArai | 1:9db0e321a9f4 | 106 | */ |
kenjiArai | 1:9db0e321a9f4 | 107 | bool full(); |
kenjiArai | 1:9db0e321a9f4 | 108 | |
kenjiArai | 1:9db0e321a9f4 | 109 | /** |
kenjiArai | 1:9db0e321a9f4 | 110 | * Check if this byte buffer is empty |
kenjiArai | 1:9db0e321a9f4 | 111 | * |
kenjiArai | 1:9db0e321a9f4 | 112 | * @return true if empty, false otherwise |
kenjiArai | 1:9db0e321a9f4 | 113 | */ |
kenjiArai | 1:9db0e321a9f4 | 114 | bool empty(); |
kenjiArai | 1:9db0e321a9f4 | 115 | |
kenjiArai | 1:9db0e321a9f4 | 116 | private: |
kenjiArai | 1:9db0e321a9f4 | 117 | |
kenjiArai | 1:9db0e321a9f4 | 118 | uint32_t _head; |
kenjiArai | 1:9db0e321a9f4 | 119 | uint32_t _tail; |
kenjiArai | 1:9db0e321a9f4 | 120 | uint32_t _size; |
kenjiArai | 1:9db0e321a9f4 | 121 | uint8_t *_buf; |
kenjiArai | 1:9db0e321a9f4 | 122 | }; |
kenjiArai | 1:9db0e321a9f4 | 123 | |
kenjiArai | 1:9db0e321a9f4 | 124 | /** @}*/ |
kenjiArai | 1:9db0e321a9f4 | 125 | |
kenjiArai | 1:9db0e321a9f4 | 126 | #endif |