Skip to content
This repository has been archived by the owner on Mar 6, 2022. It is now read-only.

delay functionality when switching both, via HDMI and a relay #86

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions MMM-PIR-Sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ Module.register('MMM-PIR-Sensor',{
sensorPin: 22,
sensorState: 1,
relayPin: false,
switchHDMI: true,
relayOnDelay: 0,
hdmiOffDelay: 0,
relayState: 1,
alwaysOnPin: false,
alwaysOnState: 1,
Expand Down
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,33 @@ The following properties can be configured:
</tr>
<tr>
<td><code>relayPin</code></td>
<td>If you want to use a relay to turn of the mirror provide the pin here. If no pin is provided HDMI is turned off instead.<br>
<td>If you want to use a relay to turn of the mirror provide the pin here.<br>
<br><b>Possible values:</b> <code>int</code>
<br><b>Default value:</b> <code>false</code>
<br><b>Note:</b> Please use BCM-numbering.
</td>
</tr>
<tr>
<td><code>switchHDMI</code></td>
<td>If you want to use HDMI to turn the monitor on and off.<br>
<br><b>Possible values:</b> <code>boolean</code>
<br><b>Default value:</b> <code>true</code>
</td>
</tr>
<tr>
<td><code>relayOnDelay</code></td>
<td>Delay before switching on the relay after motion was detected. This delay is only applied when the monitor was switched off via HDMI before.<br>
<br><b>Possible values:</b> <code>int (ms)</code>
<br><b>Default value:</b> <code>0</code>
</td>
</tr>
<tr>
<td><code>hdmiOffDelay</code></td>
<td>Delay before switching off the monitor via HDMI after no motion was detected.<br>
<br><b>Possible values:</b> <code>int (ms)</code>
<br><b>Default value:</b> <code>0</code>
</td>
</tr>
<tr>
<td><code>relayState</code></td>
<td>GPIO-state your relay is turned on.<br>
Expand Down
67 changes: 51 additions & 16 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,79 @@
const NodeHelper = require('node_helper');
const Gpio = require('onoff').Gpio;
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;

module.exports = NodeHelper.create({
start: function () {
this.started = false;
},

activateMonitor: function () {
let self = this;
// If always-off is enabled, keep monitor deactivated
let alwaysOffTrigger = this.alwaysOff && (this.alwaysOff.readSync() === this.config.alwaysOffState)
let alwaysOffTrigger = self.alwaysOff && (self.alwaysOff.readSync() === self.config.alwaysOffState)
if (alwaysOffTrigger) {
return;
}
// If relays are being used in place of HDMI
if (this.config.relayPin !== false) {
this.relay.writeSync(this.config.relayState);
}
else if (this.config.relayPin === false) {

if (self.config.switchHDMI === true) {
// cancle any scheduled off events
if(self.hdmiOffTimeout !== undefined) {
clearTimeout(self.hdmiOffTimeout);
self.hdmiOffTimeout = undefined;
}

// Check if hdmi output is already on
exec("/usr/bin/vcgencmd display_power").stdout.on('data', function(data) {
if (data.indexOf("display_power=0") === 0)
exec("/usr/bin/vcgencmd display_power 1", null);
});
let displayOff = execSync("/usr/bin/vcgencmd display_power").indexOf("display_power=0") === 0
if (displayOff){
exec("/usr/bin/vcgencmd display_power 1", null);
}

let switchOnDelay = displayOff ? self.config.relayOnDelay : 0;

// If relays are being used
if (self.config.relayPin !== false) {
// check if a switch on is already scheduled
if(self.relayOnTimeout === undefined) {
self.relayOnTimeout = setTimeout(function() {
self.relay.writeSync(self.config.relayState);
self.relayOnTimeout = undefined;
}, switchOnDelay);
}
}
} else {
//switch the relay immediately
self.relay.writeSync(self.config.relayState);
}
},

deactivateMonitor: function () {
let self = this;
// If always-on is enabled, keep monitor activated
let alwaysOnTrigger = this.alwaysOn && (this.alwaysOn.readSync() === this.config.alwaysOnState)
let alwaysOffTrigger = this.alwaysOff && (this.alwaysOff.readSync() === this.config.alwaysOffState)
let alwaysOnTrigger = self.alwaysOn && (self.alwaysOn.readSync() === self.config.alwaysOnState)
let alwaysOffTrigger = self.alwaysOff && (self.alwaysOff.readSync() === self.config.alwaysOffState)
if (alwaysOnTrigger && !alwaysOffTrigger) {
return;
}
// If relays are being used in place of HDMI
if (this.config.relayPin !== false) {
this.relay.writeSync((this.config.relayState + 1) % 2);
if (self.config.relayPin !== false) {
// cancel any scheduled turn-on events
if(self.relayOnTimeout !== undefined) {
clearTimeout(self.relayOnTimeout);
self.relayOnTimeout = undefined;
}

self.relay.writeSync((self.config.relayState + 1) % 2);
}
else if (this.config.relayPin === false) {
exec("/usr/bin/vcgencmd display_power 0", null);

if (self.config.switchHDMI === true) {
// check if a switch off is already scheduled
if(self.hdmiOffTimeout === undefined) {
self.hdmiOffTimeout = setTimeout(function() {
exec("/usr/bin/vcgencmd display_power 0", null);
self.hdmiOffTimeout = undefined;
}, self.config.hdmiOffDelay);
}
}
},

Expand Down