Refactor digital pin.

This commit is contained in:
Edgar O Silva 2013-11-12 11:47:25 -06:00
parent 2a72ebbf6f
commit 11fda85e56
2 changed files with 100 additions and 92 deletions

105
dist/digital-pin.js vendored
View File

@ -53,17 +53,9 @@
}
return FS.exists(this._pinPath(), function(exists) {
if (exists) {
_this.self._setMode(_this.mode, true);
return _this.self.emit('open');
return _this._openPin();
} 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');
}
});
return _this._createGPIOPin();
}
});
};
@ -80,14 +72,6 @@
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') {
@ -122,6 +106,46 @@
}, interval);
};
DigitalPin.prototype.setHigh = function() {
return this.self.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.self.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.self.setHigh();
} else {
return this.self.setLow();
}
};
DigitalPin.prototype._createGPIOPin = function() {
var _this = this;
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) {
if (err) {
return _this.self.emit('error', 'Error while creating pin files');
} else {
return _this._openPin();
}
});
};
DigitalPin.prototype._openPin = function() {
this.self._setMode(this.mode, true);
return this.self.emit('open');
};
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._setMode = function(mode, emitConnect) {
var _this = this;
if (emitConnect == null) {
@ -129,29 +153,26 @@
}
if (mode === 'w') {
return FS.writeFile(this._directionPath(), GPIO_DIRECTION_WRITE, function(err) {
if (err) {
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_this.ready = true;
if (emitConnect) {
return _this.self.emit('connect', mode);
}
}
return _this._setModeCallback();
});
} else if (mode === 'r') {
return FS.writeFile(this._directionPath(), GPIO_DIRECTION_READ, function(err) {
if (err) {
return _this.self.emit('error', "Setting up pin direction failed");
} else {
_this.ready = true;
if (emitConnect) {
return _this.self.emit('connect', mode);
}
}
return _this._setModeCallback();
});
}
};
DigitalPin.prototype._setModeCallback = function() {
if (err) {
return this.self.emit('error', "Setting up pin direction failed");
} else {
this.ready = true;
if (emitConnect) {
return this.self.emit('connect', mode);
}
}
};
DigitalPin.prototype._directionPath = function() {
return "" + (this._pinPath()) + "/direction";
};
@ -172,22 +193,6 @@
return "" + GPIO_PATH + "/unexport";
};
DigitalPin.prototype.setHigh = function() {
return this.self.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.self.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.self.setHigh();
} else {
return this.self.setLow();
}
};
return DigitalPin;
})(EventEmitter);

View File

@ -36,20 +36,11 @@ namespace 'Cylon.IO', ->
# 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')
@_openPin()
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')
)
@_createGPIOPin()
)
close: ->
FS.writeFile(@unexportPath(), "#{ @pinNum }", (err) =>
@_closeCallback()
@ -59,12 +50,6 @@ namespace 'Cylon.IO', ->
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'
@ -76,6 +61,9 @@ namespace 'Cylon.IO', ->
@self.emit('digitalWrite', value)
)
# Reads the pin input every interval amount of time:
# params:
# interval: amount in miliseconds
digitalRead: (interval) ->
@self._setMode('r') unless @mode == 'r'
readData = null
@ -90,24 +78,50 @@ namespace 'Cylon.IO', ->
)
, interval)
setHigh: ->
@self.digitalWrite(1)
setLow: ->
@self.digitalWrite(0)
toggle: ->
if @status == 'low'
@self.setHigh()
else
@self.setLow()
# Creates the GPIO file to read/write from
_createGPIOPin: () ->
FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) =>
if(err)
@self.emit('error', 'Error while creating pin files')
else
@_openPin()
)
_openPin: () ->
@self._setMode(@mode, true)
@self.emit('open')
_closeCallback: (err) ->
if(err)
@self.emit('error', 'Error while closing pin files')
else
@self.emit('close', @pinNum)
# 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(@_directionPath(), GPIO_DIRECTION_WRITE, (err) =>
if (err)
@self.emit('error', "Setting up pin direction failed")
else
@ready = true
@self.emit('connect', mode) if emitConnect
)
FS.writeFile(@_directionPath(), GPIO_DIRECTION_WRITE, (err) => @_setModeCallback() )
else if mode =='r'
FS.writeFile(@_directionPath(), GPIO_DIRECTION_READ, (err) =>
if (err)
@self.emit('error', "Setting up pin direction failed")
else
@ready = true
@self.emit('connect', mode) if emitConnect
)
FS.writeFile(@_directionPath(), GPIO_DIRECTION_READ, (err) => @_setModeCallback() )
_setModeCallback: () ->
if (err)
@self.emit('error', "Setting up pin direction failed")
else
@ready = true
@self.emit('connect', mode) if emitConnect
_directionPath: () ->
"#{ @_pinPath() }/direction"
@ -124,14 +138,3 @@ namespace 'Cylon.IO', ->
_unexportPath: () ->
"#{ GPIO_PATH }/unexport"
setHigh: ->
@self.digitalWrite(1)
setLow: ->
@self.digitalWrite(0)
toggle: ->
if @status == 'low'
@self.setHigh()
else
@self.setLow()