aboutsummaryrefslogtreecommitdiff
path: root/include/fifo.h
blob: c6eee2df323c232588f86b2d1567cddbc2ac7385 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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