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