]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBDevice/USBSerial/CircBuffer.h
remove experimental return, cleanup slash_question key
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBDevice / USBSerial / CircBuffer.h
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #ifndef CIRCBUFFER_H
20 #define CIRCBUFFER_H
21
22 template <class T, int Size>
23 class CircBuffer {
24 public:
25 CircBuffer():write(0), read(0){}
26 bool isFull() {
27 return ((write + 1) % size == read);
28 };
29
30 bool isEmpty() {
31 return (read == write);
32 };
33
34 void queue(T k) {
35 if (isFull()) {
36 read++;
37 read %= size;
38 }
39 buf[write++] = k;
40 write %= size;
41 }
42
43 uint16_t available() {
44 return (write >= read) ? write - read : size - read + write;
45 };
46
47 bool dequeue(T * c) {
48 bool empty = isEmpty();
49 if (!empty) {
50 *c = buf[read++];
51 read %= size;
52 }
53 return(!empty);
54 };
55
56 private:
57 volatile uint16_t write;
58 volatile uint16_t read;
59 static const int size = Size+1; //a modern optimizer should be able to remove this so it uses no ram.
60 T buf[Size];
61 };
62
63 #endif
Imprint / Impressum