From 1502c0854005509af2e15b0217b75e9f18a03fb4 Mon Sep 17 00:00:00 2001 From: Erik Date: Thu, 28 Jan 2021 16:41:59 +0100 Subject: [PATCH] Add support for once off speed and fade --- tasmota/xdrv_04_light.ino | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index 7e0907dd1..7fb7052c3 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -242,6 +242,10 @@ struct LIGHT { uint16_t fade_end_10[LST_MAX]; // 10 bits resolution target channel values uint16_t fade_duration = 0; // duration of fade in milliseconds uint32_t fade_start = 0; // fade start time in milliseconds, compared to millis() + bool fade_once_enabled = false; // override fade a single time + bool fade_once_value = false; // override fade a single time + bool speed_once_enabled = false; // override speed a single time + uint8_t speed_once_value = 0; // override speed a single time uint16_t pwm_min = 0; // minimum value for PWM, from DimmerRange, 0..1023 uint16_t pwm_max = 1023; // maxumum value for PWM, from DimmerRange, 0..1023 @@ -1598,6 +1602,16 @@ void LightSetPower(void) LightAnimate(); } +bool LightGetFadeSetting(void) { + if (Light.fade_once_enabled) return Light.fade_once_value; + return Settings.light_fade; +} + +uint8_t LightGetSpeedSetting(void) { + if (Light.speed_once_enabled) return Light.speed_once_value; + return Settings.light_speed; +} + // On entry Light.new_color[5] contains the color to be displayed // and Light.last_color[5] the color currently displayed // Light.power tells which lights or channels (SetOption68) are on/off @@ -1748,12 +1762,14 @@ void LightAnimate(void) cur_col_10[i] = orig_col_10bits[Light.color_remap[i]]; } - if (!Settings.light_fade || TasmotaGlobal.skip_light_fade || power_off || (!Light.fade_initialized)) { // no fade + if (!LightGetFadeSetting() || TasmotaGlobal.skip_light_fade || power_off || (!Light.fade_initialized)) { // no fade // record the current value for a future Fade memcpy(Light.fade_start_10, cur_col_10, sizeof(Light.fade_start_10)); // push the final values at 8 and 10 bits resolution to the PWMs LightSetOutputs(cur_col_10); Light.fade_initialized = true; // it is now ok to fade + Light.fade_once_enabled = false; // light has been set, reset fade_once_enabled + Light.speed_once_enabled = false; // light has been set, reset speed_once_enabled } else { // fade on if (Light.fade_running) { // if fade is running, we take the curring value as the start for the next fade @@ -1763,6 +1779,7 @@ void LightAnimate(void) Light.fade_running = true; Light.fade_duration = 0; // set the value to zero to force a recompute Light.fade_start = 0; + Light.fade_once_enabled = false; // fade will be applied, reset fade_once_enabled // Fade will applied immediately below } } @@ -1847,7 +1864,8 @@ bool LightApplyFade(void) { // did the value chanegd and needs to be applied // compute the duration of the animation // Note: Settings.light_speed is the number of half-seconds for a 100% fade, // i.e. light_speed=1 means 1024 steps in 500ms - Light.fade_duration = Settings.light_speed * 500; + Light.fade_duration = LightGetSpeedSetting() * 500; + Light.speed_once_enabled = false; // The once off speed value has been read, reset it if (!Settings.flag5.fade_fixed_duration) { Light.fade_duration = (distance * Light.fade_duration) / 1023; // time is proportional to distance, except with SO117 } @@ -2809,6 +2827,17 @@ void CmndRgbwwTable(void) void CmndFade(void) { + if (XdrvMailbox.index == 2) { + switch (XdrvMailbox.payload) { + case 0: // Off + case 1: // On + Light.fade_once_enabled = true; + Light.fade_once_value = XdrvMailbox.payload; + break; + } + return; + } + // Fade - Show current Fade state // Fade 0 - Turn Fade Off // Fade On - Turn Fade On @@ -2833,6 +2862,14 @@ void CmndFade(void) void CmndSpeed(void) { + if (XdrvMailbox.index == 2) { + if ((XdrvMailbox.payload > 0) && (XdrvMailbox.payload <= 40)) { + Light.speed_once_enabled = true; + Light.speed_once_value = XdrvMailbox.payload; + } + return; + } + // Speed 1 - Fast // Speed 40 - Very slow // Speed + - Increment Speed