aboutsummaryrefslogtreecommitdiff
path: root/include/fifo.h
diff options
context:
space:
mode:
authorThomas Albers Raviola <thomas@thomaslabs.org>2024-11-21 15:55:03 +0100
committerThomas Albers Raviola <thomas@thomaslabs.org>2024-11-21 15:55:03 +0100
commit6d4ad089c5b758ad8af4f68bf385a26ec4e9653a (patch)
tree2fd65006b57d646b53e121aef42bba5ee5852840 /include/fifo.h
Initial commit
Diffstat (limited to 'include/fifo.h')
-rw-r--r--include/fifo.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/include/fifo.h b/include/fifo.h
new file mode 100644
index 0000000..c6eee2d
--- /dev/null
+++ b/include/fifo.h
@@ -0,0 +1,45 @@
+#ifndef FIFO_H
+#define FIFO_H
+
+#include <zeta.h>
+
+#define FIFO_LEN 32
+
+struct fifo {
+ uint8_t head;
+ uint8_t tail;
+ uint8_t data[FIFO_LEN];
+};
+
+static inline uint8_t
+fifo_pop(struct fifo *fifo)
+{
+ uint8_t ret = fifo->data[fifo->head];
+ if (++fifo->head >= LENGTH(fifo->data))
+ fifo->head = 0;
+ return ret;
+}
+
+static inline void
+fifo_push(struct fifo *fifo, uint8_t v)
+{
+ fifo->data[fifo->tail] = v;
+
+ if (++fifo->tail >= LENGTH(fifo->data))
+ fifo->tail = 0;
+}
+
+static inline bool
+fifo_empty(const struct fifo *fifo)
+{
+ return (fifo->head == fifo->tail);
+}
+
+static inline void
+fifo_clear(struct fifo *fifo)
+{
+ fifo->head = 0;
+ fifo->tail = 0;
+}
+
+#endif // FIFO_H