Adding interval reads to digitalRead.
This commit is contained in:
parent
c370041329
commit
250a0d0166
|
@ -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) {
|
||||
|
|
|
@ -12,6 +12,6 @@ Cylon.robot
|
|||
]
|
||||
|
||||
work: (my) ->
|
||||
my.button.on 'push', -> my.led.toggle()
|
||||
my.button.on 'push', console.log("Reading pin...")
|
||||
|
||||
.start()
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue