]>
Commit | Line | Data |
---|---|---|
762a42ef L |
1 | #ifndef RING_H |
2 | #define RING_H | |
3 | ||
4 | struct ring { | |
5 | uint8_t *data; | |
6 | uint_fast8_t mask; | |
7 | volatile uint_fast8_t begin; | |
8 | volatile uint_fast8_t end; | |
9 | }; | |
10 | ||
11 | ||
12 | static inline | |
13 | void ring_init(struct ring *ring, uint8_t *buf, int size) | |
14 | { | |
15 | ring->data = buf; | |
16 | ring->mask = (size-1) & 0xff; | |
17 | ring->begin = 0; | |
18 | ring->end = 0; | |
19 | } | |
20 | ||
21 | static inline | |
22 | int ring_write_ch(struct ring *ring, uint8_t ch) | |
23 | { | |
24 | uint_fast8_t ep = ring->end; | |
25 | ||
26 | ring->data[ep] = ch; | |
27 | ep = (ep + 1) & ring->mask; | |
28 | ||
29 | if ((ep) != ring->begin) { | |
30 | ring->end = ep; | |
31 | return 1; | |
32 | } | |
33 | ||
34 | return -1; | |
35 | } | |
36 | ||
37 | #if 0 | |
38 | static inline | |
39 | int ring_write(struct ring *ring, uint8_t *data, int size) | |
40 | { | |
41 | int i; | |
42 | ||
43 | for (i = 0; i < size; i++) { | |
44 | if (ring_write_ch(ring, data[i]) < 0) | |
45 | return -i; | |
46 | } | |
47 | ||
48 | return i; | |
49 | } | |
50 | #endif | |
51 | ||
52 | static inline | |
53 | int ring_read_ch(struct ring *ring) | |
54 | { | |
55 | int ret = -1; | |
56 | uint_fast8_t bp = ring->begin; | |
57 | ||
58 | if (bp != ring->end) { | |
59 | ret = ring->data[bp]; | |
60 | ring->begin = (bp + 1) & ring->mask; | |
61 | } | |
62 | ||
63 | return ret; | |
64 | } | |
65 | ||
66 | ||
67 | static inline | |
68 | int_fast8_t ring_is_empty(struct ring *ring) | |
69 | { | |
70 | return ring->begin == ring->end; | |
71 | } | |
72 | ||
73 | #endif /* RING_H */ | |
74 |