aboutsummaryrefslogtreecommitdiff
path: root/protocol/lufa/midi/bytequeue/bytequeue.c
diff options
context:
space:
mode:
Diffstat (limited to 'protocol/lufa/midi/bytequeue/bytequeue.c')
-rwxr-xr-xprotocol/lufa/midi/bytequeue/bytequeue.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/protocol/lufa/midi/bytequeue/bytequeue.c b/protocol/lufa/midi/bytequeue/bytequeue.c
new file mode 100755
index 000000000..e43495632
--- /dev/null
+++ b/protocol/lufa/midi/bytequeue/bytequeue.c
@@ -0,0 +1,65 @@
1//this is a single reader [maybe multiple writer?] byte queue
2//Copyright 2008 Alex Norman
3//writen by Alex Norman
4//
5//This file is part of avr-bytequeue.
6//
7//avr-bytequeue is free software: you can redistribute it and/or modify
8//it under the terms of the GNU General Public License as published by
9//the Free Software Foundation, either version 3 of the License, or
10//(at your option) any later version.
11//
12//avr-bytequeue is distributed in the hope that it will be useful,
13//but WITHOUT ANY WARRANTY; without even the implied warranty of
14//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15//GNU General Public License for more details.
16//
17//You should have received a copy of the GNU General Public License
18//along with avr-bytequeue. If not, see <http://www.gnu.org/licenses/>.
19
20#include "bytequeue.h"
21#include "interrupt_setting.h"
22
23void bytequeue_init(byteQueue_t * queue, uint8_t * dataArray, byteQueueIndex_t arrayLen){
24 queue->length = arrayLen;
25 queue->data = dataArray;
26 queue->start = queue->end = 0;
27}
28
29bool bytequeue_enqueue(byteQueue_t * queue, uint8_t item){
30 interrupt_setting_t setting = store_and_clear_interrupt();
31 //full
32 if(((queue->end + 1) % queue->length) == queue->start){
33 restore_interrupt_setting(setting);
34 return false;
35 } else {
36 queue->data[queue->end] = item;
37 queue->end = (queue->end + 1) % queue->length;
38 restore_interrupt_setting(setting);
39 return true;
40 }
41}
42
43byteQueueIndex_t bytequeue_length(byteQueue_t * queue){
44 byteQueueIndex_t len;
45 interrupt_setting_t setting = store_and_clear_interrupt();
46 if(queue->end >= queue->start)
47 len = queue->end - queue->start;
48 else
49 len = (queue->length - queue->start) + queue->end;
50 restore_interrupt_setting(setting);
51 return len;
52}
53
54//we don't need to avoid interrupts if there is only one reader
55uint8_t bytequeue_get(byteQueue_t * queue, byteQueueIndex_t index){
56 return queue->data[(queue->start + index) % queue->length];
57}
58
59//we just update the start index to remove elements
60void bytequeue_remove(byteQueue_t * queue, byteQueueIndex_t numToRemove){
61 interrupt_setting_t setting = store_and_clear_interrupt();
62 queue->start = (queue->start + numToRemove) % queue->length;
63 restore_interrupt_setting(setting);
64}
65