forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
py/ringbuf: Add micropython.ringbuffer() interface for general use.
Signed-off-by: Andrew Leech <[email protected]>
- Loading branch information
Showing
9 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# check that micropython.ringbuffer works correctly. | ||
|
||
import micropython | ||
|
||
try: | ||
micropython.ringbuffer | ||
except AttributeError: | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
rb = micropython.ringbuffer(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)) | ||
|
||
print(rb.write(b"\x00\x01" * 10)) | ||
print(rb.read()) | ||
|
||
try: | ||
# size must be int. | ||
micropython.ringbuffer(None) | ||
except TypeError as ex: | ||
print(ex) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<ringbuffer> | ||
0 | ||
1 | ||
2 | ||
b'\x00\x00' | ||
0 | ||
b'\x00\x01' | ||
None | ||
16 | ||
b'\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01' | ||
can't convert NoneType to int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# check that micropython.ringbuffer works correctly with asyncio.Stream. | ||
|
||
import micropython | ||
|
||
try: | ||
import asyncio | ||
|
||
micropython.ringbuffer | ||
except (AttributeError, ImportError): | ||
print("SKIP") | ||
raise SystemExit | ||
|
||
rb = micropython.ringbuffer(16) | ||
rba = asyncio.StreamWriter(rb) | ||
|
||
data = b"ABC123" * 20 | ||
print("w", len(data)) | ||
|
||
|
||
async def data_writer(): | ||
global data | ||
rba.write(data) | ||
await rba.drain() | ||
|
||
|
||
async def main(): | ||
task = asyncio.create_task(data_writer()) | ||
await asyncio.sleep_ms(10) | ||
# buff = bytearray(len(data)) | ||
read = await rba.readexactly(len(data)) | ||
print(read) | ||
print("r", len(read)) | ||
print(read == data) | ||
|
||
|
||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
w 120 | ||
b'ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123ABC123' | ||
r 120 | ||
True |