nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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