|
36 | 36 |
|
37 | 37 | //| """USB CDC Serial streams |
38 | 38 | //| |
39 | | -//| The `usb_cdc` module allows access to USB CDC (serial) communications.""" |
| 39 | +//| The `usb_cdc` module allows access to USB CDC (serial) communications. |
40 | 40 | //| |
41 | | -//| serials: Tuple[Serial, ...] |
42 | | -//| """Tuple of all CDC streams. Each item is a `Serial`. |
43 | | -//| ``serials[0]`` is the USB REPL connection. |
44 | | -//| ``serials[1]`` is a second USB serial connection, unconnected to the REPL. |
| 41 | +//| On Windows, each `Serial` is visible as a separate COM port. The ports will often |
| 42 | +//| be assigned consecutively, REPL first, but this is not always true. |
| 43 | +//| |
| 44 | +//| On Linux, the ports are typically ``/dev/ttyACM0`` and ``/dev/ttyACM1``. The REPL |
| 45 | +//| is usually first. |
| 46 | +//| |
| 47 | +//| On MacOS, the ports are typically ``/dev/cu.usbmodem<something>``. The something |
| 48 | +//| varies based on the USB bus and port used. The REPL is usually first. |
45 | 49 | //| """ |
46 | 50 | //| |
| 51 | +//| repl: Optional[Serial] |
| 52 | +//| """The `Serial` object that can be used to communicate over the REPL serial |
| 53 | +//| channel. ``None`` if disabled. |
| 54 | +//| |
| 55 | +//| Note that`sys.stdin` and `sys.stdout` are also connected to the REPL, though |
| 56 | +//| they are text-based streams, and the `repl` object is a binary stream.""" |
| 57 | +//| |
| 58 | +//| data: Optional[Serial] |
| 59 | +//| """A `Serial` object that can be used to send and receive binary data to and from |
| 60 | +//| the host. |
| 61 | +//| Note that `data` is *disabled* by default.""" |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +//| def enable_repl(enabled:bool) -> None: |
| 67 | +//| """Enable or disable the `repl` USB serial connection. The REPL |
| 68 | +//| is enabled by default. |
| 69 | +//| Can be changed in ``boot.py``, before USB is connected.""" |
| 70 | +//| ... |
| 71 | +//| |
| 72 | +STATIC mp_obj_t usb_cdc_enable_repl(mp_obj_t enabled) { |
| 73 | + if (!common_hal_usb_cdc_enable_repl(mp_obj_is_true(enabled))) { |
| 74 | + mp_raise_RuntimeError(translate("Cannot change USB devices now")); |
| 75 | + } |
| 76 | + return mp_const_none; |
| 77 | +} |
| 78 | +MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_repl_obj, usb_cdc_enable_repl); |
| 79 | + |
| 80 | +//| def enable_data(enabled: bool) -> None: |
| 81 | +//| """Enable or disable the `data` USB serial connection;n |
| 82 | +//| *disabled* by default. |
| 83 | +//| Can be changed in ``boot.py``, before USB is connected.""" |
| 84 | +//| ... |
| 85 | +//| |
| 86 | +STATIC mp_obj_t usb_cdc_enable_data(mp_obj_t enabled) { |
| 87 | + if (!common_hal_usb_cdc_enable_data(mp_obj_is_true(enabled))) { |
| 88 | + mp_raise_RuntimeError(translate("Cannot change USB devices now")); |
| 89 | + } |
| 90 | + return mp_const_none; |
| 91 | +} |
| 92 | +MP_DEFINE_CONST_FUN_OBJ_1(usb_cdc_enable_data_obj, usb_cdc_enable_data); |
47 | 93 |
|
48 | | -static const mp_map_elem_t usb_cdc_module_globals_table[] = { |
49 | | - { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) }, |
50 | | - { MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) }, |
51 | | - { MP_ROM_QSTR(MP_QSTR_serials), MP_OBJ_FROM_PTR(&usb_cdc_serials_tuple) }, |
| 94 | +// The usb_cdc module dict is mutable so that .repl and .data may |
| 95 | +// be set to a Serial or to None depending on whether they are enabled or not. |
| 96 | +static mp_map_elem_t usb_cdc_module_globals_table[] = { |
| 97 | + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_usb_cdc) }, |
| 98 | + { MP_ROM_QSTR(MP_QSTR_Serial), MP_OBJ_FROM_PTR(&usb_cdc_serial_type) }, |
| 99 | + { MP_ROM_QSTR(MP_QSTR_repl), mp_const_none }, |
| 100 | + { MP_ROM_QSTR(MP_QSTR_data), mp_const_none }, |
| 101 | + { MP_ROM_QSTR(MP_QSTR_enable_repl), MP_OBJ_FROM_PTR(&usb_cdc_enable_repl_obj) }, |
| 102 | + { MP_ROM_QSTR(MP_QSTR_enable_data), MP_OBJ_FROM_PTR(&usb_cdc_enable_data_obj) }, |
52 | 103 | }; |
53 | 104 |
|
54 | | -static MP_DEFINE_CONST_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table); |
| 105 | +static MP_DEFINE_MUTABLE_DICT(usb_cdc_module_globals, usb_cdc_module_globals_table); |
55 | 106 |
|
56 | 107 | const mp_obj_module_t usb_cdc_module = { |
57 | 108 | .base = { &mp_type_module }, |
|
0 commit comments