Skip to content

Commit bb18fdf

Browse files
committed
bricks/common/micropython: use transport type to select default stdio
Pass the transport type that requested the user program to start to the MicroPython launcher to allow it to select the appropriate default stdio stream (USB or Bluetooth).
1 parent 51f4cb2 commit bb18fdf

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

bricks/_common/micropython.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <stdio.h>
88
#include <string.h>
99

10+
#include <pbdrv/bluetooth.h>
1011
#include <pbdrv/stack.h>
1112

1213
#include <pbio/button.h>
@@ -391,11 +392,36 @@ void pbsys_main_run_program(pbsys_main_program_t *program) {
391392
mp_init();
392393

393394
#if MICROPY_PY_SYS_MUTABLE_STDIO
394-
// TODO: add logic to allow USB instead of Bluetooth
395-
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDIN]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
396-
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDOUT]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
397-
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDERR]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
398-
#endif
395+
396+
bool use_bluetooth;
397+
398+
// If the program was started remotely, use the same transport for stdio.
399+
switch (program->start_request_type) {
400+
case PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_BLUETOOTH:
401+
use_bluetooth = true;
402+
break;
403+
case PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_USB:
404+
use_bluetooth = false;
405+
break;
406+
default:
407+
// Use Bluetooth if available, otherwise USB.
408+
// REVISIT: might want to keep track of last used transport and use
409+
// that instead.
410+
use_bluetooth = pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS);
411+
break;
412+
}
413+
414+
if (use_bluetooth) {
415+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDIN]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
416+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDOUT]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
417+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDERR]) = MP_OBJ_FROM_PTR(&pb_bluetooth_stdio_wrapper_obj);
418+
} else {
419+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDIN]) = MP_OBJ_FROM_PTR(&pb_usb_stdio_wrapper_obj);
420+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDOUT]) = MP_OBJ_FROM_PTR(&pb_usb_stdio_wrapper_obj);
421+
MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_STDERR]) = MP_OBJ_FROM_PTR(&pb_usb_stdio_wrapper_obj);
422+
}
423+
424+
#endif // MICROPY_PY_SYS_MUTABLE_STDIO
399425

400426
// Runs the requested downloaded or builtin user program.
401427
switch (program->id) {

lib/pbio/include/pbsys/command.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
#include <stdint.h>
1717

1818
#include <pbio/protocol.h>
19+
#include <pbsys/main.h>
1920

2021
typedef enum {
21-
PBSYS_COMMAND_TRANSPORT_BLE,
22-
PBSYS_COMMAND_TRANSPORT_USB,
22+
// NB: these values allow passing pbsys_command_transport_t directly as
23+
// pbsys_main_program_start_request_type_t without a lookup table.
24+
PBSYS_COMMAND_TRANSPORT_BLE = PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_BLUETOOTH,
25+
PBSYS_COMMAND_TRANSPORT_USB = PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_USB,
2326
} pbsys_command_transport_t;
2427

2528
pbio_pybricks_error_t pbsys_command(const uint8_t *data, uint32_t size, pbsys_command_transport_t transport);

lib/pbio/include/pbsys/main.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,18 @@ typedef enum {
3636
PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_HUB_UI = 2,
3737
/**
3838
* The program was requested to start remotely, such as with an IDE.
39+
*
40+
* @deprecated Use transport-specific items instead.
3941
*/
4042
PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_REMOTE = 3,
43+
/**
44+
* The program was requested to start using Bluetooth.
45+
*/
46+
PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_BLUETOOTH = 4,
47+
/**
48+
* The program was requested to start using USB.
49+
*/
50+
PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_USB = 5,
4151
} pbsys_main_program_start_request_type_t;
4252

4353
/**

lib/pbio/sys/command.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ pbio_pybricks_error_t pbsys_command(const uint8_t *data, uint32_t size, pbsys_co
5151
}
5252
// Use payload as program ID, otherwise use active user slot.
5353
return pbio_pybricks_error_from_pbio_error(
54-
pbsys_main_program_request_start((size == 2 ? data[1] : pbsys_hmi_get_selected_program_slot()), PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_REMOTE));
54+
pbsys_main_program_request_start((size == 2 ? data[1] : pbsys_hmi_get_selected_program_slot()), (pbsys_main_program_start_request_type_t)transport));
5555
}
5656
#if PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_REPL
5757
case PBIO_PYBRICKS_COMMAND_START_REPL:
5858
// Deprecated. For backwards compatibility with Pybricks
5959
// Profile < v1.4.0, make it work anyway.
6060
return pbio_pybricks_error_from_pbio_error(
61-
pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_REPL, PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_REMOTE));
61+
pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_REPL, (pbsys_main_program_start_request_type_t)transport));
6262
#endif // PBSYS_CONFIG_FEATURE_BUILTIN_USER_PROGRAM_REPL
6363

6464
case PBIO_PYBRICKS_COMMAND_WRITE_USER_PROGRAM_META:

0 commit comments

Comments
 (0)