wip: revamp API names

This commit is contained in:
Dan Halbert 2021-04-14 22:10:09 -04:00
parent 4a7e129287
commit 6cb751ab06
10 changed files with 81 additions and 79 deletions

View File

@ -158,30 +158,33 @@ mp_obj_t storage_erase_filesystem(void) {
} }
MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem); MP_DEFINE_CONST_FUN_OBJ_0(storage_erase_filesystem_obj, storage_erase_filesystem);
//| def enable_usb(enabled: True) -> None: //| def configure_usb(enabled: True) -> None:
//| """Enable or disable presenting ``CIRCUITPY`` as a USB mass storage device. //| """Configure the USB mass storage device.
//| By default, ``CIRCUITPY`` is visible. //| Enable or disable presenting ``CIRCUITPY`` as a USB mass storage device.
//| Use ``storage.enable_usb(False)`` to hide CIRCUITPY from the host computer. //| By default, the device is enabled and ``CIRCUITPY`` is visible.
//| Can be changed in ``boot.py``, before USB is connected.""" //| Can be called in ``boot.py``, before USB is connected.
//|
//| :param enabled bool: Enable or disable the USB mass storage device.
//| True to enable; False to disable. Enabled by default."""
//| ... //| ...
//| //|
STATIC mp_obj_t storage_enable_usb(mp_obj_t enabled) { STATIC mp_obj_t storage_configure_usb(mp_obj_t enabled) {
if (!common_hal_storage_enable_usb(mp_obj_is_true(enabled))) { if (!common_hal_storage_configure_usb(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change usb devices now")); mp_raise_RuntimeError(translate("Cannot change usb devices now"));
} }
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(storage_enable_usb_obj, storage_enable_usb); MP_DEFINE_CONST_FUN_OBJ_1(storage_configure_usb_obj, storage_configure_usb);
STATIC const mp_rom_map_elem_t storage_module_globals_table[] = { STATIC const mp_rom_map_elem_t storage_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_storage) },
{ MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) }, { MP_ROM_QSTR(MP_QSTR_mount), MP_ROM_PTR(&storage_mount_obj) },
{ MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) }, { MP_ROM_QSTR(MP_QSTR_umount), MP_ROM_PTR(&storage_umount_obj) },
{ MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) }, { MP_ROM_QSTR(MP_QSTR_remount), MP_ROM_PTR(&storage_remount_obj) },
{ MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) }, { MP_ROM_QSTR(MP_QSTR_getmount), MP_ROM_PTR(&storage_getmount_obj) },
{ MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) }, { MP_ROM_QSTR(MP_QSTR_erase_filesystem), MP_ROM_PTR(&storage_erase_filesystem_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_usb), MP_ROM_PTR(&storage_enable_usb_obj) }, { MP_ROM_QSTR(MP_QSTR_configure_usb), MP_ROM_PTR(&storage_configure_usb_obj) },
//| class VfsFat: //| class VfsFat:
//| def __init__(self, block_device: str) -> None: //| def __init__(self, block_device: str) -> None:

View File

