esp8266: Introduce multiterminal module for managing a secondary

serial connection such as WebREPL.

Fixes #181.
This commit is contained in:
Scott Shawcroft 2017-08-07 14:30:11 -07:00
parent 5d509ecace
commit 6ace744667
10 changed files with 300 additions and 12 deletions

View File

@ -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 \

View File

@ -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();
}

View File

@ -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:

View File

@ -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 }, \

View File

@ -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;

View File

@ -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
---------

View File

@ -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,
};

View File

@ -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

View File

@ -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;
}

View File

@ -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