31 lines
No EOL
657 B
JavaScript
31 lines
No EOL
657 B
JavaScript
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.arduino.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 = Sensor; |