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.
This commit is contained in:
Eduard Iten 2025-07-11 00:16:43 +02:00
parent 5fd904de9e
commit a3e8d5c168
11 changed files with 90 additions and 129 deletions

View File

@ -1,2 +1,7 @@
rsource "lib/Kconfig" rsource "lib/Kconfig"
rsource "lib/shell_valve/Kconfig" rsource "lib/shell_valve/Kconfig"
config SLAVE_NODE_APP
bool "Slave Node Application"
default y
select SHELL_VALVE

View File

@ -5,6 +5,9 @@ CONFIG_LOG=y
# Enable Shell # Enable Shell
CONFIG_SHELL=y CONFIG_SHELL=y
CONFIG_REBOOT=y CONFIG_REBOOT=y
CONFIG_SHELL_MODBUS=y
CONFIG_SHELL_VALVE=y
CONFIG_SHELL_SYSTEM=y
# Enable Settings Subsystem # Enable Settings Subsystem
CONFIG_SETTINGS=y CONFIG_SETTINGS=y

View File

@ -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); 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. * @brief Gets the configured maximum opening time.
* *

View File

@ -5,4 +5,5 @@ rsource "modbus_server/Kconfig"
rsource "valve/Kconfig" rsource "valve/Kconfig"
rsource "shell_system/Kconfig" rsource "shell_system/Kconfig"
rsource "shell_modbus/Kconfig" rsource "shell_modbus/Kconfig"
rsource "shell_valve/Kconfig"
endmenu endmenu

View File

@ -1,5 +1,7 @@
config SHELL_MODBUS config SHELL_MODBUS
bool "Enable Shell Modbus" bool "Enable Shell Modbus"
default y default n
depends on SHELL
depends on LIB_MODBUS_SERVER
help help
Enable the modnbus shell commands. Enable the modbus shell commands.

View File

