2020-02-24 13:19:15 +00:00
|
|
|
/*
|
|
|
|
xdrv_35_pwm_dimmer.ino - PWM Dimmer Switch support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Paul C Diem
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_PWM_DIMMER
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Support for Martin Jerry/acenx/Tessan/NTONPOWER SD0x PWM dimmer switches. The brightness of
|
|
|
|
* the load for these dimmers is controlled by a PWM GPIO pin. Examples are:
|
|
|
|
*
|
|
|
|
* https://www.amazon.com/dp/B07FXYSVR1
|
|
|
|
* https://www.amazon.com/dp/B07V26Q3VD
|
|
|
|
* https://www.amazon.com/dp/B07K67D43J
|
|
|
|
* https://www.amazon.com/dp/B07TTGFWFM
|
|
|
|
*
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XDRV_35 35
|
2020-10-20 02:12:41 +01:00
|
|
|
#define MAX_PWM_DIMMER_KEYS 3
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
const char kPWMDimmerCommands[] PROGMEM = "|" // No prefix
|
2020-10-02 18:40:45 +01:00
|
|
|
D_CMND_BRI_PRESET
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
"|" D_CMND_PWM_DIMMER_PWMS
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
;
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
void (* const PWMDimmerCommand[])(void) PROGMEM = {
|
2020-10-02 18:40:45 +01:00
|
|
|
&CmndBriPreset,
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
&CmndPWMDimmerPWMs,
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
};
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
struct remote_pwm_dimmer {
|
2020-04-19 06:36:26 +01:00
|
|
|
bool power_on;
|
2020-02-27 03:27:09 +00:00
|
|
|
uint8_t bri_power_on;
|
|
|
|
uint8_t bri_preset_low;
|
|
|
|
uint8_t bri_preset_high;
|
2020-02-24 13:19:15 +00:00
|
|
|
uint8_t fixed_color_index;
|
|
|
|
uint8_t bri;
|
|
|
|
bool power_button_increases_bri;
|
|
|
|
};
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
uint32_t ignore_any_key_time = 0;
|
2020-02-24 13:19:15 +00:00
|
|
|
uint32_t button_hold_time[3];
|
2020-04-03 17:49:01 +01:00
|
|
|
uint8_t led_timeout_seconds = 0;
|
|
|
|
uint8_t restore_powered_off_led_counter = 0;
|
2020-02-24 13:19:15 +00:00
|
|
|
uint8_t power_button_index = 0;
|
2020-10-20 02:12:41 +01:00
|
|
|
uint8_t down_button_index = 1;
|
2020-02-28 05:38:55 +00:00
|
|
|
uint8_t buttons_pressed = 0;
|
2020-10-05 21:15:32 +01:00
|
|
|
uint8_t local_fixed_color_index = 128;
|
|
|
|
bool button_tapped = false;
|
2020-03-13 17:08:44 +00:00
|
|
|
bool down_button_tapped = false;
|
2020-10-20 02:12:41 +01:00
|
|
|
bool ignore_power_button = false;
|
|
|
|
bool multibutton_in_progress = false;
|
2020-02-24 13:19:15 +00:00
|
|
|
bool power_button_increases_bri = true;
|
2020-10-20 02:12:41 +01:00
|
|
|
bool tap_handled = false;
|
2020-02-24 13:19:15 +00:00
|
|
|
bool invert_power_button_bri_direction = false;
|
|
|
|
bool button_pressed[3] = { false, false, false };
|
2020-10-20 02:12:41 +01:00
|
|
|
bool button_held[3];
|
2020-12-12 19:32:14 +00:00
|
|
|
bool button_unprocessed[3] = { false, false, false };
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
struct remote_pwm_dimmer remote_pwm_dimmers[MAX_PWM_DIMMER_KEYS];
|
2020-02-24 13:19:15 +00:00
|
|
|
struct remote_pwm_dimmer * active_remote_pwm_dimmer;
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
void PWMModulePreInit(void)
|
2020-02-24 13:19:15 +00:00
|
|
|
{
|
|
|
|
Settings.seriallog_level = 0;
|
|
|
|
Settings.flag.mqtt_serial = 0; // Disable serial logging
|
|
|
|
Settings.ledstate = 0; // Disable LED usage
|
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
// If the module was just changed to PWM Dimmer, set the defaults.
|
2020-11-04 14:57:40 +00:00
|
|
|
if (TasmotaGlobal.module_changed) {
|
2020-04-03 17:49:01 +01:00
|
|
|
Settings.flag.pwm_control = true; // SetOption15 - Switch between commands PWM or COLOR/DIMMER/CT/CHANNEL
|
|
|
|
Settings.bri_power_on = Settings.bri_preset_low = Settings.bri_preset_high = 0;
|
2020-11-07 23:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Previous versions of PWM Dimmer used SetOption32 - Button held for factor times longer as the
|
|
|
|
// hold time. The hold time is now fixed and SetOption32 is used as normal including to
|
|
|
|
// determine how long a button is held before a reset command is executed. If SetOption32 is
|
|
|
|
// still 5, change it to 40 (the default).
|
|
|
|
if (Settings.param[P_HOLD_TIME] == 5) Settings.param[P_HOLD_TIME] = 40;
|
2021-01-01 12:44:04 +00:00
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
// Make sure the brightness level settings are sensible.
|
2020-03-14 23:53:09 +00:00
|
|
|
if (!Settings.bri_power_on) Settings.bri_power_on = 128;
|
|
|
|
if (!Settings.bri_preset_low) Settings.bri_preset_low = 10;
|
|
|
|
if (Settings.bri_preset_high < Settings.bri_preset_low) Settings.bri_preset_high = 255;
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
PWMDimmerSetPoweredOffLed();
|
2020-03-14 23:53:09 +00:00
|
|
|
|
|
|
|
// The relay initializes to on. If the power is supposed to be off, turn the relay off.
|
2020-10-28 18:03:39 +00:00
|
|
|
// if (!TasmotaGlobal.power && PinUsed(GPIO_REL1)) digitalWrite(Pin(GPIO_REL1), bitRead(TasmotaGlobal.rel_inverted, 0) ? 1 : 0);
|
2020-03-14 23:53:09 +00:00
|
|
|
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
// If remote device mode is enabled, set the device group count to the number of buttons
|
|
|
|
// present.
|
2020-08-09 16:16:15 +01:00
|
|
|
if (Settings.flag4.multiple_device_groups) {
|
2020-03-14 23:53:09 +00:00
|
|
|
Settings.flag4.device_groups_enabled = true;
|
|
|
|
|
2020-04-06 18:29:50 +01:00
|
|
|
device_group_count = 0;
|
2020-10-20 02:12:41 +01:00
|
|
|
for (uint32_t button_index = 0; button_index < MAX_PWM_DIMMER_KEYS; button_index++) {
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_KEY1, button_index)) device_group_count++;
|
2020-03-14 23:53:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If no relay or PWM is defined, all buttons control remote devices.
|
|
|
|
if (!PinUsed(GPIO_REL1) && !PinUsed(GPIO_PWM1)) {
|
|
|
|
first_device_group_is_local = false;
|
|
|
|
|
|
|
|
// Back out the changes made in the light module under the assumtion we have a relay or PWM.
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.devices_present--;
|
|
|
|
TasmotaGlobal.light_type = 0;
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < MAX_PWM_DIMMER_KEYS; i++) {
|
|
|
|
active_remote_pwm_dimmer = &remote_pwm_dimmers[i];
|
|
|
|
active_remote_pwm_dimmer->bri_power_on = 128;
|
|
|
|
active_remote_pwm_dimmer->bri_preset_low = 10;
|
|
|
|
active_remote_pwm_dimmer->bri_preset_high = 255;
|
|
|
|
active_remote_pwm_dimmer->fixed_color_index = 128;
|
2020-03-14 23:53:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:15:32 +01:00
|
|
|
// bri: -1 = set to current light bri, -2 = timeout, 0-255 = set to bri
|
|
|
|
void PWMDimmerSetBrightnessLeds(int32_t bri)
|
2020-02-24 13:19:15 +00:00
|
|
|
{
|
2020-10-20 02:12:41 +01:00
|
|
|
// Find out how many of the LEDs have their ledmask bit set.
|
|
|
|
uint32_t leds = 0;
|
|
|
|
uint32_t mask = 1;
|
|
|
|
int32_t led;
|
2020-10-30 11:29:48 +00:00
|
|
|
for (led = 0; led < TasmotaGlobal.leds_present; led++) {
|
2020-10-20 02:12:41 +01:00
|
|
|
if (Settings.ledmask & mask) leds++;
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we found at least one LED, get the brightness to show and calculate the brightness level
|
|
|
|
// difference between each LED.
|
|
|
|
if (leds) {
|
2020-10-05 21:15:32 +01:00
|
|
|
led_timeout_seconds = 5;
|
|
|
|
if (bri < 0) {
|
|
|
|
bri = ((bri == -2 && Settings.flag4.led_timeout) || !Light.power ? 0 : light_state.getBri());
|
|
|
|
if (!bri || !Settings.flag4.led_timeout) led_timeout_seconds = 0;
|
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
uint32_t step = 256 / (leds + 1);
|
|
|
|
|
|
|
|
// Turn the LED's on/off.
|
|
|
|
uint32_t level = 0;
|
|
|
|
led = -1;
|
|
|
|
mask = 0;
|
|
|
|
for (uint32_t count = 0; count < leds; count++) {
|
2020-02-24 13:19:15 +00:00
|
|
|
level += step;
|
2020-10-20 02:12:41 +01:00
|
|
|
for (;;) {
|
|
|
|
led++;
|
|
|
|
mask <<= 1;
|
|
|
|
if (!mask) mask = 1;
|
|
|
|
if (Settings.ledmask & mask) break;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
SetLedPowerIdx(led, bri >= level);
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PWMDimmerSetPoweredOffLed(void)
|
|
|
|
{
|
|
|
|
// Set the powered-off LED state.
|
2020-04-27 11:54:07 +01:00
|
|
|
if (PinUsed(GPIO_LEDLNK)) {
|
2020-10-28 18:03:39 +00:00
|
|
|
bool power_off_led_on = !TasmotaGlobal.power && Settings.flag4.powered_off_led;
|
2020-10-30 11:29:48 +00:00
|
|
|
if (TasmotaGlobal.ledlnk_inverted) power_off_led_on ^= 1;
|
2020-04-26 16:33:27 +01:00
|
|
|
digitalWrite(Pin(GPIO_LEDLNK), power_off_led_on);
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:08:44 +00:00
|
|
|
void PWMDimmerSetPower(void)
|
2020-02-24 13:19:15 +00:00
|
|
|
{
|
2020-10-28 18:03:39 +00:00
|
|
|
DigitalWrite(GPIO_REL1, 0, bitRead(TasmotaGlobal.rel_inverted, 0) ? !TasmotaGlobal.power : TasmotaGlobal.power);
|
2020-10-05 21:15:32 +01:00
|
|
|
PWMDimmerSetBrightnessLeds(-1);
|
2020-03-13 17:08:44 +00:00
|
|
|
PWMDimmerSetPoweredOffLed();
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-04-06 18:29:50 +01:00
|
|
|
void PWMDimmerHandleDevGroupItem(void)
|
2020-02-24 13:19:15 +00:00
|
|
|
{
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-04-06 18:29:50 +01:00
|
|
|
uint8_t device_group_index = *(uint8_t *)XdrvMailbox.topic;
|
2020-10-20 02:12:41 +01:00
|
|
|
bool is_local = ((XdrvMailbox.index & DGR_FLAG_LOCAL) != 0);
|
|
|
|
if (device_group_index > MAX_PWM_DIMMER_KEYS) return;
|
|
|
|
struct remote_pwm_dimmer * remote_pwm_dimmer = &remote_pwm_dimmers[device_group_index];
|
2020-04-07 19:25:58 +01:00
|
|
|
#else // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (!(XdrvMailbox.index & DGR_FLAG_LOCAL)) return;
|
2020-04-07 19:25:58 +01:00
|
|
|
#endif // !USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
uint32_t value = XdrvMailbox.payload;
|
2020-02-28 02:41:29 +00:00
|
|
|
|
2020-02-24 13:19:15 +00:00
|
|
|
switch (XdrvMailbox.command_code) {
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-03-13 17:08:44 +00:00
|
|
|
case DGR_ITEM_LIGHT_BRI:
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->bri = value;
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
2020-04-03 17:49:01 +01:00
|
|
|
case DGR_ITEM_POWER:
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->power_on = value & 1;
|
|
|
|
remote_pwm_dimmer->power_button_increases_bri = (remote_pwm_dimmer->bri < 128);
|
2020-04-03 17:49:01 +01:00
|
|
|
break;
|
2020-10-07 21:06:50 +01:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-28 02:41:29 +00:00
|
|
|
case DGR_ITEM_LIGHT_FIXED_COLOR:
|
2020-10-07 21:06:50 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->fixed_color_index = value;
|
|
|
|
if (is_local)
|
2020-02-28 02:41:29 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-07 21:06:50 +01:00
|
|
|
local_fixed_color_index = value;
|
|
|
|
break;
|
2020-03-13 17:08:44 +00:00
|
|
|
case DGR_ITEM_BRI_POWER_ON:
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->bri_power_on = value;
|
|
|
|
if (is_local)
|
2020-02-28 02:41:29 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-03-13 17:08:44 +00:00
|
|
|
Settings.bri_power_on = value;
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
case DGR_ITEM_BRI_PRESET_LOW:
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->bri_preset_low = value;
|
|
|
|
if (is_local)
|
2020-02-28 02:41:29 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-24 13:19:15 +00:00
|
|
|
Settings.bri_preset_low = value;
|
|
|
|
break;
|
|
|
|
case DGR_ITEM_BRI_PRESET_HIGH:
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
remote_pwm_dimmer->bri_preset_high = value;
|
|
|
|
if (is_local)
|
2020-02-28 02:41:29 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-24 13:19:15 +00:00
|
|
|
Settings.bri_preset_high = value;
|
|
|
|
break;
|
|
|
|
case DGR_ITEM_STATUS:
|
2020-02-28 02:41:29 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (is_local)
|
2020-02-28 02:41:29 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-03-13 17:08:44 +00:00
|
|
|
SendLocalDeviceGroupMessage(DGR_MSGTYP_UPDATE, DGR_ITEM_BRI_POWER_ON, Settings.bri_power_on,
|
2020-10-20 02:12:41 +01:00
|
|
|
DGR_ITEM_BRI_PRESET_LOW, Settings.bri_preset_low, DGR_ITEM_BRI_PRESET_HIGH, Settings.bri_preset_high);
|
2021-01-22 13:53:10 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
else
|
|
|
|
SendDeviceGroupMessage(device_group_index, DGR_MSGTYP_UPDATE, DGR_ITEM_POWER, remote_pwm_dimmer->power_on,
|
|
|
|
DGR_ITEM_BRI_POWER_ON, remote_pwm_dimmer->bri_power_on, DGR_ITEM_BRI_PRESET_LOW, remote_pwm_dimmer->bri_preset_low,
|
|
|
|
DGR_ITEM_BRI_PRESET_HIGH, remote_pwm_dimmer->bri_preset_high);
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
void PWMDimmerHandleButton(uint32_t button_index, bool pressed)
|
2020-02-24 13:19:15 +00:00
|
|
|
{
|
2020-10-05 21:15:32 +01:00
|
|
|
bool handle_tap = false;
|
2020-03-15 18:56:26 +00:00
|
|
|
bool state_updated = false;
|
2020-04-24 15:12:43 +01:00
|
|
|
int32_t bri_offset = 0;
|
2020-03-13 17:08:44 +00:00
|
|
|
uint8_t power_on_bri = 0;
|
2020-02-24 13:19:15 +00:00
|
|
|
uint8_t dgr_item = 0;
|
2020-10-02 04:30:37 +01:00
|
|
|
uint8_t dgr_value = 0;
|
2020-10-05 21:15:32 +01:00
|
|
|
uint8_t dgr_more_to_come = true;
|
|
|
|
uint8_t mqtt_trigger = 0;
|
2020-04-26 16:33:27 +01:00
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
// Initialize some variables.
|
2020-03-13 17:08:44 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-28 18:03:39 +00:00
|
|
|
bool power_is_on = (active_remote_pwm_dimmer ? active_remote_pwm_dimmer->power_on : TasmotaGlobal.power);
|
2020-04-03 17:49:01 +01:00
|
|
|
bool is_power_button = (button_index == power_button_index);
|
2020-10-20 02:12:41 +01:00
|
|
|
bool is_down_button = (button_index == down_button_index);
|
2020-03-13 17:08:44 +00:00
|
|
|
#else // USE_PWM_DIMMER_REMOTE
|
2020-10-28 18:03:39 +00:00
|
|
|
bool power_is_on = TasmotaGlobal.power;
|
2020-04-03 17:49:01 +01:00
|
|
|
bool is_power_button = !button_index;
|
2020-10-05 21:15:32 +01:00
|
|
|
bool is_down_button = (button_index == (power_button_index ? 0 : 1));
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the button is being held, ...
|
|
|
|
if (pressed) {
|
|
|
|
uint32_t now = millis();
|
2020-04-03 17:49:01 +01:00
|
|
|
|
2020-12-12 19:32:14 +00:00
|
|
|
// If the button was pressed and released but was not processed by support_button because the
|
2021-01-01 12:44:04 +00:00
|
|
|
// button interval had not elapsed,
|
2020-12-12 19:32:14 +00:00
|
|
|
if (button_unprocessed[button_index]) {
|
|
|
|
mqtt_trigger = 5;
|
2020-12-12 21:09:49 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-12-12 19:32:14 +00:00
|
|
|
if (!active_remote_pwm_dimmer) mqtt_trigger += button_index;
|
2020-12-12 21:09:49 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-12-12 19:32:14 +00:00
|
|
|
button_hold_time[button_index] = now + 750;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if this is about the power button, ...
|
|
|
|
else if (is_power_button) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If we're not ignoring the power button and no other buttons are pressed, ...
|
|
|
|
if (!ignore_power_button && buttons_pressed == 1) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power is on, adjust the brightness. Set the direction based on the current
|
|
|
|
// direction for the device and then invert the direction when the power button is released.
|
|
|
|
// The new brightness will be calculated below.
|
|
|
|
if (power_is_on) {
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
bri_offset = (active_remote_pwm_dimmer ? (active_remote_pwm_dimmer->power_button_increases_bri ? 1 : -1) : (power_button_increases_bri ? 1 : -1));
|
2020-02-24 13:19:15 +00:00
|
|
|
#else // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
bri_offset = (power_button_increases_bri ? 1 : -1);
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
invert_power_button_bri_direction = true;
|
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power is not on, turn it on using an initial brightness of bri_preset_low and set
|
|
|
|
// the power button hold time to delay before we start increasing the brightness.
|
|
|
|
else {
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
|
|
|
power_on_bri = active_remote_pwm_dimmer->bri = active_remote_pwm_dimmer->bri_preset_low;
|
|
|
|
else
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
power_on_bri = Settings.bri_preset_low;
|
|
|
|
button_hold_time[button_index] = now + 500;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If this is about the down or up buttons, ...
|
|
|
|
else {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power button is also pressed, ...
|
|
|
|
if (button_pressed[power_button_index]) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the up or down button was tapped while holding the power button before this, handle
|
|
|
|
// the operation below.
|
|
|
|
if (button_tapped) {
|
2020-12-12 19:32:14 +00:00
|
|
|
handle_tap = ignore_power_button = true;
|
|
|
|
button_hold_time[button_index] = now + 750;
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
2020-03-13 17:08:44 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Otherwise, if the power is on and remote mode is enabled, adjust the brightness. Set the
|
|
|
|
// direction based on which button is pressed. The new brightness will be calculated below.
|
|
|
|
else if (power_is_on && Settings.flag4.multiple_device_groups) {
|
|
|
|
bri_offset = (is_down_button ? -1 : 1);
|
|
|
|
}
|
2020-03-13 17:08:44 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Otherwise, publish MQTT Event Trigger#.
|
|
|
|
else {
|
|
|
|
mqtt_trigger = (is_down_button ? 1 : 2);
|
|
|
|
button_hold_time[button_index] = now + 60000;
|
|
|
|
ignore_power_button = true;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
2020-03-13 17:08:44 +00:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
|
|
|
|
// Otherwise, if the power is on, adjust the brightness. Set the direction based on which
|
|
|
|
// button is pressed. The new brightness will be calculated below.
|
|
|
|
else if (power_is_on && !button_tapped) {
|
|
|
|
bri_offset = (is_down_button ? -1 : 1);
|
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the button was just released, ...
|
2020-03-13 17:08:44 +00:00
|
|
|
else {
|
2020-10-20 02:12:41 +01:00
|
|
|
bool button_was_held = button_held[button_index];
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If this is about the power button, ...
|
|
|
|
if (is_power_button) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the up or down button was tapped while the power button was held and the up or down
|
|
|
|
// buttons weren't tapped or held afterwards, handle the operation based on which button was
|
|
|
|
// tapped.
|
|
|
|
if (button_tapped) {
|
|
|
|
if (!tap_handled) {
|
2020-10-05 21:15:32 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (!active_remote_pwm_dimmer) {
|
2020-10-05 21:15:32 +01:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Toggle the powered-off LED option.
|
|
|
|
if (down_button_tapped) {
|
|
|
|
Settings.flag4.led_timeout ^= 1;
|
|
|
|
if (Light.power) PWMDimmerSetBrightnessLeds(Settings.flag4.led_timeout ? 0 : -1);
|
|
|
|
}
|
2020-10-05 21:15:32 +01:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Toggle the LED timeout.
|
|
|
|
else {
|
|
|
|
Settings.flag4.powered_off_led ^= 1;
|
|
|
|
PWMDimmerSetPoweredOffLed();
|
2020-10-05 21:15:32 +01:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-05 21:15:32 +01:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-05 21:15:32 +01:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
tap_handled = false;
|
|
|
|
}
|
2020-10-05 21:15:32 +01:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Otherwise, if the power button was held, ...
|
|
|
|
else if (button_was_held) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power button was held with no other buttons pressed, we changed the brightness
|
|
|
|
// so invert the bri direction for the next time and send a final update.
|
|
|
|
if (invert_power_button_bri_direction) {
|
|
|
|
invert_power_button_bri_direction = false;
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
|
|
|
active_remote_pwm_dimmer->power_button_increases_bri ^= 1;
|
|
|
|
else
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
power_button_increases_bri ^= 1;
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
dgr_item = DGR_ITEM_FLAGS;
|
|
|
|
state_updated = true;
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-03-13 17:08:44 +00:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power button was not held and we're not ignoring the next power button release,
|
|
|
|
// toggle the power.
|
|
|
|
else if (!ignore_power_button) {
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
|
|
|
power_on_bri = active_remote_pwm_dimmer->bri_power_on;
|
|
|
|
else
|
2020-03-13 17:08:44 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
power_on_bri = Settings.bri_power_on;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If this is about the up or down buttons, ...
|
|
|
|
else {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power button is also pressed, set the flag to ignore the power button until it is
|
|
|
|
// released.
|
|
|
|
if (button_pressed[power_button_index]) {
|
|
|
|
ignore_power_button = true;
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the button was tapped, handle it below.
|
|
|
|
if (button_tapped) {
|
|
|
|
handle_tap = true;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// Otherwise, if the button was not held, flag the tap.
|
|
|
|
else if (!button_was_held) {
|
|
|
|
button_tapped = true;
|
|
|
|
down_button_tapped = is_down_button;
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 17:49:01 +01:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power button is not also pressed, ...
|
|
|
|
else {
|
2020-04-26 16:33:27 +01:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the power is on, ...
|
|
|
|
if (power_is_on) {
|
2020-02-24 13:19:15 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the button was not held, adjust the brightness. Set the direction based on which
|
|
|
|
// button is pressed. The new brightness will be calculated below.
|
|
|
|
if (!button_was_held) {
|
|
|
|
bri_offset = (is_down_button ? -5 : 5);
|
|
|
|
dgr_more_to_come = false;
|
|
|
|
state_updated = true;
|
2020-03-13 17:08:44 +00:00
|
|
|
}
|
2020-02-28 03:35:13 +00:00
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the button was held, we changed the brightness and sent updates with the
|
|
|
|
// more-to-come message type while the button was held. Send a final update.
|
2020-04-03 17:49:01 +01:00
|
|
|
else {
|
2020-10-20 02:12:41 +01:00
|
|
|
dgr_item = DGR_ITEM_FLAGS;
|
|
|
|
state_updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the power is off, turn it on using a temporary brightness of bri_preset_low if the
|
|
|
|
// down button is pressed or bri_preset_low if the up button is pressed.
|
|
|
|
else {
|
2020-03-13 17:08:44 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
|
|
|
power_on_bri = active_remote_pwm_dimmer->bri = (is_down_button ? active_remote_pwm_dimmer->bri_preset_low : active_remote_pwm_dimmer->bri_preset_high);
|
|
|
|
else
|
2020-03-13 17:08:44 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
power_on_bri = (is_down_button ? Settings.bri_preset_low : Settings.bri_preset_high);
|
2020-03-13 17:08:44 +00:00
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
2020-04-03 17:49:01 +01:00
|
|
|
}
|
2020-02-28 05:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
// If we need to adjust the brightness, do it.
|
|
|
|
if (bri_offset) {
|
|
|
|
int32_t bri;
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
2020-04-03 17:49:01 +01:00
|
|
|
bri = active_remote_pwm_dimmer->bri;
|
|
|
|
else
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
bri = light_state.getBri();
|
2020-11-07 23:37:06 +00:00
|
|
|
int32_t new_bri = bri + bri_offset * (Settings.light_correction ? 4 : bri / 16 + 1);
|
2020-04-24 15:12:43 +01:00
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
if (bri_offset > 0) {
|
|
|
|
if (new_bri > 255) new_bri = 255;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (new_bri < 1) new_bri = 1;
|
|
|
|
}
|
|
|
|
if (new_bri != bri) {
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-11-07 23:37:06 +00:00
|
|
|
SendDeviceGroupMessage(power_button_index, (dgr_more_to_come ? DGR_MSGTYP_UPDATE_MORE_TO_COME : DGR_MSGTYP_UPDATE_DIRECT), DGR_ITEM_LIGHT_BRI, new_bri);
|
2020-04-03 17:49:01 +01:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer) {
|
2020-04-03 17:49:01 +01:00
|
|
|
active_remote_pwm_dimmer->bri_power_on = active_remote_pwm_dimmer->bri = new_bri;
|
2020-10-05 21:15:32 +01:00
|
|
|
PWMDimmerSetBrightnessLeds(new_bri);
|
|
|
|
}
|
2020-04-03 17:49:01 +01:00
|
|
|
else {
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.skip_light_fade = true;
|
2020-04-22 03:56:26 +01:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
ignore_dgr_sends = true;
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-04-03 17:49:01 +01:00
|
|
|
light_state.setBri(new_bri);
|
2020-11-07 23:37:06 +00:00
|
|
|
Settings.light_dimmer = light_state.BriToDimmer(new_bri);
|
2020-04-03 17:49:01 +01:00
|
|
|
LightAnimate();
|
2020-10-30 11:29:48 +00:00
|
|
|
TasmotaGlobal.skip_light_fade = false;
|
2020-04-22 03:56:26 +01:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
ignore_dgr_sends = false;
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-04-03 17:49:01 +01:00
|
|
|
Settings.bri_power_on = new_bri;
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
2020-10-05 21:15:32 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
else if (active_remote_pwm_dimmer)
|
2020-10-05 21:15:32 +01:00
|
|
|
PWMDimmerSetBrightnessLeds(new_bri);
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
else
|
|
|
|
PWMDimmerSetBrightnessLeds(-1);
|
2020-04-03 17:49:01 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:15:32 +01:00
|
|
|
// If we need to toggle the power on, do it.
|
2020-04-03 17:49:01 +01:00
|
|
|
else if (power_on_bri) {
|
2020-02-24 13:19:15 +00:00
|
|
|
power_t new_power;
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer) {
|
2020-04-19 06:36:26 +01:00
|
|
|
active_remote_pwm_dimmer->power_on ^= 1;
|
|
|
|
new_power = active_remote_pwm_dimmer->power_on;
|
2020-10-05 21:15:32 +01:00
|
|
|
PWMDimmerSetBrightnessLeds(new_power ? -power_on_bri : 0);
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-28 18:03:39 +00:00
|
|
|
new_power = TasmotaGlobal.power ^ 1;
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
if (new_power)
|
2020-03-13 17:08:44 +00:00
|
|
|
SendDeviceGroupMessage(power_button_index, DGR_MSGTYP_UPDATE, DGR_ITEM_LIGHT_BRI, power_on_bri, DGR_ITEM_POWER, new_power);
|
2020-02-24 13:19:15 +00:00
|
|
|
else
|
|
|
|
SendDeviceGroupMessage(power_button_index, DGR_MSGTYP_UPDATE, DGR_ITEM_POWER, new_power);
|
2020-03-13 17:08:44 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer)
|
2020-04-03 17:49:01 +01:00
|
|
|
active_remote_pwm_dimmer->power_button_increases_bri = (power_on_bri < 128);
|
|
|
|
else {
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-03-13 17:08:44 +00:00
|
|
|
light_state.setBri(power_on_bri);
|
2020-11-07 23:37:06 +00:00
|
|
|
Settings.light_dimmer = light_state.BriToDimmer(power_on_bri);
|
2020-10-05 21:58:17 +01:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-10-05 21:15:32 +01:00
|
|
|
Light.devgrp_no_channels_out = true;
|
2020-10-05 21:58:17 +01:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-04-03 17:49:01 +01:00
|
|
|
ExecuteCommandPower(1, POWER_TOGGLE, SRC_RETRY);
|
2020-10-05 21:58:17 +01:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-10-05 21:15:32 +01:00
|
|
|
Light.devgrp_no_channels_out = false;
|
2020-10-05 21:58:17 +01:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-03-13 17:08:44 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 21:15:32 +01:00
|
|
|
// If the up or down button was tapped while holding the power button and the up or down button
|
|
|
|
// is being held or was just released after not being held, handle the operation.
|
|
|
|
if (handle_tap) {
|
2020-12-12 19:32:14 +00:00
|
|
|
tap_handled = true;
|
2020-10-05 21:15:32 +01:00
|
|
|
|
|
|
|
// If the down button was tapped while holding the power button, send a device group update to
|
|
|
|
// select the previous/next fixed color.
|
|
|
|
if (down_button_tapped) {
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
int8_t add_value = (is_down_button ? -1 : 1);
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (active_remote_pwm_dimmer) {
|
2020-10-05 21:15:32 +01:00
|
|
|
active_remote_pwm_dimmer->fixed_color_index += add_value;
|
|
|
|
dgr_value = active_remote_pwm_dimmer->fixed_color_index;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
local_fixed_color_index += add_value;
|
|
|
|
dgr_value = local_fixed_color_index;
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
dgr_item = DGR_ITEM_LIGHT_FIXED_COLOR;
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
;
|
|
|
|
}
|
2020-12-12 19:32:14 +00:00
|
|
|
// If the up button was tapped while holding the power button, publish an MQTT Event Trigger#.
|
2020-10-05 21:15:32 +01:00
|
|
|
else {
|
2020-12-12 19:32:14 +00:00
|
|
|
mqtt_trigger = 3;
|
|
|
|
if (is_down_button) mqtt_trigger = 4;
|
2020-10-05 21:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we need to publish an MQTT trigger, do it.
|
|
|
|
if (mqtt_trigger) {
|
|
|
|
char topic[TOPSZ];
|
2020-10-30 11:29:48 +00:00
|
|
|
sprintf_P(TasmotaGlobal.mqtt_data, PSTR("Trigger%u"), mqtt_trigger);
|
2020-10-05 21:15:32 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2021-01-22 13:53:10 +00:00
|
|
|
if (Settings.flag4.multiple_device_groups) {
|
2020-10-20 02:12:41 +01:00
|
|
|
snprintf_P(topic, sizeof(topic), PSTR("cmnd/%s/EVENT"), device_groups[power_button_index].group_name);
|
2020-10-05 21:15:32 +01:00
|
|
|
MqttPublish(topic);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
MqttPublishPrefixTopic_P(CMND, PSTR("EVENT"));
|
2020-10-05 21:15:32 +01:00
|
|
|
}
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
// If we need to send a device group update, do it.
|
2020-05-21 02:55:02 +01:00
|
|
|
if (dgr_item) {
|
2020-02-24 13:19:15 +00:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-10-05 21:15:32 +01:00
|
|
|
DevGroupMessageType message_type = DGR_MSGTYP_UPDATE_DIRECT;
|
2020-10-20 02:12:41 +01:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-28 18:03:39 +00:00
|
|
|
if (handle_tap && !active_remote_pwm_dimmer)
|
2020-10-20 02:12:41 +01:00
|
|
|
#else // USE_PWM_DIMMER_REMOTE
|
2020-10-28 18:03:39 +00:00
|
|
|
if (handle_tap)
|
2020-10-20 02:12:41 +01:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
message_type = (DevGroupMessageType)(message_type + DGR_MSGTYPFLAG_WITH_LOCAL);
|
2020-10-05 21:15:32 +01:00
|
|
|
SendDeviceGroupMessage(power_button_index, message_type, dgr_item, dgr_value);
|
2020-02-24 13:19:15 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-03-15 18:56:26 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (!active_remote_pwm_dimmer)
|
2020-03-15 18:56:26 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
light_controller.saveSettings();
|
2020-10-06 23:25:13 +01:00
|
|
|
}
|
|
|
|
|
2020-10-20 02:12:41 +01:00
|
|
|
if (state_updated && Settings.flag3.hass_tele_on_power) { // SetOption59 - Send tele/%topic%/STATE in addition to stat/%topic%/RESULT
|
2020-03-15 18:56:26 +00:00
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
if (!active_remote_pwm_dimmer)
|
2020-03-15 18:56:26 +00:00
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
2020-10-20 02:12:41 +01:00
|
|
|
MqttPublishTeleState();
|
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Commands
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void CmndBriPreset(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0) {
|
|
|
|
bool valid = true;
|
|
|
|
uint32_t value;
|
|
|
|
uint8_t parm[2];
|
|
|
|
parm[0] = Settings.bri_preset_low;
|
|
|
|
parm[1] = Settings.bri_preset_high;
|
|
|
|
char * ptr = XdrvMailbox.data;
|
|
|
|
for (uint32_t i = 0; i < 2; i++) {
|
|
|
|
while (*ptr == ' ') ptr++;
|
|
|
|
if (*ptr == '+') {
|
|
|
|
if (parm[i] < 255) parm[i]++;
|
|
|
|
}
|
|
|
|
else if (*ptr == '-') {
|
2020-03-13 17:08:44 +00:00
|
|
|
if (parm[i] > 1) parm[i]--;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = strtoul(ptr, &ptr, 0);
|
2020-03-13 17:08:44 +00:00
|
|
|
if (value < 1 || value > 255) {
|
2020-02-24 13:19:15 +00:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
parm[i] = value;
|
|
|
|
if (*ptr != ',') break;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
if (valid && !*ptr) {
|
|
|
|
if (parm[0] < parm[1]) {
|
|
|
|
Settings.bri_preset_low = parm[0];
|
|
|
|
Settings.bri_preset_high = parm[1];
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
Settings.bri_preset_low = parm[1];
|
|
|
|
Settings.bri_preset_high = parm[0];
|
|
|
|
}
|
2020-02-27 19:38:33 +00:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-02-24 13:19:15 +00:00
|
|
|
SendLocalDeviceGroupMessage(DGR_MSGTYP_UPDATE, DGR_ITEM_BRI_PRESET_LOW, Settings.bri_preset_low, DGR_ITEM_BRI_PRESET_HIGH, Settings.bri_preset_high);
|
2020-02-27 19:38:33 +00:00
|
|
|
#endif // USE_DEVICE_GROUPS
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_BRI_PRESET "\":{\"Low\":%d,\"High\":%d}}"), Settings.bri_preset_low, Settings.bri_preset_high);
|
|
|
|
}
|
|
|
|
|
2020-10-02 18:40:45 +01:00
|
|
|
#ifdef USE_DEVICE_GROUPS
|
|
|
|
void CmndPWMDimmerPWMs(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len > 0 && XdrvMailbox.payload <= 5) {
|
|
|
|
Settings.pwm_dimmer_cfg.pwm_count = XdrvMailbox.payload - 1;
|
2020-10-29 11:21:24 +00:00
|
|
|
TasmotaGlobal.restart_flag = 2;
|
2020-10-02 18:40:45 +01:00
|
|
|
}
|
|
|
|
Response_P(PSTR("{\"" D_CMND_PWM_DIMMER_PWMS "\":%u}"), Settings.pwm_dimmer_cfg.pwm_count + 1);
|
|
|
|
}
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
|
2020-02-24 13:19:15 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Xdrv35(uint8_t function)
|
|
|
|
{
|
|
|
|
bool result = false;
|
|
|
|
|
2020-10-30 11:29:48 +00:00
|
|
|
if (PWM_DIMMER != TasmotaGlobal.module_type) return result;
|
2020-02-24 13:19:15 +00:00
|
|
|
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
2020-10-05 21:15:32 +01:00
|
|
|
// Turn off/restore the brightness LED's if it's time.
|
|
|
|
if (led_timeout_seconds && !--led_timeout_seconds) {
|
|
|
|
PWMDimmerSetBrightnessLeds(-2);
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 17:08:44 +00:00
|
|
|
// The powered-off LED is also the LedLink LED. If we lose the WiFi or MQTT server connection,
|
|
|
|
// the LED will be set to a blinking state and will be turned off when the connection is
|
|
|
|
// restored. If the state is blinking now, set a flag so we know that we need to restore it
|
|
|
|
// when it stops blinking.
|
2020-10-30 11:29:48 +00:00
|
|
|
if (TasmotaGlobal.global_state.data)
|
2020-04-03 17:49:01 +01:00
|
|
|
restore_powered_off_led_counter = 5;
|
|
|
|
else if (restore_powered_off_led_counter) {
|
2020-02-24 13:19:15 +00:00
|
|
|
PWMDimmerSetPoweredOffLed();
|
2020-04-03 17:49:01 +01:00
|
|
|
restore_powered_off_led_counter--;
|
2020-02-24 13:19:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNC_BUTTON_PRESSED:
|
2020-10-20 02:12:41 +01:00
|
|
|
// If the button is pressed or was just released, ...
|
|
|
|
if (!XdrvMailbox.payload || button_pressed[XdrvMailbox.index]) {
|
|
|
|
uint32_t button_index = XdrvMailbox.index;
|
|
|
|
uint32_t now = millis();
|
|
|
|
|
|
|
|
// If the button is pressed, ...
|
|
|
|
if (!XdrvMailbox.payload) {
|
|
|
|
|
|
|
|
// If the button was just pressed, flag the button as pressed, set the hold time and
|
|
|
|
// increment the buttons pressed count.
|
|
|
|
if (!button_pressed[button_index]) {
|
|
|
|
button_pressed[button_index] = true;
|
2020-12-12 19:32:14 +00:00
|
|
|
uint32_t hold_delay = 250;
|
|
|
|
if (button_index == power_button_index) {
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
if (!(active_remote_pwm_dimmer ? active_remote_pwm_dimmer->power_on : TasmotaGlobal.power)) hold_delay = 500;
|
|
|
|
#else // USE_PWM_DIMMER_REMOTE
|
|
|
|
if (!TasmotaGlobal.power) hold_delay = 500;
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
button_hold_time[button_index] = now + hold_delay;
|
2020-10-20 02:12:41 +01:00
|
|
|
buttons_pressed++;
|
|
|
|
if (buttons_pressed > 1) multibutton_in_progress = true;
|
|
|
|
|
|
|
|
#ifdef USE_PWM_DIMMER_REMOTE
|
|
|
|
// If there are no other buttons pressed right now and remote mode is enabled, make the
|
|
|
|
// device associated with this button the device we're going to control. In remote mode,
|
|
|
|
// whichever button is pressed first becomes the power button and any buttons pressed
|
|
|
|
// while it is held affect the device associated with it. The up and down buttons change
|
|
|
|
// depeneding on which button is the current power button and are based on the GPIOs
|
|
|
|
// used on the MJ-SD01 type dimmers.
|
|
|
|
//
|
|
|
|
// Power Down Up
|
|
|
|
// Position GPIO Button GPIO GPIO
|
|
|
|
// -------- ---- ------ ---- ----
|
|
|
|
// Top 0 1 1 0
|
|
|
|
// Middle 1 2 15 0
|
|
|
|
// Bottom 15 3 15 1
|
|
|
|
if (buttons_pressed == 1 && Settings.flag4.multiple_device_groups) {
|
|
|
|
power_button_index = button_index;
|
2020-10-29 12:58:50 +00:00
|
|
|
down_button_index = (Pin(GPIO_KEY1, power_button_index) == 15 ? TasmotaGlobal.gpio_pin[1] : TasmotaGlobal.gpio_pin[15]) - 32;
|
2020-10-20 02:12:41 +01:00
|
|
|
active_remote_pwm_dimmer = nullptr;
|
|
|
|
if (power_button_index || !first_device_group_is_local)
|
|
|
|
active_remote_pwm_dimmer = &remote_pwm_dimmers[power_button_index];
|
|
|
|
}
|
|
|
|
#endif // USE_PWM_DIMMER_REMOTE
|
|
|
|
}
|
|
|
|
|
2020-11-26 16:36:43 +00:00
|
|
|
// If hold time has arrived and a rule is enabled that handles the button hold, handle it.
|
2020-10-20 02:12:41 +01:00
|
|
|
else if (button_hold_time[button_index] <= now) {
|
2020-11-26 16:36:43 +00:00
|
|
|
#ifdef USE_RULES
|
|
|
|
sprintf(TasmotaGlobal.mqtt_data, PSTR("{\"Button%u\":{\"State\":3}}"), button_index + 1);
|
|
|
|
Rules.no_execute = true;
|
|
|
|
if (!XdrvRulesProcess()) {
|
|
|
|
#endif // USE_RULES
|
|
|
|
PWMDimmerHandleButton(button_index, true);
|
|
|
|
button_held[button_index] = true;
|
|
|
|
#ifdef USE_RULES
|
|
|
|
}
|
|
|
|
Rules.no_execute = false;
|
|
|
|
#endif // USE_RULES
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the button was just released, flag the button as released and decrement the buttons
|
|
|
|
// pressed count.
|
|
|
|
else {
|
|
|
|
button_pressed[button_index] = false;
|
|
|
|
buttons_pressed--;
|
|
|
|
|
|
|
|
// If this is a multibutton press or the button was held, handle it.
|
|
|
|
if (multibutton_in_progress || button_held[button_index]) {
|
|
|
|
PWMDimmerHandleButton(button_index, false);
|
|
|
|
|
|
|
|
// Set a timer so FUNC_ANY_KEY ignores the button if support_button winds up sending a
|
|
|
|
// key because of this.
|
|
|
|
ignore_any_key_time = now + 500;
|
2020-12-12 19:32:14 +00:00
|
|
|
|
|
|
|
// If a multi-button operation is in progress or the button was pressed, released and
|
|
|
|
// then held, tell support_button that we've handled it.
|
|
|
|
result = true;
|
|
|
|
Button.press_counter[button_index] = 0;
|
|
|
|
if (buttons_pressed == 0) multibutton_in_progress = false;
|
|
|
|
button_unprocessed[button_index] = false;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
button_unprocessed[button_index] = true;
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the power button was just released, clear the flags associated with it.
|
|
|
|
if (button_index == power_button_index) {
|
|
|
|
if (ignore_power_button) ignore_any_key_time = now + 500;
|
|
|
|
ignore_power_button = false;
|
|
|
|
button_tapped = false;
|
|
|
|
}
|
|
|
|
button_held[button_index] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNC_ANY_KEY:
|
|
|
|
{
|
|
|
|
uint32_t state = (XdrvMailbox.payload >> 8) & 0xFF; // 0 = Off, 1 = On, 2 = Toggle, 3 = Hold, 10,11,12,13 and 14 for Button Multipress
|
|
|
|
if ((state == 2 || state == 10) && ignore_any_key_time < millis()) {
|
2020-12-12 19:32:14 +00:00
|
|
|
uint32_t button_index = (XdrvMailbox.payload & 0xFF) - 1;
|
|
|
|
button_unprocessed[button_index] = false;
|
|
|
|
PWMDimmerHandleButton(button_index, false);
|
2020-10-20 02:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef USE_DEVICE_GROUPS
|
2020-03-12 17:51:54 +00:00
|
|
|
case FUNC_DEVICE_GROUP_ITEM:
|
2020-04-06 18:29:50 +01:00
|
|
|
PWMDimmerHandleDevGroupItem();
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
#endif // USE_DEVICE_GROUPS
|
|
|
|
|
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = DecodeCommand(kPWMDimmerCommands, PWMDimmerCommand);
|
|
|
|
break;
|
|
|
|
|
2020-03-13 17:08:44 +00:00
|
|
|
case FUNC_SET_DEVICE_POWER:
|
|
|
|
// If we're turning the power on, turn the relay and the brightness LEDs on and turn the
|
|
|
|
// powered-off LED off.
|
2020-04-03 17:49:01 +01:00
|
|
|
if (XdrvMailbox.index) {
|
2020-03-13 17:08:44 +00:00
|
|
|
PWMDimmerSetPower();
|
|
|
|
|
2020-04-03 17:49:01 +01:00
|
|
|
// Set the power button hold dimmer direction based on the current brightness.
|
|
|
|
power_button_increases_bri = (light_state.getBri() < 128);
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:08:44 +00:00
|
|
|
// If we're turning the power off, return true so SetDevicePower doesn't turn the relay off.
|
|
|
|
// It will be turned off in LightApplyFade when the fade is done.
|
|
|
|
else
|
|
|
|
result = true;
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FUNC_PRE_INIT:
|
2020-03-14 23:53:09 +00:00
|
|
|
PWMModulePreInit();
|
2020-02-24 13:19:15 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:08:44 +00:00
|
|
|
#endif // USE_PWM_DIMMER
|