@ -36,6 +36,6 @@ void common_hal_storage_umount_object(mp_obj_t vfs_obj);
void common_hal_storage_remount(const char *path, bool readonly, bool disable_concurrent_write_protection); void common_hal_storage_remount(const char *path, bool readonly, bool disable_concurrent_write_protection);
mp_obj_t common_hal_storage_getmount(const char *path); mp_obj_t common_hal_storage_getmount(const char *path);
void common_hal_storage_erase_filesystem(void); void common_hal_storage_erase_filesystem(void);
bool common_hal_storage_enable_usb(bool enabled); bool common_hal_storage_configure_usb(bool enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_STORAGE___INIT___H

View File

@ -60,46 +60,33 @@
//| the host. //| the host.
//| Note that `data` is *disabled* by default.""" //| Note that `data` is *disabled* by default."""
//| def configure_usb(repl_enabled: bool, data_enabled: bool) -> None:
//| """Configure the USB CDC devices. Can be called in ``boot.py``, before USB is connected.
//|
//| def enable_repl(enabled:bool) -> None: //| :param repl_enabled bool: Enable or disable the `repl` USB serial device.
//| """Enable or disable the `repl` USB serial connection. The REPL //| True to enable; False to disable. Enabled by default.
//| is enabled by default. //| :param data_enabled bool: Enable or disable the `data` USB serial device.
//| Can be changed in ``boot.py``, before USB is connected.""" //| True to enable; False to disable. *Disabled* by default."""
//| ... //| ...
//| //|
STATIC mp_obj_t usb_cdc_enable_repl(mp_obj_t enabled) { STATIC mp_obj_t usb_cdc_configure_usb(mp_obj_t repl_enabled, mp_obj_t data_enabled) {
if (!common_hal_usb_cdc_enable_repl(mp_obj_is_true(enabled))) { if (!common_hal_usb_cdc_configure_usb(
mp_obj_is_true(repl_enabled),
mp_obj_is_true(data_enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now")); mp_raise_RuntimeError(translate("Cannot change USB devices now"));
} }
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_repl_obj, usb_cdc_enable_repl); MP_DEFINE_CONST_FUN_OBJ_2(usb_cdc_configure_usb_obj, usb_cdc_configure_usb);
//| def enable_data(enabled: bool) -> None:
//| """Enable or disable the `data` USB serial connection;n
//| *disabled* by default.
//| Can be changed in ``boot.py``, before USB is connected."""
//| ...
//|
STATIC mp_obj_t usb_cdc_enable_data(mp_obj_t enabled) {
if (!common_hal_usb_cdc_enable_data(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now"));
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_data_obj, usb_cdc_enable_data);
// The usb_cdc module dict is mutable so that .repl and .data may // The usb_cdc module dict is mutable so that .repl and .data may
// be set to a Serial or to None depending on whether they are enabled or not. // be set to a Serial or to None depending on whether they are enabled or not.
static mp_map_elem_t usb_cdc_module_globals_table[] = { static mp_map_elem_t usb_cdc_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) },
{ MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) }, { MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) },
{ MP_ROM_QSTR(MP_QSTR_repl), mp_const_none }, { MP_ROM_QSTR(MP_QSTR_repl), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_data), mp_const_none }, { MP_ROM_QSTR(MP_QSTR_data), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_enable_repl), MP_OBJ_FROM_PTR(&usb_cdc_enable_repl_obj) }, { MP_ROM_QSTR(MP_QSTR_configure_usb), MP_OBJ_FROM_PTR(&usb_cdc_configure_usb_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_data), MP_OBJ_FROM_PTR(&usb_cdc_enable_data_obj) },
}; };
static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table); static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table);
@ -108,3 +95,18 @@ const mp_obj_module_t usb_cdc_module = {
.base = { &mp_type_module }, .base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&usb_cdc_module_globals, .globals = (mp_obj_dict_t *)&usb_cdc_module_globals,
}; };
static void set_module_dict_entry(mp_obj_t key_qstr, mp_obj_t serial_obj) {
mp_map_elem_t *elem = mp_map_lookup(&usb_cdc_module_globals.map, key_qstr, MP_MAP_LOOKUP);
if (elem) {
elem->value = serial_obj;
}
}
void usb_cdc_set_repl(mp_obj_t serial_obj) {
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_repl), serial_obj);
}
void usb_cdc_set_data(mp_obj_t serial_obj) {
set_module_dict_entry(MP_ROM_QSTR(MP_QSTR_data), serial_obj);
}

View File

@ -29,7 +29,10 @@
#include "shared-module/usb_cdc/__init__.h" #include "shared-module/usb_cdc/__init__.h"
bool common_hal_usb_cdc_enable_repl(bool enabled); // Set the module dict entries.
bool common_hal_usb_cdc_enable_data(bool enabled); void usb_cdc_set_repl(mp_obj_t serial_obj);
void usb_cdc_set_data(mp_obj_t serial_obj);
extern bool common_hal_usb_cdc_configure_usb(bool repl_enabled, bool data_enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_CDC___INIT___H

View File

@ -43,25 +43,28 @@
//| """Tuple of all MIDI ports. Each item is ether `PortIn` or `PortOut`.""" //| """Tuple of all MIDI ports. Each item is ether `PortIn` or `PortOut`."""
//| //|
//| def enable(enabled: bool) -> None: //| def configure_usb(enabled: True) -> None:
//| """Enable or disable USB MIDI device. By default, MIDI is enabled. //| """Configure the USB MIDI device.
//| Can be changed in ``boot.py``, before USB is connected.""" //| Can be called in ``boot.py``, before USB is connected.
//|
//| :param enabled bool: Enable or disable the USB MIDI Device.
//| True to enable; False to disable. Enabled by default."""
//| ... //| ...
//| //|
STATIC mp_obj_t usb_midi_enable(mp_obj_t enabled) { STATIC mp_obj_t usb_midi_configure_usb(mp_obj_t enabled) {
if (!common_hal_usb_midi_enable(mp_obj_is_true(enabled))) { if (!common_hal_usb_midi_configure_usb(mp_obj_is_true(enabled))) {
mp_raise_RuntimeError(translate("Cannot change USB devices now")); mp_raise_RuntimeError(translate("Cannot change USB devices now"));
} }
return mp_const_none; return mp_const_none;
} }
MP_DEFINE_CONST_FUN_OBJ_1(usb_midi_enable_obj, usb_midi_enable); MP_DEFINE_CONST_FUN_OBJ_1(usb_midi_configure_usb_obj, usb_midi_configure_usb);
mp_map_elem_t usb_midi_module_globals_table[] = { mp_map_elem_t usb_midi_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_midi) }, { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_midi) },
{ MP_ROM_QSTR(MP_QSTR_enable), MP_OBJ_FROM_PTR(&usb_midi_enable_obj) }, { MP_ROM_QSTR(MP_QSTR_configure_usb), MP_OBJ_FROM_PTR(&usb_midi_configure_usb_obj) },
{ MP_ROM_QSTR(MP_QSTR_ports), mp_const_empty_tuple }, { MP_ROM_QSTR(MP_QSTR_ports), mp_const_empty_tuple },
{ MP_ROM_QSTR(MP_QSTR_PortIn), MP_OBJ_FROM_PTR(&usb_midi_portin_type) }, { MP_ROM_QSTR(MP_QSTR_PortIn), MP_OBJ_FROM_PTR(&usb_midi_portin_type) },
{ MP_ROM_QSTR(MP_QSTR_PortOut), MP_OBJ_FROM_PTR(&usb_midi_portout_type) }, { MP_ROM_QSTR(MP_QSTR_PortOut), MP_OBJ_FROM_PTR(&usb_midi_portout_type) },
}; };
// This isn't const so we can set ports dynamically. // This isn't const so we can set ports dynamically.

