2017-08-07 17:30:11 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "shared-bindings/multiterminal/__init__.h"
|
2017-08-07 17:30:11 -04:00
|
|
|
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/runtime.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2017-08-07 17:30:11 -04:00
|
|
|
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Manage additional terminal sources
|
2017-08-07 17:30:11 -04:00
|
|
|
//|
|
|
|
|
//| The `multiterminal` module allows you to configure an additional serial
|
|
|
|
//| terminal source. Incoming characters are accepted from both the internal
|
2020-05-07 15:59:52 -04:00
|
|
|
//| serial connection and the optional secondary connection."""
|
2017-08-07 17:30:11 -04:00
|
|
|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| def get_secondary_terminal() -> Optional[typing.BinaryIO]:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Returns the current secondary terminal."""
|
|
|
|
//| ...
|
2017-08-07 17:30:11 -04:00
|
|
|
STATIC mp_obj_t multiterminal_obj_get_secondary_terminal() {
|
|
|
|
return common_hal_multiterminal_get_secondary_terminal();
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(multiterminal_get_secondary_terminal_obj, multiterminal_obj_get_secondary_terminal);
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def set_secondary_terminal(stream: typing.BinaryIO) -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Read additional input from the given stream and write out back to it.
|
2017-08-07 17:30:11 -04:00
|
|
|
//| This doesn't replace the core stream (usually UART or native USB) but is
|
|
|
|
//| mixed in instead.
|
|
|
|
//|
|
2020-05-07 15:59:52 -04:00
|
|
|
//| :param stream stream: secondary stream"""
|
|
|
|
//| ...
|
2017-08-07 17:30:11 -04:00
|
|
|
STATIC mp_obj_t multiterminal_obj_set_secondary_terminal(mp_obj_t secondary_terminal) {
|
|
|
|
mp_obj_t write_m[3];
|
|
|
|
mp_load_method_maybe(secondary_terminal, MP_QSTR_write, write_m);
|
|
|
|
mp_obj_t readinto_m[3];
|
|
|
|
mp_load_method_maybe(secondary_terminal, MP_QSTR_readinto, readinto_m);
|
|
|
|
if (write_m[0] == MP_OBJ_NULL || readinto_m[0] == MP_OBJ_NULL) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Stream missing readinto() or write() method."));
|
2017-08-07 17:30:11 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
common_hal_multiterminal_set_secondary_terminal(secondary_terminal);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(multiterminal_set_secondary_terminal_obj, multiterminal_obj_set_secondary_terminal);
|
|
|
|
|
2020-07-03 15:08:16 -04:00
|
|
|
//| def clear_secondary_terminal() -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """Clears the secondary terminal."""
|
|
|
|
//| ...
|
2017-08-07 17:30:11 -04:00
|
|
|
STATIC mp_obj_t multiterminal_obj_clear_secondary_terminal() {
|
|
|
|
common_hal_multiterminal_clear_secondary_terminal();
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(multiterminal_clear_secondary_terminal_obj, multiterminal_obj_clear_secondary_terminal);
|
|
|
|
|
2020-08-03 00:35:43 -04:00
|
|
|
//| def schedule_secondary_terminal_read(socket: socket.socket) -> None:
|
2020-05-07 15:59:52 -04:00
|
|
|
//| """In cases where the underlying OS is doing task scheduling, this notifies
|
2017-08-07 17:30:11 -04:00
|
|
|
//| the OS when more data is available on the socket to read. This is useful
|
2020-05-07 15:59:52 -04:00
|
|
|
//| as a callback for lwip sockets."""
|
|
|
|
//| ...
|
2017-08-07 17:30:11 -04:00
|
|
|
// TODO(tannewt): This is a funny API. Replace it with a direct call into the OS
|
|
|
|
// by the lwip object.
|
|
|
|
STATIC mp_obj_t multiterminal_obj_schedule_secondary_terminal_read(mp_obj_t socket) {
|
|
|
|
common_hal_multiterminal_schedule_secondary_terminal_read(socket);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(multiterminal_schedule_secondary_terminal_read_obj, multiterminal_obj_schedule_secondary_terminal_read);
|
|
|
|
|
|
|
|
// TODO(tannewt): Expose the internal serial connection as `primary_terminal`
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t multiterminal_module_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_multiterminal) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get_secondary_terminal), MP_ROM_PTR(&multiterminal_get_secondary_terminal_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_set_secondary_terminal), MP_ROM_PTR(&multiterminal_set_secondary_terminal_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_clear_secondary_terminal), MP_ROM_PTR(&multiterminal_clear_secondary_terminal_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_schedule_secondary_terminal_read), MP_ROM_PTR(&multiterminal_schedule_secondary_terminal_read_obj) },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(multiterminal_module_globals, multiterminal_module_globals_table);
|
|
|
|
|
|
|
|
const mp_obj_module_t multiterminal_module = {
|
|
|
|
.base = { &mp_type_module },
|
2021-03-15 09:57:36 -04:00
|
|
|
.globals = (mp_obj_dict_t *)&multiterminal_module_globals,
|
2017-08-07 17:30:11 -04:00
|
|
|
};
|