diff options
| author | Scott Shawcroft <scott@chickadee.tech> | 2017-08-07 14:30:11 -0700 |
|---|---|---|
| committer | Scott Shawcroft <scott@chickadee.tech> | 2017-08-07 14:30:11 -0700 |
| commit | 6ace74466709e0a25c60375638afb5cc3471828c (patch) | |
| tree | 47793c1de972e9be2139c1d44b48d04b0d5dc1cf | |
| parent | 5d509ecace763d2e0fad357f4af7c5979cc228fa (diff) | |
esp8266: Introduce `multiterminal` module for managing a secondary
serial connection such as WebREPL.
Fixes #181.
| -rw-r--r-- | esp8266/Makefile | 2 | ||||
| -rw-r--r-- | esp8266/common-hal/multiterminal/__init__.c | 48 | ||||
| -rw-r--r-- | esp8266/modules/webrepl.py | 10 | ||||
| -rw-r--r-- | esp8266/mpconfigport.h | 2 | ||||
| -rw-r--r-- | py/ringbuf.h | 2 | ||||
| -rw-r--r-- | shared-bindings/index.rst | 14 | ||||
| -rw-r--r-- | shared-bindings/multiterminal/__init__.c | 115 | ||||
| -rw-r--r-- | shared-bindings/multiterminal/__init__.h | 35 | ||||
| -rw-r--r-- | shared-module/multiterminal/__init__.c | 50 | ||||
| -rw-r--r-- | shared-module/multiterminal/__init__.h | 34 |
10 files changed, 300 insertions, 12 deletions
diff --git a/esp8266/Makefile b/esp8266/Makefile index 071e202d4..4a3006a11 100644 --- a/esp8266/Makefile +++ b/esp8266/Makefile @@ -116,6 +116,7 @@ SRC_COMMON_HAL = \ busio/__init__.c \ busio/SPI.c \ busio/UART.c \ + multiterminal/__init__.c \ neopixel_write/__init__.c \ os/__init__.c \ storage/__init__.c \ @@ -139,6 +140,7 @@ SRC_SHARED_MODULE = \ bitbangio/SPI.c \ busio/I2C.c \ busio/OneWire.c \ + multiterminal/__init__.c \ os/__init__.c \ random/__init__.c \ storage/__init__.c \ diff --git a/esp8266/common-hal/multiterminal/__init__.c b/esp8266/common-hal/multiterminal/__init__.c new file mode 100644 index 000000000..492174f0b --- /dev/null +++ b/esp8266/common-hal/multiterminal/__init__.c @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * 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. + */ + +#include "esp_mphal.h" + +#include "shared-bindings/multiterminal/__init__.h" +#include "shared-module/multiterminal/__init__.h" + +void common_hal_multiterminal_schedule_secondary_terminal_read(mp_obj_t socket) { + (void) socket; + mp_hal_signal_dupterm_input(); +} + +mp_obj_t common_hal_multiterminal_get_secondary_terminal() { + return shared_module_multiterminal_get_secondary_terminal(); +} + +void common_hal_multiterminal_set_secondary_terminal(mp_obj_t secondary_terminal) { + shared_module_multiterminal_set_secondary_terminal(secondary_terminal); +} + +void common_hal_multiterminal_clear_secondary_terminal() { + shared_module_multiterminal_clear_secondary_terminal(); +} diff --git a/esp8266/modules/webrepl.py b/esp8266/modules/webrepl.py index 5a76e9b26..a6c30e2ca 100644 --- a/esp8266/modules/webrepl.py +++ b/esp8266/modules/webrepl.py @@ -1,6 +1,6 @@ # This module should be imported from REPL, not run from command line. import socket -import uos +import multiterminal import network import websocket import websocket_helper @@ -31,7 +31,7 @@ def setup_conn(port, accept_handler): def accept_conn(listen_sock): global client_s cl, remote_addr = listen_sock.accept() - if uos.dupterm(): + if multiterminal.get_secondary_terminal(): print("\nConcurrent WebREPL connection from", remote_addr, "rejected") cl.close() return @@ -42,13 +42,13 @@ def accept_conn(listen_sock): ws = _webrepl._webrepl(ws) cl.setblocking(False) # notify REPL on socket incoming data - cl.setsockopt(socket.SOL_SOCKET, 20, uos.dupterm_notify) - uos.dupterm(ws) + cl.setsockopt(socket.SOL_SOCKET, 20, multiterminal.schedule_secondary_terminal_read) + multiterminal.set_secondary_terminal(ws) def stop(): global listen_s, client_s - uos.dupterm(None) + multiterminal.clear_secondary_terminal() if client_s: client_s.close() if listen_s: diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h index faff05bba..804b3e51c 100644 --- a/esp8266/mpconfigport.h +++ b/esp8266/mpconfigport.h @@ -169,6 +169,7 @@ extern const struct _mp_obj_module_t pulseio_module; extern const struct _mp_obj_module_t busio_module; extern const struct _mp_obj_module_t bitbangio_module; extern const struct _mp_obj_module_t time_module; +extern const struct _mp_obj_module_t multiterminal_module; #define MICROPY_PORT_BUILTIN_MODULES \ { MP_OBJ_NEW_QSTR(MP_QSTR_esp), (mp_obj_t)&esp_module }, \ @@ -188,6 +189,7 @@ extern const struct _mp_obj_module_t time_module; { MP_OBJ_NEW_QSTR(MP_QSTR_storage), (mp_obj_t)&storage_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_random), (mp_obj_t)&random_module }, \ { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_module }, \ + { MP_OBJ_NEW_QSTR(MP_QSTR_multiterminal), (mp_obj_t)&multiterminal_module }, \ #define MICROPY_PORT_BUILTIN_MODULE_WEAK_LINKS \ { MP_OBJ_NEW_QSTR(MP_QSTR_json), (mp_obj_t)&mp_module_ujson }, \ diff --git a/py/ringbuf.h b/py/ringbuf.h index 5e108afad..fa817e2b6 100644 --- a/py/ringbuf.h +++ b/py/ringbuf.h @@ -26,6 +26,8 @@ #ifndef __MICROPY_INCLUDED_PY_RINGBUF_H__ #define __MICROPY_INCLUDED_PY_RINGBUF_H__ +#include <stdint.h> + typedef struct _ringbuf_t { uint8_t *buf; uint16_t size; diff --git a/shared-bindings/index.rst b/shared-bindings/index.rst index 4c0741554..ab92fd57c 100644 --- a/shared-bindings/index.rst +++ b/shared-bindings/index.rst @@ -12,13 +12,13 @@ limited. For example, a microcontroller without analog features will not have Support Matrix --------------- -=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ======== ========= ======== ========= ======= ========= -Port `analogio` `audioio` `bitbangio` `board` `busio` `digitalio` `microcontroller` `neopixel_write` `os` `pulseio` `random` `storage` `time` `touchio` `uheap` `usb_hid` -=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ======== ========= ======== ========= ======= ========= -SAMD21 **Yes** No No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** **Yes** **Yes** Debug **Yes** -SAMD21 Express **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** Debug **Yes** -ESP8266 **Yes** No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** **Yes** No Debug No -=============== ========== ========= =========== ======= ======= =========== ================= ================ ======= ========= ======== ========= ======== ========= ======= ========= +=============== ========== ========= =========== ======= ======= =========== ================= =============== ================ ======= ========= ======== ========= ======== ========= ======= ========= +Port `analogio` `audioio` `bitbangio` `board` `busio` `digitalio` `microcontroller` `multiterminal` `neopixel_write` `os` `pulseio` `random` `storage` `time` `touchio` `uheap` `usb_hid` +=============== ========== ========= =========== ======= ======= =========== ================= =============== ================ ======= ========= ======== ========= ======== ========= ======= ========= +SAMD21 **Yes** No No **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** No **Yes** **Yes** **Yes** **Yes** Debug **Yes** +SAMD21 Express **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** Debug **Yes** +ESP8266 **Yes** No **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** **Yes** No **Yes** **Yes** **Yes** No Debug No +=============== ========== ========= =========== ======= ======= =========== ================= =============== ================ ======= ========= ======== ========= ======== ========= ======= ========= Modules --------- diff --git a/shared-bindings/multiterminal/__init__.c b/shared-bindings/multiterminal/__init__.c new file mode 100644 index 000000000..32dcdd75d --- /dev/null +++ b/shared-bindings/multiterminal/__init__.c @@ -0,0 +1,115 @@ +/* + * 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. + */ + +#include "py/obj.h" +#include "py/mphal.h" +#include "py/runtime.h" + +#include "shared-bindings/multiterminal/__init__.h" + +//| :mod:`multiterminal` --- Manage additional terminal sources +//| =========================================================== +//| +//| .. module:: multiterminal +//| :synopsis: Manage additional terminal sources +//| :platform: ESP8266 +//| +//| The `multiterminal` module allows you to configure an additional serial +//| terminal source. Incoming characters are accepted from both the internal +//| serial connection and the optional secondary connection. +//| + +//| .. function:: get_secondary_terminal() +//| +//| Returns the current secondary terminal. +//| +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); + +//| .. function:: set_secondary_terminal(stream) +//| +//| Read additional input from the given stream and write out back to it. +//| This doesn't replace the core stream (usually UART or native USB) but is +//| mixed in instead. +//| +//| :param stream stream: secondary stream +//| +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) { + mp_raise_ValueError("Stream missing readinto() or write() method."); + 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); + +//| .. function:: clear_secondary_terminal() +//| +//| Clears the secondary terminal. +//| +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); + +//| .. function:: schedule_secondary_terminal_read(socket) +//| +//| In cases where the underlying OS is doing task scheduling, this notifies +//| the OS when more data is available on the socket to read. This is useful +//| as a callback for lwip sockets. +//| +// 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 }, + .globals = (mp_obj_dict_t*)&multiterminal_module_globals, +}; diff --git a/shared-bindings/multiterminal/__init__.h b/shared-bindings/multiterminal/__init__.h new file mode 100644 index 000000000..91099a5df --- /dev/null +++ b/shared-bindings/multiterminal/__init__.h @@ -0,0 +1,35 @@ +/* + * 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. + */ + +#ifndef SHARED_BINDINGS_MULTITERMINAL___INIT___H +#define SHARED_BINDINGS_MULTITERMINAL___INIT___H + +void common_hal_multiterminal_schedule_secondary_terminal_read(mp_obj_t socket); +mp_obj_t common_hal_multiterminal_get_secondary_terminal(); +void common_hal_multiterminal_set_secondary_terminal(mp_obj_t secondary_terminal); +void common_hal_multiterminal_clear_secondary_terminal(); + +#endif // SHARED_BINDINGS_MULTITERMINAL___INIT___H diff --git a/shared-module/multiterminal/__init__.c b/shared-module/multiterminal/__init__.c new file mode 100644 index 000000000..5f6e298f0 --- /dev/null +++ b/shared-module/multiterminal/__init__.c @@ -0,0 +1,50 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2016 Paul Sokolovsky + * 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. + */ + +#include "py/mpstate.h" + +#include "shared-bindings/multiterminal/__init__.h" + +mp_obj_t shared_module_multiterminal_get_secondary_terminal() { + if (MP_STATE_PORT(term_obj) == MP_OBJ_NULL) { + return mp_const_none; + } else { + return MP_STATE_PORT(term_obj); + } +} + +void shared_module_multiterminal_set_secondary_terminal(mp_obj_t secondary_terminal) { + MP_STATE_PORT(term_obj) = secondary_terminal; + if (MP_STATE_PORT(dupterm_arr_obj) == MP_OBJ_NULL) { + MP_STATE_PORT(dupterm_arr_obj) = mp_obj_new_bytearray(1, ""); + } +} + +void shared_module_multiterminal_clear_secondary_terminal() { + MP_STATE_PORT(term_obj) = MP_OBJ_NULL; + MP_STATE_PORT(dupterm_arr_obj) = MP_OBJ_NULL; +} diff --git a/shared-module/multiterminal/__init__.h b/shared-module/multiterminal/__init__.h new file mode 100644 index 000000000..30044da06 --- /dev/null +++ b/shared-module/multiterminal/__init__.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef SHARED_MODULE_MULTITERMINAL___INIT___H +#define SHARED_MODULE_MULTITERMINAL___INIT___H + +mp_obj_t shared_module_multiterminal_get_secondary_terminal(); +void shared_module_multiterminal_set_secondary_terminal(mp_obj_t secondary_terminal); +void shared_module_multiterminal_clear_secondary_terminal(); + +#endif // SHARED_MODULE_MULTITERMINAL___INIT___H |
