This repository has been archived on 2024-01-30. You can view files and clone it, but cannot push or open issues or pull requests.
trev/nodejs/ar-com/ir.js

39 lines
782 B
JavaScript
Raw Normal View History

2016-01-29 21:33:17 +01:00
var events = require('events'),
util = require('util');
var IR = function (arduino) {
if(!arduino) {console.log("Arduino not set!");}
this.arduino = arduino;
2016-02-17 12:33:17 +01:00
this.arduino.on('data', function (message) {
var m = message.slice(0, -1).split('::'),
err = null;
if (m.length != 3) {
return;
}
this.emit('read', err, m);
}.bind(this));
2016-01-29 21:33:17 +01:00
}
2016-02-17 12:33:17 +01:00
util.inherits(IR, events.EventEmitter);
2016-01-29 21:33:17 +01:00
IR.prototype.send = function(type, len, val) {
type = String.fromCharCode(type);
len = String.fromCharCode(len);
val = this.arduino.valueToLastFour(val);
this.arduino.write(new Buffer('X0' + type + len + val,'ascii'));
};
IR.prototype.receive = function() {
this.arduino.write(new Buffer('Y0000000', 'ascii'));
};
module.exports = IR;