mirror of
https://github.com/micropython/micropython.git
synced 2025-07-21 21:11:12 +02:00
Some checks are pending
JavaScript code lint and formatting with Biome / eslint (push) Waiting to run
Check code formatting / code-formatting (push) Waiting to run
Check code size / build (push) Waiting to run
Check spelling with codespell / codespell (push) Waiting to run
Check commit message formatting / build (push) Waiting to run
Build docs / build (push) Waiting to run
Check examples / embedding (push) Waiting to run
Package mpremote / build (push) Waiting to run
.mpy file format and tools / test (push) Waiting to run
Build ports metadata / build (push) Waiting to run
cc3200 port / build (push) Waiting to run
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Waiting to run
esp32 port / build_idf (esp32_build_s3_c3) (push) Waiting to run
esp8266 port / build (push) Waiting to run
mimxrt port / build (push) Waiting to run
nrf port / build (push) Waiting to run
powerpc port / build (push) Waiting to run
qemu port / build_and_test_arm (push) Waiting to run
qemu port / build_and_test_rv32 (push) Waiting to run
renesas-ra port / build_renesas_ra_board (push) Waiting to run
rp2 port / build (push) Waiting to run
samd port / build (push) Waiting to run
stm32 port / build_stm32 (stm32_misc_build) (push) Waiting to run
stm32 port / build_stm32 (stm32_nucleo_build) (push) Waiting to run
stm32 port / build_stm32 (stm32_pyb_build) (push) Waiting to run
unix port / minimal (push) Waiting to run
unix port / reproducible (push) Waiting to run
unix port / standard (push) Waiting to run
unix port / standard_v2 (push) Waiting to run
unix port / coverage (push) Waiting to run
unix port / coverage_32bit (push) Waiting to run
unix port / nanbox (push) Waiting to run
unix port / float (push) Waiting to run
unix port / stackless_clang (push) Waiting to run
unix port / float_clang (push) Waiting to run
unix port / settrace (push) Waiting to run
unix port / settrace_stackless (push) Waiting to run
unix port / macos (push) Waiting to run
unix port / qemu_mips (push) Waiting to run
unix port / qemu_arm (push) Waiting to run
unix port / qemu_riscv64 (push) Waiting to run
webassembly port / build (push) Waiting to run
windows port / build-vs (Debug, x64, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Debug, x64, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Debug, x86, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Debug, x86, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2019, dev, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2019, standard, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-2022, standard, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x64, windows-latest, standard, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2019, dev, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2019, standard, 2019, [16, 17)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2022, dev, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-2022, standard, 2022, [17, 18)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-latest, dev, 2017, [15, 16)) (push) Waiting to run
windows port / build-vs (Release, x86, windows-latest, standard, 2017, [15, 16)) (push) Waiting to run
windows port / build-mingw (i686, mingw32, dev) (push) Waiting to run
windows port / build-mingw (i686, mingw32, standard) (push) Waiting to run
windows port / build-mingw (x86_64, mingw64, dev) (push) Waiting to run
windows port / build-mingw (x86_64, mingw64, standard) (push) Waiting to run
windows port / cross-build-on-linux (push) Waiting to run
zephyr port / build (push) Waiting to run
Python code lint and formatting with ruff / ruff (push) Waiting to run
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>
131 lines
5.0 KiB
C
131 lines
5.0 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* Copyright (c) 2024 Andrew Leech
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include "ringbuf.h"
|
|
#include "py/mpconfig.h"
|
|
|
|
#if MICROPY_PY_MICROPYTHON_RINGIO
|
|
|
|
#include "py/runtime.h"
|
|
#include "py/stream.h"
|
|
|
|
typedef struct _micropython_ringio_obj_t {
|
|
mp_obj_base_t base;
|
|
ringbuf_t ringbuffer;
|
|
} micropython_ringio_obj_t;
|
|
|
|
static mp_obj_t micropython_ringio_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
|
mp_int_t buff_size = -1;
|
|
mp_buffer_info_t bufinfo = {NULL, 0, 0};
|
|
|
|
if (!mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
|
|
buff_size = mp_obj_get_int(args[0]);
|
|
}
|
|
micropython_ringio_obj_t *self = mp_obj_malloc(micropython_ringio_obj_t, type);
|
|
if (bufinfo.buf != NULL) {
|
|
// buffer passed in, use it directly for ringbuffer.
|
|
self->ringbuffer.buf = bufinfo.buf;
|
|
self->ringbuffer.size = bufinfo.len;
|
|
self->ringbuffer.iget = self->ringbuffer.iput = 0;
|
|
} else {
|
|
// Allocate new buffer, add one extra to buff_size as ringbuf consumes one byte for tracking.
|
|
ringbuf_alloc(&(self->ringbuffer), buff_size + 1);
|
|
}
|
|
return MP_OBJ_FROM_PTR(self);
|
|
}
|
|
|
|
static mp_uint_t micropython_ringio_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
|
|
micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
size = MIN(size, ringbuf_avail(&self->ringbuffer));
|
|
ringbuf_memcpy_get_internal(&(self->ringbuffer), buf_in, size);
|
|
*errcode = 0;
|
|
return size;
|
|
}
|
|
|
|
static mp_uint_t micropython_ringio_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
|
|
micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
size = MIN(size, ringbuf_free(&self->ringbuffer));
|
|
ringbuf_memcpy_put_internal(&(self->ringbuffer), buf_in, size);
|
|
*errcode = 0;
|
|
return size;
|
|
}
|
|
|
|
static mp_uint_t micropython_ringio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
|
|
micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
switch (request) {
|
|
case MP_STREAM_POLL: {
|
|
mp_uint_t ret = 0;
|
|
if ((arg & MP_STREAM_POLL_RD) && ringbuf_avail(&self->ringbuffer) > 0) {
|
|
ret |= MP_STREAM_POLL_RD;
|
|
}
|
|
if ((arg & MP_STREAM_POLL_WR) && ringbuf_free(&self->ringbuffer) > 0) {
|
|
ret |= MP_STREAM_POLL_WR;
|
|
}
|
|
return ret;
|
|
}
|
|
case MP_STREAM_CLOSE:
|
|
return 0;
|
|
}
|
|
*errcode = MP_EINVAL;
|
|
return MP_STREAM_ERROR;
|
|
}
|
|
|
|
static mp_obj_t micropython_ringio_any(mp_obj_t self_in) {
|
|
micropython_ringio_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->ringbuffer));
|
|
}
|
|
static MP_DEFINE_CONST_FUN_OBJ_1(micropython_ringio_any_obj, micropython_ringio_any);
|
|
|
|
static const mp_rom_map_elem_t micropython_ringio_locals_dict_table[] = {
|
|
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(µpython_ringio_any_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&mp_stream_close_obj) },
|
|
|
|
};
|
|
static MP_DEFINE_CONST_DICT(micropython_ringio_locals_dict, micropython_ringio_locals_dict_table);
|
|
|
|
static const mp_stream_p_t ringio_stream_p = {
|
|
.read = micropython_ringio_read,
|
|
.write = micropython_ringio_write,
|
|
.ioctl = micropython_ringio_ioctl,
|
|
.is_text = false,
|
|
};
|
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
mp_type_ringio,
|
|
MP_QSTR_RingIO,
|
|
MP_TYPE_FLAG_NONE,
|
|
make_new, micropython_ringio_make_new,
|
|
protocol, &ringio_stream_p,
|
|
locals_dict, µpython_ringio_locals_dict
|
|
);
|
|
|
|
#endif // MICROPY_PY_MICROPYTHON_RINGIO
|