Browse Source

Allow defining reverse logic relays (issue #28)

fastled
Xose Pérez 7 years ago
parent
commit
b263ad8a6f
2 changed files with 32 additions and 10 deletions
  1. +17
    -0
      code/espurna/config/hardware.h
  2. +15
    -10
      code/espurna/relay.ino

+ 17
- 0
code/espurna/config/hardware.h View File

@ -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


+ 15
- 10
code/espurna/relay.ino View File

@ -11,7 +11,11 @@ Copyright (C) 2016 by Xose Pérez <xose dot perez at gmail dot com>
#include <ArduinoJson.h>
#include <vector>
std::vector<unsigned char> _relays;
typedef struct {
unsigned char pin;
bool reverse;
} relay_t;
std::vector<relay_t> _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);
}


Loading…
Cancel
Save