summaryrefslogtreecommitdiff
path: root/firmware/inc/peripheral/ring.h
blob: d2a713d60652d036d89b40f481592f6022628337 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
 * 
 * Author: Dylan Muller
 * Copyright (c) 2025 
 * All rights reserved.
 * 
 * - Commercial/IP use prohibited.
 * - Attribution required.
 * See License.txt
 *
 */

#ifndef RING_H_
#define RING_H_

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

typedef struct
{
    uint8_t *buf;
    uint8_t *end;
    uint8_t *write;
    uint8_t *read;
} ring_t;

#define RING_INIT(buf_, len_) \
    ((ring_t){ \
    .buf = (buf_), \
    .end = (buf_)+(len_), \
    .write = (buf_), \
    .read = (buf_)} \
    )

ring_t ring_init(
    uint8_t *buf,
    size_t len
);

bool ring_is_empty(
    ring_t ring
);

bool ring_is_full(
    ring_t ring
);

size_t ring_push_available(
    ring_t ring
);

size_t ring_pop_available(
    ring_t ring
);

bool ring_push(
    ring_t *ring,
    uint8_t data
);

bool ring_push_over(
    ring_t *ring,
    uint8_t data
);

bool ring_pop(
    ring_t *ring,
    uint8_t *data
);

bool ring_peek(
    ring_t *ring,
    uint8_t *data
);

#endif