From 1be10e0a8586071d39c8b4cee54a7c6dfae555d9 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sat, 30 Jan 2016 19:15:54 +0100 Subject: [PATCH] add sensor module --- nodejs/ar-com/sensor.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 nodejs/ar-com/sensor.js diff --git a/nodejs/ar-com/sensor.js b/nodejs/ar-com/sensor.js new file mode 100644 index 0000000..ee74a8e --- /dev/null +++ b/nodejs/ar-com/sensor.js @@ -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; \ No newline at end of file