Merge pull request #30 from hybridgroup/digital-pin-refactor

Digital pin refactor
This commit is contained in:
Ron Evans 2013-11-12 16:18:16 -08:00
commit 9811cd4b14
2 changed files with 118 additions and 110 deletions

123
dist/digital-pin.js vendored
View File

@ -36,7 +36,6 @@
LOW = 0;
function DigitalPin(opts) {
this.self = this;
this.pinNum = opts.pin;
this.status = 'low';
this.ready = false;
@ -53,52 +52,36 @@
}
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();
}
});
};
DigitalPin.prototype.close = function() {
var _this = this;
return FS.writeFile(this.unexportPath(), "" + this.pinNum, function(err) {
return FS.writeFile(this._unexportPath(), "" + this.pinNum, function(err) {
return _this._closeCallback();
});
};
DigitalPin.prototype.closeSync = function() {
FS.writeFileSync(this.unexportPath(), "" + this.pinNum);
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._setMode('w');
}
this.status = value === 1 ? 'high' : 'low';
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);
return _this.emit('error', "Error occurred while writing value " + value + " to pin " + _this.pinNum);
} else {
return _this.self.emit('digitalWrite', value);
return _this.emit('digitalWrite', value);
}
});
};
@ -107,51 +90,89 @@
var readData,
_this = this;
if (this.mode !== 'r') {
this.self._setMode('r');
this._setMode('r');
}
readData = null;
return setInterval(function() {
return FS.readFile(_this._valuePath(), function(err, data) {
if (err) {
return _this.self.emit('error', "Error occurred while reading from pin " + _this.pinNum);
return _this.emit('error', "Error occurred while reading from pin " + _this.pinNum);
} else {
readData = data;
return _this.self.emit('digitalRead', data);
return _this.emit('digitalRead', data);
}
});
}, interval);
};
DigitalPin.prototype.setHigh = function() {
return this.digitalWrite(1);
};
DigitalPin.prototype.setLow = function() {
return this.digitalWrite(0);
};
DigitalPin.prototype.toggle = function() {
if (this.status === 'low') {
return this.setHigh();
} else {
return this.setLow();
}
};
DigitalPin.prototype._createGPIOPin = function() {
var _this = this;
return FS.writeFile(this._exportPath(), "" + this.pinNum, function(err) {
if (err) {
return _this.emit('error', 'Error while creating pin files');
} else {
return _this._openPin();
}
});
};
DigitalPin.prototype._openPin = function() {
this._setMode(this.mode, true);
return this.emit('open');
};
DigitalPin.prototype._closeCallback = function(err) {
if (err) {
return this.emit('error', 'Error while closing pin files');
} else {
return this.emit('close', this.pinNum);
}
};
DigitalPin.prototype._setMode = function(mode, emitConnect) {
var _this = this;
if (emitConnect == null) {
emitConnect = false;
}
this.mode = mode;
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(err, emitConnect);
});
} 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(err, emitConnect);
});
}
};
DigitalPin.prototype._setModeCallback = function(err, emitConnect) {
if (err) {
return this.emit('error', "Setting up pin direction failed");
} else {
this.ready = true;
if (emitConnect) {
return this.emit('connect', this.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

@ -25,7 +25,6 @@ namespace 'Cylon.IO', ->
LOW = 0
constructor: (opts) ->
@self = this
@pinNum = opts.pin
@status = 'low'
@ready = false
@ -36,78 +35,93 @@ 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) =>
FS.writeFile(@_unexportPath(), "#{ @pinNum }", (err) =>
@_closeCallback()
)
closeSync: ->
FS.writeFileSync(@unexportPath(), "#{ @pinNum }")
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'
@_setMode('w') unless @mode == 'w'
@status = if (value == 1) then 'high' else 'low'
FS.writeFile(@_valuePath(), value, (err) =>
if (err)
@self.emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
@emit('error', "Error occurred while writing value #{ value } to pin #{ @pinNum }")
else
@self.emit('digitalWrite', value)
@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'
@_setMode('r') unless @mode == 'r'
readData = null
setInterval(() =>
FS.readFile(@_valuePath(), (err, data) =>
if err
@self.emit('error', "Error occurred while reading from pin #{ @pinNum }")
@emit('error', "Error occurred while reading from pin #{ @pinNum }")
else
readData = data
@self.emit('digitalRead', data)
@emit('digitalRead', data)
)
, interval)
setHigh: ->
@digitalWrite(1)
setLow: ->
@digitalWrite(0)
toggle: ->
if @status == 'low'
@setHigh()
else
@setLow()
# Creates the GPIO file to read/write from
_createGPIOPin: () ->
FS.writeFile(@_exportPath(), "#{ @pinNum }", (err) =>
if(err)
@emit('error', 'Error while creating pin files')
else
@_openPin()
)
_openPin: () ->
@_setMode(@mode, true)
@emit('open')
_closeCallback: (err) ->
if(err)
@emit('error', 'Error while closing pin files')
else
@emit('close', @pinNum)
# Sets the mode for the GPIO pin by writing the correct values to the pin reference files
_setMode: (mode, emitConnect = false) ->
@mode = mode
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(err, emitConnect) )
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(err, emitConnect) )
_setModeCallback: (err, emitConnect) ->
if (err)
@emit('error', "Setting up pin direction failed")
else
@ready = true
@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()