feat: Default-Transition, Weckschalter und farbfreier Ausschaltfade

Zwei neue Einstellungen, beide wie fade_in/fade_out ueber
POST /api/v1/device/state les- und schreibbar und sofort wirksam:

- transition_default (Voreinstellung 300 ms) fuellt die Transition fuer
  Farb-, Weiss- und Helligkeitswechsel bei eingeschalteter Lampe, wenn der
  Aufruf kein eigenes "transition" mitbringt. Beim Ein- und Ausschalten
  behalten fade_in/fade_out weiterhin Vorrang.
- wake_on_change (Voreinstellung aus) schaltet die Lampe bei einer Farb-,
  Weiss- oder Helligkeitsaenderung ein, wenn sie aus war. Ein
  ausdrueckliches "on": false im selben Aufruf gewinnt immer.

Beide Felder haengen am Ende von nl_settings_t, damit bestehende Lampen
ueber die Laengentoleranz von read_rec() ihre echten Voreinstellungen
bekommen statt Nullen. nl_ctrl_set_fades() heisst jetzt
nl_ctrl_set_options().

Dazu ein Treiberfehler, der erst an der migrierten GU10 RGB auffiel: der
WY-Zweig von nl_sm2135eh_set() schrieb nur Header und WY-Daten, nie die
RGB-Datenregister. Der Chip behielt also die zuletzt gesetzte Farbe, und
bei einem direkten Wechsel Farbe -> Weiss (ohne Zwischenschritt ueber
dunkel) schaelte sie sich beim Ausblenden wieder frei. Der Header sendet
jetzt die - in Weiss immer nullwertigen - RGB-Daten mit.

