johnny-five
Version:
Firmata based Arduino Programming Framework.
70 lines (54 loc) • 1.72 kB
JavaScript
var Board = require("../lib/board.js"),
events = require("events"),
util = require("util");
/**
* Pir, IR.Motion
* @param {Object} opts Options: pin, type, id, range
*/
function Pir( opts ) {
if ( !(this instanceof Pir) ) {
return new Pir( opts );
}
// Initialize a Device instance on a Board
Board.Device.call(
this, opts = Board.options( opts )
);
// Set the pin to INPUT mode
this.mode = this.firmata.MODES.INPUT;
this.firmata.pinMode( this.pin, this.mode );
// PIR instance properties
this.state = null;
this.calibrated = false;
// Analog Read event loop
// TODO: make this "throttle-able"
this.firmata.digitalRead( this.pin, function( data ) {
var timestamp = new Date(),
err = null;
// If this is not a calibration event
if ( this.state != null && this.state !== +data ) {
// Update current state of PIR instance
this.state = +data;
// "motionstart" event fired when motion occurs
// within the observable range of the PIR sensor
if ( data ) {
this.emit( "motionstart", err, timestamp );
}
// "motionend" event fired when motion has ceased
// within the observable range of the PIR sensor
if ( !data ) {
this.emit( "motionend", err, timestamp );
}
}
// "calibrated" event fired when PIR sensor is
// ready to detect movement/motion in observable range
if ( !this.calibrated ) {
this.calibrated = true;
this.state = +data;
this.emit( "calibrated", err, timestamp );
}
}.bind(this));
}
util.inherits( Pir, events.EventEmitter );
module.exports = Pir;
// More information:
// http://www.ladyada.net/learn/sensors/pir.html