test public

Dependencies:   HttpServer_snapshot_mbed-os

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CircBufferHostSerial.h Source File

CircBufferHostSerial.h

00001 /* mbed USBHost Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef CIRCBUFFERHOSTSERIAL_H
00018 #define CIRCBUFFERHOSTSERIAL_H
00019 
00020 template <class T>
00021 class CircBufferHostSerial {
00022 public:
00023     CircBufferHostSerial(uint32_t buf_size):write(0), read(0), size(buf_size + 1) {
00024         _buf = new T [size];
00025     }
00026 
00027     ~CircBufferHostSerial() {
00028         delete [] _buf;
00029     }
00030 
00031     bool isFull() {
00032         return ((write + 1) % size == read);
00033     }
00034 
00035     bool isEmpty() {
00036         return (read == write);
00037     }
00038 
00039     void flush() {
00040         write = 0;
00041         read = 0;
00042     }
00043 
00044     void queue(T k) {
00045         if (isFull()) {
00046             read++;
00047             read %= size;
00048         }
00049         _buf[write++] = k;
00050         write %= size;
00051     }
00052 
00053     uint32_t available() {
00054         return (write >= read) ? write - read : size - read + write;
00055     }
00056 
00057     bool dequeue(T * c) {
00058         bool empty = isEmpty();
00059         if (!empty) {
00060             *c = _buf[read++];
00061             read %= size;
00062         }
00063         return(!empty);
00064     }
00065 
00066 private:
00067     volatile uint32_t write;
00068     volatile uint32_t read;
00069     int size;
00070     T * _buf;
00071 };
00072 #endif
00073