|
7 | 7 | #include <stdio.h> |
8 | 8 | #include <string.h> |
9 | 9 |
|
| 10 | +#include <pbdrv/bluetooth.h> |
10 | 11 | #include <pbdrv/stack.h> |
11 | 12 |
|
12 | 13 | #include <pbio/button.h> |
@@ -391,11 +392,36 @@ void pbsys_main_run_program(pbsys_main_program_t *program) { |
391 | 392 | mp_init(); |
392 | 393 |
|
393 | 394 | #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 |
399 | 425 |
|
400 | 426 | // Runs the requested downloaded or builtin user program. |
401 | 427 | switch (program->id) { |
|
0 commit comments