Adding interval reads to digitalRead.

This commit is contained in:
Edgar O Silva 2013-11-06 12:27:36 -06:00
parent c370041329
commit 250a0d0166
3 changed files with 16 additions and 11 deletions

16
dist/digital-pin.js vendored
View File

@ -87,24 +87,28 @@
});
};
DigitalPin.prototype.digitalRead = function() {
var readData,
_this = this;
DigitalPin.prototype.digitalRead = function(interval) {
var readData;
if (this.mode !== 'r') {
this.self._setMode('r');
}
readData = null;
FS.readFile(this.pinFile, function(err, data) {
return setInterval(this._readFileCallback, interval);
};
DigitalPin.prototype._readFileCallback = function() {
var _this = this;
return FS.readFile(this.pinFile, function(err, data) {
var readData;
if (err) {
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
} else {
readData = data;
console.log("Digital read VALUE ===>");
console.log(data);
console.log(data.toString());
return _this.self.emit('digitalRead', data);
}
});
return readData;
};
DigitalPin.prototype._setMode = function(mode, emitConnect) {

View File

@ -12,6 +12,6 @@ Cylon.robot
]
work: (my) ->
my.button.on 'push', -> my.led.toggle()
my.button.on 'push', console.log("Reading pin...")
.start()

View File

@ -62,22 +62,23 @@ namespace 'Cylon.IO', ->
@self.emit('digitalWrite', value)
)
digitalRead: ->
digitalRead: (interval) ->
@self._setMode('r') unless @mode == 'r'
readData = null
setInterval(@_readFileCallback, interval)
_readFileCallback: ->
FS.readFile(@pinFile, (err, data) =>
if err
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
else
readData = data
console.log("Digital read VALUE ===>")
console.log(data)
console.log(data.toString())
@self.emit('digitalRead', data)
)
readData
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
_setMode: (mode, emitConnect = false) ->
if mode == 'w'