diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2019-11-11 17:07:11 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-11-21 12:04:53 +1100 |
commit | 4f966892813b1b0c43f7ad1d97a7dcd77c646302 (patch) | |
tree | 56d87176b16b32ed4aaff703823dd213cb80a2ea /py | |
parent | 973f68780d2eb974e4921ffdf513079efc19e0a4 (diff) |
py/ringbuf: Add peek16 method.
Diffstat (limited to 'py')
-rw-r--r-- | py/ringbuf.c | 19 | ||||
-rw-r--r-- | py/ringbuf.h | 1 |
2 files changed, 14 insertions, 6 deletions
diff --git a/py/ringbuf.c b/py/ringbuf.c index 8795e1eda..83887b300 100644 --- a/py/ringbuf.c +++ b/py/ringbuf.c @@ -26,6 +26,18 @@ #include "ringbuf.h" int ringbuf_get16(ringbuf_t *r) { + int v = ringbuf_peek16(r); + if (v == -1) { + return v; + } + r->iget += 2; + if (r->iget >= r->size) { + r->iget -= r->size; + } + return v; +} + +int ringbuf_peek16(ringbuf_t *r) { if (r->iget == r->iput) { return -1; } @@ -36,12 +48,7 @@ int ringbuf_get16(ringbuf_t *r) { if (iget_a == r->iput) { return -1; } - uint16_t v = (r->buf[r->iget] << 8) | (r->buf[iget_a]); - r->iget = iget_a + 1; - if (r->iget == r->size) { - r->iget = 0; - } - return v; + return (r->buf[r->iget] << 8) | (r->buf[iget_a]); } int ringbuf_put16(ringbuf_t *r, uint16_t v) { diff --git a/py/ringbuf.h b/py/ringbuf.h index 6b0d209bd..8d4ed1643 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -82,6 +82,7 @@ static inline size_t ringbuf_avail(ringbuf_t *r) { // Note: big-endian. No-op if not enough room available for both bytes. int ringbuf_get16(ringbuf_t *r); +int ringbuf_peek16(ringbuf_t *r); int ringbuf_put16(ringbuf_t *r, uint16_t v); #endif // MICROPY_INCLUDED_PY_RINGBUF_H |