Die Bruno-Collection zeigt Transition jetzt zusammen mit einem Farb- und
einem Weisswechsel im selben Aufruf und hat einen Request fuer die
beiden neuen Einstellungen.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Michael Sauer
2026-09-13 02:56:56 +02:00
co-authored by Claude Opus 5
parent 62a2b43175
commit 00d5a4c532
13 changed files with 256 additions and 19 deletions
+2 -1
View File
@@ -202,7 +202,8 @@ static void app_task(void *arg)
after_ota);
nl_app_lock();
nl_engine_init(&cfg);
nl_ctrl_init(board, s_settings.fade_in_ms, s_settings.fade_out_ms);
nl_ctrl_init(board, s_settings.fade_in_ms, s_settings.fade_out_ms, s_settings.transition_ms,
s_settings.wake_on_change != 0);
nl_ctrl_boot(&initial);
nl_app_unlock();
+2
View File
@@ -47,6 +47,8 @@ void nl_settings_defaults(nl_settings_t *s)
s->power_on_behaviour = NL_POB_LAST;
s->fade_in_ms = 500;
s->fade_out_ms = 500;
s->transition_ms = 300;
s->wake_on_change = 0;
}
void nl_settings_init(uint32_t addr_a, uint32_t addr_b)
+9
View File
@@ -19,6 +19,15 @@ typedef struct {
uint16_t fade_in_ms;
uint16_t fade_out_ms;
uint8_t ota_pending; /* set before an OTA; the next boot restores the last state */
/* Appended fields: read_rec() in nl_settings.c pre-fills the struct with
* nl_settings_defaults() and only overwrites the bytes actually stored, so records
* written by an older firmware pick up the real default here, not zero. Keep adding new
* fields at the end for that to keep working. */
uint16_t transition_ms; /* default transition (ms) for a color/brightness/CT change
* while the lamp stays on; fade_in/fade_out still win when
* switching on or off. */
uint8_t wake_on_change; /* if set, such a change also switches the lamp on when it was
* off, unless the same call says "on": false. */
} nl_settings_t;
void nl_settings_defaults(nl_settings_t *s);
+8 -2
View File
@@ -88,9 +88,15 @@ void nl_sm2135eh_set(const uint8_t ch[NL_CH_COUNT])
uint8_t d[6] = {SM_ADDR_CURRENT, s_current, chan, ch[NL_CH_B], ch[NL_CH_G], ch[NL_CH_R]};
sm_send(d, 6);
} else {
uint8_t h[3] = {SM_ADDR_CURRENT, s_current, SM_CHANNEL_WY};
/* The RGB data registers are written along with the channel select, even though they
are always zero here: the chip keeps whatever colour was last written, and a direct
colour -> white change never passes through the all-dark frame above. The stale
colour then bleeds through as soon as the white dims - visible as a coloured
fade-out on a migrated GU10 RGB, 2026-09-13. */
uint8_t h[6] = {SM_ADDR_CURRENT, s_current, SM_CHANNEL_WY,
ch[NL_CH_B], ch[NL_CH_G], ch[NL_CH_R]};
uint8_t d[3] = {SM_ADDR_WY, ch[NL_CH_WW], ch[NL_CH_CW]};
sm_send(h, 3);
sm_send(h, 6);
sm_send(d, 3);
}
}
+17 -4
View File
@@ -7,23 +7,29 @@ static const nl_board_t *s_board;
static nl_light_state_t s_state;
static uint16_t s_fade_in;
static uint16_t s_fade_out;
static uint16_t s_transition;
static bool s_wake_on_change;
static bool s_dirty;
static uint32_t s_changed_at;
void nl_ctrl_init(const nl_board_t *board, uint16_t fade_in_ms, uint16_t fade_out_ms)
void nl_ctrl_init(const nl_board_t *board, uint16_t fade_in_ms, uint16_t fade_out_ms,
uint16_t transition_ms, bool wake_on_change)
{
s_board = board;
s_state = nl_boot_default_state(board);
s_state.on = false;
s_dirty = false;
s_changed_at = 0;
nl_ctrl_set_fades(fade_in_ms, fade_out_ms);
nl_ctrl_set_options(fade_in_ms, fade_out_ms, transition_ms, wake_on_change);
}
void nl_ctrl_set_fades(uint16_t fade_in_ms, uint16_t fade_out_ms)
void nl_ctrl_set_options(uint16_t fade_in_ms, uint16_t fade_out_ms, uint16_t transition_ms,
bool wake_on_change)
{
s_fade_in = fade_in_ms > NL_MAX_FADE_MS ? NL_MAX_FADE_MS : fade_in_ms;
s_fade_out = fade_out_ms > NL_MAX_FADE_MS ? NL_MAX_FADE_MS : fade_out_ms;
s_transition = transition_ms > NL_MAX_TRANSITION_MS ? NL_MAX_TRANSITION_MS : transition_ms;
s_wake_on_change = wake_on_change;
}
void nl_ctrl_boot(const nl_light_state_t *initial)
@@ -62,6 +68,13 @@ nl_ctrl_err_t nl_ctrl_apply(const nl_light_change_t *c, uint32_t now_ms)
n.mode = NL_MODE_WHITE;
}
/* Weckschalter: a color/CT/brightness change arriving while the lamp is off switches it
* on, unless this same call also says "on": false explicitly - that always wins. Applied
* before the duration below so an off->on transition here uses fade_in like any other. */
if (s_wake_on_change && !s_state.on && !n.on && !(c->has_on && !c->on) &&
(c->has_color || c->has_ct || c->has_brightness))
n.on = true;
if (c->has_transition)
dur = c->transition_ms;
else if (!s_state.on && n.on)
@@ -69,7 +82,7 @@ nl_ctrl_err_t nl_ctrl_apply(const nl_light_change_t *c, uint32_t now_ms)
else if (s_state.on && !n.on)
dur = s_fade_out;
else
dur = 0;
dur = s_transition;
s_state = n;
nl_engine_set(&s_state, dur);
+11 -3
View File
@@ -32,9 +32,17 @@ typedef enum {
NL_CTRL_ERR_TRANSITION
} nl_ctrl_err_t;
/* The engine must be initialised before. Not thread safe: callers serialise access. */
void nl_ctrl_init(const nl_board_t *board, uint16_t fade_in_ms, uint16_t fade_out_ms);
void nl_ctrl_set_fades(uint16_t fade_in_ms, uint16_t fade_out_ms);
/* The engine must be initialised before. Not thread safe: callers serialise access.
*
* transition_ms: default transition applied to a color/brightness/CT change while the lamp
* stays on and the caller did not send its own "transition" (fade_in/fade_out still take
* precedence when the lamp is switching on or off).
* wake_on_change: if true, a color/CT/brightness change arriving while the lamp is off
* switches it on (using fade_in), unless the same call also says "on": false. */
void nl_ctrl_init(const nl_board_t *board, uint16_t fade_in_ms, uint16_t fade_out_ms,
uint16_t transition_ms, bool wake_on_change);
void nl_ctrl_set_options(uint16_t fade_in_ms, uint16_t fade_out_ms, uint16_t transition_ms,
bool wake_on_change);
void nl_ctrl_boot(const nl_light_state_t *initial);
nl_ctrl_err_t nl_ctrl_apply(const nl_light_change_t *c, uint32_t now_ms);
void nl_ctrl_get(nl_light_state_t *out);
+16
View File
@@ -115,6 +115,11 @@ static cJSON *state_json(const nl_api_ctx_t *ctx)
POB_NAMES[s.power_on_behaviour <= NL_POB_OFF ? s.power_on_behaviour : NL_POB_LAST]);
cJSON_AddNumberToObject(o, "fade_in", s.fade_in_ms);
cJSON_AddNumberToObject(o, "fade_out", s.fade_out_ms);
/* Named "transition_default", not "transition": the latter is already the per-call field
* above the settings section of this same request/response and a second field with the
* same key in one JSON object would collide with it. */
cJSON_AddNumberToObject(o, "transition_default", s.transition_ms);
cJSON_AddBoolToObject(o, "wake_on_change", s.wake_on_change != 0);
cap = cJSON_AddObjectToObject(o, "capabilities");
cJSON_AddBoolToObject(cap, "color", ctx->board->has_rgb);
ct[0] = ctx->board->ct_min;
@@ -193,6 +198,17 @@ static void handle_post_state(const nl_api_ctx_t *ctx, const nl_http_request_t *
case 1: s.fade_out_ms = (uint16_t)v; settings = true; break;
default: break;
}
switch (get_int(j, "transition_default", 0, NL_MAX_TRANSITION_MS, &v)) {
case -1: err = "invalid transition_default"; goto out;
case 1: s.transition_ms = (uint16_t)v; settings = true; break;
default: break;
}
it = cJSON_GetObjectItemCaseSensitive(j, "wake_on_change");
if (it) {
if (!cJSON_IsBool(it)) { err = "invalid wake_on_change"; goto out; }
s.wake_on_change = cJSON_IsTrue(it) ? 1u : 0u;
settings = true;
}
if (light) {
switch (ctx->apply(&ch)) {
+1 -1
View File
@@ -58,7 +58,7 @@ static int save_settings(const nl_settings_t *s)
memcpy(merged.wifi_pass, cur->wifi_pass, sizeof merged.wifi_pass);
*cur = merged;
}
nl_ctrl_set_fades(cur->fade_in_ms, cur->fade_out_ms);
nl_ctrl_set_options(cur->fade_in_ms, cur->fade_out_ms, cur->transition_ms, cur->wake_on_change != 0);
memcpy(name, cur->name, sizeof name);
nl_app_unlock();
if (name_changed)
+1 -1
View File
@@ -78,7 +78,7 @@ static void exec(const nl_cmd_t *c)
nl_app_lock();
cfg->fade_in_ms = (uint16_t)c->arg[0];
cfg->fade_out_ms = (uint16_t)c->arg[1];
nl_ctrl_set_fades(cfg->fade_in_ms, cfg->fade_out_ms);
nl_ctrl_set_options(cfg->fade_in_ms, cfg->fade_out_ms, cfg->transition_ms, cfg->wake_on_change != 0);
nl_app_unlock();
printf(nl_app_save_settings() == 0 ? "ok\r\n" : "error: save failed\r\n");
print_state();