mirror of
				https://github.com/node-red/node-red-nodes.git
				synced 2025-03-01 10:37:43 +00:00 
			
		
		
		
	set makeymakey node to use the module.exports style
This commit is contained in:
		| @@ -14,71 +14,73 @@ | |||||||
|  * limitations under the License. |  * limitations under the License. | ||||||
|  **/ |  **/ | ||||||
|  |  | ||||||
| var RED = require(process.env.NODE_RED_HOME+"/red/red"); | module.exports = function(RED) { | ||||||
| var hids = require('hidstream'); |     "use strict"; | ||||||
|  |     var hids = require('hidstream'); | ||||||
|  |  | ||||||
| function MakeyNode(n) { |     function MakeyNode(n) { | ||||||
|     RED.nodes.createNode(this,n); |         RED.nodes.createNode(this,n); | ||||||
|     this.vid = 0x1b4f;      // MakeyMakey vendor ID |         this.vid = 0x1b4f;      // MakeyMakey vendor ID | ||||||
|     this.pid = 0x2b75;      // MakeyMakey product ID |         this.pid = 0x2b75;      // MakeyMakey product ID | ||||||
|     var node = this; |         var node = this; | ||||||
|     var path = null; |         var path = null; | ||||||
|     var tout; |         var tout; | ||||||
|     var click = false; |         var click = false; | ||||||
|     var keylookup = { "44":"space", "82":"up", "81":"down", "79":"right", "80":"left", |         var keylookup = { "44":"space", "82":"up", "81":"down", "79":"right", "80":"left", | ||||||
|                         "26":"w", "4":"a", "22":"s", "7":"d", "9":"f", "10":"g" }; |                             "26":"w", "4":"a", "22":"s", "7":"d", "9":"f", "10":"g" }; | ||||||
|  |  | ||||||
|     var findmakey = function() { |         var findmakey = function() { | ||||||
|         node.log("looking for MakeyMakey"); |             node.log("looking for MakeyMakey"); | ||||||
|         var devices = hids.getDevices(); |             var devices = hids.getDevices(); | ||||||
|         for (var dev = 0; dev < devices.length; dev++) { |             for (var dev = 0; dev < devices.length; dev++) { | ||||||
|             //node.log("Find:"+devices[dev].vendorId.toString(16)+":"+devices[dev].productId.toString(16)); |                 //node.log("Find:"+devices[dev].vendorId.toString(16)+":"+devices[dev].productId.toString(16)); | ||||||
|             if ((devices[dev].vendorId == node.vid) && (devices[dev].productId == node.pid)) { |                 if ((devices[dev].vendorId == node.vid) && (devices[dev].productId == node.pid)) { | ||||||
|                 path = devices[dev].path; |                     path = devices[dev].path; | ||||||
|                 node.log("found MakeyMakey at: "+path);     //path = "0003:0004:00"; |                     node.log("found MakeyMakey at: "+path);     //path = "0003:0004:00"; | ||||||
|                 break; // stop on first match |                     break; // stop on first match | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |             if (path === null) { | ||||||
|  |                 tout = setTimeout( function () { | ||||||
|  |                     findmakey(); | ||||||
|  |                 },15000); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         if (path === null) { |         findmakey(); | ||||||
|             tout = setTimeout( function () { |  | ||||||
|                 findmakey(); |  | ||||||
|             },15000); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     findmakey(); |  | ||||||
|  |  | ||||||
|     if (path != null) { |         if (path != null) { | ||||||
|         try { |             try { | ||||||
|             node.makey = new hids.device(path); |                 node.makey = new hids.device(path); | ||||||
|             node.makey.on("data", function(key) { |                 node.makey.on("data", function(key) { | ||||||
|                 var msg = {payload:[]}; |                     var msg = {payload:[]}; | ||||||
|                 //console.log(key); |                     //console.log(key); | ||||||
|                 if (key.modKeys[0] === "shift") { |                     if (key.modKeys[0] === "shift") { | ||||||
|                     for (var keys = 0; keys < key.keyCodes.length; keys++) { |                         for (var keys = 0; keys < key.keyCodes.length; keys++) { | ||||||
|                         node.log(key.keyCodes[keys]+" : "+keylookup[key.keyCodes[keys].toString()]); |                             node.log(key.keyCodes[keys]+" : "+keylookup[key.keyCodes[keys].toString()]); | ||||||
|                         msg.payload.push(keylookup[key.keyCodes[keys].toString()]); |                             msg.payload.push(keylookup[key.keyCodes[keys].toString()]); | ||||||
|                     } |                         } | ||||||
|                     msg.payload = msg.payload.join(); |                         msg.payload = msg.payload.join(); | ||||||
|                     node.send(msg); |  | ||||||
|                 } |  | ||||||
|                 else if (key.modKeys[0] === "ctrl") { |  | ||||||
|                     if (key.charCodes.length === 0) { |  | ||||||
|                         click = !click; |  | ||||||
|                         msg.payload = (click) ? "click" : "clock"; |  | ||||||
|                         node.send(msg); |                         node.send(msg); | ||||||
|                     } |                     } | ||||||
|                 } |                     else if (key.modKeys[0] === "ctrl") { | ||||||
|                 else { console.log(key); } |                         if (key.charCodes.length === 0) { | ||||||
|             }); |                             click = !click; | ||||||
|         } catch(err) { node.warn("can't open MakeyMakey: Do you need root access ?"); } |                             msg.payload = (click) ? "click" : "clock"; | ||||||
|     } |                             node.send(msg); | ||||||
|     else { |                         } | ||||||
|         findmakey(); |                     } | ||||||
|     } |                     else { console.log(key); } | ||||||
|  |                 }); | ||||||
|  |             } catch(err) { node.warn("can't open MakeyMakey: Do you need root access ?"); } | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             findmakey(); | ||||||
|  |         } | ||||||
|  |  | ||||||
|     this.on("close", function() { |         this.on("close", function() { | ||||||
|         if (tout) { clearTimeout(tout); } |             if (tout) { clearTimeout(tout); } | ||||||
|         if (node.makey) { node.makey.device.close(); } |             if (node.makey) { node.makey.device.close(); } | ||||||
|     }); |         }); | ||||||
|  |     } | ||||||
|  |     RED.nodes.registerType("makeymakey",MakeyNode); | ||||||
| } | } | ||||||
| RED.nodes.registerType("makeymakey",MakeyNode); |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user