From b100a8acf7e5cabd2c3483582be3b6409a62b1e0 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Tue, 1 Jul 2025 18:24:20 +0200 Subject: [PATCH] feat(shell): Add commands for valve timing - Add shell commands 'valve set_open_time' and 'valve set_close_time' to configure the virtual valve. - Extend the 'show_config' command to display the new timing values. - The new settings are persisted to flash storage. --- software/apps/slave_node/src/main.c | 22 ++++++++ software/apps/slave_node/src/modbus_bridge.h | 5 ++ software/apps/slave_node/src/shell_modbus.c | 55 ++++++++++++++++---- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/software/apps/slave_node/src/main.c b/software/apps/slave_node/src/main.c index b088e99..9d60372 100644 --- a/software/apps/slave_node/src/main.c +++ b/software/apps/slave_node/src/main.c @@ -238,6 +238,28 @@ uint8_t modbus_get_unit_id(void) return server_param.server.unit_id; } +void valve_set_max_open_time(uint16_t seconds) +{ + max_opening_time_s = seconds; + settings_save_one("valve/max_open_time", &max_opening_time_s, sizeof(max_opening_time_s)); +} + +void valve_set_max_close_time(uint16_t seconds) +{ + max_closing_time_s = seconds; + settings_save_one("valve/max_close_time", &max_closing_time_s, sizeof(max_closing_time_s)); +} + +uint16_t valve_get_max_open_time(void) +{ + return max_opening_time_s; +} + +uint16_t valve_get_max_close_time(void) +{ + return max_closing_time_s; +} + static int settings_load_cb(const char *name, size_t len, settings_read_cb read_cb, void *cb_arg) { diff --git a/software/apps/slave_node/src/modbus_bridge.h b/software/apps/slave_node/src/modbus_bridge.h index f88b67c..d2ea51b 100644 --- a/software/apps/slave_node/src/modbus_bridge.h +++ b/software/apps/slave_node/src/modbus_bridge.h @@ -24,4 +24,9 @@ uint32_t modbus_get_baudrate(void); */ uint8_t modbus_get_unit_id(void); +void valve_set_max_open_time(uint16_t seconds); +void valve_set_max_close_time(uint16_t seconds); +uint16_t valve_get_max_open_time(void); +uint16_t valve_get_max_close_time(void); + #endif // MODBUS_BRIDGE_H diff --git a/software/apps/slave_node/src/shell_modbus.c b/software/apps/slave_node/src/shell_modbus.c index 5d83d54..13ecbbf 100644 --- a/software/apps/slave_node/src/shell_modbus.c +++ b/software/apps/slave_node/src/shell_modbus.c @@ -1,6 +1,5 @@ #include #include -#include #include "modbus_bridge.h" static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) @@ -33,12 +32,10 @@ static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) if (modbus_reconfigure(new_baud, modbus_get_unit_id()) != 0) { shell_error(sh, "Failed to apply new baudrate"); - return -1; + } else { + shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud); } - settings_save_one("modbus/baudrate", &new_baud, sizeof(new_baud)); - shell_print(sh, "Modbus baudrate set to: %u (and saved)", new_baud); - return 0; } @@ -58,28 +55,64 @@ static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) if (modbus_reconfigure(modbus_get_baudrate(), new_id) != 0) { shell_error(sh, "Failed to apply new slave ID"); - return -1; + } else { + shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id); } - settings_save_one("modbus/unit_id", &new_id, sizeof(new_id)); - shell_print(sh, "Modbus slave ID set to: %u (and saved)", new_id); - return 0; } -static int cmd_modbus_show(const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_set_open_time(const struct shell *sh, size_t argc, char **argv) +{ + if (argc != 2) { + shell_error(sh, "Usage: set_open_time "); + return -EINVAL; + } + + uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10); + valve_set_max_open_time(seconds); + shell_print(sh, "Max opening time set to: %u seconds (and saved)", seconds); + + return 0; +} + +static int cmd_valve_set_close_time(const struct shell *sh, size_t argc, char **argv) +{ + if (argc != 2) { + shell_error(sh, "Usage: set_close_time "); + return -EINVAL; + } + + uint16_t seconds = (uint16_t)strtoul(argv[1], NULL, 10); + valve_set_max_close_time(seconds); + shell_print(sh, "Max closing time set to: %u seconds (and saved)", seconds); + + return 0; +} + +static int cmd_config_show(const struct shell *sh, size_t argc, char **argv) { shell_print(sh, "Current Modbus Configuration:"); shell_print(sh, " Baudrate: %u", modbus_get_baudrate()); shell_print(sh, " Slave ID: %u", modbus_get_unit_id()); + shell_print(sh, "Current Valve Configuration:"); + shell_print(sh, " Max Opening Time: %u s", valve_get_max_open_time()); + shell_print(sh, " Max Closing Time: %u s", valve_get_max_close_time()); return 0; } SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds, SHELL_CMD(set_baud, NULL, "Set Modbus baudrate", cmd_modbus_set_baud), SHELL_CMD(set_id, NULL, "Set Modbus slave ID", cmd_modbus_set_id), - SHELL_CMD(show, NULL, "Show current Modbus configuration", cmd_modbus_show), SHELL_SUBCMD_SET_END ); +SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_cmds, + SHELL_CMD(set_open_time, NULL, "Set max valve opening time", cmd_valve_set_open_time), + SHELL_CMD(set_close_time, NULL, "Set max valve closing time", cmd_valve_set_close_time), + SHELL_SUBCMD_SET_END +); + SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus configuration", NULL); +SHELL_CMD_REGISTER(valve, &sub_valve_cmds, "Valve configuration", NULL); +SHELL_CMD_REGISTER(show_config, NULL, "Show all configurations", cmd_config_show); \ No newline at end of file