Skip to content

Commit

Permalink
Merge pull request #11 from rsporny/issue-7-move-slider-slowly
Browse files Browse the repository at this point in the history
issue #7: fix set position by slider
  • Loading branch information
rsporny authored May 18, 2018
2 parents 2b22075 + 688b9f1 commit f43b8c1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Sample accessory:
"durationDown": 13000,
"pinClosed": 17,
"pinOpen": 18,
"activeLow": false
"activeLow": false,
"reedSwitchActiveLow": false
}
]
```
Expand Down
10 changes: 5 additions & 5 deletions config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"pinUp": 5,
"pinDown": 11,
"durationUp": 13000,
"durationDown": 13000,
"activeLow": false
"durationDown": 13000
},
{
"accessory": "Blinds",
Expand All @@ -23,9 +22,10 @@
"pinDown": 3,
"durationUp": 27000,
"durationDown": 25000,
"pinClosed":17,
"pinOpen":18,
"activeLow": false
"pinClosed": 17,
"pinOpen": 18,
"activeLow": false,
"reedSwitchActiveLow": false
}
]
}
45 changes: 31 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ function BlindsAccessory(log, config) {
this.infoService
.setCharacteristic(Characteristic.Manufacturer, 'Radoslaw Sporny')
.setCharacteristic(Characteristic.Model, 'RaspberryPi GPIO Blinds')
.setCharacteristic(Characteristic.SerialNumber, 'Version 1.1.0');
.setCharacteristic(Characteristic.SerialNumber, 'Version 1.1.1');

this.finalBlindsStateTimeout;
this.togglePinTimeout;
this.intervalUp = this.durationUp / 100;
this.intervalDown = this.durationDown / 100;
this.currentPositionInterval;

// use gpio pin numbering
rpio.init({
Expand Down Expand Up @@ -93,11 +99,19 @@ BlindsAccessory.prototype.getTargetPosition = function(callback) {

BlindsAccessory.prototype.setTargetPosition = function(position, callback) {
this.log("Setting target position to %s", position);
this.targetPosition = position;
var moveUp = (this.targetPosition >= this.currentPosition);
var duration;

if (this.positionState != STATE_STOPPED) {
this.log('Blinds are moving. You need to wait. I will do nothing.');
callback();
return false;
this.log("Blind is moving, current position %s", this.currentPosition);
if (this.oppositeDirection(moveUp)) {
this.log('Stopping the blind because of opposite direction');
rpio.write((moveUp ? this.pinDown : this.pinUp), this.initialState);
}
clearInterval(this.currentPositionInterval);
clearTimeout(this.finalBlindsStateTimeout);
clearTimeout(this.togglePinTimeout);
}

if (this.currentPosition == position) {
Expand All @@ -106,36 +120,35 @@ BlindsAccessory.prototype.setTargetPosition = function(position, callback) {
return true;
}

this.targetPosition = position;
var moveUp = (this.targetPosition >= this.currentPosition);
var duration;
if (moveUp) {
duration = (this.targetPosition - this.currentPosition) / 100 * this.durationUp;
duration = Math.round((this.targetPosition - this.currentPosition) / 100 * this.durationUp);
this.currentPositionInterval = setInterval(function(){ this.currentPosition++; }.bind(this), this.intervalUp);
} else {
duration = (this.currentPosition - this.targetPosition) / 100 * this.durationDown;
duration = Math.round((this.currentPosition - this.targetPosition) / 100 * this.durationDown);
this.currentPositionInterval = setInterval(function(){ this.currentPosition--; }.bind(this), this.intervalDown);
}

this.log("Duration: %s ms", duration);
this.log(moveUp ? 'Moving up' : 'Moving down');
this.log((moveUp ? 'Moving up' : 'Moving down') + ". Duration: %s ms.", duration);

this.service.setCharacteristic(Characteristic.PositionState, (moveUp ? STATE_INCREASING : STATE_DECREASING));
this.positionState = (moveUp ? STATE_INCREASING : STATE_DECREASING);

setTimeout(this.setFinalBlindsState.bind(this), duration);
this.finalBlindsStateTimeout = setTimeout(this.setFinalBlindsState.bind(this), duration);
this.togglePin((moveUp ? this.pinUp : this.pinDown), duration);

callback();
return true;
}

BlindsAccessory.prototype.togglePin = function(pin, duration) {
rpio.write(pin, this.activeState);
setTimeout(function() {
if (rpio.read(pin) != this.activeState) rpio.write(pin, this.activeState);
this.togglePinTimeout = setTimeout(function() {
rpio.write(pin, this.initialState);
}.bind(this), duration);
}

BlindsAccessory.prototype.setFinalBlindsState = function() {
clearInterval(this.currentPositionInterval);
this.positionState = STATE_STOPPED;
this.service.setCharacteristic(Characteristic.PositionState, STATE_STOPPED);
this.service.setCharacteristic(Characteristic.CurrentPosition, this.targetPosition);
Expand All @@ -156,6 +169,10 @@ BlindsAccessory.prototype.partiallyOpenAndOutOfSync = function() {
(this.currentPosition == 100 && this.pinOpen && (rpio.read(this.pinOpen) != this.reedSwitchActiveState));
}

BlindsAccessory.prototype.oppositeDirection = function(moveUp) {
return (this.positionState == STATE_INCREASING && !moveUp) || (this.positionState == STATE_DECREASING && moveUp);
}

BlindsAccessory.prototype.getServices = function() {
return [this.infoService, this.service];
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-gpio-blinds",
"version": "1.1.0",
"version": "1.1.1",
"description": "Homebridge plugin to control blinds via Raspberry Pi GPIO pins",
"license": "MIT",
"keywords": [
Expand All @@ -9,7 +9,8 @@
"raspberry",
"gpio",
"blinds",
"window-blinds"
"window-blinds",
"homekit"
],
"repository": {
"type": "git",
Expand Down

0 comments on commit f43b8c1

Please sign in to comment.