Skip to content

Commit

Permalink
Fixes sendSignal and message emitting to ignore invalid messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcr committed Dec 6, 2014
1 parent 9ba7509 commit d6bab5a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ Infrared.prototype._fetchRXDurations = function (callback) {

// Emit the buffer
self.emit('data', buf);
self.emit('message', self._parseMessage(buf));
var msg = self._parseMessage(buf);
if (msg != null) {
self.emit('message', msg);
}
callback && callback();
}
});
Expand All @@ -208,14 +211,20 @@ Infrared.prototype._parseMessage = function (buf) {
var arr = [];
for (var i = 0; i < buf.length; i += 2) {
var val = buf.readInt16BE(i);
for (var j = 0; j < this.expected.concat([0]); j++) {
if (this.expected[j] == 0) {
val = 0;
break;
for (var j = 0; j < this.expected.length; j++) {
if (this.expected[j] == null) {
return null;
}
if (val > this.expected[j]*(1-this.tolerance) && val < this.expected[j]*(1+this.tolerance)) {
val = this.expected[j];
break;
if (val < 0) {
if (val < this.expected[j]*(1-this.tolerance) && val > this.expected[j]*(1+this.tolerance)) {
val = this.expected[j];
break;
}
} else {
if (val > this.expected[j]*(1-this.tolerance) && val < this.expected[j]*(1+this.tolerance)) {
val = this.expected[j];
break;
}
}
}
arr.push(val);
Expand All @@ -226,7 +235,7 @@ Infrared.prototype._parseMessage = function (buf) {
Infrared.prototype.sendSignal = function (frequency, arrsignal, callback) {
var buf = new Buffer(arrsignal.length * 2)
arrsignal.forEach(function (val, i) {
buf.push(arrsignal.writeInt16BE(val, i * 2));
buf.writeInt16BE(val, i * 2);
})
return this.sendRawSignal(frequency, buf, callback);
}
Expand Down

0 comments on commit d6bab5a

Please sign in to comment.