Merge pull request #29 from hybridgroup/20-raspi-adaptor
20 raspi adaptor with GPIO support.
This commit is contained in:
commit
2a72ebbf6f
|
@ -51,34 +51,50 @@
|
|||
if (this.mode == null) {
|
||||
this.mode = mode;
|
||||
}
|
||||
return FS.writeFile("" + GPIO_PATH + "/export", "" + this.pinNum, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', 'Error while creating pin files');
|
||||
} else {
|
||||
return FS.exists(this._pinPath(), function(exists) {
|
||||
if (exists) {
|
||||
_this.self._setMode(_this.mode, true);
|
||||
return _this.self.emit('open');
|
||||
} else {
|
||||
return FS.writeFile(_this._exportPath(), "" + _this.pinNum, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', 'Error while creating pin files');
|
||||
} else {
|
||||
_this.self._setMode(_this.mode, true);
|
||||
return _this.self.emit('open');
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
DigitalPin.prototype.close = function() {
|
||||
var _this = this;
|
||||
return FS.writeFile("" + GPIO_PATH + "/unexport", "" + this.pinNum, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', 'Error while closing pin files');
|
||||
} else {
|
||||
return _this.self.emit('close');
|
||||
}
|
||||
return FS.writeFile(this.unexportPath(), "" + this.pinNum, function(err) {
|
||||
return _this._closeCallback();
|
||||
});
|
||||
};
|
||||
|
||||
DigitalPin.prototype.closeSync = function() {
|
||||
FS.writeFileSync(this.unexportPath(), "" + this.pinNum);
|
||||
return this._closeCallback(false);
|
||||
};
|
||||
|
||||
DigitalPin.prototype._closeCallback = function(err) {
|
||||
if (err) {
|
||||
return this.self.emit('error', 'Error while closing pin files');
|
||||
} else {
|
||||
return this.self.emit('close', this.pinNum);
|
||||
}
|
||||
};
|
||||
|
||||
DigitalPin.prototype.digitalWrite = function(value) {
|
||||
var _this = this;
|
||||
if (this.mode !== 'w') {
|
||||
this.self._setMode('w');
|
||||
}
|
||||
this.status = value === 1 ? 'high' : 'low';
|
||||
return FS.writeFile(this.pinFile, value, function(err) {
|
||||
return FS.writeFile(this._valuePath(), value, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
|
||||
} else {
|
||||
|
@ -95,7 +111,7 @@
|
|||
}
|
||||
readData = null;
|
||||
return setInterval(function() {
|
||||
return FS.readFile(_this.pinFile, function(err, data) {
|
||||
return FS.readFile(_this._valuePath(), function(err, data) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
|
||||
} else {
|
||||
|
@ -112,11 +128,10 @@
|
|||
emitConnect = false;
|
||||
}
|
||||
if (mode === 'w') {
|
||||
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_WRITE, function(err) {
|
||||
return FS.writeFile(this._directionPath(), GPIO_DIRECTION_WRITE, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Setting up pin direction failed");
|
||||
} else {
|
||||
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
|
||||
_this.ready = true;
|
||||
if (emitConnect) {
|
||||
return _this.self.emit('connect', mode);
|
||||
|
@ -124,11 +139,10 @@
|
|||
}
|
||||
});
|
||||
} else if (mode === 'r') {
|
||||
return FS.writeFile("" + GPIO_PATH + "/gpio" + this.pinNum + "/direction", GPIO_DIRECTION_READ, function(err) {
|
||||
return FS.writeFile(this._directionPath(), GPIO_DIRECTION_READ, function(err) {
|
||||
if (err) {
|
||||
return _this.self.emit('error', "Setting up pin direction failed");
|
||||
} else {
|
||||
_this.pinFile = "" + GPIO_PATH + "/gpio" + _this.pinNum + "/value";
|
||||
_this.ready = true;
|
||||
if (emitConnect) {
|
||||
return _this.self.emit('connect', mode);
|
||||
|
@ -138,6 +152,26 @@
|
|||
}
|
||||
};
|
||||
|
||||
DigitalPin.prototype._directionPath = function() {
|
||||
return "" + (this._pinPath()) + "/direction";
|
||||
};
|
||||
|
||||
DigitalPin.prototype._valuePath = function() {
|
||||
return "" + (this._pinPath()) + "/value";
|
||||
};
|
||||
|
||||
DigitalPin.prototype._pinPath = function() {
|
||||
return "" + GPIO_PATH + "/gpio" + this.pinNum;
|
||||
};
|
||||
|
||||
DigitalPin.prototype._exportPath = function() {
|
||||
return "" + GPIO_PATH + "/export";
|
||||
};
|
||||
|
||||
DigitalPin.prototype._unexportPath = function() {
|
||||
return "" + GPIO_PATH + "/unexport";
|
||||
};
|
||||
|
||||
DigitalPin.prototype.setHigh = function() {
|
||||
return this.self.digitalWrite(1);
|
||||
};
|
||||
|
|
|
@ -33,29 +33,43 @@ namespace 'Cylon.IO', ->
|
|||
|
||||
connect: (mode = null) ->
|
||||
@mode ?= mode
|
||||
# Creates the GPIO file to read/write from
|
||||
FS.writeFile("#{ GPIO_PATH }/export", "#{ @pinNum }", (err) =>
|
||||
if(err)
|
||||
@self.emit('error', 'Error while creating pin files')
|
||||
else
|
||||
# Check if the pin acceess file is already in the GPIO folder
|
||||
FS.exists(@_pinPath(), (exists) =>
|
||||
if exists
|
||||
@self._setMode(@mode, true)
|
||||
@self.emit('open')
|
||||
else
|
||||
# Creates the GPIO file to read/write from
|
||||
FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) =>
|
||||
if(err)
|
||||
@self.emit('error', 'Error while creating pin files')
|
||||
else
|
||||
@self._setMode(@mode, true)
|
||||
@self.emit('open')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
close: ->
|
||||
FS.writeFile("#{ GPIO_PATH }/unexport", "#{ @pinNum }", (err) =>
|
||||
if(err)
|
||||
@self.emit('error', 'Error while closing pin files')
|
||||
else
|
||||
@self.emit('close')
|
||||
FS.writeFile(@unexportPath(), "#{ @pinNum }", (err) =>
|
||||
@_closeCallback()
|
||||
)
|
||||
|
||||
closeSync: ->
|
||||
FS.writeFileSync(@unexportPath(), "#{ @pinNum }")
|
||||
@_closeCallback(false)
|
||||
|
||||
_closeCallback: (err) ->
|
||||
if(err)
|
||||
@self.emit('error', 'Error while closing pin files')
|
||||
else
|
||||
@self.emit('close', @pinNum)
|
||||
|
||||
digitalWrite: (value) ->
|
||||
@self._setMode('w') unless @mode == 'w'
|
||||
@status = if (value == 1) then 'high' else 'low'
|
||||
|
||||
FS.writeFile(@pinFile, value, (err) =>
|
||||
FS.writeFile(@_valuePath(), value, (err) =>
|
||||
if (err)
|
||||
@self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
|
||||
else
|
||||
|
@ -67,7 +81,7 @@ namespace 'Cylon.IO', ->
|
|||
readData = null
|
||||
|
||||
setInterval(() =>
|
||||
FS.readFile(@pinFile, (err, data) =>
|
||||
FS.readFile(@_valuePath(), (err, data) =>
|
||||
if err
|
||||
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
|
||||
else
|
||||
|
@ -79,24 +93,37 @@ namespace 'Cylon.IO', ->
|
|||
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
|
||||
_setMode: (mode, emitConnect = false) ->
|
||||
if mode == 'w'
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_WRITE, (err) =>
|
||||
FS.writeFile(@_directionPath(), GPIO_DIRECTION_WRITE, (err) =>
|
||||
if (err)
|
||||
@self.emit('error', "Setting up pin direction failed")
|
||||
else
|
||||
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
|
||||
@ready = true
|
||||
@self.emit('connect', mode) if emitConnect
|
||||
)
|
||||
else if mode =='r'
|
||||
FS.writeFile("#{ GPIO_PATH }/gpio#{ @pinNum }/direction", GPIO_DIRECTION_READ, (err) =>
|
||||
FS.writeFile(@_directionPath(), GPIO_DIRECTION_READ, (err) =>
|
||||
if (err)
|
||||
@self.emit('error', "Setting up pin direction failed")
|
||||
else
|
||||
@pinFile = "#{ GPIO_PATH }/gpio#{ @pinNum }/value"
|
||||
@ready = true
|
||||
@self.emit('connect', mode) if emitConnect
|
||||
)
|
||||
|
||||
_directionPath: () ->
|
||||
"#{ @_pinPath() }/direction"
|
||||
|
||||
_valuePath: () ->
|
||||
"#{ @_pinPath() }/value"
|
||||
|
||||
_pinPath: () ->
|
||||
"#{ GPIO_PATH }/gpio#{ @pinNum }"
|
||||
|
||||
_exportPath: () ->
|
||||
"#{ GPIO_PATH }/export"
|
||||
|
||||
_unexportPath: () ->
|
||||
"#{ GPIO_PATH }/unexport"
|
||||
|
||||
setHigh: ->
|
||||
@self.digitalWrite(1)
|
||||
|
||||
|
|
Loading…
Reference in New Issue