Remove qmk archive generation (#8462)
[jackhill/qmk/firmware.git] / tmk_core / ring_buffer.h
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
7 #include <util/atomic.h>
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 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
13 uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
14 if (next != rbuf_tail) {
15 rbuf[rbuf_head] = data;
16 rbuf_head = next;
17 } else {
18 print("rbuf: full\n");
19 }
20 }
21 }
22 static inline uint8_t rbuf_dequeue(void) {
23 uint8_t val = 0;
24 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
25 if (rbuf_head != rbuf_tail) {
26 val = rbuf[rbuf_tail];
27 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
28 }
29 }
30
31 return val;
32 }
33 static inline bool rbuf_has_data(void) {
34 bool has_data;
35 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { has_data = (rbuf_head != rbuf_tail); }
36 return has_data;
37 }
38 static inline void rbuf_clear(void) {
39 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { rbuf_head = rbuf_tail = 0; }
40 }
41
42 #endif /* RING_BUFFER_H */