@@ -77,7 +77,7 @@ var _testwrite = function (file, fn) {
77
77
// fs.watch doesn't get fired because the file never
78
78
// gets 'accessed' when setting header via hardware
79
79
// manually watching value changes
80
- var FileWatcher = function ( path , interval , fn ) {
80
+ var FileWatcher = function ( path , interval , fn , readyCallback ) {
81
81
if ( typeof fn === 'undefined' ) {
82
82
fn = interval ;
83
83
interval = 100 ;
@@ -86,14 +86,20 @@ var FileWatcher = function (path, interval, fn) {
86
86
if ( typeof fn !== 'function' ) return false ;
87
87
88
88
var value ;
89
- var readTimer = setInterval ( function ( ) {
89
+ function watchForChange ( ) {
90
90
_read ( path , function ( val ) {
91
91
if ( value !== val ) {
92
- if ( typeof value !== 'undefined' ) fn ( val ) ;
92
+ if ( typeof value === 'undefined' ) {
93
+ readyCallback ( ) ;
94
+ } else {
95
+ fn ( val ) ;
96
+ }
93
97
value = val ;
94
98
}
95
99
} ) ;
96
- } , interval ) ;
100
+ }
101
+ watchForChange ( ) ;
102
+ var readTimer = setInterval ( watchForChange , interval ) ;
97
103
98
104
this . stop = function ( ) {
99
105
clearInterval ( readTimer ) ;
@@ -171,7 +177,7 @@ GPIO.prototype.setDirection = function (dir, fn) {
171
177
172
178
logMessage ( 'Setting direction "' + dir + '" on gpio' + this . headerNum ) ;
173
179
174
- function watch ( ) {
180
+ function watch ( watchFinished ) {
175
181
if ( dir === 'in' ) {
176
182
if ( ! self . valueWatcher ) {
177
183
// watch for value changes only for direction "in"
@@ -181,14 +187,17 @@ GPIO.prototype.setDirection = function (dir, fn) {
181
187
self . value = val ;
182
188
self . emit ( "valueChange" , val ) ;
183
189
self . emit ( "change" , val ) ;
184
- } ) ;
190
+ } , watchFinished ) ;
191
+ } else {
192
+ watchFinished ( ) ;
185
193
}
186
194
} else {
187
195
// if direction is "out", try to clear the valueWatcher
188
196
if ( self . valueWatcher ) {
189
197
self . valueWatcher . stop ( ) ;
190
198
self . valueWatcher = null ;
191
199
}
200
+ watchFinished ( ) ;
192
201
}
193
202
}
194
203
@@ -201,12 +210,12 @@ GPIO.prototype.setDirection = function (dir, fn) {
201
210
changedDirection = true ;
202
211
}
203
212
_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
+ } ) ;
210
219
} , 1 ) ;
211
220
212
221
} ) ;
0 commit comments