View File

@ -31,6 +31,6 @@
extern mp_obj_dict_t usb_midi_module_globals; extern mp_obj_dict_t usb_midi_module_globals;
bool common_hal_usb_midi_enable(bool enabled); bool common_hal_usb_midi_configure_usb(bool enabled);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H #endif // MICROPY_INCLUDED_SHARED_BINDINGS_USB_MIDI___INIT___H

View File

@ -175,9 +175,9 @@ void common_hal_storage_erase_filesystem(void) {
// We won't actually get here, since we're resetting. // We won't actually get here, since we're resetting.
} }
bool common_hal_storage_enable_usb(bool enabled) { bool common_hal_storage_configure_usb(bool enabled) {
// We can't change the descriptors once we're connected. // We can't change the descriptors once we're connected.
if (!tud_connected()) { if (tud_connected()) {
return false; return false;
} }
usb_storage_enabled = enabled; usb_storage_enabled = enabled;

View File

@ -60,24 +60,17 @@ void usb_cdc_init(void) {
usb_cdc_data_enabled = false; usb_cdc_data_enabled = false;
} }
bool common_hal_usb_cdc_enable_repl(bool enabled) { bool common_hal_usb_cdc_configure_usb(bool repl_enabled, bool data_enabled) {
// We can't change the descriptors once we're connected. // We can't change the descriptors once we're connected.
if (!tud_connected()) { if (tud_connected()) {
// TODO set entry in dict
return false; return false;
} }
usb_cdc_repl_enabled = enabled;
// TODO set entry in dict
return true;
}
bool common_hal_usb_cdc_enable_data(bool enabled) { usb_cdc_repl_enabled = repl_enabled;
// We can't change the descriptors once we're connected. usb_cdc_set_repl(repl_enabled ? MP_OBJ_FROM_PTR(&usb_cdc_repl_obj) : mp_const_none);
if (!tud_connected()) {
// TODO set entry in dict usb_cdc_data_enabled = data_enabled;
return false; usb_cdc_set_data(data_enabled ? MP_OBJ_FROM_PTR(&usb_cdc_data_obj) : mp_const_none);
}
usb_cdc_data_enabled = enabled;
// TODO set entry in dict
return true; return true;
} }

View File

@ -29,8 +29,6 @@
#include "py/objtuple.h" #include "py/objtuple.h"
extern const mp_rom_obj_tuple_t usb_cdc_serials_tuple;
void usb_cdc_init(void); void usb_cdc_init(void);
#endif /* SHARED_MODULE_USB_CDC___INIT___H */ #endif /* SHARED_MODULE_USB_CDC___INIT___H */

View File

@ -75,9 +75,9 @@ void usb_midi_usb_init(void) {
mp_map_lookup(&usb_midi_module_globals.map, MP_ROM_QSTR(MP_QSTR_ports), MP_MAP_LOOKUP)->value = MP_OBJ_FROM_PTR(ports); mp_map_lookup(&usb_midi_module_globals.map, MP_ROM_QSTR(MP_QSTR_ports), MP_MAP_LOOKUP)->value = MP_OBJ_FROM_PTR(ports);
} }
bool common_hal_usb_midi_enable(bool enabled) { bool common_hal_usb_midi_configure_usb(bool enabled) {
// We can't change the descriptors once we're connected. // We can't change the descriptors once we're connected.
if (!tud_connected()) { if (tud_connected()) {
return false; return false;
} }
usb_midi_enabled = enabled; usb_midi_enabled = enabled;