From a305f435e80934f3cc56b9094eec6ff4b45f1cbb Mon Sep 17 00:00:00 2001 From: George Date: Tue, 19 May 2020 15:15:39 +1000 Subject: [PATCH] First pass at led pwm settings * Added settings for ledpwm_on and ledpwm_off with defaults that mimic current digitalwrite function * Changed ledpoweridx from digitalwrite to analogwrite * Add commands to change new settings --- tasmota/i18n.h | 4 ++++ tasmota/settings.h | 4 +++- tasmota/settings.ino | 9 +++++++++ tasmota/support_command.ino | 28 ++++++++++++++++++++++++++-- tasmota/support_tasmota.ino | 8 +++++++- 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tasmota/i18n.h b/tasmota/i18n.h index e8286e585..18c073d0d 100644 --- a/tasmota/i18n.h +++ b/tasmota/i18n.h @@ -586,6 +586,10 @@ // Commands xsns_02_analog.ino #define D_CMND_ADCPARAM "AdcParam" +// Commands led pwm settings +#define D_CMND_SETLEDPWMOFF "SetLedPwmOff" +#define D_CMND_SETLEDPWMON "SetLedPwmOn" + /********************************************************************************************/ // Log message prefix diff --git a/tasmota/settings.h b/tasmota/settings.h index edd3de523..11546fea3 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -569,8 +569,10 @@ struct { uint16_t windmeter_pulse_debounce; // F3A int16_t windmeter_speed_factor; // F3C uint8_t windmeter_tele_pchange; // F3E + uint16_t ledpwm_on; // F3F + uint16_t ledpwm_off; // F41 - uint8_t free_f3f[121]; // F3F - Decrement if adding new Setting variables just above and below + uint8_t free_f42[117]; // F42 - Decrement if adding new Setting variables just above and below // Only 32 bit boundary variables below uint16_t pulse_counter_debounce_low; // FB8 diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 5214add7a..7a6a27ac3 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -1055,6 +1055,10 @@ void SettingsDefaultSet2(void) Settings.flag2 = flag2; Settings.flag3 = flag3; Settings.flag4 = flag4; + + // Led PWM + Settings.ledpwm_off = 0; + Settings.ledpwm_on = 1023; } /********************************************************************************************/ @@ -1419,4 +1423,9 @@ void SettingsDelta(void) Settings.version = VERSION; SettingsSave(1); } + // ledpwm + if (Settings.version < 0x080300002) { + Settings.ledpwm_off = 0; + Settings.ledpwm_on = 1023; + } } diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index f262b5fcb..5adcc708f 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -27,7 +27,7 @@ const char kTasmotaCommands[] PROGMEM = "|" // No prefix D_CMND_SERIALDELIMITER "|" D_CMND_IPADDRESS "|" D_CMND_NTPSERVER "|" D_CMND_AP "|" D_CMND_SSID "|" D_CMND_PASSWORD "|" D_CMND_HOSTNAME "|" D_CMND_WIFICONFIG "|" D_CMND_DEVICENAME "|" D_CMND_FRIENDLYNAME "|" D_CMND_SWITCHMODE "|" D_CMND_INTERLOCK "|" D_CMND_TELEPERIOD "|" D_CMND_RESET "|" D_CMND_TIME "|" D_CMND_TIMEZONE "|" D_CMND_TIMESTD "|" D_CMND_TIMEDST "|" D_CMND_ALTITUDE "|" D_CMND_LEDPOWER "|" D_CMND_LEDSTATE "|" D_CMND_LEDMASK "|" D_CMND_WIFIPOWER "|" D_CMND_TEMPOFFSET "|" D_CMND_HUMOFFSET "|" - D_CMND_SPEEDUNIT "|" D_CMND_GLOBAL_TEMP "|" D_CMND_GLOBAL_HUM "|" + D_CMND_SPEEDUNIT "|" D_CMND_GLOBAL_TEMP "|" D_CMND_GLOBAL_HUM "|" D_CMND_SETLEDPWMON "|" D_CMND_SETLEDPWMOFF "|" #ifdef USE_I2C D_CMND_I2CSCAN "|" D_CMND_I2CDRIVER "|" #endif @@ -50,7 +50,7 @@ void (* const TasmotaCommand[])(void) PROGMEM = { &CmndSerialDelimiter, &CmndIpAddress, &CmndNtpServer, &CmndAp, &CmndSsid, &CmndPassword, &CmndHostname, &CmndWifiConfig, &CmndDevicename, &CmndFriendlyname, &CmndSwitchMode, &CmndInterlock, &CmndTeleperiod, &CmndReset, &CmndTime, &CmndTimezone, &CmndTimeStd, &CmndTimeDst, &CmndAltitude, &CmndLedPower, &CmndLedState, &CmndLedMask, &CmndWifiPower, &CmndTempOffset, &CmndHumOffset, - &CmndSpeedUnit, &CmndGlobalTemp, &CmndGlobalHum, + &CmndSpeedUnit, &CmndGlobalTemp, &CmndGlobalHum, &CmndSetLedPwmOn, &CmndSetLedPwmOff, #ifdef USE_I2C &CmndI2cScan, CmndI2cDriver, #endif @@ -1890,3 +1890,27 @@ void CmndDriver(void) { XdrvCall(FUNC_COMMAND_DRIVER); } + +void CmndSetLedPwmOff(void) +{ + if ((XdrvMailbox.payload < 0) { + Settings.ledpwm_off = 0; + } else if (XdrvMailbox.payload > Settings.pwm_range) { + Settings.ledpwm_off = Settings.pwm_range; + } else { + Settings.ledpwm_off = XdrvMailbox.payload; + } + ResponseCmndNumber(Settings.ledpwm_off); +} + +void CmndSetLedPwmOn(void) +{ + if ((XdrvMailbox.payload < 0) { + Settings.ledpwm_on = 0; + } else if (XdrvMailbox.payload > Settings.pwm_range) { + Settings.ledpwm_on = Settings.pwm_range; + } else { + Settings.ledpwm_on = XdrvMailbox.payload; + } + ResponseCmndNumber(Settings.ledpwm_on); +} \ No newline at end of file diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 90df92a37..65d842686 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -351,7 +351,13 @@ void SetLedPowerIdx(uint32_t led, uint32_t state) } else { led_power &= (0xFF ^ mask); } - DigitalWrite(GPIO_LED1, led, bitRead(led_inverted, led) ? !state : state); + uint16_t led_pwm_set = 0; + if (bitRead(led_inverted, led)) { + led_pwm_set = state ? Settings.pwm_range - Settings.ledpwm_on : Settings.pwm_range - Settings.ledpwm_off; + } else { + led_pwm_set = state ? Settings.ledpwm_on : Settings.ledpwm_off; + } + analogWrite(led, led_pwm_set) } #ifdef USE_BUZZER if (led == 0) {