var events = require("events"), util = require("util"); var priv = new Map(); /** * Ping * @param {Object} opts Options: addr freq */ function Ping(opts) { if (!(this instanceof Ping)) { return new Ping(opts); } var last = null; this.addr = opts && opts.addr || 0x27; this.freq = opts.freq || 100; this.board = opts.board || null; var state = { value: 0 }; // Private settings object var settings = { addr: this.addr, value: this.board.io.HIGH, readBytes: 2, // the duration that comes off the ping is a 16 bit int pingFreq: 110, // an optimium period to poll the sensor on i2c. }; this.board.io.setMaxListeners(100); this.board.io.i2cConfig(); // Interval for polling pulse duration as reported in microseconds setInterval(function() { this.board.io.i2cReadOnce(settings.addr, settings.readBytes, function(data) { state.value = (data[0] << 8) + data[1]; }); }.bind(this), settings.pingFreq); // Interval for throttled event setInterval(function() { var err = null; if (!state.value) { return; } // The "read" event has been deprecated in // favor of a "data" event. this.emit("data", err, state.value); // If the state.value for this interval is not the same as the // state.value in the last interval, fire a "change" event. if (state.value !== last) { this.emit("change", err, state.value); } // Store state.value for comparison in next interval last = state.value; // Reset samples; // samples.length = 0; }.bind(this), this.freq); Object.defineProperties(this, { value: { get: function() { return state.value; } }, // Based on the round trip travel time in microseconds, // Calculate the distance in inches and centimeters inches: { get: function() { return +(state.value / 74 / 2).toFixed(2); } }, in: { get: function() { return this.inches; } }, cm: { get: function() { return +(state.value / 29 / 2).toFixed(3); } } }); priv.set(this, state); } util.inherits(Ping, events.EventEmitter); module.exports = Ping; //http://protolab.pbworks.com/w/page/19403657/TutorialPings