summaryrefslogtreecommitdiff
path: root/py/ringbuf.c
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2022-09-26 11:02:31 +1000
committerDamien George <damien@micropython.org>2024-09-19 18:00:44 +1000
commit7e14680a83525bf0822ef9cab899a5625496d662 (patch)
treee5176548c07d8973956b48e1f3a462c36ed1e532 /py/ringbuf.c
parent6c73573b34c3fbd3d4da8d56767db03a8e5dd540 (diff)
py/objringio: Add micropython.RingIO() interface for general use.
This commit adds a new `RingIO` type which exposes the internal ring-buffer code for general use in Python programs. It has the stream interface making it similar to `StringIO` and `BytesIO`, except `RingIO` has a fixed buffer size and is automatically safe when reads and writes are in different threads or an IRQ. This new type is enabled at the "extra features" ROM level. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Diffstat (limited to 'py/ringbuf.c')
-rw-r--r--py/ringbuf.c26
1 files changed, 2 insertions, 24 deletions
diff --git a/py/ringbuf.c b/py/ringbuf.c
index 10dca6208..5f77271a0 100644
--- a/py/ringbuf.c
+++ b/py/ringbuf.c
@@ -24,8 +24,6 @@
* THE SOFTWARE.
*/
-#include <string.h>
-
#include "ringbuf.h"
int ringbuf_get16(ringbuf_t *r) {
@@ -83,17 +81,7 @@ int ringbuf_get_bytes(ringbuf_t *r, uint8_t *data, size_t data_len) {
if (ringbuf_avail(r) < data_len) {
return (r->size <= data_len) ? -2 : -1;
}
- uint32_t iget = r->iget;
- uint32_t iget_a = (iget + data_len) % r->size;
- uint8_t *datap = data;
- if (iget_a < iget) {
- // Copy part of the data from the space left at the end of the buffer
- memcpy(datap, r->buf + iget, r->size - iget);
- datap += (r->size - iget);
- iget = 0;
- }
- memcpy(datap, r->buf + iget, iget_a - iget);
- r->iget = iget_a;
+ ringbuf_memcpy_get_internal(r, data, data_len);
return 0;
}
@@ -105,16 +93,6 @@ int ringbuf_put_bytes(ringbuf_t *r, const uint8_t *data, size_t data_len) {
if (ringbuf_free(r) < data_len) {
return (r->size <= data_len) ? -2 : -1;
}
- uint32_t iput = r->iput;
- uint32_t iput_a = (iput + data_len) % r->size;
- const uint8_t *datap = data;
- if (iput_a < iput) {
- // Copy part of the data to the end of the buffer
- memcpy(r->buf + iput, datap, r->size - iput);
- datap += (r->size - iput);
- iput = 0;
- }
- memcpy(r->buf + iput, datap, iput_a - iput);
- r->iput = iput_a;
+ ringbuf_memcpy_put_internal(r, data, data_len);
return 0;
}