diff options
Diffstat (limited to 'include/fifo.h')
-rw-r--r-- | include/fifo.h | 45 |
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 |