add sensor module

This commit is contained in:
Shannon Sterz 2016-01-30 19:15:54 +01:00
parent 63d6213285
commit 1be10e0a85

31
nodejs/ar-com/sensor.js Normal file
View 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;