]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/protocol/pbuff.h
xt_usb: Fix comment of scancode
[max/tmk_keyboard.git] / tmk_core / protocol / pbuff.h
1 /*--------------------------------------------------------------------
2  * Ring buffer to store scan codes from keyboard
3  *------------------------------------------------------------------*/
4
5 #ifndef PBUFF_H
6 #define PBUFF_H
7
8 #include "print.h"
9
10 #define PBUF_SIZE 32
11 static uint16_t pbuf[PBUF_SIZE];
12 static uint16_t pbuf_head = 0;
13 static uint16_t pbuf_tail = 0;
14 static inline void pbuf_enqueue(uint16_t data)
15 {
16     uint8_t sreg = SREG;
17     cli();
18     uint16_t next = (pbuf_head + 1) % PBUF_SIZE;
19     if (next != pbuf_tail) {
20         pbuf[pbuf_head] = data;
21         pbuf_head = next;
22     } else {
23         print("pbuf: full\n");
24     }
25     SREG = sreg;
26 }
27 static inline uint16_t pbuf_dequeue(void)
28 {
29     uint16_t val = 0;
30
31     uint8_t sreg = SREG;
32     cli();
33     if (pbuf_head != pbuf_tail) {
34         val = pbuf[pbuf_tail];
35         pbuf_tail = (pbuf_tail + 1) % PBUF_SIZE;
36     }
37     SREG = sreg;
38
39     return val;
40 }
41 static inline bool pbuf_has_data(void)
42 {
43     uint8_t sreg = SREG;
44     cli();
45     bool has_data = (pbuf_head != pbuf_tail);
46     SREG = sreg;
47     return has_data;
48 }
49 static inline void pbuf_clear(void)
50 {
51     uint8_t sreg = SREG;
52     cli();
53     pbuf_head = pbuf_tail = 0;
54     SREG = sreg;
55 }
56
57 #endif