|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/obj.h" |
| 28 | +#include "py/mphal.h" |
| 29 | +#include "py/runtime.h" |
| 30 | + |
| 31 | +#include "shared-bindings/multiterminal/__init__.h" |
| 32 | + |
| 33 | +//| :mod:`multiterminal` --- Manage additional terminal sources |
| 34 | +//| =========================================================== |
| 35 | +//| |
| 36 | +//| .. module:: multiterminal |
| 37 | +//| :synopsis: Manage additional terminal sources |
| 38 | +//| :platform: ESP8266 |
| 39 | +//| |
| 40 | +//| The `multiterminal` module allows you to configure an additional serial |
| 41 | +//| terminal source. Incoming characters are accepted from both the internal |
| 42 | +//| serial connection and the optional secondary connection. |
| 43 | +//| |
| 44 | + |
| 45 | +//| .. function:: get_secondary_terminal() |
| 46 | +//| |
| 47 | +//| Returns the current secondary terminal. |
| 48 | +//| |
| 49 | +STATIC mp_obj_t multiterminal_obj_get_secondary_terminal() { |
| 50 | + return common_hal_multiterminal_get_secondary_terminal(); |
| 51 | +} |
| 52 | +MP_DEFINE_CONST_FUN_OBJ_0(multiterminal_get_secondary_terminal_obj, multiterminal_obj_get_secondary_terminal); |
| 53 | + |
| 54 | +//| .. function:: set_secondary_terminal(stream) |
| 55 | +//| |
| 56 | +//| Read additional input from the given stream and write out back to it. |
| 57 | +//| This doesn't replace the core stream (usually UART or native USB) but is |
| 58 | +//| mixed in instead. |
| 59 | +//| |
| 60 | +//| :param stream stream: secondary stream |
| 61 | +//| |
| 62 | +STATIC mp_obj_t multiterminal_obj_set_secondary_terminal(mp_obj_t secondary_terminal) { |
| 63 | + mp_obj_t write_m[3]; |
| 64 | + mp_load_method_maybe(secondary_terminal, MP_QSTR_write, write_m); |
| 65 | + mp_obj_t readinto_m[3]; |
| 66 | + mp_load_method_maybe(secondary_terminal, MP_QSTR_readinto, readinto_m); |
| 67 | + if (write_m[0] == MP_OBJ_NULL || readinto_m[0] == MP_OBJ_NULL) { |
| 68 | + mp_raise_ValueError("Stream missing readinto() or write() method."); |
| 69 | + return mp_const_none; |
| 70 | + } |
| 71 | + common_hal_multiterminal_set_secondary_terminal(secondary_terminal); |
| 72 | + return mp_const_none; |
| 73 | +} |
| 74 | +MP_DEFINE_CONST_FUN_OBJ_1(multiterminal_set_secondary_terminal_obj, multiterminal_obj_set_secondary_terminal); |
| 75 | + |
| 76 | +//| .. function:: clear_secondary_terminal() |
| 77 | +//| |
| 78 | +//| Clears the secondary terminal. |
| 79 | +//| |
| 80 | +STATIC mp_obj_t multiterminal_obj_clear_secondary_terminal() { |
| 81 | + common_hal_multiterminal_clear_secondary_terminal(); |
| 82 | + return mp_const_none; |
| 83 | +} |
| 84 | +MP_DEFINE_CONST_FUN_OBJ_0(multiterminal_clear_secondary_terminal_obj, multiterminal_obj_clear_secondary_terminal); |
| 85 | + |
| 86 | +//| .. function:: schedule_secondary_terminal_read(socket) |
| 87 | +//| |
| 88 | +//| In cases where the underlying OS is doing task scheduling, this notifies |
| 89 | +//| the OS when more data is available on the socket to read. This is useful |
| 90 | +//| as a callback for lwip sockets. |
| 91 | +//| |
| 92 | +// TODO(tannewt): This is a funny API. Replace it with a direct call into the OS |
| 93 | +// by the lwip object. |
| 94 | +STATIC mp_obj_t multiterminal_obj_schedule_secondary_terminal_read(mp_obj_t socket) { |
| 95 | + common_hal_multiterminal_schedule_secondary_terminal_read(socket); |
| 96 | + return mp_const_none; |
| 97 | +} |
| 98 | +MP_DEFINE_CONST_FUN_OBJ_1(multiterminal_schedule_secondary_terminal_read_obj, multiterminal_obj_schedule_secondary_terminal_read); |
| 99 | + |
| 100 | +// TODO(tannewt): Expose the internal serial connection as `primary_terminal` |
| 101 | + |
| 102 | +STATIC const mp_rom_map_elem_t multiterminal_module_globals_table[] = { |
| 103 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_multiterminal) }, |
| 104 | + { MP_ROM_QSTR(MP_QSTR_get_secondary_terminal), MP_ROM_PTR(&multiterminal_get_secondary_terminal_obj) }, |
| 105 | + { MP_ROM_QSTR(MP_QSTR_set_secondary_terminal), MP_ROM_PTR(&multiterminal_set_secondary_terminal_obj) }, |
| 106 | + { MP_ROM_QSTR(MP_QSTR_clear_secondary_terminal), MP_ROM_PTR(&multiterminal_clear_secondary_terminal_obj) }, |
| 107 | + { MP_ROM_QSTR(MP_QSTR_schedule_secondary_terminal_read), MP_ROM_PTR(&multiterminal_schedule_secondary_terminal_read_obj) }, |
| 108 | +}; |
| 109 | + |
| 110 | +STATIC MP_DEFINE_CONST_DICT(multiterminal_module_globals, multiterminal_module_globals_table); |
| 111 | + |
| 112 | +const mp_obj_module_t multiterminal_module = { |
| 113 | + .base = { &mp_type_module }, |
| 114 | + .globals = (mp_obj_dict_t*)&multiterminal_module_globals, |
| 115 | +}; |
0 commit comments