Browse Source

py/ringbuf: Add peek16 method.

pull/5105/head
Jim Mussared 5 years ago
committed by Damien George
parent
commit
4f96689281
  1. 19
      py/ringbuf.c
  2. 1
      py/ringbuf.h

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

1
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

Loading…
Cancel
Save