Skip to content

Commit d09a3f3

Browse files
committed
call the ready callback only after the initial gpio value was read
1 parent a114ce2 commit d09a3f3

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

lib/gpio.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ var _testwrite = function (file, fn) {
7777
// fs.watch doesn't get fired because the file never
7878
// gets 'accessed' when setting header via hardware
7979
// manually watching value changes
80-
var FileWatcher = function (path, interval, fn) {
80+
var FileWatcher = function (path, interval, fn, readyCallback) {
8181
if (typeof fn === 'undefined') {
8282
fn = interval;
8383
interval = 100;
@@ -86,14 +86,20 @@ var FileWatcher = function (path, interval, fn) {
8686
if (typeof fn !== 'function') return false;
8787

8888
var value;
89-
var readTimer = setInterval(function () {
89+
function watchForChange() {
9090
_read(path, function (val) {
9191
if (value !== val) {
92-
if (typeof value !== 'undefined') fn(val);
92+
if (typeof value === 'undefined') {
93+
readyCallback();
94+
} else {
95+
fn(val);
96+
}
9397
value = val;
9498
}
9599
});
96-
}, interval);
100+
}
101+
watchForChange();
102+
var readTimer = setInterval(watchForChange, interval);
97103

98104
this.stop = function () {
99105
clearInterval(readTimer);
@@ -171,7 +177,7 @@ GPIO.prototype.setDirection = function (dir, fn) {
171177

172178
logMessage('Setting direction "' + dir + '" on gpio' + this.headerNum);
173179

174-
function watch() {
180+
function watch(watchFinished) {
175181
if (dir === 'in') {
176182
if (!self.valueWatcher) {
177183
// watch for value changes only for direction "in"
@@ -181,14 +187,17 @@ GPIO.prototype.setDirection = function (dir, fn) {
181187
self.value = val;
182188
self.emit("valueChange", val);
183189
self.emit("change", val);
184-
});
190+
}, watchFinished);
191+
} else {
192+
watchFinished();
185193
}
186194
} else {
187195
// if direction is "out", try to clear the valueWatcher
188196
if (self.valueWatcher) {
189197
self.valueWatcher.stop();
190198
self.valueWatcher = null;
191199
}
200+
watchFinished();
192201
}
193202
}
194203

@@ -201,12 +210,12 @@ GPIO.prototype.setDirection = function (dir, fn) {
201210
changedDirection = true;
202211
}
203212
_write(dir, path, function () {
204-
watch();
205-
206-
if (typeof fn === 'function') fn();
207-
if (changedDirection) {
208-
self.emit('directionChange', dir);
209-
}
213+
watch(function() {
214+
if (typeof fn === 'function') fn();
215+
if (changedDirection) {
216+
self.emit('directionChange', dir);
217+
}
218+
});
210219
}, 1);
211220

212221
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpio",
3-
"version": "0.3.0",
3+
"version": "0.3.1",
44
"author": {
55
"name": "Sebastian Alkemade",
66
"email": "[email protected]"

0 commit comments

Comments
 (0)