From c39e7d13a4e02128f96796321dc304eb2255ad40 Mon Sep 17 00:00:00 2001 From: Radoslaw Sporny Date: Sat, 3 Feb 2018 12:23:52 +0100 Subject: [PATCH] issue #6: add reed switch option --- README.md | 7 ++++++- config-sample.json | 2 ++ index.js | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c5693c..9d7561d 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Sample accessory: "pinDown": 11, "durationUp": 13000, "durationDown": 13000, + "pinClosed": 17, + "pinOpen": 18, "activeLow": false } ] @@ -35,7 +37,10 @@ Fields: - `pinDown` pin for moving down - `durationUp` milliseconds to open blinds completely - `durationDown` milliseconds to close blinds completely -- `activeLow` set to false if your relay is activated by high state (default: *true*) +- `pinClosed` [optional] pin connected to reed switch which is active when blind is closed, see *reedActiveLow* +- `pinOpen` [optional] pin connected to reed switch which is active when blind is open, see *reedActiveLow* +- `activeLow` [optional, default: *true*] true: relay activated by low state (0), false: relay activated by high state (1), affects *pinUp*, *pinDown* +- `reedSwitchActiveLow` [optional, default: *true*] true: reed switch activated by low state (0), false: reed switch activated by high state (1), affects *pinClosed*, *pinOpen* ## Raspberry Pi setup - Raspberry Pi 3 (should work with all versions) diff --git a/config-sample.json b/config-sample.json index f92da62..64f1744 100644 --- a/config-sample.json +++ b/config-sample.json @@ -23,6 +23,8 @@ "pinDown": 3, "durationUp": 27000, "durationDown": 25000, + "pinClosed":17, + "pinOpen":18, "activeLow": false } ] diff --git a/index.js b/index.js index bcb6296..52524a2 100644 --- a/index.js +++ b/index.js @@ -14,7 +14,7 @@ module.exports = function(homebridge) { } function BlindsAccessory(log, config) { - _.defaults(config, {activeLow: true}); + _.defaults(config, {activeLow: true, reedSwitchActiveLow: true}); this.log = log; this.name = config['name']; @@ -22,8 +22,11 @@ function BlindsAccessory(log, config) { this.pinDown = config['pinDown']; this.durationUp = config['durationUp']; this.durationDown = config['durationDown']; + this.pinClosed = config['pinClosed']; + this.pinOpen = config['pinOpen']; this.initialState = config['activeLow'] ? rpio.HIGH : rpio.LOW; this.activeState = config['activeLow'] ? rpio.LOW : rpio.HIGH; + this.reedSwitchActiveState = config['reedSwitchActiveLow'] ? rpio.LOW : rpio.HIGH; this.currentPosition = 0; // down by default this.targetPosition = 0; // down by default @@ -35,7 +38,7 @@ function BlindsAccessory(log, config) { this.infoService .setCharacteristic(Characteristic.Manufacturer, 'Radoslaw Sporny') .setCharacteristic(Characteristic.Model, 'RaspberryPi GPIO Blinds') - .setCharacteristic(Characteristic.SerialNumber, 'Version 1.0.0'); + .setCharacteristic(Characteristic.SerialNumber, 'Version 1.0.1'); // use gpio pin numbering rpio.init({ @@ -43,6 +46,8 @@ function BlindsAccessory(log, config) { }); rpio.open(this.pinUp, rpio.OUTPUT, this.initialState); rpio.open(this.pinDown, rpio.OUTPUT, this.initialState); + if (this.pinClosed) rpio.open(this.pinClosed, rpio.INPUT, rpio.PULL_UP); + if (this.pinOpen) rpio.open(this.pinOpen, rpio.INPUT, rpio.PULL_UP); this.service .getCharacteristic(Characteristic.CurrentPosition) @@ -69,6 +74,19 @@ BlindsAccessory.prototype.getCurrentPosition = function(callback) { } BlindsAccessory.prototype.getTargetPosition = function(callback) { + if (this.closedAndOutOfSync()) { + this.log("Current position is out of sync, setting to 0"); + this.currentPosition = 0; + this.targetPosition = 0; + } else if (this.openAndOutOfSync()) { + this.log("Current position is out of sync, setting to 100"); + this.currentPosition = 100; + this.targetPosition = 100; + } else if (this.partiallyOpenAndOutOfSync()) { + this.log("Current position is out of sync, setting to 50"); + this.currentPosition = 50; + this.targetPosition = 50; + } this.log("Target position: %s", this.targetPosition); callback(null, this.targetPosition); } @@ -125,6 +143,19 @@ BlindsAccessory.prototype.setFinalBlindsState = function() { this.log("Successfully moved to target position: %s", this.targetPosition); } +BlindsAccessory.prototype.closedAndOutOfSync = function() { + return this.currentPosition != 0 && this.pinClosed && (rpio.read(this.pinClosed) == this.reedSwitchActiveState); +} + +BlindsAccessory.prototype.openAndOutOfSync = function() { + return this.currentPosition != 100 && this.pinOpen && (rpio.read(this.pinOpen) == this.reedSwitchActiveState); +} + +BlindsAccessory.prototype.partiallyOpenAndOutOfSync = function() { + return (this.currentPosition == 0 && this.pinClosed && (rpio.read(this.pinClosed) != this.reedSwitchActiveState)) || + (this.currentPosition == 100 && this.pinOpen && (rpio.read(this.pinOpen) != this.reedSwitchActiveState)); +} + BlindsAccessory.prototype.getServices = function() { return [this.infoService, this.service]; }