From 6d4ad089c5b758ad8af4f68bf385a26ec4e9653a Mon Sep 17 00:00:00 2001 From: Thomas Albers Raviola Date: Thu, 21 Nov 2024 15:55:03 +0100 Subject: Initial commit --- include/fifo.h | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 include/fifo.h (limited to 'include/fifo.h') 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 + +#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 -- cgit v1.2.3