aboutsummaryrefslogtreecommitdiff
path: root/common/ring_buffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/ring_buffer.h')
-rw-r--r--common/ring_buffer.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/common/ring_buffer.h b/common/ring_buffer.h
new file mode 100644
index 000000000..7bdebbcf3
--- /dev/null
+++ b/common/ring_buffer.h
@@ -0,0 +1,53 @@
1#ifndef RING_BUFFER_H
2#define RING_BUFFER_H
3/*--------------------------------------------------------------------
4 * Ring buffer to store scan codes from keyboard
5 *------------------------------------------------------------------*/
6#define RBUF_SIZE 32
7static uint8_t rbuf[RBUF_SIZE];
8static uint8_t rbuf_head = 0;
9static uint8_t rbuf_tail = 0;
10static inline void rbuf_enqueue(uint8_t data)
11{
12 uint8_t sreg = SREG;
13 cli();
14 uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
15 if (next != rbuf_tail) {
16 rbuf[rbuf_head] = data;
17 rbuf_head = next;
18 } else {
19 print("rbuf: full\n");
20 }
21 SREG = sreg;
22}
23static inline uint8_t rbuf_dequeue(void)
24{
25 uint8_t val = 0;
26
27 uint8_t sreg = SREG;
28 cli();
29 if (rbuf_head != rbuf_tail) {
30 val = rbuf[rbuf_tail];
31 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
32 }
33 SREG = sreg;
34
35 return val;
36}
37static inline bool rbuf_has_data(void)
38{
39 uint8_t sreg = SREG;
40 cli();
41 bool has_data = (rbuf_head != rbuf_tail);
42 SREG = sreg;
43 return has_data;
44}
45static inline void rbuf_clear(void)
46{
47 uint8_t sreg = SREG;
48 cli();
49 rbuf_head = rbuf_tail = 0;
50 SREG = sreg;
51}
52
53#endif /* RING_BUFFER_H */