diff options
| author | dmlunar <root@lunar.sh> | 2025-01-22 16:47:21 +0200 |
|---|---|---|
| committer | dmlunar <root@lunar.sh> | 2025-10-15 23:42:50 +0200 |
| commit | 729f2a2c3ebfb2612d873caf453a1d7ca02180d9 (patch) | |
| tree | 7bab2fcc0c7f50eab3013348697bc06ddd71d551 /firmware/src/peripheral/ring.c | |
| download | varpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.tar.gz varpa-729f2a2c3ebfb2612d873caf453a1d7ca02180d9.zip | |
varpa: initial public commit
Diffstat (limited to 'firmware/src/peripheral/ring.c')
| -rw-r--r-- | firmware/src/peripheral/ring.c | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/firmware/src/peripheral/ring.c b/firmware/src/peripheral/ring.c new file mode 100644 index 0000000..eacb654 --- /dev/null +++ b/firmware/src/peripheral/ring.c @@ -0,0 +1,115 @@ +/** + * + * Author: Dylan Muller + * Copyright (c) 2025 + * All rights reserved. + * + * - Commercial/IP use prohibited. + * - Attribution required. + * See License.txt + * + */ + +#include "peripheral/ring.h" + +#define RING_INC_ROLL_OVER(n, s, e) (((n)+1>=(e)) ? (s) : (n)+1) + +ring_t ring_init( + uint8_t *buf, + size_t len +) +{ + return RING_INIT(buf, len); +} + +bool ring_is_empty( + ring_t ring +) +{ + return ring.read == ring.write; +} + +bool ring_is_full( + ring_t ring +) +{ + return RING_INC_ROLL_OVER(ring.write, ring.buf, ring.end) == ring.read; +} + +size_t ring_push_available( + ring_t ring +) +{ + if(ring.write < ring.read) + return ring.read - ring.write - 1; + else + return (ring.end - ring.buf) - (ring.write - ring.read) - 1; +} + +size_t ring_pop_available( + ring_t ring +) +{ + if(ring.read <= ring.write) + return ring.write - ring.read; + else + return (ring.end - ring.buf) - (ring.read - ring.write); +} + +bool ring_push( + ring_t *ring, + uint8_t data +) +{ + if(ring_is_full(*ring)) + return 1; + + *ring->write = data; + ring->write = RING_INC_ROLL_OVER(ring->write, ring->buf, ring->end); + + return 0; +} + +bool ring_push_over( + ring_t *ring, + uint8_t data +) +{ + *ring->write = data; + ring->write = RING_INC_ROLL_OVER(ring->write, ring->buf, ring->end); + + if(ring->read == ring->write) + { + ring->read = RING_INC_ROLL_OVER(ring->read, ring->buf, ring->end); + return 1; + } + + return 0; +} + +bool ring_pop( + ring_t *ring, + uint8_t *data +) +{ + if(ring_is_empty(*ring)) + return 1; + + *data = *ring->read; + ring->read = RING_INC_ROLL_OVER(ring->read, ring->buf, ring->end); + + return 0; +} + +bool ring_peek( + ring_t *ring, + uint8_t *data +) +{ + if(ring_is_empty(*ring)) + return 1; + + *data = *ring->read; + + return 0; +} |
