Andrew Leech 7e14680a83 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>
2024-09-19 18:00:44 +10:00

49 lines
739 B
Python

# Check that micropython.RingIO works correctly.
import micropython
try:
micropython.RingIO
except AttributeError:
print("SKIP")
raise SystemExit
rb = micropython.RingIO(16)
print(rb)
print(rb.any())
rb.write(b"\x00")
print(rb.any())
rb.write(b"\x00")
print(rb.any())
print(rb.read(2))
print(rb.any())
rb.write(b"\x00\x01")
print(rb.read())
print(rb.read(1))
# Try to write more data than can fit at one go.
print(rb.write(b"\x00\x01" * 10))
print(rb.write(b"\x00"))
print(rb.read())
ba = bytearray(17)
rb = micropython.RingIO(ba)
print(rb)
print(rb.write(b"\x00\x01" * 10))
print(rb.write(b"\x00"))
print(rb.read())
try:
# Size must be int.
micropython.RingIO(None)
except TypeError as ex:
print(ex)