Commit | Line | Data |
---|---|---|
d42aa478 | 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 | |
be4e7542 | 7 | #include <util/atomic.h> |
d42aa478 | 8 | static uint8_t rbuf[RBUF_SIZE]; |
9 | static uint8_t rbuf_head = 0; | |
10 | static uint8_t rbuf_tail = 0; | |
11 | static inline void rbuf_enqueue(uint8_t data) | |
12 | { | |
be4e7542 | 13 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
d42aa478 | 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 | } | |
be4e7542 | 21 | } |
d42aa478 | 22 | } |
23 | static inline uint8_t rbuf_dequeue(void) | |
24 | { | |
25 | uint8_t val = 0; | |
be4e7542 | 26 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
d42aa478 | 27 | |
d42aa478 | 28 | if (rbuf_head != rbuf_tail) { |
29 | val = rbuf[rbuf_tail]; | |
30 | rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE; | |
31 | } | |
be4e7542 | 32 | } |
d42aa478 | 33 | |
34 | return val; | |
35 | } | |
36 | static inline bool rbuf_has_data(void) | |
37 | { | |
be4e7542 WF |
38 | bool has_data; |
39 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { | |
40 | has_data = (rbuf_head != rbuf_tail); | |
41 | } | |
42 | return has_data; | |
d42aa478 | 43 | } |
44 | static inline void rbuf_clear(void) | |
45 | { | |
be4e7542 | 46 | ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { |
d42aa478 | 47 | rbuf_head = rbuf_tail = 0; |
be4e7542 | 48 | } |
d42aa478 | 49 | } |
50 | ||
51 | #endif /* RING_BUFFER_H */ |