@ -10,7 +10,6 @@
#include <zephyr/shell/shell.h> #include <zephyr/shell/shell.h>
#include <lib/modbus_server.h> #include <lib/modbus_server.h>
#include <lib/valve.h>
#include <stdlib.h> #include <stdlib.h>
/** /**
@ -21,10 +20,10 @@
* @param argv Argument values. * @param argv Argument values.
* @return 0 on success, -EINVAL on error. * @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) { if (argc != 2) {
shell_error(sh, "Usage: set_baud <baudrate>"); shell_error(sh, "Usage: setb <baudrate>");
return -EINVAL; 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. * @param argv Argument values.
* @return 0 on success, -EINVAL on error. * @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) { if (argc != 2) {
shell_error(sh, "Usage: set_id <slave_id>"); shell_error(sh, "Usage: setid <slave_id>");
return -EINVAL; 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. * @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, -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 <seconds>");
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 <seconds>");
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.
* *
* @param sh The shell instance. * @param sh The shell instance.
* @param argc Argument count. * @param argc Argument count.
* @param argv Argument values. * @param argv Argument values.
* @return 0 on success. * @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:"); const int label_width = 15;
shell_print(sh, " Baudrate: %u", modbus_get_baudrate());
shell_print(sh, " Slave ID: %u", modbus_get_unit_id()); shell_print(sh, "Modbus Settings:");
shell_print(sh, "Current Valve Configuration:"); shell_print(sh, "%*s %u", label_width, "Baudrate:", modbus_get_baudrate());
shell_print(sh, " Max Opening Time: %u s", valve_get_max_open_time()); shell_print(sh, "%*s %u", label_width, "Slave ID:", modbus_get_unit_id());
shell_print(sh, " Max Closing Time: %u s", valve_get_max_close_time());
return 0; return 0;
} }
SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds, SHELL_STATIC_SUBCMD_SET_CREATE(sub_modbus_cmds,
SHELL_CMD(set_baud, NULL, "Set Modbus baudrate", cmd_modbus_set_baud), SHELL_CMD(setb, NULL, "Set Modbus baudrate", cmd_modbus_setb),
SHELL_CMD(set_id, NULL, "Set Modbus slave ID", cmd_modbus_set_id), 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_SUBCMD_SET_END);
SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_cmds, SHELL_CMD_REGISTER(modbus, &sub_modbus_cmds, "Modbus commands", NULL);
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);

View File

@ -1,5 +1,5 @@
config SHELL_SYSTEM config SHELL_SYSTEM
bool "Enable Shell System" bool "Enable Shell System"
default y default n
help help
Enable the system commands. Enable the system commands.

View File

@ -1 +1 @@
zephyr_library() zephyr_library_sources(shell_valve.c)

View File

@ -1,6 +1,7 @@
config SHELL_VALVE config SHELL_VALVE
bool "Shell Valve commands" bool "Shell Valve commands"
default y default n
depends on SHELL depends on SHELL
help depends on LIB_VALVE
Enable the shell commands for valve control. help
Enable the valve shell commands.

View File

@ -3,10 +3,10 @@
#include <lib/valve.h> #include <lib/valve.h>
#include <stdlib.h> #include <stdlib.h>
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) { if (argc != 2) {
shell_print(sh, "Usage: valve set_max_open_time <seconds>"); shell_print(sh, "Usage: valve set_open_t <seconds>");
return -EINVAL; return -EINVAL;
} }
@ -16,17 +16,10 @@ static int cmd_valve_set_max_open_time(const struct shell *sh, size_t argc, char
return 0; return 0;
} }
static int cmd_valve_get_max_open_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)
{
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)
{ {
if (argc != 2) { if (argc != 2) {
shell_print(sh, "Usage: valve set_max_close_time <seconds>"); shell_print(sh, "Usage: valve set_close_t <seconds>");
return -EINVAL; return -EINVAL;
} }
@ -36,18 +29,10 @@ static int cmd_valve_set_max_close_time(const struct shell *sh, size_t argc, cha
return 0; return 0;
} }
static int cmd_valve_get_max_close_time(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)
{
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)
{ {
if (argc != 2) { if (argc != 2) {
shell_print(sh, "Usage: valve set_end_current_threshold_open <milliamps>"); shell_print(sh, "Usage: valve set_end_curr_open <milliamps>");
return -EINVAL; return -EINVAL;
} }
@ -57,19 +42,10 @@ static int cmd_valve_set_end_current_threshold_open(
return 0; return 0;
} }
static int cmd_valve_get_end_current_threshold_open( static int cmd_valve_set_end_curr_close(const struct shell *sh, size_t argc, char **argv)
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)
{ {
if (argc != 2) { if (argc != 2) {
shell_print(sh, "Usage: valve set_end_current_threshold_close <milliamps>"); shell_print(sh, "Usage: valve set_end_curr_close <milliamps>");
return -EINVAL; return -EINVAL;
} }
@ -79,37 +55,38 @@ static int cmd_valve_set_end_current_threshold_close(
return 0; return 0;
} }
static int cmd_valve_get_end_current_threshold_close( static int cmd_valve_show(const struct shell *sh, size_t argc, char **argv)
const struct shell *sh, size_t argc, char **argv)
{ {
uint16_t current_ma = valve_get_end_current_threshold_close(); const int label_width = 30;
shell_print(sh, "End current threshold (close): %u mA.", current_ma);
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; return 0;
} }
SHELL_STATIC_SUBCMD_SET_CREATE(sub_valve_settings, 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(set_open_t, NULL, "Set max open time (seconds)", cmd_valve_set_open_t),
SHELL_CMD(get_max_open_time, NULL, "Get max open time (seconds)", cmd_valve_get_max_open_time), SHELL_CMD(set_close_t, NULL, "Set max close time (seconds)", cmd_valve_set_close_t),
SHELL_CMD( SHELL_CMD(set_end_curr_open,
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,
NULL, NULL,
"Set end current threshold for opening (mA)", "Set end current threshold for opening (mA)",
cmd_valve_set_end_current_threshold_open), cmd_valve_set_end_curr_open),
SHELL_CMD(get_end_current_threshold_open, SHELL_CMD(set_end_curr_close,
NULL,
"Get end current threshold for opening (mA)",
cmd_valve_get_end_current_threshold_open),
SHELL_CMD(set_end_current_threshold_close,
NULL, NULL,
"Set end current threshold for closing (mA)", "Set end current threshold for closing (mA)",
cmd_valve_set_end_current_threshold_close), cmd_valve_set_end_curr_close),
SHELL_CMD(get_end_current_threshold_close, SHELL_CMD(show, NULL, "Show valve configuration", cmd_valve_show),
NULL,
"Get end current threshold for closing (mA)",
cmd_valve_get_end_current_threshold_close),
SHELL_SUBCMD_SET_END); SHELL_SUBCMD_SET_END);
SHELL_CMD_REGISTER(valve, &sub_valve_settings, "Valve commands", NULL); SHELL_CMD_REGISTER(valve, &sub_valve_settings, "Valve commands", NULL);

View File

@ -199,6 +199,16 @@ uint16_t valve_get_max_close_time(void)
return max_closing_time_s; 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 valve_get_opening_current(void)
{ {
int32_t current; int32_t current;