Fix Tuya PowerOn state

Fix Tuya PowerOn state (#7412)
This commit is contained in:
Theo Arends 2020-01-11 15:39:56 +01:00
parent 62904c53db
commit e7b061ce5a
3 changed files with 56 additions and 49 deletions

View File

@ -276,6 +276,60 @@ void SetAllPower(uint32_t state, uint32_t source)
} }
} }
void SetPowerOnState(void)
{
if (MOTOR == my_module_type) {
Settings.poweronstate = POWER_ALL_ON; // Needs always on else in limbo!
}
if (POWER_ALL_ALWAYS_ON == Settings.poweronstate) {
SetDevicePower(1, SRC_RESTART);
} else {
if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) {
switch (Settings.poweronstate) {
case POWER_ALL_OFF:
case POWER_ALL_OFF_PULSETIME_ON:
power = 0;
SetDevicePower(power, SRC_RESTART);
break;
case POWER_ALL_ON: // All on
power = (1 << devices_present) -1;
SetDevicePower(power, SRC_RESTART);
break;
case POWER_ALL_SAVED_TOGGLE:
power = (Settings.power & ((1 << devices_present) -1)) ^ POWER_MASK;
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
break;
case POWER_ALL_SAVED:
power = Settings.power & ((1 << devices_present) -1);
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
break;
}
} else {
power = Settings.power & ((1 << devices_present) -1);
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
}
}
// Issue #526 and #909
for (uint32_t i = 0; i < devices_present; i++) {
if (!Settings.flag3.no_power_feedback) { // SetOption63 - Don't scan relay power state at restart - #5594 and #5663
if ((i < MAX_RELAYS) && (pin[GPIO_REL1 +i] < 99)) {
bitWrite(power, i, digitalRead(pin[GPIO_REL1 +i]) ^ bitRead(rel_inverted, i));
}
}
if ((i < MAX_PULSETIMERS) && (bitRead(power, i) || (POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate))) {
SetPulseTimer(i, Settings.pulse_timer[i]);
}
}
blink_powersave = power;
}
void SetLedPowerIdx(uint32_t led, uint32_t state) void SetLedPowerIdx(uint32_t led, uint32_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

View File

@ -276,54 +276,7 @@ void setup(void)
WifiConnect(); WifiConnect();
if (MOTOR == my_module_type) { Settings.poweronstate = POWER_ALL_ON; } // Needs always on else in limbo! SetPowerOnState();
if (POWER_ALL_ALWAYS_ON == Settings.poweronstate) {
SetDevicePower(1, SRC_RESTART);
} else {
if ((ResetReason() == REASON_DEFAULT_RST) || (ResetReason() == REASON_EXT_SYS_RST)) {
switch (Settings.poweronstate) {
case POWER_ALL_OFF:
case POWER_ALL_OFF_PULSETIME_ON:
power = 0;
SetDevicePower(power, SRC_RESTART);
break;
case POWER_ALL_ON: // All on
power = (1 << devices_present) -1;
SetDevicePower(power, SRC_RESTART);
break;
case POWER_ALL_SAVED_TOGGLE:
power = (Settings.power & ((1 << devices_present) -1)) ^ POWER_MASK;
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
break;
case POWER_ALL_SAVED:
power = Settings.power & ((1 << devices_present) -1);
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
break;
}
} else {
power = Settings.power & ((1 << devices_present) -1);
if (Settings.flag.save_state) { // SetOption0 - Save power state and use after restart
SetDevicePower(power, SRC_RESTART);
}
}
}
// Issue #526 and #909
for (uint32_t i = 0; i < devices_present; i++) {
if (!Settings.flag3.no_power_feedback) { // SetOption63 - Don't scan relay power state at restart - #5594 and #5663
if ((i < MAX_RELAYS) && (pin[GPIO_REL1 +i] < 99)) {
bitWrite(power, i, digitalRead(pin[GPIO_REL1 +i]) ^ bitRead(rel_inverted, i));
}
}
if ((i < MAX_PULSETIMERS) && (bitRead(power, i) || (POWER_ALL_OFF_PULSETIME_ON == Settings.poweronstate))) {
SetPulseTimer(i, Settings.pulse_timer[i]);
}
}
blink_powersave = power;
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_PROJECT " %s %s " D_VERSION " %s%s-" ARDUINO_ESP8266_RELEASE), PROJECT, SettingsText(SET_FRIENDLYNAME1), my_version, my_image); AddLog_P2(LOG_LEVEL_INFO, PSTR(D_PROJECT " %s %s " D_VERSION " %s%s-" ARDUINO_ESP8266_RELEASE), PROJECT, SettingsText(SET_FRIENDLYNAME1), my_version, my_image);
#ifdef FIRMWARE_MINIMAL #ifdef FIRMWARE_MINIMAL

View File

@ -794,7 +794,7 @@ bool Xdrv16(uint8_t function)
case FUNC_MODULE_INIT: case FUNC_MODULE_INIT:
result = TuyaModuleSelected(); result = TuyaModuleSelected();
break; break;
case FUNC_INIT: case FUNC_PRE_INIT:
TuyaInit(); TuyaInit();
break; break;
case FUNC_SET_DEVICE_POWER: case FUNC_SET_DEVICE_POWER: