From 2a89d2f08f7d7eea2787eb5ea3fa9a25ae865483 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 25 May 2026 09:06:36 +0300 Subject: [PATCH] Small fixes --- src/config.c | 6 +-- src/shell.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 144 insertions(+), 12 deletions(-) diff --git a/src/config.c b/src/config.c index fece7f6..0c8565c 100644 --- a/src/config.c +++ b/src/config.c @@ -19,9 +19,9 @@ static const app_config_t default_config = { .phase_b_ms = (20 * 1000), }, .light_config = { - .min = 5, - .max = 600, - .gain = 0b00000100, + .min = 20, + .max = 3000, + .gain = 0x10, // x256 }, .color = { .r = 1, diff --git a/src/shell.c b/src/shell.c index e967ec1..3206ef6 100644 --- a/src/shell.c +++ b/src/shell.c @@ -1,12 +1,19 @@ // User-facing shell commands. -// mode set — save config and restart -// mode color — save color by name and restart -// mode duration — save color duration and restart +// mode set — save config and restart mode +// mode color — save color by name and restart mode +// mode duration — save color duration and restart mode +// mode light min — save light sensor min threshold and reinit +// mode light max — save light sensor max threshold and reinit +// mode light gain — save light sensor gain and reinit +// valid gain values: 0x01 (x1), 0x02 (x4), +// 0x04 (x16), 0x08 (x64), 0x10 (x256) // mode status — show current config -// +// mode colors — list available colors + #include "mode.h" #include "config.h" #include "led.h" +#include "light.h" #include #include @@ -22,6 +29,10 @@ static void print_color(const struct shell *sh, const struct color_t *color) (int)(color->b * 255)); } +/* ------------------------------------------------------------------ */ +/* mode set / color / duration */ +/* ------------------------------------------------------------------ */ + static int cmd_mode_set(const struct shell *sh, size_t argc, char **argv) { app_config_t cfg; @@ -72,6 +83,101 @@ static int cmd_mode_duration(const struct shell *sh, size_t argc, char **argv) return 0; } +/* ------------------------------------------------------------------ */ +/* mode light min / max / gain */ +/* ------------------------------------------------------------------ */ + +static int cmd_mode_light_min(const struct shell *sh, size_t argc, char **argv) +{ + int val = atoi(argv[1]); + if (val < 0 || val > UINT16_MAX) { + shell_error(sh, "min out of range (0-%d)", UINT16_MAX); + return -EINVAL; + } + + app_config_t cfg; + config_get(&cfg); + + if ((uint16_t)val >= cfg.light_config.max) { + shell_error(sh, "min (%d) must be less than current max (%d)", val, cfg.light_config.max); + return -EINVAL; + } + + cfg.light_config.min = (uint16_t)val; + config_save(&cfg); + light_init(); + mode_init(); + shell_print(sh, "Light min saved: %d. Sensor reinitialized.", val); + return 0; +} + +static int cmd_mode_light_max(const struct shell *sh, size_t argc, char **argv) +{ + int val = atoi(argv[1]); + if (val < 0 || val > UINT16_MAX) { + shell_error(sh, "max out of range (0-%d)", UINT16_MAX); + return -EINVAL; + } + + app_config_t cfg; + config_get(&cfg); + + if ((uint16_t)val <= cfg.light_config.min) { + shell_error(sh, "max (%d) must be greater than current min (%d)", val, cfg.light_config.min); + return -EINVAL; + } + + cfg.light_config.max = (uint16_t)val; + config_save(&cfg); + light_init(); + mode_init(); + shell_print(sh, "Light max saved: %d. Sensor reinitialized.", val); + return 0; +} + +/* Valid gain register values and their human-readable multipliers. */ +static const struct { + uint8_t reg; + const char *label; +} gain_table[] = { + { 0x01, "x1" }, + { 0x02, "x4" }, + { 0x04, "x16" }, + { 0x08, "x64" }, + { 0x10, "x256" }, +}; + +static int cmd_mode_light_gain(const struct shell *sh, size_t argc, char **argv) +{ + long val = strtol(argv[1], NULL, 0); /* accept 0x.. or decimal */ + + const char *label = NULL; + for (int i = 0; i < ARRAY_SIZE(gain_table); i++) { + if ((uint8_t)val == gain_table[i].reg) { + label = gain_table[i].label; + break; + } + } + + if (!label) { + shell_error(sh, "Invalid gain. Valid values: 0x01 (x1), 0x02 (x4), 0x04 (x16), 0x08 (x64), 0x10 (x256)"); + return -EINVAL; + } + + app_config_t cfg; + config_get(&cfg); + cfg.light_config.gain = (uint8_t)val; + config_save(&cfg); + light_init(); + mode_init(); + shell_print(sh, "Light gain saved: 0x%02x (%s). Sensor reinitialized.", (uint8_t)val, label); + return 0; +} + +/* ------------------------------------------------------------------ */ +/* mode status / colors */ +/* ------------------------------------------------------------------ */ + static int cmd_mode_status(const struct shell *sh, size_t argc, char **argv) { app_config_t cfg; @@ -82,6 +188,20 @@ static int cmd_mode_status(const struct shell *sh, size_t argc, char **argv) cfg.mode_config.phase_b_ms); print_color(sh, &cfg.color); shell_print(sh, " duration=%d ms", cfg.color_duration); + + /* Resolve gain label for display */ + const char *gain_label = "unknown"; + for (int i = 0; i < ARRAY_SIZE(gain_table); i++) { + if (cfg.light_config.gain == gain_table[i].reg) { + gain_label = gain_table[i].label; + break; + } + } + shell_print(sh, " light: min=%d, max=%d, gain=0x%02x (%s)", + cfg.light_config.min, + cfg.light_config.max, + cfg.light_config.gain, + gain_label); return 0; } @@ -98,12 +218,24 @@ static int cmd_mode_colors(const struct shell *sh, size_t argc, char **argv) return 0; } +/* ------------------------------------------------------------------ */ +/* Command registration */ +/* ------------------------------------------------------------------ */ + +SHELL_STATIC_SUBCMD_SET_CREATE(light_cmds, + SHELL_CMD_ARG(min, NULL, "Set light sensor min threshold , save and reinit.", cmd_mode_light_min, 2, 0), + SHELL_CMD_ARG(max, NULL, "Set light sensor max threshold , save and reinit.", cmd_mode_light_max, 2, 0), + SHELL_CMD_ARG(gain, NULL, "Set light sensor gain <0x01|0x02|0x04|0x08|0x10>, save and reinit.", cmd_mode_light_gain, 2, 0), + SHELL_SUBCMD_SET_END +); + SHELL_STATIC_SUBCMD_SET_CREATE(mode_cmds, - SHELL_CMD_ARG(set, NULL, "Set , save and restart.", cmd_mode_set, 3, 0), - SHELL_CMD_ARG(color, NULL, "Set color by name (e.g. deep_orange), save and restart.", cmd_mode_color, 2, 0), - SHELL_CMD_ARG(duration, NULL, "Set color duration , save and restart.", cmd_mode_duration, 2, 0), - SHELL_CMD( status, NULL, "Show current config.", cmd_mode_status), - SHELL_CMD( colors, NULL, "List all available colors.", cmd_mode_colors), + SHELL_CMD_ARG(set, NULL, "Set , save and restart.", cmd_mode_set, 3, 0), + SHELL_CMD_ARG(color, NULL, "Set color by name (e.g. deep_orange), save and restart.", cmd_mode_color, 2, 0), + SHELL_CMD_ARG(duration, NULL, "Set color duration , save and restart.", cmd_mode_duration, 2, 0), + SHELL_CMD( light, &light_cmds, "Light sensor config (min / max / gain).", NULL), + SHELL_CMD( status, NULL, "Show current config.", cmd_mode_status), + SHELL_CMD( colors, NULL, "List all available colors.", cmd_mode_colors), SHELL_SUBCMD_SET_END );