USBHost library for WAVE file player
Fork of USBHost by
USBHostSerial/MtxCircBuffer.h@4:b320d68e98e7, 2013-03-12 (annotated)
- Committer:
- samux
- Date:
- Tue Mar 12 17:23:37 2013 +0000
- Revision:
- 4:b320d68e98e7
- Parent:
- 0:a554658735bf
- Child:
- 8:93da8ea2708b
bug fixes - support composite device
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:a554658735bf | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
mbed_official | 0:a554658735bf | 2 | * |
mbed_official | 0:a554658735bf | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
mbed_official | 0:a554658735bf | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
mbed_official | 0:a554658735bf | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
mbed_official | 0:a554658735bf | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
mbed_official | 0:a554658735bf | 7 | * Software is furnished to do so, subject to the following conditions: |
mbed_official | 0:a554658735bf | 8 | * |
mbed_official | 0:a554658735bf | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
mbed_official | 0:a554658735bf | 10 | * substantial portions of the Software. |
mbed_official | 0:a554658735bf | 11 | * |
mbed_official | 0:a554658735bf | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
mbed_official | 0:a554658735bf | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
mbed_official | 0:a554658735bf | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
mbed_official | 0:a554658735bf | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:a554658735bf | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
mbed_official | 0:a554658735bf | 17 | */ |
mbed_official | 0:a554658735bf | 18 | |
mbed_official | 0:a554658735bf | 19 | #ifndef MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 20 | #define MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 21 | |
mbed_official | 0:a554658735bf | 22 | #include "stdint.h" |
mbed_official | 0:a554658735bf | 23 | #include "rtos.h" |
mbed_official | 0:a554658735bf | 24 | |
mbed_official | 0:a554658735bf | 25 | //Mutex protected circular buffer |
mbed_official | 0:a554658735bf | 26 | template<typename T, int size> |
mbed_official | 0:a554658735bf | 27 | class MtxCircBuffer { |
mbed_official | 0:a554658735bf | 28 | public: |
mbed_official | 0:a554658735bf | 29 | |
mbed_official | 0:a554658735bf | 30 | MtxCircBuffer() { |
mbed_official | 0:a554658735bf | 31 | write = 0; |
mbed_official | 0:a554658735bf | 32 | read = 0; |
mbed_official | 0:a554658735bf | 33 | } |
mbed_official | 0:a554658735bf | 34 | |
mbed_official | 0:a554658735bf | 35 | bool isFull() { |
mbed_official | 0:a554658735bf | 36 | mtx.lock(); |
mbed_official | 0:a554658735bf | 37 | bool r = (((write + 1) % size) == read); |
mbed_official | 0:a554658735bf | 38 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 39 | return r; |
mbed_official | 0:a554658735bf | 40 | } |
mbed_official | 0:a554658735bf | 41 | |
mbed_official | 0:a554658735bf | 42 | bool isEmpty() { |
mbed_official | 0:a554658735bf | 43 | mtx.lock(); |
mbed_official | 0:a554658735bf | 44 | bool r = (read == write); |
mbed_official | 0:a554658735bf | 45 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 46 | return r; |
mbed_official | 0:a554658735bf | 47 | } |
samux | 4:b320d68e98e7 | 48 | |
samux | 4:b320d68e98e7 | 49 | void flush() { |
samux | 4:b320d68e98e7 | 50 | write = 0; |
samux | 4:b320d68e98e7 | 51 | read = 0; |
samux | 4:b320d68e98e7 | 52 | } |
mbed_official | 0:a554658735bf | 53 | |
mbed_official | 0:a554658735bf | 54 | void queue(T k) { |
mbed_official | 0:a554658735bf | 55 | mtx.lock(); |
mbed_official | 0:a554658735bf | 56 | while (((write + 1) % size) == read) { |
mbed_official | 0:a554658735bf | 57 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 58 | Thread::wait(10); |
mbed_official | 0:a554658735bf | 59 | mtx.lock(); |
mbed_official | 0:a554658735bf | 60 | } |
mbed_official | 0:a554658735bf | 61 | buf[write++] = k; |
mbed_official | 0:a554658735bf | 62 | write %= size; |
mbed_official | 0:a554658735bf | 63 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 64 | } |
mbed_official | 0:a554658735bf | 65 | |
mbed_official | 0:a554658735bf | 66 | uint16_t available() { |
mbed_official | 0:a554658735bf | 67 | mtx.lock(); |
mbed_official | 0:a554658735bf | 68 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
mbed_official | 0:a554658735bf | 69 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 70 | return a; |
mbed_official | 0:a554658735bf | 71 | } |
mbed_official | 0:a554658735bf | 72 | |
mbed_official | 0:a554658735bf | 73 | bool dequeue(T * c) { |
mbed_official | 0:a554658735bf | 74 | mtx.lock(); |
mbed_official | 0:a554658735bf | 75 | bool empty = (read == write); |
mbed_official | 0:a554658735bf | 76 | if (!empty) { |
mbed_official | 0:a554658735bf | 77 | *c = buf[read++]; |
mbed_official | 0:a554658735bf | 78 | read %= size; |
mbed_official | 0:a554658735bf | 79 | } |
mbed_official | 0:a554658735bf | 80 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 81 | return (!empty); |
mbed_official | 0:a554658735bf | 82 | } |
mbed_official | 0:a554658735bf | 83 | |
mbed_official | 0:a554658735bf | 84 | private: |
mbed_official | 0:a554658735bf | 85 | volatile uint16_t write; |
mbed_official | 0:a554658735bf | 86 | volatile uint16_t read; |
mbed_official | 0:a554658735bf | 87 | volatile T buf[size]; |
mbed_official | 0:a554658735bf | 88 | Mutex mtx; |
mbed_official | 0:a554658735bf | 89 | }; |
mbed_official | 0:a554658735bf | 90 | |
mbed_official | 0:a554658735bf | 91 | #endif |