From 6df66b77ba5b27bce5630694742f2dac57b8d3eb Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 17 Jul 2018 15:43:26 +0200 Subject: Isolate usb-serial-rs485 tree --- usb-serial-rs485/ringb.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 usb-serial-rs485/ringb.c (limited to 'usb-serial-rs485/ringb.c') diff --git a/usb-serial-rs485/ringb.c b/usb-serial-rs485/ringb.c new file mode 100644 index 0000000..245325b --- /dev/null +++ b/usb-serial-rs485/ringb.c @@ -0,0 +1,59 @@ + +#include "ringb.h" +void ringb_init(struct ringb *ring, uint8_t *buf, int len) +{ + ring->buf_len = len; + ring->buf = buf; + ring->idx_r = 0; + ring->idx_w = 0; +} + +int ringb_get(struct ringb *ring) { +#if 1 + int rval; + if (ring->idx_r != ring->idx_w) { + rval = ring->buf[ring->idx_r]; + ring->idx_r = (ring->idx_r + 1) % ring->buf_len; + return rval; + } + return -1; +#else + if (((ring->idx_w - ring->idx_r) % ring->buf_len) >= 0) { + int rval = ring->buf[ring->idx_r]; + ring->idx_r = (ring->idx_r + 1) % ring->buf_len; + return rval; + } else { + return -1; + } +#endif +} + +bool ringb_put(struct ringb *ring, uint8_t c) { +#if 1 + unsigned int next = (ring->idx_w + 1) % ring->buf_len; + if (next != ring->idx_r) { + ring->buf[ring->idx_w] = c; + ring->idx_w = next; + return true; + } + return false; +#else + if (((ring->idx_w - ring->idx_r) % ring->buf_len) == 0) { + return false; + } + ring->buf[ring->idx_w] = c; + ring->idx_w = (ring->idx_w + 1) % ring->buf_len; + return true; +#endif + +} + + +void ringb_flush(struct ringb *ring) { + ring->idx_r = 0; + ring->idx_w = 0; +} + +int ringb_depth(struct ringb *ring) { + return ((unsigned int)(ring->idx_w - ring->idx_r) % ring->buf_len); +} \ No newline at end of file -- cgit