From 4dd0f358b4ea42d076974a1a1d3b00799366b3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 24 Apr 2017 10:47:16 +0200 Subject: [PATCH] Backend for RELAY_MODE_FOLLOW --- code/espurna/button.ino | 12 ++++++++++++ code/espurna/config/general.h | 2 ++ code/espurna/relay.ino | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/code/espurna/button.ino b/code/espurna/button.ino index 5bd75918..5acaacbb 100644 --- a/code/espurna/button.ino +++ b/code/espurna/button.ino @@ -30,6 +30,18 @@ void buttonMQTT(unsigned char id, uint8_t event) { } #endif +int buttonFromRelay(unsigned int relayID) { + for (unsigned int i=0; i < _buttons.size(); i++) { + if (_buttons[i].relayID == relayID) return i; + } + return -1; +} + +bool buttonState(unsigned char id) { + if (id >= _buttons.size()) return false; + return _buttons[id].button->pressed(); +} + unsigned char buttonAction(unsigned char id, unsigned char event) { if (id >= _buttons.size()) return BUTTON_MODE_NONE; unsigned long actions = _buttons[id].actions; diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 26696471..90a1ccef 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -62,6 +62,7 @@ #define RELAY_MODE_ON 1 #define RELAY_MODE_SAME 2 #define RELAY_MODE_TOOGLE 3 +#define RELAY_MODE_FOLLOW 4 #define RELAY_SYNC_ANY 0 #define RELAY_SYNC_NONE_OR_ONE 1 @@ -90,6 +91,7 @@ // Relay requests flood protection window - in seconds #define RELAY_FLOOD_WINDOW 3 + // Allowed actual relay changes inside requests flood protection window #define RELAY_FLOOD_CHANGES 5 diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index d571f97f..a4b0daaf 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -477,6 +477,11 @@ void relaySetup() { pinMode(_relays[i].pin, OUTPUT); if (relayMode == RELAY_MODE_OFF) relayStatus(i, false); if (relayMode == RELAY_MODE_ON) relayStatus(i, true); + if (relayMode == RELAY_MODE_FOLLOW) { + // retrieve the status of the linked button + int buttonID = buttonFromRelay(i); + relayStatus(i, (buttonID >=0) ? buttonState(buttonID) : false); + } } if (relayMode == RELAY_MODE_SAME) relayRetrieve(false); if (relayMode == RELAY_MODE_TOOGLE) relayRetrieve(true);