mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-30 14:16:32 +00:00
Refactor and document power control
Refactor and document power control
This commit is contained in:
parent
636e842545
commit
f88e87cfde
@ -231,7 +231,11 @@ enum EmulationOptions {EMUL_NONE, EMUL_WEMO, EMUL_HUE, EMUL_MAX};
|
|||||||
|
|
||||||
enum TopicOptions { CMND, STAT, TELE, nu1, RESULT_OR_CMND, RESULT_OR_STAT, RESULT_OR_TELE };
|
enum TopicOptions { CMND, STAT, TELE, nu1, RESULT_OR_CMND, RESULT_OR_STAT, RESULT_OR_TELE };
|
||||||
|
|
||||||
enum ExecuteCommandPowerOptions { POWER_OFF, POWER_ON, POWER_TOGGLE, POWER_BLINK, POWER_BLINK_STOP, power_nu1, POWER_OFF_NO_STATE, POWER_ON_NO_STATE, power_nu2, POWER_SHOW_STATE };
|
enum ExecuteCommandPowerOptions { POWER_OFF, POWER_ON, POWER_TOGGLE, POWER_BLINK, POWER_BLINK_STOP,
|
||||||
|
POWER_OFF_NO_STATE = 8, POWER_ON_NO_STATE, POWER_TOGGLE_NO_STATE,
|
||||||
|
POWER_SHOW_STATE = 16 };
|
||||||
|
enum SendKeyPowerOptions { POWER_HOLD = 3, CLEAR_RETAIN = 9 };
|
||||||
|
enum SendKeyOptions { KEY_BUTTON, KEY_SWITCH };
|
||||||
|
|
||||||
enum PowerOnStateOptions { POWER_ALL_OFF, POWER_ALL_ON, POWER_ALL_SAVED_TOGGLE, POWER_ALL_SAVED, POWER_ALL_ALWAYS_ON, POWER_ALL_OFF_PULSETIME_ON };
|
enum PowerOnStateOptions { POWER_ALL_OFF, POWER_ALL_ON, POWER_ALL_SAVED_TOGGLE, POWER_ALL_SAVED, POWER_ALL_ALWAYS_ON, POWER_ALL_OFF_PULSETIME_ON };
|
||||||
|
|
||||||
|
@ -90,6 +90,7 @@ unsigned long pulse_timer[MAX_PULSETIMERS] = { 0 }; // Power off timer
|
|||||||
unsigned long blink_timer = 0; // Power cycle timer
|
unsigned long blink_timer = 0; // Power cycle timer
|
||||||
unsigned long backlog_delay = 0; // Command backlog delay
|
unsigned long backlog_delay = 0; // Command backlog delay
|
||||||
power_t power = 0; // Current copy of Settings.power
|
power_t power = 0; // Current copy of Settings.power
|
||||||
|
power_t last_power = 0; // Last power set state
|
||||||
power_t blink_power; // Blink power state
|
power_t blink_power; // Blink power state
|
||||||
power_t blink_mask = 0; // Blink relay active mask
|
power_t blink_mask = 0; // Blink relay active mask
|
||||||
power_t blink_powersave; // Blink start power save state
|
power_t blink_powersave; // Blink start power save state
|
||||||
@ -201,7 +202,9 @@ char* Format(char* output, const char* input, int size)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!digits) { strlcpy(output, input, size); }
|
if (!digits) {
|
||||||
|
strlcpy(output, input, size);
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,7 +261,9 @@ char* GetTopic_P(char *stopic, uint8_t prefix, char *topic, const char* subtopic
|
|||||||
}
|
}
|
||||||
fulltopic.replace(F("#"), "");
|
fulltopic.replace(F("#"), "");
|
||||||
fulltopic.replace(F("//"), "/");
|
fulltopic.replace(F("//"), "/");
|
||||||
if (!fulltopic.endsWith("/")) fulltopic += "/";
|
if (!fulltopic.endsWith("/")) {
|
||||||
|
fulltopic += "/";
|
||||||
|
}
|
||||||
snprintf_P(stopic, TOPSZ, PSTR("%s%s"), fulltopic.c_str(), romram);
|
snprintf_P(stopic, TOPSZ, PSTR("%s%s"), fulltopic.c_str(), romram);
|
||||||
return stopic;
|
return stopic;
|
||||||
}
|
}
|
||||||
@ -270,7 +275,9 @@ char* GetFallbackTopic_P(char *stopic, uint8_t prefix, const char* subtopic)
|
|||||||
|
|
||||||
char* GetStateText(uint8_t state)
|
char* GetStateText(uint8_t state)
|
||||||
{
|
{
|
||||||
if (state > 3) { state = 1; }
|
if (state > 3) {
|
||||||
|
state = 1;
|
||||||
|
}
|
||||||
return Settings.state_text[state];
|
return Settings.state_text[state];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +319,9 @@ void SetDevicePower(power_t rpower, int source)
|
|||||||
power_t mask = 1;
|
power_t mask = 1;
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (uint32_t j = 0; j < devices_present; j++) {
|
for (uint32_t j = 0; j < devices_present; j++) {
|
||||||
if ((Settings.interlock[i] & mask) && (rpower & mask)) { count++; }
|
if ((Settings.interlock[i] & mask) && (rpower & mask)) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
mask <<= 1;
|
mask <<= 1;
|
||||||
}
|
}
|
||||||
if (count > 1) {
|
if (count > 1) {
|
||||||
@ -323,6 +332,10 @@ void SetDevicePower(power_t rpower, int source)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rpower) { // Any power set
|
||||||
|
last_power = rpower;
|
||||||
|
}
|
||||||
|
|
||||||
XdrvMailbox.index = rpower;
|
XdrvMailbox.index = rpower;
|
||||||
XdrvCall(FUNC_SET_POWER); // Signal power state
|
XdrvCall(FUNC_SET_POWER); // Signal power state
|
||||||
|
|
||||||
@ -353,10 +366,56 @@ void SetDevicePower(power_t rpower, int source)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RestorePower(bool publish_power, int source)
|
||||||
|
{
|
||||||
|
if (power != last_power) {
|
||||||
|
SetDevicePower(last_power, source);
|
||||||
|
if (publish_power) {
|
||||||
|
MqttPublishAllPowerState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetAllPower(uint8_t state, int source)
|
||||||
|
{
|
||||||
|
// state 0 = POWER_OFF = Relay Off
|
||||||
|
// state 1 = POWER_ON = Relay On (turn off after Settings.pulse_timer * 100 mSec if enabled)
|
||||||
|
// state 2 = POWER_TOGGLE = Toggle relay
|
||||||
|
// state 8 = POWER_OFF_NO_STATE = Relay Off and no publishPowerState
|
||||||
|
// state 9 = POWER_ON_NO_STATE = Relay On and no publishPowerState
|
||||||
|
// state 10 = POWER_TOGGLE_NO_STATE = Toggle relay and no publishPowerState
|
||||||
|
// state 16 = POWER_SHOW_STATE = Show power state
|
||||||
|
|
||||||
|
bool publish_power = true;
|
||||||
|
if ((state >= POWER_OFF_NO_STATE) && (state <= POWER_TOGGLE_NO_STATE)) {
|
||||||
|
state &= 3; // POWER_OFF, POWER_ON or POWER_TOGGLE
|
||||||
|
publish_power = false;
|
||||||
|
}
|
||||||
|
if ((state >= POWER_OFF) && (state <= POWER_TOGGLE)) {
|
||||||
|
power_t all_on = (1 << devices_present) -1;
|
||||||
|
switch (state) {
|
||||||
|
case POWER_OFF:
|
||||||
|
power = 0;
|
||||||
|
break;
|
||||||
|
case POWER_ON:
|
||||||
|
power = all_on;
|
||||||
|
break;
|
||||||
|
case POWER_TOGGLE:
|
||||||
|
power ^= all_on; // Complement current state
|
||||||
|
}
|
||||||
|
SetDevicePower(power, source);
|
||||||
|
}
|
||||||
|
if (publish_power) {
|
||||||
|
MqttPublishAllPowerState();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SetLedPowerIdx(uint8_t led, uint8_t state)
|
void SetLedPowerIdx(uint8_t led, uint8_t state)
|
||||||
{
|
{
|
||||||
if ((99 == pin[GPIO_LEDLNK]) && (0 == led)) { // Legacy - LED1 is link led only if LED2 is present
|
if ((99 == pin[GPIO_LEDLNK]) && (0 == led)) { // Legacy - LED1 is link led only if LED2 is present
|
||||||
if (pin[GPIO_LED2] < 99) { led = 1; }
|
if (pin[GPIO_LED2] < 99) {
|
||||||
|
led = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (pin[GPIO_LED1 + led] < 99) {
|
if (pin[GPIO_LED1 + led] < 99) {
|
||||||
uint8_t mask = 1 << led;
|
uint8_t mask = 1 << led;
|
||||||
@ -426,13 +485,13 @@ uint16_t GetPulseTimer(uint8_t index)
|
|||||||
|
|
||||||
bool SendKey(uint8_t key, uint8_t device, uint8_t state)
|
bool SendKey(uint8_t key, uint8_t device, uint8_t state)
|
||||||
{
|
{
|
||||||
// key 0 = button_topic
|
// key 0 = KEY_BUTTON = button_topic
|
||||||
// key 1 = switch_topic
|
// key 1 = KEY_SWITCH = switch_topic
|
||||||
// state 0 = off
|
// state 0 = POWER_OFF = off
|
||||||
// state 1 = on
|
// state 1 = POWER_ON = on
|
||||||
// state 2 = toggle
|
// state 2 = POWER_TOGGLE = toggle
|
||||||
// state 3 = hold
|
// state 3 = POWER_HOLD = hold
|
||||||
// state 9 = clear retain flag
|
// state 9 = CLEAR_RETAIN = clear retain flag
|
||||||
|
|
||||||
char stopic[TOPSZ];
|
char stopic[TOPSZ];
|
||||||
char scommand[CMDSZ];
|
char scommand[CMDSZ];
|
||||||
@ -442,23 +501,25 @@ bool SendKey(uint8_t key, uint8_t device, uint8_t state)
|
|||||||
char *tmp = (key) ? Settings.switch_topic : Settings.button_topic;
|
char *tmp = (key) ? Settings.switch_topic : Settings.button_topic;
|
||||||
Format(key_topic, tmp, sizeof(key_topic));
|
Format(key_topic, tmp, sizeof(key_topic));
|
||||||
if (Settings.flag.mqtt_enabled && MqttIsConnected() && (strlen(key_topic) != 0) && strcmp(key_topic, "0")) {
|
if (Settings.flag.mqtt_enabled && MqttIsConnected() && (strlen(key_topic) != 0) && strcmp(key_topic, "0")) {
|
||||||
if (!key && (device > devices_present)) { device = 1; } // Only allow number of buttons up to number of devices
|
if (!key && (device > devices_present)) {
|
||||||
|
device = 1; // Only allow number of buttons up to number of devices
|
||||||
|
}
|
||||||
GetTopic_P(stopic, CMND, key_topic,
|
GetTopic_P(stopic, CMND, key_topic,
|
||||||
GetPowerDevice(scommand, device, sizeof(scommand), (key + Settings.flag.device_index_enable))); // cmnd/switchtopic/POWERx
|
GetPowerDevice(scommand, device, sizeof(scommand), (key + Settings.flag.device_index_enable))); // cmnd/switchtopic/POWERx
|
||||||
if (9 == state) {
|
if (CLEAR_RETAIN == state) {
|
||||||
mqtt_data[0] = '\0';
|
mqtt_data[0] = '\0';
|
||||||
} else {
|
} else {
|
||||||
if ((Settings.flag3.button_switch_force_local || !strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic)) && (2 == state)) {
|
if ((Settings.flag3.button_switch_force_local || !strcmp(mqtt_topic, key_topic) || !strcmp(Settings.mqtt_grptopic, key_topic)) && (POWER_TOGGLE == state)) {
|
||||||
state = ~(power >> (device -1)) &1;
|
state = ~(power >> (device -1)) &1; // POWER_OFF or POWER_ON
|
||||||
}
|
}
|
||||||
snprintf_P(mqtt_data, sizeof(mqtt_data), GetStateText(state));
|
snprintf_P(mqtt_data, sizeof(mqtt_data), GetStateText(state));
|
||||||
}
|
}
|
||||||
#ifdef USE_DOMOTICZ
|
#ifdef USE_DOMOTICZ
|
||||||
if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) {
|
if (!(DomoticzSendKey(key, device, state, strlen(mqtt_data)))) {
|
||||||
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain) && (state != 3 || !Settings.flag3.no_hold_retain));
|
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain) && (state != POWER_HOLD || !Settings.flag3.no_hold_retain));
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain) && (state != 3 || !Settings.flag3.no_hold_retain));
|
MqttPublishDirect(stopic, ((key) ? Settings.flag.mqtt_switch_retain : Settings.flag.mqtt_button_retain) && (state != POWER_HOLD || !Settings.flag3.no_hold_retain));
|
||||||
#endif // USE_DOMOTICZ
|
#endif // USE_DOMOTICZ
|
||||||
result = !Settings.flag3.button_switch_force_local;
|
result = !Settings.flag3.button_switch_force_local;
|
||||||
} else {
|
} else {
|
||||||
@ -474,14 +535,15 @@ bool SendKey(uint8_t key, uint8_t device, uint8_t state)
|
|||||||
void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
|
void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
|
||||||
{
|
{
|
||||||
// device = Relay number 1 and up
|
// device = Relay number 1 and up
|
||||||
// state 0 = Relay Off
|
// state 0 = POWER_OFF = Relay Off
|
||||||
// state 1 = Relay On (turn off after Settings.pulse_timer * 100 mSec if enabled)
|
// state 1 = POWER_ON = Relay On (turn off after Settings.pulse_timer * 100 mSec if enabled)
|
||||||
// state 2 = Toggle relay
|
// state 2 = POWER_TOGGLE = Toggle relay
|
||||||
// state 3 = Blink relay
|
// state 3 = POWER_BLINK = Blink relay
|
||||||
// state 4 = Stop blinking relay
|
// state 4 = POWER_BLINK_STOP = Stop blinking relay
|
||||||
// state 6 = Relay Off and no publishPowerState
|
// state 8 = POWER_OFF_NO_STATE = Relay Off and no publishPowerState
|
||||||
// state 7 = Relay On and no publishPowerState
|
// state 9 = POWER_ON_NO_STATE = Relay On and no publishPowerState
|
||||||
// state 9 = Show power state
|
// state 10 = POWER_TOGGLE_NO_STATE = Toggle relay and no publishPowerState
|
||||||
|
// state 16 = POWER_SHOW_STATE = Show power state
|
||||||
|
|
||||||
// ShowSource(source);
|
// ShowSource(source);
|
||||||
|
|
||||||
@ -495,16 +557,20 @@ void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
|
|||||||
}
|
}
|
||||||
#endif // USE_SONOFF_IFAN
|
#endif // USE_SONOFF_IFAN
|
||||||
|
|
||||||
uint8_t publish_power = 1;
|
bool publish_power = true;
|
||||||
if ((POWER_OFF_NO_STATE == state) || (POWER_ON_NO_STATE == state)) {
|
if ((state >= POWER_OFF_NO_STATE) && (state <= POWER_TOGGLE_NO_STATE)) {
|
||||||
state &= 1;
|
state &= 3; // POWER_OFF, POWER_ON or POWER_TOGGLE
|
||||||
publish_power = 0;
|
publish_power = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((device < 1) || (device > devices_present)) device = 1;
|
if ((device < 1) || (device > devices_present)) {
|
||||||
|
device = 1;
|
||||||
|
}
|
||||||
active_device = device;
|
active_device = device;
|
||||||
|
|
||||||
if (device <= MAX_PULSETIMERS) { SetPulseTimer(device -1, 0); }
|
if (device <= MAX_PULSETIMERS) {
|
||||||
|
SetPulseTimer(device -1, 0);
|
||||||
|
}
|
||||||
power_t mask = 1 << (device -1); // Device to control
|
power_t mask = 1 << (device -1); // Device to control
|
||||||
if (state <= POWER_TOGGLE) {
|
if (state <= POWER_TOGGLE) {
|
||||||
if ((blink_mask & mask)) {
|
if ((blink_mask & mask)) {
|
||||||
@ -546,7 +612,9 @@ void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
|
|||||||
#ifdef USE_KNX
|
#ifdef USE_KNX
|
||||||
KnxUpdatePowerState(device, power);
|
KnxUpdatePowerState(device, power);
|
||||||
#endif // USE_KNX
|
#endif // USE_KNX
|
||||||
if (publish_power && Settings.flag3.hass_tele_on_power) { MqttPublishTeleState(); }
|
if (publish_power && Settings.flag3.hass_tele_on_power) {
|
||||||
|
MqttPublishTeleState();
|
||||||
|
}
|
||||||
if (device <= MAX_PULSETIMERS) { // Restart PulseTime if powered On
|
if (device <= MAX_PULSETIMERS) { // Restart PulseTime if powered On
|
||||||
SetPulseTimer(device -1, (((POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate) ? ~power : power) & mask) ? Settings.pulse_timer[device -1] : 0);
|
SetPulseTimer(device -1, (((POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate) ? ~power : power) & mask) ? Settings.pulse_timer[device -1] : 0);
|
||||||
}
|
}
|
||||||
@ -566,10 +634,14 @@ void ExecuteCommandPower(uint8_t device, uint8_t state, int source)
|
|||||||
uint8_t flag = (blink_mask & mask);
|
uint8_t flag = (blink_mask & mask);
|
||||||
blink_mask &= (POWER_MASK ^ mask); // Clear device mask
|
blink_mask &= (POWER_MASK ^ mask); // Clear device mask
|
||||||
MqttPublishPowerBlinkState(device);
|
MqttPublishPowerBlinkState(device);
|
||||||
if (flag) ExecuteCommandPower(device, (blink_powersave >> (device -1))&1, SRC_IGNORE); // Restore state
|
if (flag) {
|
||||||
|
ExecuteCommandPower(device, (blink_powersave >> (device -1))&1, SRC_IGNORE); // Restore state
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (publish_power) MqttPublishPowerState(device);
|
if (publish_power) {
|
||||||
|
MqttPublishPowerState(device);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void StopAllPowerBlink(void)
|
void StopAllPowerBlink(void)
|
||||||
@ -586,27 +658,6 @@ void StopAllPowerBlink(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SetAllPower(uint8_t state, int source)
|
|
||||||
{
|
|
||||||
if ((POWER_ALL_OFF == state) || (POWER_ALL_ON == state)) {
|
|
||||||
power = 0;
|
|
||||||
if (POWER_ALL_ON == state) {
|
|
||||||
power = (1 << devices_present) -1;
|
|
||||||
}
|
|
||||||
SetDevicePower(power, source);
|
|
||||||
MqttPublishAllPowerState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RestorePower(power_t power_set, int source)
|
|
||||||
{
|
|
||||||
power_t new_state = power | power_set;
|
|
||||||
if (power != new_state) {
|
|
||||||
SetDevicePower(new_state, source);
|
|
||||||
MqttPublishAllPowerState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MqttShowPWMState(void)
|
void MqttShowPWMState(void)
|
||||||
{
|
{
|
||||||
ResponseAppend_P(PSTR("\"" D_CMND_PWM "\":{"));
|
ResponseAppend_P(PSTR("\"" D_CMND_PWM "\":{"));
|
||||||
|
@ -165,7 +165,7 @@ void ButtonHandler(void)
|
|||||||
if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second
|
if (!Button.hold_timer[button_index]) { button_pressed = true; } // Do not allow within 1 second
|
||||||
}
|
}
|
||||||
if (button_pressed) {
|
if (button_pressed) {
|
||||||
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
||||||
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
|
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -174,7 +174,7 @@ void ButtonHandler(void)
|
|||||||
if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) {
|
if ((PRESSED == button) && (NOT_PRESSED == Button.last_state[button_index])) {
|
||||||
if (Settings.flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action
|
if (Settings.flag.button_single) { // SetOption13 (0) - Allow only single button press for immediate action
|
||||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1);
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR(D_LOG_APPLICATION D_BUTTON "%d " D_IMMEDIATE), button_index +1);
|
||||||
if (!SendKey(0, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
if (!SendKey(KEY_BUTTON, button_index +1, POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
||||||
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
|
ExecuteCommandPower(button_index +1, POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -206,7 +206,7 @@ void ButtonHandler(void)
|
|||||||
}
|
}
|
||||||
if (Button.hold_timer[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button hold
|
if (Button.hold_timer[button_index] == loops_per_second * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button hold
|
||||||
Button.press_counter[button_index] = 0;
|
Button.press_counter[button_index] = 0;
|
||||||
SendKey(0, button_index +1, 3); // Execute Hold command via MQTT if ButtonTopic is set
|
SendKey(KEY_BUTTON, button_index +1, POWER_HOLD); // Execute Hold command via MQTT if ButtonTopic is set
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer
|
if (Button.hold_timer[button_index] == loops_per_second * hold_time_extent * Settings.param[P_HOLD_TIME] / 10) { // SetOption32 (40) - Button held for factor times longer
|
||||||
@ -241,7 +241,7 @@ void ButtonHandler(void)
|
|||||||
#if defined(USE_LIGHT) && defined(ROTARY_V1)
|
#if defined(USE_LIGHT) && defined(ROTARY_V1)
|
||||||
if (!((0 == button_index) && RotaryButtonPressed())) {
|
if (!((0 == button_index) && RotaryButtonPressed())) {
|
||||||
#endif
|
#endif
|
||||||
if (single_press && SendKey(0, button_index + Button.press_counter[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
if (single_press && SendKey(KEY_BUTTON, button_index + Button.press_counter[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set
|
||||||
// Success
|
// Success
|
||||||
} else {
|
} else {
|
||||||
if (Button.press_counter[button_index] < 3) { // Single or Double press
|
if (Button.press_counter[button_index] < 3) { // Single or Double press
|
||||||
|
@ -257,20 +257,20 @@ void CmndDelay(void)
|
|||||||
void CmndPower(void)
|
void CmndPower(void)
|
||||||
{
|
{
|
||||||
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= devices_present)) {
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= devices_present)) {
|
||||||
if ((XdrvMailbox.payload < 0) || (XdrvMailbox.payload > 4)) { XdrvMailbox.payload = 9; }
|
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_BLINK_STOP)) {
|
||||||
|
XdrvMailbox.payload = POWER_SHOW_STATE;
|
||||||
|
}
|
||||||
// Settings.flag.device_index_enable = XdrvMailbox.usridx;
|
// Settings.flag.device_index_enable = XdrvMailbox.usridx;
|
||||||
ExecuteCommandPower(XdrvMailbox.index, XdrvMailbox.payload, SRC_IGNORE);
|
ExecuteCommandPower(XdrvMailbox.index, XdrvMailbox.payload, SRC_IGNORE);
|
||||||
mqtt_data[0] = '\0';
|
mqtt_data[0] = '\0';
|
||||||
}
|
}
|
||||||
else if (0 == XdrvMailbox.index) {
|
else if (0 == XdrvMailbox.index) {
|
||||||
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
if ((XdrvMailbox.payload < POWER_OFF) || (XdrvMailbox.payload > POWER_TOGGLE)) {
|
||||||
if (2 == XdrvMailbox.payload) {
|
XdrvMailbox.payload = POWER_SHOW_STATE;
|
||||||
XdrvMailbox.payload = (power) ? 0 : 1;
|
|
||||||
}
|
}
|
||||||
SetAllPower(XdrvMailbox.payload, SRC_IGNORE);
|
SetAllPower(XdrvMailbox.payload, SRC_IGNORE);
|
||||||
mqtt_data[0] = '\0';
|
mqtt_data[0] = '\0';
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CmndStatus(void)
|
void CmndStatus(void)
|
||||||
|
@ -143,7 +143,7 @@ void SwitchHandler(uint8_t mode)
|
|||||||
if (Switch.hold_timer[i]) {
|
if (Switch.hold_timer[i]) {
|
||||||
Switch.hold_timer[i]--;
|
Switch.hold_timer[i]--;
|
||||||
if (0 == Switch.hold_timer[i]) {
|
if (0 == Switch.hold_timer[i]) {
|
||||||
SendKey(1, i +1, 3); // Execute command via MQTT
|
SendKey(KEY_SWITCH, i +1, POWER_HOLD); // Execute command via MQTT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,10 +152,10 @@ void SwitchHandler(uint8_t mode)
|
|||||||
// enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, MAX_SWITCH_OPTION};
|
// enum SwitchModeOptions {TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE, MAX_SWITCH_OPTION};
|
||||||
|
|
||||||
if (button != Switch.last_state[i]) {
|
if (button != Switch.last_state[i]) {
|
||||||
switchflag = 3;
|
switchflag = POWER_TOGGLE +1;
|
||||||
switch (Settings.switchmode[i]) {
|
switch (Settings.switchmode[i]) {
|
||||||
case TOGGLE:
|
case TOGGLE:
|
||||||
switchflag = 2; // Toggle
|
switchflag = POWER_TOGGLE; // Toggle
|
||||||
break;
|
break;
|
||||||
case FOLLOW:
|
case FOLLOW:
|
||||||
switchflag = button &1; // Follow wall switch state
|
switchflag = button &1; // Follow wall switch state
|
||||||
@ -165,17 +165,17 @@ void SwitchHandler(uint8_t mode)
|
|||||||
break;
|
break;
|
||||||
case PUSHBUTTON:
|
case PUSHBUTTON:
|
||||||
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i])) {
|
||||||
switchflag = 2; // Toggle with pushbutton to Gnd
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PUSHBUTTON_INV:
|
case PUSHBUTTON_INV:
|
||||||
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i])) {
|
||||||
switchflag = 2; // Toggle with releasing pushbutton from Gnd
|
switchflag = POWER_TOGGLE; // Toggle with releasing pushbutton from Gnd
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PUSHBUTTON_TOGGLE:
|
case PUSHBUTTON_TOGGLE:
|
||||||
if (button != Switch.last_state[i]) {
|
if (button != Switch.last_state[i]) {
|
||||||
switchflag = 2; // Toggle with any pushbutton change
|
switchflag = POWER_TOGGLE; // Toggle with any pushbutton change
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PUSHBUTTONHOLD:
|
case PUSHBUTTONHOLD:
|
||||||
@ -184,7 +184,7 @@ void SwitchHandler(uint8_t mode)
|
|||||||
}
|
}
|
||||||
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
if ((NOT_PRESSED == button) && (PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
||||||
Switch.hold_timer[i] = 0;
|
Switch.hold_timer[i] = 0;
|
||||||
switchflag = 2; // Toggle with pushbutton to Gnd
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PUSHBUTTONHOLD_INV:
|
case PUSHBUTTONHOLD_INV:
|
||||||
@ -193,13 +193,13 @@ void SwitchHandler(uint8_t mode)
|
|||||||
}
|
}
|
||||||
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
if ((PRESSED == button) && (NOT_PRESSED == Switch.last_state[i]) && (Switch.hold_timer[i])) {
|
||||||
Switch.hold_timer[i] = 0;
|
Switch.hold_timer[i] = 0;
|
||||||
switchflag = 2; // Toggle with pushbutton to Gnd
|
switchflag = POWER_TOGGLE; // Toggle with pushbutton to Gnd
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (switchflag < 3) {
|
if (switchflag <= POWER_TOGGLE) {
|
||||||
if (!SendKey(1, i +1, switchflag)) { // Execute command via MQTT
|
if (!SendKey(KEY_SWITCH, i +1, switchflag)) { // Execute command via MQTT
|
||||||
ExecuteCommandPower(i +1, switchflag, SRC_SWITCH); // Execute command internally (if i < devices_present)
|
ExecuteCommandPower(i +1, switchflag, SRC_SWITCH); // Execute command internally (if i < devices_present)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -894,7 +894,7 @@ void CmndButtonRetain(void)
|
|||||||
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
|
||||||
if (!XdrvMailbox.payload) {
|
if (!XdrvMailbox.payload) {
|
||||||
for (uint32_t i = 1; i <= MAX_KEYS; i++) {
|
for (uint32_t i = 1; i <= MAX_KEYS; i++) {
|
||||||
SendKey(0, i, 9); // Clear MQTT retain in broker
|
SendKey(KEY_BUTTON, i, CLEAR_RETAIN); // Clear MQTT retain in broker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Settings.flag.mqtt_button_retain = XdrvMailbox.payload;
|
Settings.flag.mqtt_button_retain = XdrvMailbox.payload;
|
||||||
@ -907,7 +907,7 @@ void CmndSwitchRetain(void)
|
|||||||
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 1)) {
|
||||||
if (!XdrvMailbox.payload) {
|
if (!XdrvMailbox.payload) {
|
||||||
for (uint32_t i = 1; i <= MAX_SWITCHES; i++) {
|
for (uint32_t i = 1; i <= MAX_SWITCHES; i++) {
|
||||||
SendKey(1, i, 9); // Clear MQTT retain in broker
|
SendKey(KEY_SWITCH, i, CLEAR_RETAIN); // Clear MQTT retain in broker
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Settings.flag.mqtt_switch_retain = XdrvMailbox.payload;
|
Settings.flag.mqtt_switch_retain = XdrvMailbox.payload;
|
||||||
|
@ -110,8 +110,6 @@ struct ENERGY {
|
|||||||
bool max_current_flag = false;
|
bool max_current_flag = false;
|
||||||
|
|
||||||
#ifdef USE_ENERGY_POWER_LIMIT
|
#ifdef USE_ENERGY_POWER_LIMIT
|
||||||
power_t mp_last_power = 0;
|
|
||||||
power_t me_last_power = 0;
|
|
||||||
uint16_t mplh_counter = 0;
|
uint16_t mplh_counter = 0;
|
||||||
uint16_t mplw_counter = 0;
|
uint16_t mplw_counter = 0;
|
||||||
uint8_t mplr_counter = 0;
|
uint8_t mplr_counter = 0;
|
||||||
@ -319,7 +317,6 @@ void EnergyMarginCheck(void)
|
|||||||
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHED "\":\"%d%s\"}"), energy_power_u, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
|
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHED "\":\"%d%s\"}"), energy_power_u, (Settings.flag.value_units) ? " " D_UNIT_WATT : "");
|
||||||
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
||||||
EnergyMqttShow();
|
EnergyMqttShow();
|
||||||
Energy.mp_last_power = power;
|
|
||||||
SetAllPower(POWER_ALL_OFF, SRC_MAXPOWER);
|
SetAllPower(POWER_ALL_OFF, SRC_MAXPOWER);
|
||||||
if (!Energy.mplr_counter) {
|
if (!Energy.mplr_counter) {
|
||||||
Energy.mplr_counter = Settings.param[P_MAX_POWER_RETRY] +1;
|
Energy.mplr_counter = Settings.param[P_MAX_POWER_RETRY] +1;
|
||||||
@ -342,7 +339,7 @@ void EnergyMarginCheck(void)
|
|||||||
if (Energy.mplr_counter) {
|
if (Energy.mplr_counter) {
|
||||||
Response_P(PSTR("{\"" D_JSON_POWERMONITOR "\":\"%s\"}"), GetStateText(1));
|
Response_P(PSTR("{\"" D_JSON_POWERMONITOR "\":\"%s\"}"), GetStateText(1));
|
||||||
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_POWERMONITOR));
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_POWERMONITOR));
|
||||||
RestorePower(Energy.mp_last_power, SRC_MAXPOWER);
|
RestorePower(true, SRC_MAXPOWER);
|
||||||
} else {
|
} else {
|
||||||
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHEDRETRY "\":\"%s\"}"), GetStateText(0));
|
Response_P(PSTR("{\"" D_JSON_MAXPOWERREACHEDRETRY "\":\"%s\"}"), GetStateText(0));
|
||||||
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
||||||
@ -360,7 +357,7 @@ void EnergyMarginCheck(void)
|
|||||||
Energy.max_energy_state = 1;
|
Energy.max_energy_state = 1;
|
||||||
Response_P(PSTR("{\"" D_JSON_ENERGYMONITOR "\":\"%s\"}"), GetStateText(1));
|
Response_P(PSTR("{\"" D_JSON_ENERGYMONITOR "\":\"%s\"}"), GetStateText(1));
|
||||||
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_ENERGYMONITOR));
|
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_JSON_ENERGYMONITOR));
|
||||||
RestorePower(Energy.me_last_power, SRC_MAXENERGY);
|
RestorePower(true, SRC_MAXENERGY);
|
||||||
}
|
}
|
||||||
else if ((1 == Energy.max_energy_state ) && (energy_daily_u >= Settings.energy_max_energy)) {
|
else if ((1 == Energy.max_energy_state ) && (energy_daily_u >= Settings.energy_max_energy)) {
|
||||||
Energy.max_energy_state = 2;
|
Energy.max_energy_state = 2;
|
||||||
@ -368,7 +365,6 @@ void EnergyMarginCheck(void)
|
|||||||
Response_P(PSTR("{\"" D_JSON_MAXENERGYREACHED "\":\"%s%s\"}"), mqtt_data, (Settings.flag.value_units) ? " " D_UNIT_KILOWATTHOUR : "");
|
Response_P(PSTR("{\"" D_JSON_MAXENERGYREACHED "\":\"%s%s\"}"), mqtt_data, (Settings.flag.value_units) ? " " D_UNIT_KILOWATTHOUR : "");
|
||||||
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
MqttPublishPrefixTopic_P(STAT, S_RSLT_WARNING);
|
||||||
EnergyMqttShow();
|
EnergyMqttShow();
|
||||||
Energy.me_last_power = power;
|
|
||||||
SetAllPower(POWER_ALL_OFF, SRC_MAXENERGY);
|
SetAllPower(POWER_ALL_OFF, SRC_MAXENERGY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -306,7 +306,7 @@ bool DomoticzSendKey(uint8_t key, uint8_t device, uint8_t state, uint8_t svalflg
|
|||||||
if (device <= MAX_DOMOTICZ_IDX) {
|
if (device <= MAX_DOMOTICZ_IDX) {
|
||||||
if ((Settings.domoticz_key_idx[device -1] || Settings.domoticz_switch_idx[device -1]) && (svalflg)) {
|
if ((Settings.domoticz_key_idx[device -1] || Settings.domoticz_switch_idx[device -1]) && (svalflg)) {
|
||||||
Response_P(PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
|
Response_P(PSTR("{\"command\":\"switchlight\",\"idx\":%d,\"switchcmd\":\"%s\"}"),
|
||||||
(key) ? Settings.domoticz_switch_idx[device -1] : Settings.domoticz_key_idx[device -1], (state) ? (2 == state) ? "Toggle" : "On" : "Off");
|
(key) ? Settings.domoticz_switch_idx[device -1] : Settings.domoticz_key_idx[device -1], (state) ? (POWER_TOGGLE == state) ? "Toggle" : "On" : "Off");
|
||||||
MqttPublish(domoticz_in_topic);
|
MqttPublish(domoticz_in_topic);
|
||||||
result = true;
|
result = true;
|
||||||
}
|
}
|
||||||
|
@ -296,7 +296,7 @@ void TimerEverySecond(void)
|
|||||||
if (xtimer.days & days) {
|
if (xtimer.days & days) {
|
||||||
Settings.timer[i].arm = xtimer.repeat;
|
Settings.timer[i].arm = xtimer.repeat;
|
||||||
#if defined(USE_RULES) || defined(USE_SCRIPT)
|
#if defined(USE_RULES) || defined(USE_SCRIPT)
|
||||||
if (3 == xtimer.power) { // Blink becomes Rule disregarding device and allowing use of Backlog commands
|
if (POWER_BLINK == xtimer.power) { // Blink becomes Rule disregarding device and allowing use of Backlog commands
|
||||||
Response_P(PSTR("{\"Clock\":{\"Timer\":%d}}"), i +1);
|
Response_P(PSTR("{\"Clock\":{\"Timer\":%d}}"), i +1);
|
||||||
XdrvRulesProcess();
|
XdrvRulesProcess();
|
||||||
} else
|
} else
|
||||||
|
@ -575,7 +575,7 @@ void KNX_CB_Action(message_t const &msg, void *arg)
|
|||||||
else if (chan->type < 17) // Toggle Relays
|
else if (chan->type < 17) // Toggle Relays
|
||||||
{
|
{
|
||||||
if (!toggle_inhibit) {
|
if (!toggle_inhibit) {
|
||||||
ExecuteCommandPower((chan->type) -8, 2, SRC_KNX);
|
ExecuteCommandPower((chan->type) -8, POWER_TOGGLE, SRC_KNX);
|
||||||
if (Settings.flag.knx_enable_enhancement) {
|
if (Settings.flag.knx_enable_enhancement) {
|
||||||
toggle_inhibit = TOGGLE_INHIBIT_TIME;
|
toggle_inhibit = TOGGLE_INHIBIT_TIME;
|
||||||
}
|
}
|
||||||
|
@ -96,7 +96,7 @@ void SonoffIFanSetFanspeed(uint8_t fanspeed, bool sequence)
|
|||||||
fans = kIFan03Speed[fanspeed];
|
fans = kIFan03Speed[fanspeed];
|
||||||
}
|
}
|
||||||
for (uint32_t i = 2; i < 5; i++) {
|
for (uint32_t i = 2; i < 5; i++) {
|
||||||
uint8_t state = (fans &1) + 6; // Add no publishPowerState
|
uint8_t state = (fans &1) + POWER_OFF_NO_STATE; // Add no publishPowerState
|
||||||
ExecuteCommandPower(i, state, SRC_IGNORE); // Use relay 2, 3 and 4
|
ExecuteCommandPower(i, state, SRC_IGNORE); // Use relay 2, 3 and 4
|
||||||
fans >>= 1;
|
fans >>= 1;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,7 @@ if (2 == ili9488_ctouch_counter) {
|
|||||||
uint8_t bflags=buttons[count]->vpower&0x7f;
|
uint8_t bflags=buttons[count]->vpower&0x7f;
|
||||||
if (!bflags) {
|
if (!bflags) {
|
||||||
// real button
|
// real button
|
||||||
if (!SendKey(0, count+1, POWER_TOGGLE)) {
|
if (!SendKey(KEY_BUTTON, count+1, POWER_TOGGLE)) {
|
||||||
ExecuteCommandPower(count+1, POWER_TOGGLE, SRC_BUTTON);
|
ExecuteCommandPower(count+1, POWER_TOGGLE, SRC_BUTTON);
|
||||||
}
|
}
|
||||||
buttons[count]->xdrawButton(bitRead(power,count));
|
buttons[count]->xdrawButton(bitRead(power,count));
|
||||||
|
@ -166,7 +166,7 @@ if (2 == ra8876_ctouch_counter) {
|
|||||||
uint8_t bflags=buttons[count]->vpower&0x7f;
|
uint8_t bflags=buttons[count]->vpower&0x7f;
|
||||||
if (!bflags) {
|
if (!bflags) {
|
||||||
// real button
|
// real button
|
||||||
if (!SendKey(0, count+1, POWER_TOGGLE)) {
|
if (!SendKey(KEY_BUTTON, count+1, POWER_TOGGLE)) {
|
||||||
ExecuteCommandPower(count+1, POWER_TOGGLE, SRC_BUTTON);
|
ExecuteCommandPower(count+1, POWER_TOGGLE, SRC_BUTTON);
|
||||||
}
|
}
|
||||||
buttons[count]->xdrawButton(bitRead(power,count));
|
buttons[count]->xdrawButton(bitRead(power,count));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user