summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2014-10-27 21:39:02 +0900
committerJun Wako <wakojun@gmail.com>2015-04-22 14:18:13 +0900
commitd42aa47809c97a77c49e9396201c2b5c3956bb54 (patch)
tree07331fbb4b67630a5571f31af071021fbc9eb160 /common
parent5ea50bfb30fa9917f6c1209a7ae7f31b7e7b4095 (diff)
Fix ibm4704 protocol with using interrupt
Diffstat (limited to 'common')
-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 0000000000..7bdebbcf34
--- /dev/null
+++ b/common/ring_buffer.h
@@ -0,0 +1,53 @@
+#ifndef RING_BUFFER_H
+#define RING_BUFFER_H
+/*--------------------------------------------------------------------
+ * Ring buffer to store scan codes from keyboard
+ *------------------------------------------------------------------*/
+#define RBUF_SIZE 32
+static uint8_t rbuf[RBUF_SIZE];
+static uint8_t rbuf_head = 0;
+static uint8_t rbuf_tail = 0;
+static inline void rbuf_enqueue(uint8_t data)
+{
+ uint8_t sreg = SREG;
+ cli();
+ uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
+ if (next != rbuf_tail) {
+ rbuf[rbuf_head] = data;
+ rbuf_head = next;
+ } else {
+ print("rbuf: full\n");
+ }
+ SREG = sreg;
+}
+static inline uint8_t rbuf_dequeue(void)
+{
+ uint8_t val = 0;
+
+ uint8_t sreg = SREG;
+ cli();
+ if (rbuf_head != rbuf_tail) {
+ val = rbuf[rbuf_tail];
+ rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
+ }
+ SREG = sreg;
+
+ return val;
+}
+static inline bool rbuf_has_data(void)
+{
+ uint8_t sreg = SREG;
+ cli();
+ bool has_data = (rbuf_head != rbuf_tail);
+ SREG = sreg;
+ return has_data;
+}
+static inline void rbuf_clear(void)
+{
+ uint8_t sreg = SREG;
+ cli();
+ rbuf_head = rbuf_tail = 0;
+ SREG = sreg;
+}
+
+#endif /* RING_BUFFER_H */