py/ringbuf: Add peek16 method.

This commit is contained in:
Jim Mussared 2019-11-11 17:07:11 +11:00 committed by Damien George
parent 973f68780d
commit 4f96689281
2 changed files with 14 additions and 6 deletions

View File

@ -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) {

View File

@ -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