![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The preloaded firmware shipped on the mBot.
Please use the version of this project/repo found at: http://developer.mbed.org/teams/Outrageous-Circuits/code/Official_mBot/
USBDevice/USBSerial/CircBuffer.h@1:ffd9a51e7d35, 2014-11-25 (annotated)
- Committer:
- jeffknaggs
- Date:
- Tue Nov 25 14:49:40 2014 +0000
- Revision:
- 1:ffd9a51e7d35
- Parent:
- 0:865d42c46692
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jeffknaggs | 0:865d42c46692 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
jeffknaggs | 0:865d42c46692 | 2 | * |
jeffknaggs | 0:865d42c46692 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jeffknaggs | 0:865d42c46692 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
jeffknaggs | 0:865d42c46692 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
jeffknaggs | 0:865d42c46692 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
jeffknaggs | 0:865d42c46692 | 7 | * Software is furnished to do so, subject to the following conditions: |
jeffknaggs | 0:865d42c46692 | 8 | * |
jeffknaggs | 0:865d42c46692 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
jeffknaggs | 0:865d42c46692 | 10 | * substantial portions of the Software. |
jeffknaggs | 0:865d42c46692 | 11 | * |
jeffknaggs | 0:865d42c46692 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jeffknaggs | 0:865d42c46692 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jeffknaggs | 0:865d42c46692 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jeffknaggs | 0:865d42c46692 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jeffknaggs | 0:865d42c46692 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jeffknaggs | 0:865d42c46692 | 17 | */ |
jeffknaggs | 0:865d42c46692 | 18 | |
jeffknaggs | 0:865d42c46692 | 19 | #ifndef CIRCBUFFER_H |
jeffknaggs | 0:865d42c46692 | 20 | #define CIRCBUFFER_H |
jeffknaggs | 0:865d42c46692 | 21 | |
jeffknaggs | 0:865d42c46692 | 22 | template <class T> |
jeffknaggs | 0:865d42c46692 | 23 | class CircBuffer { |
jeffknaggs | 0:865d42c46692 | 24 | public: |
jeffknaggs | 0:865d42c46692 | 25 | CircBuffer(int length) { |
jeffknaggs | 0:865d42c46692 | 26 | write = 0; |
jeffknaggs | 0:865d42c46692 | 27 | read = 0; |
jeffknaggs | 0:865d42c46692 | 28 | size = length + 1; |
jeffknaggs | 0:865d42c46692 | 29 | buf = (T *)malloc(size * sizeof(T)); |
jeffknaggs | 0:865d42c46692 | 30 | }; |
jeffknaggs | 0:865d42c46692 | 31 | |
jeffknaggs | 0:865d42c46692 | 32 | ~CircBuffer() { |
jeffknaggs | 0:865d42c46692 | 33 | free(buf); |
jeffknaggs | 0:865d42c46692 | 34 | } |
jeffknaggs | 0:865d42c46692 | 35 | |
jeffknaggs | 0:865d42c46692 | 36 | bool isFull() { |
jeffknaggs | 0:865d42c46692 | 37 | return ((write + 1) % size == read); |
jeffknaggs | 0:865d42c46692 | 38 | }; |
jeffknaggs | 0:865d42c46692 | 39 | |
jeffknaggs | 0:865d42c46692 | 40 | bool isEmpty() { |
jeffknaggs | 0:865d42c46692 | 41 | return (read == write); |
jeffknaggs | 0:865d42c46692 | 42 | }; |
jeffknaggs | 0:865d42c46692 | 43 | |
jeffknaggs | 0:865d42c46692 | 44 | void queue(T k) { |
jeffknaggs | 0:865d42c46692 | 45 | if (isFull()) { |
jeffknaggs | 0:865d42c46692 | 46 | read++; |
jeffknaggs | 0:865d42c46692 | 47 | read %= size; |
jeffknaggs | 0:865d42c46692 | 48 | } |
jeffknaggs | 0:865d42c46692 | 49 | buf[write++] = k; |
jeffknaggs | 0:865d42c46692 | 50 | write %= size; |
jeffknaggs | 0:865d42c46692 | 51 | } |
jeffknaggs | 0:865d42c46692 | 52 | |
jeffknaggs | 0:865d42c46692 | 53 | uint16_t available() { |
jeffknaggs | 0:865d42c46692 | 54 | return (write >= read) ? write - read : size - read + write; |
jeffknaggs | 0:865d42c46692 | 55 | }; |
jeffknaggs | 0:865d42c46692 | 56 | |
jeffknaggs | 0:865d42c46692 | 57 | bool dequeue(T * c) { |
jeffknaggs | 0:865d42c46692 | 58 | bool empty = isEmpty(); |
jeffknaggs | 0:865d42c46692 | 59 | if (!empty) { |
jeffknaggs | 0:865d42c46692 | 60 | *c = buf[read++]; |
jeffknaggs | 0:865d42c46692 | 61 | read %= size; |
jeffknaggs | 0:865d42c46692 | 62 | } |
jeffknaggs | 0:865d42c46692 | 63 | return(!empty); |
jeffknaggs | 0:865d42c46692 | 64 | }; |
jeffknaggs | 0:865d42c46692 | 65 | |
jeffknaggs | 0:865d42c46692 | 66 | private: |
jeffknaggs | 0:865d42c46692 | 67 | volatile uint16_t write; |
jeffknaggs | 0:865d42c46692 | 68 | volatile uint16_t read; |
jeffknaggs | 0:865d42c46692 | 69 | uint16_t size; |
jeffknaggs | 0:865d42c46692 | 70 | T * buf; |
jeffknaggs | 0:865d42c46692 | 71 | }; |
jeffknaggs | 0:865d42c46692 | 72 | |
jeffknaggs | 0:865d42c46692 | 73 | #endif |