V18.
Dependencies: FATFileSystem mbed-rtos
Fork of USBHost by
USBHostSerial/MtxCircBuffer.h@8:93da8ea2708b, 2013-03-14 (annotated)
- Committer:
- samux
- Date:
- Thu Mar 14 11:49:53 2013 +0000
- Revision:
- 8:93da8ea2708b
- Parent:
- 4:b320d68e98e7
- Child:
- 24:868cbfe611a7
update doc + license
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 8:93da8ea2708b | 1 | /* mbed USBHost Library |
samux | 8:93da8ea2708b | 2 | * Copyright (c) 2006-2013 ARM Limited |
samux | 8:93da8ea2708b | 3 | * |
samux | 8:93da8ea2708b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
samux | 8:93da8ea2708b | 5 | * you may not use this file except in compliance with the License. |
samux | 8:93da8ea2708b | 6 | * You may obtain a copy of the License at |
samux | 8:93da8ea2708b | 7 | * |
samux | 8:93da8ea2708b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
samux | 8:93da8ea2708b | 9 | * |
samux | 8:93da8ea2708b | 10 | * Unless required by applicable law or agreed to in writing, software |
samux | 8:93da8ea2708b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
samux | 8:93da8ea2708b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
samux | 8:93da8ea2708b | 13 | * See the License for the specific language governing permissions and |
samux | 8:93da8ea2708b | 14 | * limitations under the License. |
samux | 8:93da8ea2708b | 15 | */ |
mbed_official | 0:a554658735bf | 16 | |
mbed_official | 0:a554658735bf | 17 | #ifndef MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 18 | #define MTXCIRCBUFFER_H |
mbed_official | 0:a554658735bf | 19 | |
mbed_official | 0:a554658735bf | 20 | #include "stdint.h" |
mbed_official | 0:a554658735bf | 21 | #include "rtos.h" |
mbed_official | 0:a554658735bf | 22 | |
mbed_official | 0:a554658735bf | 23 | //Mutex protected circular buffer |
mbed_official | 0:a554658735bf | 24 | template<typename T, int size> |
mbed_official | 0:a554658735bf | 25 | class MtxCircBuffer { |
mbed_official | 0:a554658735bf | 26 | public: |
mbed_official | 0:a554658735bf | 27 | |
mbed_official | 0:a554658735bf | 28 | MtxCircBuffer() { |
mbed_official | 0:a554658735bf | 29 | write = 0; |
mbed_official | 0:a554658735bf | 30 | read = 0; |
mbed_official | 0:a554658735bf | 31 | } |
mbed_official | 0:a554658735bf | 32 | |
mbed_official | 0:a554658735bf | 33 | bool isFull() { |
mbed_official | 0:a554658735bf | 34 | mtx.lock(); |
mbed_official | 0:a554658735bf | 35 | bool r = (((write + 1) % size) == read); |
mbed_official | 0:a554658735bf | 36 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 37 | return r; |
mbed_official | 0:a554658735bf | 38 | } |
mbed_official | 0:a554658735bf | 39 | |
mbed_official | 0:a554658735bf | 40 | bool isEmpty() { |
mbed_official | 0:a554658735bf | 41 | mtx.lock(); |
mbed_official | 0:a554658735bf | 42 | bool r = (read == write); |
mbed_official | 0:a554658735bf | 43 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 44 | return r; |
mbed_official | 0:a554658735bf | 45 | } |
samux | 4:b320d68e98e7 | 46 | |
samux | 4:b320d68e98e7 | 47 | void flush() { |
samux | 4:b320d68e98e7 | 48 | write = 0; |
samux | 4:b320d68e98e7 | 49 | read = 0; |
samux | 4:b320d68e98e7 | 50 | } |
mbed_official | 0:a554658735bf | 51 | |
mbed_official | 0:a554658735bf | 52 | void queue(T k) { |
mbed_official | 0:a554658735bf | 53 | mtx.lock(); |
mbed_official | 0:a554658735bf | 54 | while (((write + 1) % size) == read) { |
mbed_official | 0:a554658735bf | 55 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 56 | Thread::wait(10); |
mbed_official | 0:a554658735bf | 57 | mtx.lock(); |
mbed_official | 0:a554658735bf | 58 | } |
mbed_official | 0:a554658735bf | 59 | buf[write++] = k; |
mbed_official | 0:a554658735bf | 60 | write %= size; |
mbed_official | 0:a554658735bf | 61 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 62 | } |
mbed_official | 0:a554658735bf | 63 | |
mbed_official | 0:a554658735bf | 64 | uint16_t available() { |
mbed_official | 0:a554658735bf | 65 | mtx.lock(); |
mbed_official | 0:a554658735bf | 66 | uint16_t a = (write >= read) ? (write - read) : (size - read + write); |
mbed_official | 0:a554658735bf | 67 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 68 | return a; |
mbed_official | 0:a554658735bf | 69 | } |
mbed_official | 0:a554658735bf | 70 | |
mbed_official | 0:a554658735bf | 71 | bool dequeue(T * c) { |
mbed_official | 0:a554658735bf | 72 | mtx.lock(); |
mbed_official | 0:a554658735bf | 73 | bool empty = (read == write); |
mbed_official | 0:a554658735bf | 74 | if (!empty) { |
mbed_official | 0:a554658735bf | 75 | *c = buf[read++]; |
mbed_official | 0:a554658735bf | 76 | read %= size; |
mbed_official | 0:a554658735bf | 77 | } |
mbed_official | 0:a554658735bf | 78 | mtx.unlock(); |
mbed_official | 0:a554658735bf | 79 | return (!empty); |
mbed_official | 0:a554658735bf | 80 | } |
mbed_official | 0:a554658735bf | 81 | |
mbed_official | 0:a554658735bf | 82 | private: |
mbed_official | 0:a554658735bf | 83 | volatile uint16_t write; |
mbed_official | 0:a554658735bf | 84 | volatile uint16_t read; |
mbed_official | 0:a554658735bf | 85 | volatile T buf[size]; |
mbed_official | 0:a554658735bf | 86 | Mutex mtx; |
mbed_official | 0:a554658735bf | 87 | }; |
mbed_official | 0:a554658735bf | 88 | |
mbed_official | 0:a554658735bf | 89 | #endif |