From 5c49db89af1b2410421de5bef2a9934ddbfa5de3 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Wed, 27 Nov 2019 01:06:35 +0300 Subject: [PATCH] relay: compare sum of physical + dummy relays instead of just dummy (#2012) * relay: compare sum of physical + dummy relays instead of just dummy * typo --- code/espurna/relay.h | 2 +- code/espurna/relay.ino | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/code/espurna/relay.h b/code/espurna/relay.h index b951ee6f..69913219 100644 --- a/code/espurna/relay.h +++ b/code/espurna/relay.h @@ -68,5 +68,5 @@ const String& relayPayloadToggle(); const char* relayPayload(RelayStatus status); -void relaySetupDummy(unsigned char size, bool reconfigure = false); +void relaySetupDummy(size_t size, bool reconfigure = false); diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index 38ee8cf7..0e2a44c0 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -82,7 +82,7 @@ struct relay_t { std::vector _relays; bool _relayRecursive = false; -uint8_t _relayDummy = DUMMY_RELAY_COUNT; +size_t _relayDummy = 0; unsigned long _relay_flood_window = (1000 * RELAY_FLOOD_WINDOW); unsigned long _relay_flood_changes = RELAY_FLOOD_CHANGES; @@ -256,7 +256,7 @@ void _relayProviderStatus(unsigned char id, bool status) { #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT // Real relays - uint8_t physical = _relays.size() - _relayDummy; + size_t physical = _relays.size() - _relayDummy; // Support for a mixed of dummy and real relays // Reference: https://github.com/xoseperez/espurna/issues/1305 @@ -1360,13 +1360,15 @@ void _relayLoop() { } // Dummy relays for virtual light switches, Sonoff Dual, Sonoff RF Bridge and Tuya -void relaySetupDummy(unsigned char size, bool reconfigure) { +void relaySetupDummy(size_t size, bool reconfigure) { - size = constrain(size + _relays.size(), _relays.size(), RELAYS_MAX); - if (size == _relays.size()) return; + if (size == _relayDummy) return; + + const size_t new_size = ((_relays.size() - _relayDummy) + size); + if (new_size > RELAYS_MAX) return; _relayDummy = size; - _relays.resize(size); + _relays.resize(new_size); if (reconfigure) { _relayConfigure();