Small fixes

This commit is contained in:
Your Name
2026-05-25 09:06:36 +03:00
parent f5211a4089
commit 2a89d2f08f
2 changed files with 144 additions and 12 deletions
+3 -3
View File
@@ -19,9 +19,9 @@ static const app_config_t default_config = {
.phase_b_ms = (20 * 1000), .phase_b_ms = (20 * 1000),
}, },
.light_config = { .light_config = {
.min = 5, .min = 20,
.max = 600, .max = 3000,
.gain = 0b00000100, .gain = 0x10, // x256
}, },
.color = { .color = {
.r = 1, .r = 1,
+136 -4
View File
@@ -1,12 +1,19 @@
// User-facing shell commands. // User-facing shell commands.
// mode set <phase_a_ms> <phase_b_ms> — save config and restart // mode set <phase_a_ms> <phase_b_ms> — save config and restart mode
// mode color <name> — save color by name and restart // mode color <name> — save color by name and restart mode
// mode duration <ms> — save color duration and restart // mode duration <ms> — save color duration and restart mode
// mode light min <value> — save light sensor min threshold and reinit
// mode light max <value> — save light sensor max threshold and reinit
// mode light gain <value> — 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 status — show current config
// // mode colors — list available colors
#include "mode.h" #include "mode.h"
#include "config.h" #include "config.h"
#include "led.h" #include "led.h"
#include "light.h"
#include <zephyr/shell/shell.h> #include <zephyr/shell/shell.h>
#include <stdlib.h> #include <stdlib.h>
@@ -22,6 +29,10 @@ static void print_color(const struct shell *sh, const struct color_t *color)
(int)(color->b * 255)); (int)(color->b * 255));
} }
/* ------------------------------------------------------------------ */
/* mode set / color / duration */
/* ------------------------------------------------------------------ */
static int cmd_mode_set(const struct shell *sh, size_t argc, char **argv) static int cmd_mode_set(const struct shell *sh, size_t argc, char **argv)
{ {
app_config_t cfg; app_config_t cfg;
@@ -72,6 +83,101 @@ static int cmd_mode_duration(const struct shell *sh, size_t argc, char **argv)
return 0; 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) static int cmd_mode_status(const struct shell *sh, size_t argc, char **argv)
{ {
app_config_t cfg; 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); cfg.mode_config.phase_b_ms);
print_color(sh, &cfg.color); print_color(sh, &cfg.color);
shell_print(sh, " duration=%d ms", cfg.color_duration); 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; return 0;
} }
@@ -98,10 +218,22 @@ static int cmd_mode_colors(const struct shell *sh, size_t argc, char **argv)
return 0; return 0;
} }
/* ------------------------------------------------------------------ */
/* Command registration */
/* ------------------------------------------------------------------ */
SHELL_STATIC_SUBCMD_SET_CREATE(light_cmds,
SHELL_CMD_ARG(min, NULL, "Set light sensor min threshold <value>, save and reinit.", cmd_mode_light_min, 2, 0),
SHELL_CMD_ARG(max, NULL, "Set light sensor max threshold <value>, 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_STATIC_SUBCMD_SET_CREATE(mode_cmds,
SHELL_CMD_ARG(set, NULL, "Set <phase_a_ms> <phase_b_ms>, save and restart.", cmd_mode_set, 3, 0), SHELL_CMD_ARG(set, NULL, "Set <phase_a_ms> <phase_b_ms>, 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(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 <ms>, save and restart.", cmd_mode_duration, 2, 0), SHELL_CMD_ARG(duration, NULL, "Set color duration <ms>, 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( status, NULL, "Show current config.", cmd_mode_status),
SHELL_CMD( colors, NULL, "List all available colors.", cmd_mode_colors), SHELL_CMD( colors, NULL, "List all available colors.", cmd_mode_colors),
SHELL_SUBCMD_SET_END SHELL_SUBCMD_SET_END