add sensor module
This commit is contained in:
parent
63d6213285
commit
1be10e0a85
1 changed files with 31 additions and 0 deletions
31
nodejs/ar-com/sensor.js
Normal file
31
nodejs/ar-com/sensor.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
var events = require('events'),
|
||||
util = require('util');
|
||||
|
||||
var Sensor = function (arduino, pin) {
|
||||
if(!arduino || !pin) {console.log("Arduino or pin not set!");}
|
||||
this.arduino = arduino;
|
||||
this.pin = pin;
|
||||
|
||||
this.board.on('data', function (message) {
|
||||
var m = message.slice(0, -1).split('::'),
|
||||
err = null;
|
||||
|
||||
if (m.length != 2 && this.pin != m[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.emit('read', err, m[1]);
|
||||
|
||||
}.bind(this));
|
||||
|
||||
}
|
||||
|
||||
util.inherits(Sensor, events.EventEmitter);
|
||||
|
||||
Sensor.prototype.trigger = function() {
|
||||
|
||||
this.arduino.write(new Buffer('Y' + String.fromCharCode(this.pin) + '000000', 'ascii'));
|
||||
|
||||
};
|
||||
|
||||
module.exports = IR;
|
Reference in a new issue