Merge pull request #1193 from ATMakersBill/master

Add supervisor.runtime.serial_bytes_available so that input() can be used without blocking.
This commit is contained in:
Scott Shawcroft 2018-10-09 23:58:43 -07:00 committed by GitHub
commit df80ad8e6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 0 deletions

View File

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

View File

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

View File

@ -80,8 +80,34 @@ const mp_obj_property_t supervisor_serial_connected_obj = {
(mp_obj_t)&mp_const_none_obj},
};
//| .. 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;
}
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);

View File

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