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.
This commit is contained in:
Eduard Iten 2025-07-01 18:24:20 +02:00
parent 269e9e88a1
commit b100a8acf7
3 changed files with 71 additions and 11 deletions

View File

@ -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)
{

View File

@ -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

View File

@ -1,6 +1,5 @@
#include <zephyr/shell/shell.h>
#include <stdlib.h>
#include <zephyr/settings/settings.h>
#include "modbus_bridge.h"
static int cmd_modbus_set_baud(const struct shell *sh, size_t argc, char **argv)
@ -33,11 +32,9 @@ 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;
}
settings_save_one("modbus/baudrate", &new_baud, sizeof(new_baud));
} else {
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;
}
settings_save_one("modbus/unit_id", &new_id, sizeof(new_id));
} else {
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 <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;
}
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;
}
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);