|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Copyright (c) 2018-2025 The Pybricks Authors |
| 3 | + |
| 4 | +// Drop-in for HMI module that simulates sending a multi-mpy file to the hub |
| 5 | +// and then starts it. Used in the simulated hub to test different programs. |
| 6 | +// The script to start is set with an environment variable. |
| 7 | + |
| 8 | +#include <pbsys/config.h> |
| 9 | + |
| 10 | +#if PBSYS_CONFIG_HMI_ENV_MPY |
| 11 | + |
| 12 | +#include <pbio/button.h> |
| 13 | +#include <pbio/os.h> |
| 14 | +#include <pbsys/command.h> |
| 15 | +#include <pbsys/main.h> |
| 16 | +#include <pbsys/status.h> |
| 17 | +#include "storage.h" |
| 18 | + |
| 19 | +#include <errno.h> |
| 20 | +#include <signal.h> |
| 21 | +#include <stdbool.h> |
| 22 | +#include <stddef.h> |
| 23 | +#include <stdint.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <stdlib.h> |
| 26 | +#include <sys/stat.h> |
| 27 | + |
| 28 | +void pbsys_hmi_init(void) { |
| 29 | +} |
| 30 | + |
| 31 | +void pbsys_hmi_deinit(void) { |
| 32 | +} |
| 33 | + |
| 34 | +pbio_error_t pbsys_hmi_await_program_selection(void) { |
| 35 | + |
| 36 | + pbio_os_run_processes_and_wait_for_event(); |
| 37 | + |
| 38 | + // With this HMI, we run a script once and then exit. |
| 39 | + static bool ran_once = false; |
| 40 | + if (ran_once) { |
| 41 | + return PBIO_ERROR_CANCELED; |
| 42 | + } |
| 43 | + ran_once = true; |
| 44 | + |
| 45 | + // Test if script is provided via environment. |
| 46 | + const char *script_path = getenv("TEST_SCRIPT"); |
| 47 | + if (!script_path) { |
| 48 | + // No script given, just start REPL |
| 49 | + return pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_REPL, PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_BOOT); |
| 50 | + } |
| 51 | + |
| 52 | + // Pybricksdev helper script, pipes multi-mpy to us. |
| 53 | + char command[512]; |
| 54 | + snprintf(command, sizeof(command), "python ./bricks/simhub/make_mpy.py %s", script_path); |
| 55 | + FILE *pipe = popen(command, "r"); |
| 56 | + if (!pipe) { |
| 57 | + perror("Failed to compile program with Pybricksdev\n"); |
| 58 | + return PBIO_ERROR_CANCELED; |
| 59 | + } |
| 60 | + |
| 61 | + // Read the multi-mpy file from pipe. |
| 62 | + uint8_t program_buf[PBDRV_CONFIG_BLOCK_DEVICE_RAM_SIZE]; |
| 63 | + size_t read_size = fread(program_buf, 1, sizeof(program_buf), pipe); |
| 64 | + pclose(pipe); |
| 65 | + |
| 66 | + if (read_size == 0) { |
| 67 | + perror("Error reading from pipe"); |
| 68 | + return PBIO_ERROR_CANCELED; |
| 69 | + } |
| 70 | + |
| 71 | + // Load the program in storage, as if receiving it. |
| 72 | + pbsys_storage_set_program_size(0); |
| 73 | + pbsys_storage_set_program_data(0, program_buf, read_size); |
| 74 | + pbsys_storage_set_program_size(read_size); |
| 75 | + |
| 76 | + return pbsys_main_program_request_start(PBIO_PYBRICKS_USER_PROGRAM_ID_FIRST_SLOT, PBSYS_MAIN_PROGRAM_START_REQUEST_TYPE_BOOT); |
| 77 | +} |
| 78 | + |
| 79 | +#endif // PBSYS_CONFIG_HMI_ENV_MPY |
0 commit comments