summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-01-19 19:45:35 -0500
committerDan Halbert <halbert@halwitz.org>2019-01-19 19:45:35 -0500
commit28cfd8a5135a1e420b56d693cef20fc1a4721081 (patch)
treeb6715f13a8312c1ae1e9c0ffd57c116c45dae108 /py
parent7a33e588d40e01862b60d6f0b9d2880ad19be030 (diff)
CharacteristicBuffer: make it be a stream class; add locking
Diffstat (limited to 'py')
-rw-r--r--py/ringbuf.h32
-rw-r--r--py/stream.c2
2 files changed, 31 insertions, 3 deletions
diff --git a/py/ringbuf.h b/py/ringbuf.h
index c62e20e1d..5f82cc096 100644
--- a/py/ringbuf.h
+++ b/py/ringbuf.h
@@ -26,6 +26,8 @@
#ifndef MICROPY_INCLUDED_PY_RINGBUF_H
#define MICROPY_INCLUDED_PY_RINGBUF_H
+#include "py/gc.h"
+
#include <stdint.h>
typedef struct _ringbuf_t {
@@ -40,9 +42,9 @@ typedef struct _ringbuf_t {
// ringbuf_t buf = {buf_array, sizeof(buf_array)};
// Dynamic initialization. This creates root pointer!
-#define ringbuf_alloc(r, sz) \
+#define ringbuf_alloc(r, sz, long_lived) \
{ \
- (r)->buf = m_new(uint8_t, sz); \
+ (r)->buf = gc_alloc(sz, false, long_lived); \
(r)->size = sz; \
(r)->iget = (r)->iput = 0; \
}
@@ -71,4 +73,30 @@ static inline int ringbuf_put(ringbuf_t *r, uint8_t v) {
return 0;
}
+static inline uint16_t ringbuf_count(ringbuf_t *r)
+{
+ volatile int count = r->iput - r->iget;
+ if ( count < 0 ) {
+ count += r->size;
+ }
+
+ return (uint16_t) count;
+}
+
+static inline void ringbuf_clear(ringbuf_t *r)
+{
+ r->iput = r->iget = 0;
+}
+
+// will overwrite old data
+static inline void ringbuf_put_n(ringbuf_t* r, uint8_t* buf, uint8_t bufsize)
+{
+ for(uint8_t i=0; i < bufsize; i++) {
+ if ( ringbuf_put(r, buf[i]) < 0 ) {
+ // if full overwrite old data
+ (void) ringbuf_get(r);
+ ringbuf_put(r, buf[i]);
+ }
+ }
+}
#endif // MICROPY_INCLUDED_PY_RINGBUF_H
diff --git a/py/stream.c b/py/stream.c
index aca9b8460..9d8be445c 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -103,7 +103,7 @@ STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte fl
// CPython does a readall, but here we silently let negatives through,
// and they will cause a MemoryError.
mp_int_t sz;
- if (n_args == 1 || ((sz = mp_obj_get_int(args[1])) == -1)) {
+ if (n_args == 1 || args[1] == mp_const_none || ((sz = mp_obj_get_int(args[1])) == -1)) {
return stream_readall(args[0]);
}