Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed LPC11U35_MCP41HV51-503EST
USBDevice/USBSerial/CircBuffer.h@9:0b3a7a9eed3e, 2021-03-09 (annotated)
- Committer:
- ryousato
- Date:
- Tue Mar 09 05:40:47 2021 +0000
- Revision:
- 9:0b3a7a9eed3e
- Parent:
- 0:df1e1f84ded8
20210309
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ryousato | 0:df1e1f84ded8 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
ryousato | 0:df1e1f84ded8 | 2 | * |
ryousato | 0:df1e1f84ded8 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ryousato | 0:df1e1f84ded8 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
ryousato | 0:df1e1f84ded8 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
ryousato | 0:df1e1f84ded8 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
ryousato | 0:df1e1f84ded8 | 7 | * Software is furnished to do so, subject to the following conditions: |
ryousato | 0:df1e1f84ded8 | 8 | * |
ryousato | 0:df1e1f84ded8 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ryousato | 0:df1e1f84ded8 | 10 | * substantial portions of the Software. |
ryousato | 0:df1e1f84ded8 | 11 | * |
ryousato | 0:df1e1f84ded8 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ryousato | 0:df1e1f84ded8 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ryousato | 0:df1e1f84ded8 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ryousato | 0:df1e1f84ded8 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ryousato | 0:df1e1f84ded8 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ryousato | 0:df1e1f84ded8 | 17 | */ |
ryousato | 0:df1e1f84ded8 | 18 | |
ryousato | 0:df1e1f84ded8 | 19 | #ifndef CIRCBUFFER_H |
ryousato | 0:df1e1f84ded8 | 20 | #define CIRCBUFFER_H |
ryousato | 0:df1e1f84ded8 | 21 | |
ryousato | 0:df1e1f84ded8 | 22 | template <class T> |
ryousato | 0:df1e1f84ded8 | 23 | class CircBuffer { |
ryousato | 0:df1e1f84ded8 | 24 | public: |
ryousato | 0:df1e1f84ded8 | 25 | CircBuffer(int length) { |
ryousato | 0:df1e1f84ded8 | 26 | write = 0; |
ryousato | 0:df1e1f84ded8 | 27 | read = 0; |
ryousato | 0:df1e1f84ded8 | 28 | size = length + 1; |
ryousato | 0:df1e1f84ded8 | 29 | buf = (T *)malloc(size * sizeof(T)); |
ryousato | 0:df1e1f84ded8 | 30 | }; |
ryousato | 0:df1e1f84ded8 | 31 | |
ryousato | 0:df1e1f84ded8 | 32 | ~CircBuffer() { |
ryousato | 0:df1e1f84ded8 | 33 | free(buf); |
ryousato | 0:df1e1f84ded8 | 34 | } |
ryousato | 0:df1e1f84ded8 | 35 | |
ryousato | 0:df1e1f84ded8 | 36 | bool isFull() { |
ryousato | 0:df1e1f84ded8 | 37 | return ((write + 1) % size == read); |
ryousato | 0:df1e1f84ded8 | 38 | }; |
ryousato | 0:df1e1f84ded8 | 39 | |
ryousato | 0:df1e1f84ded8 | 40 | bool isEmpty() { |
ryousato | 0:df1e1f84ded8 | 41 | return (read == write); |
ryousato | 0:df1e1f84ded8 | 42 | }; |
ryousato | 0:df1e1f84ded8 | 43 | |
ryousato | 0:df1e1f84ded8 | 44 | void queue(T k) { |
ryousato | 0:df1e1f84ded8 | 45 | if (isFull()) { |
ryousato | 0:df1e1f84ded8 | 46 | read++; |
ryousato | 0:df1e1f84ded8 | 47 | read %= size; |
ryousato | 0:df1e1f84ded8 | 48 | } |
ryousato | 0:df1e1f84ded8 | 49 | buf[write++] = k; |
ryousato | 0:df1e1f84ded8 | 50 | write %= size; |
ryousato | 0:df1e1f84ded8 | 51 | } |
ryousato | 0:df1e1f84ded8 | 52 | |
ryousato | 0:df1e1f84ded8 | 53 | uint16_t available() { |
ryousato | 0:df1e1f84ded8 | 54 | return (write >= read) ? write - read : size - read + write; |
ryousato | 0:df1e1f84ded8 | 55 | }; |
ryousato | 0:df1e1f84ded8 | 56 | |
ryousato | 0:df1e1f84ded8 | 57 | bool dequeue(T * c) { |
ryousato | 0:df1e1f84ded8 | 58 | bool empty = isEmpty(); |
ryousato | 0:df1e1f84ded8 | 59 | if (!empty) { |
ryousato | 0:df1e1f84ded8 | 60 | *c = buf[read++]; |
ryousato | 0:df1e1f84ded8 | 61 | read %= size; |
ryousato | 0:df1e1f84ded8 | 62 | } |
ryousato | 0:df1e1f84ded8 | 63 | return(!empty); |
ryousato | 0:df1e1f84ded8 | 64 | }; |
ryousato | 0:df1e1f84ded8 | 65 | |
ryousato | 0:df1e1f84ded8 | 66 | private: |
ryousato | 0:df1e1f84ded8 | 67 | volatile uint16_t write; |
ryousato | 0:df1e1f84ded8 | 68 | volatile uint16_t read; |
ryousato | 0:df1e1f84ded8 | 69 | uint16_t size; |
ryousato | 0:df1e1f84ded8 | 70 | T * buf; |
ryousato | 0:df1e1f84ded8 | 71 | }; |
ryousato | 0:df1e1f84ded8 | 72 | |
ryousato | 0:df1e1f84ded8 | 73 | #endif |