johnny-five
Version:
Firmata based Arduino Programming Framework.
110 lines (83 loc) • 2.12 kB
Plain Text
/**
* Receiver, IR.Receiver
* @param {Object} opts Options: pin
*/
IR.Receiver = function( opts ) {
if ( !(this instanceof IR.Receiver) ) {
return new IR.Receiver( opts );
}
var settings, capture, start, started, range, last, samples, median;
last = null;
samples = [];
// TODO:
// Switch to using
// Board.Device.call( this, opts );
//
opts = Board.options( opts );
// Hardware instance properties
this.board = Board.mount( opts );
this.firmata = this.board.firmata;
this.pin = opts && opts.pin || 9;
// Receiver instance properties
//
//
this.freq = opts.freq || 100;
// this.microseconds = null;
start = 2200;
range = [ 400, 1000 ];
// Private settings object
settings = {
pin: this.pin,
value: this.firmata.LOW
//,
//timeout: 65
//,
// pulseOut: 11
};
this.firmata.setMaxListeners( 10000 );
// Interval for polling pulse duration
setInterval(function() {
this.firmata.pulseIn( settings, function( value ) {
var i, result, data;
result = 0;
if ( value === 0 ) {
capture = false;
}
if ( value > start ) {
capture = true;
started = value;
}
if ( capture && value > start ) {
samples = [];
}
if ( capture && started !== value ) {
// if ( value !== last ) {
// console.log( value );
samples.push( value );
// }
if ( samples.length === 11 ) {
data = samples.slice(0);
samples = [];
result = 0;
for ( i = 0; i < 11; i++ ) {
if ( data[ i ] > 1000 ) {
data[ i ] = 1;
} else if ( data[ i ] > 400 ) {
data[ i ] = 0;
} else {
// return -1;
}
}
for ( i = 0; i < 11; i++ ) {
if ( data[ i ] === 1 ) {
result |= ( 1 << i );
}
}
console.log( result );
}
last = value;
}
}.bind(this));
}.bind(this), 10 );
};
util.inherits( IR, events.EventEmitter );