From e21cbfdc5d96a9acd3943b30a53d5f7492a13b7b Mon Sep 17 00:00:00 2001 From: George Date: Wed, 20 May 2020 11:25:32 +1000 Subject: [PATCH] More sensible behaviour * When setting PWM values, updates all the LEDs (instant response). Uses led_power values. * If LEDLINK not set, but LED1 is, LED1 is the status led. When turning on/off, setledlink uses digitalwrite (which does not respect the new pwm operation). In this case only, we will use the setledpoweridx instead of digitalwrite - costly (every 250ms this runs), but edge case / legacy. Allows more intuitive operation - if we blink an LED with the max and min PWM limits, we'd expect it to respect these. In this case, blink will also now update the led_power status, which keeps this accurate e.g. if ledpower 1 cmnd was sent, then blink occurred, led_state would read a 1 for that bit but the led would be off (but nothing was reading it for status so it didn't cause any trouble). Leaving digitalwrite when LEDLINK is defined as this is more efficient and the use case for pwm leds is to find buttons - link indicator would become more ambiguous for no benefit. --- tasmota/support_command.ino | 2 ++ tasmota/support_tasmota.ino | 13 +++++++++---- tasmota/tasmota.ino | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index 263f5f713..314097235 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -1901,6 +1901,7 @@ void CmndSetLedPwmOff(void) } else { Settings.ledpwm_off = XdrvMailbox.payload; } + UpdateLedPowerAll(); } ResponseCmndNumber(Settings.ledpwm_off); } @@ -1915,6 +1916,7 @@ void CmndSetLedPwmOn(void) } else { Settings.ledpwm_on = XdrvMailbox.payload; } + UpdateLedPowerAll(); } ResponseCmndNumber(Settings.ledpwm_on); } diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index ba4b0fb1d..5a28b76d6 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -336,6 +336,13 @@ void SetPowerOnState(void) blink_powersave = power; } +void UpdateLedPowerAll() +{ + for (uint32_t i = 0; i < leds_present; i++) { + SetLedPowerIdx(i, bitRead(led_power, i)); + } +} + void SetLedPowerIdx(uint32_t led, uint32_t state) { if (!PinUsed(GPIO_LEDLNK) && (0 == led)) { // Legacy - LED1 is link led only if LED2 is present @@ -400,10 +407,8 @@ void SetLedLink(uint32_t state) uint32_t led_pin = Pin(GPIO_LEDLNK); uint32_t led_inv = ledlnk_inverted; if (99 == led_pin) { // Legacy - LED1 is status - led_pin = Pin(GPIO_LED1); - led_inv = bitRead(led_inverted, 0); - } - if (led_pin < 99) { + SetLedPowerIdx(0, state); + } else if (led_pin < 99) { if (state) { state = 1; } digitalWrite(led_pin, (led_inv) ? !state : state); } diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index ecf1127ec..92e3169b2 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -1,4 +1,4 @@ -/* + /* tasmota.ino - Tasmota firmware for iTead Sonoff, Wemos and NodeMCU hardware Copyright (C) 2020 Theo Arends