UNPKG

node-red-contrib-leap-motion

Version:

Node-Red nodes for leap motion

743 lines (619 loc) 23.1 kB
var Frame = require('./frame') , Hand = require('./hand') , Pointable = require('./pointable') , Finger = require('./finger') , CircularBuffer = require("./circular_buffer") , Pipeline = require("./pipeline") , EventEmitter = require('events').EventEmitter , gestureListener = require('./gesture').gestureListener , Dialog = require('./dialog') , _ = require('underscore'); /** * Constructs a Controller object. * * When creating a Controller object, you may optionally pass in options * to set the host , set the port, enable gestures, or select the frame event type. * * ```javascript * var controller = new Leap.Controller({ * host: '127.0.0.1', * port: 6437, * enableGestures: true, * frameEventName: 'animationFrame' * }); * ``` * * @class Controller * @memberof Leap * @classdesc * The Controller class is your main interface to the Leap Motion Controller. * * Create an instance of this Controller class to access frames of tracking data * and configuration information. Frame data can be polled at any time using the * [Controller.frame]{@link Leap.Controller#frame}() function. Call frame() or frame(0) to get the most recent * frame. Set the history parameter to a positive integer to access previous frames. * A controller stores up to 60 frames in its frame history. * * Polling is an appropriate strategy for applications which already have an * intrinsic update loop, such as a game. * * loopWhileDisconnected defaults to true, and maintains a 60FPS frame rate even when Leap Motion is not streaming * data at that rate (such as no hands in frame). This is important for VR/WebGL apps which rely on rendering for * regular visual updates, including from other input devices. Flipping this to false should be considered an * optimization for very specific use-cases. * * */ var Controller = module.exports = function(opts) { var inNode = (typeof(process) !== 'undefined' && process.versions && process.versions.node), controller = this; opts = _.defaults(opts || {}, { inNode: inNode }); this.inNode = opts.inNode; opts = _.defaults(opts || {}, { frameEventName: this.useAnimationLoop() ? 'animationFrame' : 'deviceFrame', suppressAnimationLoop: !this.useAnimationLoop(), loopWhileDisconnected: true, useAllPlugins: false, checkVersion: true }); this.animationFrameRequested = false; this.onAnimationFrame = function(timestamp) { if (controller.lastConnectionFrame.valid){ controller.emit('animationFrame', controller.lastConnectionFrame); } controller.emit('frameEnd', timestamp); if ( controller.loopWhileDisconnected && ( ( controller.connection.focusedState !== false ) // loop while undefined, pre-ready. || controller.connection.opts.background) ){ window.requestAnimationFrame(controller.onAnimationFrame); }else{ controller.animationFrameRequested = false; } }; this.suppressAnimationLoop = opts.suppressAnimationLoop; this.loopWhileDisconnected = opts.loopWhileDisconnected; this.frameEventName = opts.frameEventName; this.useAllPlugins = opts.useAllPlugins; this.history = new CircularBuffer(200); this.lastFrame = Frame.Invalid; this.lastValidFrame = Frame.Invalid; this.lastConnectionFrame = Frame.Invalid; this.accumulatedGestures = []; this.checkVersion = opts.checkVersion; if (opts.connectionType === undefined) { this.connectionType = (this.inBrowser() ? require('./connection/browser') : require('./connection/node')); } else { this.connectionType = opts.connectionType; } this.connection = new this.connectionType(opts); this.streamingCount = 0; this.devices = {}; this.plugins = {}; this._pluginPipelineSteps = {}; this._pluginExtendedMethods = {}; if (opts.useAllPlugins) this.useRegisteredPlugins(); this.setupFrameEvents(opts); this.setupConnectionEvents(); this.startAnimationLoop(); // immediately when started } Controller.prototype.gesture = function(type, cb) { var creator = gestureListener(this, type); if (cb !== undefined) { creator.stop(cb); } return creator; } /* * @returns the controller */ Controller.prototype.setBackground = function(state) { this.connection.setBackground(state); return this; } Controller.prototype.setOptimizeHMD = function(state) { this.connection.setOptimizeHMD(state); return this; } Controller.prototype.inBrowser = function() { return !this.inNode; } Controller.prototype.useAnimationLoop = function() { return this.inBrowser() && !this.inBackgroundPage(); } Controller.prototype.inBackgroundPage = function(){ // http://developer.chrome.com/extensions/extension#method-getBackgroundPage return (typeof(chrome) !== "undefined") && chrome.extension && chrome.extension.getBackgroundPage && (chrome.extension.getBackgroundPage() === window) } /* * @returns the controller */ Controller.prototype.connect = function() { this.connection.connect(); return this; } Controller.prototype.streaming = function() { return this.streamingCount > 0; } Controller.prototype.connected = function() { return !!this.connection.connected; } Controller.prototype.startAnimationLoop = function(){ if (!this.suppressAnimationLoop && !this.animationFrameRequested) { this.animationFrameRequested = true; window.requestAnimationFrame(this.onAnimationFrame); } } /* * @returns the controller */ Controller.prototype.disconnect = function() { this.connection.disconnect(); return this; } /** * Returns a frame of tracking data from the Leap. * * Use the optional history parameter to specify which frame to retrieve. * Call frame() or frame(0) to access the most recent frame; call frame(1) to * access the previous frame, and so on. If you use a history value greater * than the number of stored frames, then the controller returns an invalid frame. * * @method frame * @memberof Leap.Controller.prototype * @param {number} history The age of the frame to return, counting backwards from * the most recent frame (0) into the past and up to the maximum age (59). * @returns {Leap.Frame} The specified frame; or, if no history * parameter is specified, the newest frame. If a frame is not available at * the specified history position, an invalid Frame is returned. **/ Controller.prototype.frame = function(num) { return this.history.get(num) || Frame.Invalid; } Controller.prototype.loop = function(callback) { if (callback) { if (typeof callback === 'function'){ this.on(this.frameEventName, callback); }else{ // callback is actually of the form: {eventName: callback} this.setupFrameEvents(callback); } } return this.connect(); } Controller.prototype.addStep = function(step) { if (!this.pipeline) this.pipeline = new Pipeline(this); this.pipeline.addStep(step); } // this is run on every deviceFrame Controller.prototype.processFrame = function(frame) { if (frame.gestures) { this.accumulatedGestures = this.accumulatedGestures.concat(frame.gestures); } // lastConnectionFrame is used by the animation loop this.lastConnectionFrame = frame; this.startAnimationLoop(); // Only has effect if loopWhileDisconnected: false this.emit('deviceFrame', frame); } // on a this.deviceEventName (usually 'animationFrame' in browsers), this emits a 'frame' Controller.prototype.processFinishedFrame = function(frame) { this.lastFrame = frame; if (frame.valid) { this.lastValidFrame = frame; } frame.controller = this; frame.historyIdx = this.history.push(frame); if (frame.gestures) { frame.gestures = this.accumulatedGestures; this.accumulatedGestures = []; for (var gestureIdx = 0; gestureIdx != frame.gestures.length; gestureIdx++) { this.emit("gesture", frame.gestures[gestureIdx], frame); } } if (this.pipeline) { frame = this.pipeline.run(frame); if (!frame) frame = Frame.Invalid; } this.emit('frame', frame); this.emitHandEvents(frame); } /** * The controller will emit 'hand' events for every hand on each frame. The hand in question will be passed * to the event callback. * * @param frame */ Controller.prototype.emitHandEvents = function(frame){ for (var i = 0; i < frame.hands.length; i++){ this.emit('hand', frame.hands[i]); } } Controller.prototype.setupFrameEvents = function(opts){ if (opts.frame){ this.on('frame', opts.frame); } if (opts.hand){ this.on('hand', opts.hand); } } /** Controller events. The old 'deviceConnected' and 'deviceDisconnected' have been depricated - use 'deviceStreaming' and 'deviceStopped' instead, except in the case of an unexpected disconnect. There are 4 pairs of device events recently added/changed: -deviceAttached/deviceRemoved - called when a device's physical connection to the computer changes -deviceStreaming/deviceStopped - called when a device is paused or resumed. -streamingStarted/streamingStopped - called when there is/is no longer at least 1 streaming device. Always comes after deviceStreaming. The first of all of the above event pairs is triggered as appropriate upon connection. All of these events receives an argument with the most recent info about the device that triggered it. These events will always be fired in the order they are listed here, with reverse ordering for the matching shutdown call. (ie, deviceStreaming always comes after deviceAttached, and deviceStopped will come before deviceRemoved). -deviceConnected/deviceDisconnected - These are considered deprecated and will be removed in the next revision. In contrast to the other events and in keeping with it's original behavior, it will only be fired when a device begins streaming AFTER a connection has been established. It is not paired, and receives no device info. Nearly identical functionality to streamingStarted/Stopped if you need to port. */ Controller.prototype.setupConnectionEvents = function() { var controller = this; this.connection.on('frame', function(frame) { controller.processFrame(frame); }); // either deviceFrame or animationFrame: this.on(this.frameEventName, function(frame) { controller.processFinishedFrame(frame); }); // here we backfill the 0.5.0 deviceEvents as best possible // backfill begin streaming events var backfillStreamingStartedEventsHandler = function(){ if (controller.connection.opts.requestProtocolVersion < 5 && controller.streamingCount == 0){ controller.streamingCount = 1; var info = { attached: true, streaming: true, type: 'unknown', id: "Lx00000000000" }; controller.devices[info.id] = info; controller.emit('deviceAttached', info); controller.emit('deviceStreaming', info); controller.emit('streamingStarted', info); controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler) } } var backfillStreamingStoppedEvents = function(){ if (controller.streamingCount > 0) { for (var deviceId in controller.devices){ controller.emit('deviceStopped', controller.devices[deviceId]); controller.emit('deviceRemoved', controller.devices[deviceId]); } // only emit streamingStopped once, with the last device controller.emit('streamingStopped', controller.devices[deviceId]); controller.streamingCount = 0; for (var deviceId in controller.devices){ delete controller.devices[deviceId]; } } } // Delegate connection events this.connection.on('focus', function() { if ( controller.loopWhileDisconnected ){ controller.startAnimationLoop(); } controller.emit('focus'); }); this.connection.on('blur', function() { controller.emit('blur') }); this.connection.on('protocol', function(protocol) { protocol.on('beforeFrameCreated', function(frameData){ controller.emit('beforeFrameCreated', frameData) }); protocol.on('afterFrameCreated', function(frame, frameData){ controller.emit('afterFrameCreated', frame, frameData) }); controller.emit('protocol', protocol); }); this.connection.on('ready', function() { if (controller.checkVersion && !controller.inNode){ // show dialog only to web users controller.checkOutOfDate(); } controller.emit('ready'); }); this.connection.on('connect', function() { controller.emit('connect'); controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler) controller.connection.on('frame', backfillStreamingStartedEventsHandler); }); this.connection.on('disconnect', function() { controller.emit('disconnect'); backfillStreamingStoppedEvents(); }); // this does not fire when the controller is manually disconnected // or for Leap Service v1.2.0+ this.connection.on('deviceConnect', function(evt) { if (evt.state){ controller.emit('deviceConnected'); controller.connection.removeListener('frame', backfillStreamingStartedEventsHandler) controller.connection.on('frame', backfillStreamingStartedEventsHandler); }else{ controller.emit('deviceDisconnected'); backfillStreamingStoppedEvents(); } }); // Does not fire for Leap Service pre v1.2.0 this.connection.on('deviceEvent', function(evt) { var info = evt.state, oldInfo = controller.devices[info.id]; //Grab a list of changed properties in the device info var changed = {}; for(var property in info) { //If a property i doesn't exist the cache, or has changed... if( !oldInfo || !oldInfo.hasOwnProperty(property) || oldInfo[property] != info[property] ) { changed[property] = true; } } //Update the device list controller.devices[info.id] = info; //Fire events based on change list if(changed.attached) { controller.emit(info.attached ? 'deviceAttached' : 'deviceRemoved', info); } if(!changed.streaming) return; if(info.streaming) { controller.streamingCount++; controller.emit('deviceStreaming', info); if( controller.streamingCount == 1 ) { controller.emit('streamingStarted', info); } //if attached & streaming both change to true at the same time, that device was streaming //already when we connected. if(!changed.attached) { controller.emit('deviceConnected'); } } //Since when devices are attached all fields have changed, don't send events for streaming being false. else if(!(changed.attached && info.attached)) { controller.streamingCount--; controller.emit('deviceStopped', info); if(controller.streamingCount == 0){ controller.emit('streamingStopped', info); } controller.emit('deviceDisconnected'); } }); this.on('newListener', function(event, listener) { if( event == 'deviceConnected' || event == 'deviceDisconnected' ) { console.warn(event + " events are depricated. Consider using 'streamingStarted/streamingStopped' or 'deviceStreaming/deviceStopped' instead"); } }); }; // Checks if the protocol version is the latest, if if not, shows the dialog. Controller.prototype.checkOutOfDate = function(){ console.assert(this.connection && this.connection.protocol); var serviceVersion = this.connection.protocol.serviceVersion; var protocolVersion = this.connection.protocol.version; var defaultProtocolVersion = this.connectionType.defaultProtocolVersion; if (defaultProtocolVersion > protocolVersion){ console.warn("Your Protocol Version is v" + protocolVersion + ", this app was designed for v" + defaultProtocolVersion); Dialog.warnOutOfDate({ sV: serviceVersion, pV: protocolVersion }); return true }else{ return false } }; Controller._pluginFactories = {}; /* * Registers a plugin, making is accessible to controller.use later on. * * @member plugin * @memberof Leap.Controller.prototype * @param {String} name The name of the plugin (usually camelCase). * @param {function} factory A factory method which will return an instance of a plugin. * The factory receives an optional hash of options, passed in via controller.use. * * Valid keys for the object include frame, hand, finger, tool, and pointable. The value * of each key can be either a function or an object. If given a function, that function * will be called once for every instance of the object, with that instance injected as an * argument. This allows decoration of objects with additional data: * * ```javascript * Leap.Controller.plugin('testPlugin', function(options){ * return { * frame: function(frame){ * frame.foo = 'bar'; * } * } * }); * ``` * * When hand is used, the callback is called for every hand in `frame.hands`. Note that * hand objects are recreated with every new frame, so that data saved on the hand will not * persist. * * ```javascript * Leap.Controller.plugin('testPlugin', function(){ * return { * hand: function(hand){ * console.log('testPlugin running on hand ' + hand.id); * } * } * }); * ``` * * A factory can return an object to add custom functionality to Frames, Hands, or Pointables. * The methods are added directly to the object's prototype. Finger and Tool cannot be used here, Pointable * must be used instead. * This is encouraged for calculations which may not be necessary on every frame. * Memoization is also encouraged, for cases where the method may be called many times per frame by the application. * * ```javascript * // This plugin allows hand.usefulData() to be called later. * Leap.Controller.plugin('testPlugin', function(){ * return { * hand: { * usefulData: function(){ * console.log('usefulData on hand', this.id); * // memoize the results on to the hand, preventing repeat work: * this.x || this.x = someExpensiveCalculation(); * return this.x; * } * } * } * }); * * Note that the factory pattern allows encapsulation for every plugin instance. * * ```javascript * Leap.Controller.plugin('testPlugin', function(options){ * options || options = {} * options.center || options.center = [0,0,0] * * privatePrintingMethod = function(){ * console.log('privatePrintingMethod - options', options); * } * * return { * pointable: { * publicPrintingMethod: function(){ * privatePrintingMethod(); * } * } * } * }); * */ Controller.plugin = function(pluginName, factory) { if (this._pluginFactories[pluginName]) { console.warn("Plugin \"" + pluginName + "\" already registered"); } return this._pluginFactories[pluginName] = factory; }; /* * Returns a list of registered plugins. * @returns {Array} Plugin Factories. */ Controller.plugins = function() { return _.keys(this._pluginFactories); }; var setPluginCallbacks = function(pluginName, type, callback){ if ( ['beforeFrameCreated', 'afterFrameCreated'].indexOf(type) != -1 ){ // todo - not able to "unuse" a plugin currently this.on(type, callback); }else { if (!this.pipeline) this.pipeline = new Pipeline(this); if (!this._pluginPipelineSteps[pluginName]) this._pluginPipelineSteps[pluginName] = []; this._pluginPipelineSteps[pluginName].push( this.pipeline.addWrappedStep(type, callback) ); } }; var setPluginMethods = function(pluginName, type, hash){ var klass; if (!this._pluginExtendedMethods[pluginName]) this._pluginExtendedMethods[pluginName] = []; switch (type) { case 'frame': klass = Frame; break; case 'hand': klass = Hand; break; case 'pointable': klass = Pointable; _.extend(Finger.prototype, hash); _.extend(Finger.Invalid, hash); break; case 'finger': klass = Finger; break; default: throw pluginName + ' specifies invalid object type "' + type + '" for prototypical extension' } _.extend(klass.prototype, hash); _.extend(klass.Invalid, hash); this._pluginExtendedMethods[pluginName].push([klass, hash]) } /* * Begin using a registered plugin. The plugin's functionality will be added to all frames * returned by the controller (and/or added to the objects within the frame). * - The order of plugin execution inside the loop will match the order in which use is called by the application. * - The plugin be run for both deviceFrames and animationFrames. * * If called a second time, the options will be merged with those of the already instantiated plugin. * * @method use * @memberOf Leap.Controller.prototype * @param pluginName * @param {Hash} Options to be passed to the plugin's factory. * @returns the controller */ Controller.prototype.use = function(pluginName, options) { var functionOrHash, pluginFactory, key, pluginInstance; pluginFactory = (typeof pluginName == 'function') ? pluginName : Controller._pluginFactories[pluginName]; if (!pluginFactory) { throw 'Leap Plugin ' + pluginName + ' not found.'; } options || (options = {}); if (this.plugins[pluginName]){ _.extend(this.plugins[pluginName], options); return this; } this.plugins[pluginName] = options; pluginInstance = pluginFactory.call(this, options); for (key in pluginInstance) { functionOrHash = pluginInstance[key]; if (typeof functionOrHash === 'function') { setPluginCallbacks.call(this, pluginName, key, functionOrHash); } else { setPluginMethods.call(this, pluginName, key, functionOrHash); } } return this; }; /* * Stop using a used plugin. This will remove any of the plugin's pipeline methods (those called on every frame) * and remove any methods which extend frame-object prototypes. * * @method stopUsing * @memberOf Leap.Controller.prototype * @param pluginName * @returns the controller */ Controller.prototype.stopUsing = function (pluginName) { var steps = this._pluginPipelineSteps[pluginName], extMethodHashes = this._pluginExtendedMethods[pluginName], i = 0, klass, extMethodHash; if (!this.plugins[pluginName]) return; if (steps) { for (i = 0; i < steps.length; i++) { this.pipeline.removeStep(steps[i]); } } if (extMethodHashes){ for (i = 0; i < extMethodHashes.length; i++){ klass = extMethodHashes[i][0]; extMethodHash = extMethodHashes[i][1]; for (var methodName in extMethodHash) { delete klass.prototype[methodName]; delete klass.Invalid[methodName]; } } } delete this.plugins[pluginName]; return this; } Controller.prototype.useRegisteredPlugins = function(){ for (var plugin in Controller._pluginFactories){ this.use(plugin); } } _.extend(Controller.prototype, EventEmitter.prototype);