diff options
author | root <root@lab.panaceas.james.local> | 2018-04-08 01:14:30 +0100 |
---|---|---|
committer | root <root@lab.panaceas.james.local> | 2018-04-08 01:14:30 +0100 |
commit | 25e6afd831b94b9735cc6691ee19c8edc921aca7 (patch) | |
tree | cc194e7cb067c929b3e8bc9c03552d2d300c301b /app/ring.c | |
download | clock-25e6afd831b94b9735cc6691ee19c8edc921aca7.tar.gz clock-25e6afd831b94b9735cc6691ee19c8edc921aca7.tar.bz2 clock-25e6afd831b94b9735cc6691ee19c8edc921aca7.zip |
working decoding
Diffstat (limited to 'app/ring.c')
-rw-r--r-- | app/ring.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/app/ring.c b/app/ring.c new file mode 100644 index 0000000..973f345 --- /dev/null +++ b/app/ring.c @@ -0,0 +1,77 @@ +#include "project.h" + + +static inline size_t +ring_next (volatile ring_t *r, size_t p) +{ + p++; + + if (p >= r->size) + p -= r->size; + + return p; +} + +void +ring_init (volatile ring_t *r, uint8_t *buf, size_t len) +{ + r->data = buf; + r->size = len; + r->write = 0; + r->read = 0; +} + +int +ring_write_byte (volatile ring_t *r, uint8_t c) +{ + size_t n = ring_next (r, r->write); + + if (n == r->read) + return -EAGAIN; + + r->data[r->write] = c; + + r->write = n; + + return 0; +} + + +int +ring_read_byte (volatile ring_t *r, uint8_t *c) +{ + size_t n = ring_next (r, r->read); + + if (r->read == r->write) + return -EAGAIN; + + *c = r->data[r->read]; + r->read = n; + + return 0; +} + +int +ring_write (volatile ring_t *r, uint8_t *buf, size_t len, int blocking) +{ + while (len--) { + if (blocking) { + while (ring_write_byte (r, *buf)); + + buf++; + } else { + if (ring_write_byte (r, * (buf++))) + return -EAGAIN; + } + } + + return 0; +} + + + +int +ring_empty (volatile ring_t *r) +{ + return (r->read == r->write) ? 1 : 0; +} |