From a3e8d5c168659724a4e2ff725f8f31b6c6a6cdb0 Mon Sep 17 00:00:00 2001 From: Eduard Iten Date: Fri, 11 Jul 2025 00:16:43 +0200 Subject: [PATCH] refactor(shell): Improve shell command naming and output formatting - Renamed shell commands in and to be shorter and remove underscores (e.g., to ). - Consolidated get functions into a single show command for both valve and Modbus settings (e.g., , ). - Adjusted output formatting for show commands to be right-aligned and remove horizontal lines for better readability. - Fixed missing getter function implementations in and their declarations in . - Ensured is correctly selected in to make valve shell commands available. --- software/Kconfig | 5 ++ software/apps/slave_node/prj.conf | 3 + software/include/lib/valve.h | 14 ++++ software/lib/Kconfig | 1 + software/lib/shell_modbus/Kconfig | 6 +- software/lib/shell_modbus/shell_modbus.c | 82 +++++------------------ software/lib/shell_system/Kconfig | 2 +- software/lib/shell_valve/CMakeLists.txt | 2 +- software/lib/shell_valve/Kconfig | 9 +-- software/lib/shell_valve/shell_valve.c | 85 +++++++++--------------- software/lib/valve/valve.c | 10 +++ 11 files changed, 90 insertions(+), 129 deletions(-) diff --git a/software/Kconfig b/software/Kconfig index cc9af39..f123eca 100644 --- a/software/Kconfig +++ b/software/Kconfig @@ -1,2 +1,7 @@ rsource "lib/Kconfig" rsource "lib/shell_valve/Kconfig" + +config SLAVE_NODE_APP + bool "Slave Node Application" + default y + select SHELL_VALVE diff --git a/software/apps/slave_node/prj.conf b/software/apps/slave_node/prj.conf index 2a03c4a..f6575e3 100644 --- a/software/apps/slave_node/prj.conf +++ b/software/apps/slave_node/prj.conf @@ -5,6 +5,9 @@ CONFIG_LOG=y # Enable Shell CONFIG_SHELL=y CONFIG_REBOOT=y +CONFIG_SHELL_MODBUS=y +CONFIG_SHELL_VALVE=y +CONFIG_SHELL_SYSTEM=y # Enable Settings Subsystem CONFIG_SETTINGS=y diff --git a/software/include/lib/valve.h b/software/include/lib/valve.h index af90018..d7ce6df 100644 --- a/software/include/lib/valve.h +++ b/software/include/lib/valve.h @@ -113,6 +113,20 @@ void valve_set_end_current_threshold_open(uint16_t current_ma); */ void valve_set_end_current_threshold_close(uint16_t current_ma); +/** + * @brief Gets the current threshold for end-position detection during opening. + * + * @return The current threshold in milliamps. + */ +uint16_t valve_get_end_current_threshold_open(void); + +/** + * @brief Gets the current threshold for end-position detection during closing. + * + * @return The current threshold in milliamps. + */ +uint16_t valve_get_end_current_threshold_close(void); + /** * @brief Gets the configured maximum opening time. * diff --git a/software/lib/Kconfig b/software/lib/Kconfig index 1e2390b..71ce8c8 100644 --- a/software/lib/Kconfig +++ b/software/lib/Kconfig @@ -5,4 +5,5 @@ rsource "modbus_server/Kconfig" rsource "valve/Kconfig" rsource "shell_system/Kconfig" rsource "shell_modbus/Kconfig" +rsource "shell_valve/Kconfig" endmenu \ No newline at end of file diff --git a/software/lib/shell_modbus/Kconfig b/software/lib/shell_modbus/Kconfig index 7d5bc9f..2e10fd7 100644 --- a/software/lib/shell_modbus/Kconfig +++ b/software/lib/shell_modbus/Kconfig @@ -1,5 +1,7 @@ config SHELL_MODBUS bool "Enable Shell Modbus" - default y + default n + depends on SHELL + depends on LIB_MODBUS_SERVER help - Enable the modnbus shell commands. \ No newline at end of file + Enable the modbus shell commands. \ No newline at end of file diff --git a/software/lib/shell_modbus/shell_modbus.c b/software/lib/shell_modbus/shell_modbus.c index 98f4376..751f7a0 100644 --- a/software/lib/shell_modbus/shell_modbus.c +++ b/software/lib/shell_modbus/shell_modbus.c @@ -10,7 +10,6 @@ #include #include -#include #include /** @@ -21,10 +20,10 @@ * @param argv Argument values. * @return 0 on success, -EINVAL on error. */ -static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) +static int cmd_modbus_setb(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_error(sh, "Usage: set_baud "); + shell_error(sh, "Usage: setb "); return -EINVAL; } @@ -70,10 +69,10 @@ static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv) * @param argv Argument values. * @return 0 on success, -EINVAL on error. */ -static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) +static int cmd_modbus_setid(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_error(sh, "Usage: set_id "); + shell_error(sh, "Usage: setid "); return -EINVAL; } @@ -94,78 +93,27 @@ static int cmd_modbus_set_id(const struct shell *sh, size_t argc, char **argv) } /** - * @brief Shell command to set the valve's maximum opening time. - * - * @param sh The shell instance. - * @param argc Argument count. - * @param argv Argument values. - * @return 0 on success, -EINVAL on error. - */ -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; -} - -/** - * @brief Shell command to set the valve's maximum closing time. - * - * @param sh The shell instance. - * @param argc Argument count. - * @param argv Argument values. - * @return 0 on success, -EINVAL on error. - */ -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; -} - -/** - * @brief Shell command to show the current configuration. + * @brief Shell command to show the current Modbus configuration. * * @param sh The shell instance. * @param argc Argument count. * @param argv Argument values. * @return 0 on success. */ -static int cmd_config_show(const struct shell *sh, size_t argc, char **argv) +static int cmd_modbus_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()); + const int label_width = 15; + + shell_print(sh, "Modbus Settings:"); + shell_print(sh, "%*s %u", label_width, "Baudrate:", modbus_get_baudrate()); + shell_print(sh, "%*s %u", label_width, "Slave ID:", modbus_get_unit_id()); 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(setb, NULL, "Set Modbus baudrate", cmd_modbus_setb), + SHELL_CMD(setid, NULL, "Set Modbus slave ID", cmd_modbus_setid), + SHELL_CMD(show, NULL, "Show 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 +SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus commands", NULL); \ No newline at end of file diff --git a/software/lib/shell_system/Kconfig b/software/lib/shell_system/Kconfig index 08e88e3..6c87de1 100644 --- a/software/lib/shell_system/Kconfig +++ b/software/lib/shell_system/Kconfig @@ -1,5 +1,5 @@ config SHELL_SYSTEM bool "Enable Shell System" - default y + default n help Enable the system commands. \ No newline at end of file diff --git a/software/lib/shell_valve/CMakeLists.txt b/software/lib/shell_valve/CMakeLists.txt index 02d6ac9..18faa0a 100644 --- a/software/lib/shell_valve/CMakeLists.txt +++ b/software/lib/shell_valve/CMakeLists.txt @@ -1 +1 @@ -zephyr_library() +zephyr_library_sources(shell_valve.c) diff --git a/software/lib/shell_valve/Kconfig b/software/lib/shell_valve/Kconfig index 262b16e..aa8784f 100644 --- a/software/lib/shell_valve/Kconfig +++ b/software/lib/shell_valve/Kconfig @@ -1,6 +1,7 @@ config SHELL_VALVE bool "Shell Valve commands" - default y - depends on SHELL - help - Enable the shell commands for valve control. + default n + depends on SHELL + depends on LIB_VALVE + help + Enable the valve shell commands. \ No newline at end of file diff --git a/software/lib/shell_valve/shell_valve.c b/software/lib/shell_valve/shell_valve.c index 36111b1..24d5d90 100644 --- a/software/lib/shell_valve/shell_valve.c +++ b/software/lib/shell_valve/shell_valve.c @@ -3,10 +3,10 @@ #include #include -static int cmd_valve_set_max_open_time(const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_set_open_t(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_print(sh, "Usage: valve set_max_open_time "); + shell_print(sh, "Usage: valve set_open_t "); return -EINVAL; } @@ -16,17 +16,10 @@ static int cmd_valve_set_max_open_time(const struct shell *sh, size_t argc, char return 0; } -static int cmd_valve_get_max_open_time(const struct shell *sh, size_t argc, char **argv) -{ - uint16_t seconds = valve_get_max_open_time(); - shell_print(sh, "Max open time: %u seconds.", seconds); - return 0; -} - -static int cmd_valve_set_max_close_time(const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_set_close_t(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_print(sh, "Usage: valve set_max_close_time "); + shell_print(sh, "Usage: valve set_close_t "); return -EINVAL; } @@ -36,18 +29,10 @@ static int cmd_valve_set_max_close_time(const struct shell *sh, size_t argc, cha return 0; } -static int cmd_valve_get_max_close_time(const struct shell *sh, size_t argc, char **argv) -{ - uint16_t seconds = valve_get_max_close_time(); - shell_print(sh, "Max close time: %u seconds.", seconds); - return 0; -} - -static int cmd_valve_set_end_current_threshold_open( - const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_set_end_curr_open(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_print(sh, "Usage: valve set_end_current_threshold_open "); + shell_print(sh, "Usage: valve set_end_curr_open "); return -EINVAL; } @@ -57,19 +42,10 @@ static int cmd_valve_set_end_current_threshold_open( return 0; } -static int cmd_valve_get_end_current_threshold_open( - const struct shell *sh, size_t argc, char **argv) -{ - uint16_t current_ma = valve_get_end_current_threshold_open(); - shell_print(sh, "End current threshold (open): %u mA.", current_ma); - return 0; -} - -static int cmd_valve_set_end_current_threshold_close( - const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_set_end_curr_close(const struct shell *sh, size_t argc, char **argv) { if (argc != 2) { - shell_print(sh, "Usage: valve set_end_current_threshold_close "); + shell_print(sh, "Usage: valve set_end_curr_close "); return -EINVAL; } @@ -79,37 +55,38 @@ static int cmd_valve_set_end_current_threshold_close( return 0; } -static int cmd_valve_get_end_current_threshold_close( - const struct shell *sh, size_t argc, char **argv) +static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv) { - uint16_t current_ma = valve_get_end_current_threshold_close(); - shell_print(sh, "End current threshold (close): %u mA.", current_ma); + const int label_width = 30; + + shell_print(sh, "Valve Settings:"); + shell_print(sh, "%*s %u s", label_width, "Max Open Time:", valve_get_max_open_time()); + shell_print(sh, "%*s %u s", label_width, "Max Close Time:", valve_get_max_close_time()); + shell_print(sh, + "%*s %u mA", + label_width, + "End Current Threshold (Open):", + valve_get_end_current_threshold_open()); + shell_print(sh, + "%*s %u mA", + label_width, + "End Current Threshold (Close):", + valve_get_end_current_threshold_close()); return 0; } SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_settings, - SHELL_CMD(set_max_open_time, NULL, "Set max open time (seconds)", cmd_valve_set_max_open_time), - SHELL_CMD(get_max_open_time, NULL, "Get max open time (seconds)", cmd_valve_get_max_open_time), - SHELL_CMD( - set_max_close_time, NULL, "Set max close time (seconds)", cmd_valve_set_max_close_time), - SHELL_CMD( - get_max_close_time, NULL, "Get max close time (seconds)", cmd_valve_get_max_close_time), - SHELL_CMD(set_end_current_threshold_open, + SHELL_CMD(set_open_t, NULL, "Set max open time (seconds)", cmd_valve_set_open_t), + SHELL_CMD(set_close_t, NULL, "Set max close time (seconds)", cmd_valve_set_close_t), + SHELL_CMD(set_end_curr_open, NULL, "Set end current threshold for opening (mA)", - cmd_valve_set_end_current_threshold_open), - SHELL_CMD(get_end_current_threshold_open, - NULL, - "Get end current threshold for opening (mA)", - cmd_valve_get_end_current_threshold_open), - SHELL_CMD(set_end_current_threshold_close, + cmd_valve_set_end_curr_open), + SHELL_CMD(set_end_curr_close, NULL, "Set end current threshold for closing (mA)", - cmd_valve_set_end_current_threshold_close), - SHELL_CMD(get_end_current_threshold_close, - NULL, - "Get end current threshold for closing (mA)", - cmd_valve_get_end_current_threshold_close), + cmd_valve_set_end_curr_close), + SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show), SHELL_SUBCMD_SET_END); SHELL_CMD_REGISTER(valve, &sub_valve_settings, "Valve commands", NULL); diff --git a/software/lib/valve/valve.c b/software/lib/valve/valve.c index a54b7bd..0ea21ae 100644 --- a/software/lib/valve/valve.c +++ b/software/lib/valve/valve.c @@ -199,6 +199,16 @@ uint16_t valve_get_max_close_time(void) return max_closing_time_s; } +uint16_t valve_get_end_current_threshold_open(void) +{ + return end_current_threshold_open_ma; +} + +uint16_t valve_get_end_current_threshold_close(void) +{ + return end_current_threshold_close_ma; +} + int32_t valve_get_opening_current(void) { int32_t current;