From b61214db4132593791ed11f628c83cad148a35c0 Mon Sep 17 00:00:00 2001 From: ATMakersBill Date: Sun, 16 Sep 2018 15:04:51 -0400 Subject: [PATCH 1/5] added timeout to input() --- py/modbuiltins.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index fc7ec24c74..8dbcec7e3f 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -34,6 +34,7 @@ #include "py/runtime.h" #include "py/builtin.h" #include "py/stream.h" +#include "py/obj.h" /* For get_int */ #include "supervisor/shared/translate.h" @@ -233,10 +234,41 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); #define mp_hal_readline readline #endif +#include "usb.h" + STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { - if (n_args == 1) { + if (n_args >= 1) { mp_obj_print(args[0], PRINT_STR); } + if (n_args == 2) + { + if (!mp_obj_is_true(args[1])) + { /* If they pass 0 or False, return immediately if there's no text available */ + if (!usb_bytes_available()) + { + return mp_const_none; + } + } + else if (MP_OBJ_IS_INT(args[1])) + { + /* Timeout has been sent... check for USB input for that # of millis */ + mp_uint_t target = mp_hal_ticks_ms() + mp_obj_get_int(args[1]); + bool bytesAvaliable = false; + + while (mp_hal_ticks_ms() < target) + { + if (usb_bytes_available()) + { + bytesAvaliable = true; + break; + } + } + if (!bytesAvaliable) + return mp_const_none; + } + } + + vstr_t line; vstr_init(&line, 16); int ret = mp_hal_readline(&line, ""); @@ -248,7 +280,7 @@ STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { } return mp_obj_new_str_from_vstr(&mp_type_str, &line); } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 2, mp_builtin_input); #endif From bc0a13552478036b75e351c21b84ae3fa3f45d30 Mon Sep 17 00:00:00 2001 From: ATMakersBill Date: Tue, 9 Oct 2018 18:25:51 -0400 Subject: [PATCH 2/5] replacing change to input() with separate method to check for USB Serial input --- py/modbuiltins.c | 36 ++---------------------------------- 1 file changed, 2 insertions(+), 34 deletions(-) diff --git a/py/modbuiltins.c b/py/modbuiltins.c index 8dbcec7e3f..fc7ec24c74 100644 --- a/py/modbuiltins.c +++ b/py/modbuiltins.c @@ -34,7 +34,6 @@ #include "py/runtime.h" #include "py/builtin.h" #include "py/stream.h" -#include "py/obj.h" /* For get_int */ #include "supervisor/shared/translate.h" @@ -234,41 +233,10 @@ MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex); #define mp_hal_readline readline #endif -#include "usb.h" - STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { - if (n_args >= 1) { + if (n_args == 1) { mp_obj_print(args[0], PRINT_STR); } - if (n_args == 2) - { - if (!mp_obj_is_true(args[1])) - { /* If they pass 0 or False, return immediately if there's no text available */ - if (!usb_bytes_available()) - { - return mp_const_none; - } - } - else if (MP_OBJ_IS_INT(args[1])) - { - /* Timeout has been sent... check for USB input for that # of millis */ - mp_uint_t target = mp_hal_ticks_ms() + mp_obj_get_int(args[1]); - bool bytesAvaliable = false; - - while (mp_hal_ticks_ms() < target) - { - if (usb_bytes_available()) - { - bytesAvaliable = true; - break; - } - } - if (!bytesAvaliable) - return mp_const_none; - } - } - - vstr_t line; vstr_init(&line, 16); int ret = mp_hal_readline(&line, ""); @@ -280,7 +248,7 @@ STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) { } return mp_obj_new_str_from_vstr(&mp_type_str, &line); } -MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 2, mp_builtin_input); +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); #endif From 9f94712ad10193c4fc6d888ef3ebc611bc0e011c Mon Sep 17 00:00:00 2001 From: ATMakersBill Date: Tue, 9 Oct 2018 18:37:52 -0400 Subject: [PATCH 3/5] replacing change to input() with separate method to check for USB Serial input --- .../common-hal/supervisor/Runtime.c | 4 ++++ ports/nrf/common-hal/supervisor/Runtime.c | 4 ++++ shared-bindings/supervisor/Runtime.c | 20 +++++++++++++++++++ shared-bindings/supervisor/Runtime.h | 2 ++ 4 files changed, 30 insertions(+) diff --git a/ports/atmel-samd/common-hal/supervisor/Runtime.c b/ports/atmel-samd/common-hal/supervisor/Runtime.c index 8efe7cb78d..2636fe6465 100755 --- a/ports/atmel-samd/common-hal/supervisor/Runtime.c +++ b/ports/atmel-samd/common-hal/supervisor/Runtime.c @@ -32,3 +32,7 @@ bool common_hal_get_serial_connected(void) { return (bool) usb_connected(); } +bool common_hal_get_serial_bytes_available(void) { + return (bool) usb_bytes_available(); +} + diff --git a/ports/nrf/common-hal/supervisor/Runtime.c b/ports/nrf/common-hal/supervisor/Runtime.c index b73a94a1ba..feab6987d8 100755 --- a/ports/nrf/common-hal/supervisor/Runtime.c +++ b/ports/nrf/common-hal/supervisor/Runtime.c @@ -32,3 +32,7 @@ bool common_hal_get_serial_connected(void) { return (bool) serial_connected(); } +bool common_hal_get_serial_bytes_available(void) { + return (bool) serial_bytes_available(); +} + diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index b061595cf8..1cf2a3548f 100755 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -80,8 +80,28 @@ const mp_obj_property_t supervisor_serial_connected_obj = { (mp_obj_t)&mp_const_none_obj}, }; +/*Added to allow for polling of USB Console*/ +STATIC mp_obj_t supervisor_get_serial_bytes_available(mp_obj_t self){ + if (!common_hal_get_serial_bytes_available()) { + return mp_const_false; + } + else { + return mp_const_true; + } +} +MP_DEFINE_CONST_FUN_OBJ_1(supervisor_get_serial_bytes_available_obj, supervisor_get_serial_bytes_available); + +const mp_obj_property_t supervisor_serial_bytes_available_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&supervisor_get_serial_bytes_available_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + + STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_serial_connected_obj) }, + { MP_ROM_QSTR(MP_QSTR_serial_bytes_available), MP_ROM_PTR(&supervisor_serial_bytes_available_obj) }, }; STATIC MP_DEFINE_CONST_DICT(supervisor_runtime_locals_dict, supervisor_runtime_locals_dict_table); diff --git a/shared-bindings/supervisor/Runtime.h b/shared-bindings/supervisor/Runtime.h index 4a67925ece..864b070cdb 100755 --- a/shared-bindings/supervisor/Runtime.h +++ b/shared-bindings/supervisor/Runtime.h @@ -35,6 +35,8 @@ const mp_obj_type_t supervisor_runtime_type; bool common_hal_get_serial_connected(void); +bool common_hal_get_serial_bytes_available(void); + //TODO: placeholders for future functions //bool common_hal_get_repl_active(void); //bool common_hal_get_usb_enumerated(void); From 05d4b8cf50685db683dd48d7d3037da868385a0a Mon Sep 17 00:00:00 2001 From: ATMakersBill Date: Tue, 9 Oct 2018 22:50:59 -0400 Subject: [PATCH 4/5] Added Documentation for the serial_bytes_available attribute --- shared-bindings/supervisor/Runtime.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index 1cf2a3548f..27b62abd41 100755 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -53,6 +53,12 @@ //| //| Returns the USB serial communication status (read-only). //| +//| .. attribute:: runtime.serial_bytes_available +//| +//| Returns the whether any bytes are available to read +//| on the USB serial input. Allows for polling to see whether +//| to call the built-in input() or wait. (read-only) +//| //| .. note:: //| //| SAMD: Will return ``True`` if the USB serial connection From 0688709c4fb965b318ad40d2cab19911bd1497e8 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 9 Oct 2018 23:15:25 -0700 Subject: [PATCH 5/5] Move the docs next to the implementation --- shared-bindings/supervisor/Runtime.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c index 27b62abd41..5ba2198e1f 100755 --- a/shared-bindings/supervisor/Runtime.c +++ b/shared-bindings/supervisor/Runtime.c @@ -53,12 +53,6 @@ //| //| Returns the USB serial communication status (read-only). //| -//| .. attribute:: runtime.serial_bytes_available -//| -//| Returns the whether any bytes are available to read -//| on the USB serial input. Allows for polling to see whether -//| to call the built-in input() or wait. (read-only) -//| //| .. note:: //| //| SAMD: Will return ``True`` if the USB serial connection @@ -86,7 +80,13 @@ const mp_obj_property_t supervisor_serial_connected_obj = { (mp_obj_t)&mp_const_none_obj}, }; -/*Added to allow for polling of USB Console*/ + +//| .. attribute:: runtime.serial_bytes_available +//| +//| Returns the whether any bytes are available to read +//| on the USB serial input. Allows for polling to see whether +//| to call the built-in input() or wait. (read-only) +//| STATIC mp_obj_t supervisor_get_serial_bytes_available(mp_obj_t self){ if (!common_hal_get_serial_bytes_available()) { return mp_const_false;