Skip to content

Commit f43b8c1

Browse files
authored
Merge pull request #11 from rsporny/issue-7-move-slider-slowly
issue #7: fix set position by slider
2 parents 2b22075 + 688b9f1 commit f43b8c1

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ Sample accessory:
2424
"durationDown": 13000,
2525
"pinClosed": 17,
2626
"pinOpen": 18,
27-
"activeLow": false
27+
"activeLow": false,
28+
"reedSwitchActiveLow": false
2829
}
2930
]
3031
```

config-sample.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"pinUp": 5,
1414
"pinDown": 11,
1515
"durationUp": 13000,
16-
"durationDown": 13000,
17-
"activeLow": false
16+
"durationDown": 13000
1817
},
1918
{
2019
"accessory": "Blinds",
@@ -23,9 +22,10 @@
2322
"pinDown": 3,
2423
"durationUp": 27000,
2524
"durationDown": 25000,
26-
"pinClosed":17,
27-
"pinOpen":18,
28-
"activeLow": false
25+
"pinClosed": 17,
26+
"pinOpen": 18,
27+
"activeLow": false,
28+
"reedSwitchActiveLow": false
2929
}
3030
]
3131
}

index.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ function BlindsAccessory(log, config) {
3838
this.infoService
3939
.setCharacteristic(Characteristic.Manufacturer, 'Radoslaw Sporny')
4040
.setCharacteristic(Characteristic.Model, 'RaspberryPi GPIO Blinds')
41-
.setCharacteristic(Characteristic.SerialNumber, 'Version 1.1.0');
41+
.setCharacteristic(Characteristic.SerialNumber, 'Version 1.1.1');
42+
43+
this.finalBlindsStateTimeout;
44+
this.togglePinTimeout;
45+
this.intervalUp = this.durationUp / 100;
46+
this.intervalDown = this.durationDown / 100;
47+
this.currentPositionInterval;
4248

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

94100
BlindsAccessory.prototype.setTargetPosition = function(position, callback) {
95101
this.log("Setting target position to %s", position);
102+
this.targetPosition = position;
103+
var moveUp = (this.targetPosition >= this.currentPosition);
104+
var duration;
96105

97106
if (this.positionState != STATE_STOPPED) {
98-
this.log('Blinds are moving. You need to wait. I will do nothing.');
99-
callback();
100-
return false;
107+
this.log("Blind is moving, current position %s", this.currentPosition);
108+
if (this.oppositeDirection(moveUp)) {
109+
this.log('Stopping the blind because of opposite direction');
110+
rpio.write((moveUp ? this.pinDown : this.pinUp), this.initialState);
111+
}
112+
clearInterval(this.currentPositionInterval);
113+
clearTimeout(this.finalBlindsStateTimeout);
114+
clearTimeout(this.togglePinTimeout);
101115
}
102116

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

109-
this.targetPosition = position;
110-
var moveUp = (this.targetPosition >= this.currentPosition);
111-
var duration;
112123
if (moveUp) {
113-
duration = (this.targetPosition - this.currentPosition) / 100 * this.durationUp;
124+
duration = Math.round((this.targetPosition - this.currentPosition) / 100 * this.durationUp);
125+
this.currentPositionInterval = setInterval(function(){ this.currentPosition++; }.bind(this), this.intervalUp);
114126
} else {
115-
duration = (this.currentPosition - this.targetPosition) / 100 * this.durationDown;
127+
duration = Math.round((this.currentPosition - this.targetPosition) / 100 * this.durationDown);
128+
this.currentPositionInterval = setInterval(function(){ this.currentPosition--; }.bind(this), this.intervalDown);
116129
}
117130

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

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

124-
setTimeout(this.setFinalBlindsState.bind(this), duration);
136+
this.finalBlindsStateTimeout = setTimeout(this.setFinalBlindsState.bind(this), duration);
125137
this.togglePin((moveUp ? this.pinUp : this.pinDown), duration);
126138

127139
callback();
128140
return true;
129141
}
130142

131143
BlindsAccessory.prototype.togglePin = function(pin, duration) {
132-
rpio.write(pin, this.activeState);
133-
setTimeout(function() {
144+
if (rpio.read(pin) != this.activeState) rpio.write(pin, this.activeState);
145+
this.togglePinTimeout = setTimeout(function() {
134146
rpio.write(pin, this.initialState);
135147
}.bind(this), duration);
136148
}
137149

138150
BlindsAccessory.prototype.setFinalBlindsState = function() {
151+
clearInterval(this.currentPositionInterval);
139152
this.positionState = STATE_STOPPED;
140153
this.service.setCharacteristic(Characteristic.PositionState, STATE_STOPPED);
141154
this.service.setCharacteristic(Characteristic.CurrentPosition, this.targetPosition);
@@ -156,6 +169,10 @@ BlindsAccessory.prototype.partiallyOpenAndOutOfSync = function() {
156169
(this.currentPosition == 100 && this.pinOpen && (rpio.read(this.pinOpen) != this.reedSwitchActiveState));
157170
}
158171

172+
BlindsAccessory.prototype.oppositeDirection = function(moveUp) {
173+
return (this.positionState == STATE_INCREASING && !moveUp) || (this.positionState == STATE_DECREASING && moveUp);
174+
}
175+
159176
BlindsAccessory.prototype.getServices = function() {
160177
return [this.infoService, this.service];
161178
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "homebridge-gpio-blinds",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Homebridge plugin to control blinds via Raspberry Pi GPIO pins",
55
"license": "MIT",
66
"keywords": [
@@ -9,7 +9,8 @@
99
"raspberry",
1010
"gpio",
1111
"blinds",
12-
"window-blinds"
12+
"window-blinds",
13+
"homekit"
1314
],
1415
"repository": {
1516
"type": "git",

0 commit comments

Comments
 (0)