From b263ad8a6f3f6f081f14daefd3c13cceab888b64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 6 Jan 2017 23:24:35 +0100 Subject: [PATCH] Allow defining reverse logic relays (issue #28) --- code/espurna/config/hardware.h | 17 +++++++++++++++++ code/espurna/relay.ino | 25 +++++++++++++++---------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index a3024a28..a2748b94 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -39,6 +39,7 @@ #define DEVICE "LOLIN" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 #define LED1_PIN_INVERSE 1 @@ -47,6 +48,7 @@ #define MANUFACTURER "WEMOS" #define DEVICE "D1_MINI" #define RELAY1_PIN 5 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 #define LED1_PIN_INVERSE 1 @@ -60,6 +62,7 @@ #define DEVICE "SONOFF" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -69,6 +72,7 @@ #define DEVICE "SONOFF_TH" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -78,6 +82,7 @@ #define DEVICE "SONOFF_SV" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -87,6 +92,7 @@ #define DEVICE "SLAMPHER" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -96,6 +102,7 @@ #define DEVICE "S20" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -105,6 +112,7 @@ #define DEVICE "SONOFF_TOUCH" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -114,6 +122,7 @@ #define DEVICE "SONOFF_POW" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 15 #define LED1_PIN_INVERSE 0 #define ENABLE_POW 1 @@ -137,9 +146,13 @@ #define BUTTON3_PIN 10 #define BUTTON4_PIN 14 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 5 + #define RELAY2_PIN_INVERSE 0 #define RELAY3_PIN 4 + #define RELAY3_PIN_INVERSE 0 #define RELAY4_PIN 15 + #define RELAY4_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 1 @@ -154,7 +167,9 @@ #define BUTTON1_PIN 0 #define BUTTON2_PIN 2 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define RELAY2_PIN 13 + #define RELAY2_PIN_INVERSE 0 #define LED1_PIN 16 #define LED1_PIN_INVERSE 0 @@ -168,6 +183,7 @@ #define DEVICE "ECOPLUG" #define BUTTON1_PIN 13 #define RELAY1_PIN 15 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 2 #define LED1_PIN_INVERSE 0 @@ -181,6 +197,7 @@ #define DEVICE "ESPURNA" #define BUTTON1_PIN 0 #define RELAY1_PIN 12 + #define RELAY1_PIN_INVERSE 0 #define LED1_PIN 13 #define LED1_PIN_INVERSE 0 diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index a0d11cb8..d7c7693d 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -11,7 +11,11 @@ Copyright (C) 2016 by Xose PĂ©rez #include #include -std::vector _relays; +typedef struct { + unsigned char pin; + bool reverse; +} relay_t; +std::vector _relays; bool recursive = false; #ifdef SONOFF_DUAL unsigned char dualRelayStatus = 0; @@ -58,7 +62,8 @@ bool relayStatus(unsigned char id) { return ((dualRelayStatus & (1 << id)) > 0); #else if (id >= _relays.size()) return false; - return (digitalRead(_relays[id]) == HIGH); + bool status = (digitalRead(_relays[id].pin) == HIGH); + return _relays[id].reverse ? !status : status; #endif } @@ -84,7 +89,7 @@ bool relayStatus(unsigned char id, bool status, bool report) { Serial.flush(); #else - digitalWrite(_relays[id], status); + digitalWrite(_relays[id].pin, _relays[id].reverse ? !status : status); #endif if (!recursive) { @@ -218,22 +223,22 @@ void relaySetup() { #ifdef SONOFF_DUAL // Two dummy relays for the dual - _relays.push_back(0); - _relays.push_back(0); + _relays.push_back((relay_t) {0, 0}); + _relays.push_back((relay_t) {0, 0}); #else #ifdef RELAY1_PIN - _relays.push_back(RELAY1_PIN); + _relays.push_back((relay_t) { RELAY1_PIN, RELAY1_PIN_INVERSE }); #endif #ifdef RELAY2_PIN - _relays.push_back(RELAY2_PIN); + _relays.push_back((relay_t) { RELAY2_PIN, RELAY2_PIN_INVERSE }); #endif #ifdef RELAY3_PIN - _relays.push_back(RELAY3_PIN); + _relays.push_back((relay_t) { RELAY3_PIN, RELAY3_PIN_INVERSE }); #endif #ifdef RELAY4_PIN - _relays.push_back(RELAY4_PIN); + _relays.push_back((relay_t) { RELAY4_PIN, RELAY4_PIN_INVERSE }); #endif #endif @@ -242,7 +247,7 @@ void relaySetup() { byte relayMode = getSetting("relayMode", RELAY_MODE).toInt(); for (unsigned int i=0; i < _relays.size(); i++) { - pinMode(_relays[i], OUTPUT); + pinMode(_relays[i].pin, OUTPUT); if (relayMode == RELAY_MODE_OFF) relayStatus(i, false); if (relayMode == RELAY_MODE_ON) relayStatus(i, true); }