Takuya Urakawa / F401RE-USBHost

Dependencies:   FATFileSystem

Dependents:   F401RE-USBHostMIDI_RecieveExample

Fork of F401RE-USBHost by Norimasa Okamoto

Revision:
18:bac56d0365e1
diff -r 4a710e2ba162 -r bac56d0365e1 USBHostMIDI/CircBuffer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostMIDI/CircBuffer.h	Wed Jun 25 20:48:01 2014 +0000
@@ -0,0 +1,80 @@
+/* USBHostMidi library
+ * Originaled by k.shoji
+ * porting by Takuya Urakawa
+ */
+
+/* mbed USBHost Library
+ * Copyright (c) 2006-2013 ARM Limited
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef CIRCBUFFER_H
+#define CIRCBUFFER_H
+
+#include "stdint.h"
+
+//circular buffer
+template<typename T, int size>
+class CircBuffer {
+public:
+    
+    CircBuffer() {
+        write = 0;
+        read = 0;
+    }
+
+    bool isFull() {
+        bool r = (((write + 1) % size) == read);
+        return r;
+    }
+
+    bool isEmpty() {
+        bool r = (read == write);
+        return r;
+    }
+    
+    void flush() {
+        write = 0;
+        read = 0;
+    }
+
+    void queue(T k) {
+        while (((write + 1) % size) == read) {
+            wait_ms(10);
+        }
+        buf[write++] = k;
+        write %= size;
+    }
+
+    uint16_t available() {
+        uint16_t a = (write >= read) ? (write - read) : (size - read + write);
+        return a;
+    }
+
+    bool dequeue(T * c) {
+        bool empty = (read == write);
+        if (!empty) {
+            *c = buf[read++];
+            read %= size;
+        }
+        return (!empty);
+    }
+
+private:
+    volatile uint16_t write;
+    volatile uint16_t read;
+    volatile T buf[size];
+};
+
+#endif