├── .gitignore ├── LICENSE ├── Readme.md ├── lib └── wii.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | node_modules 15 | npm-debug.log 16 | 17 | build 18 | .lock-wscript 19 | .DS_Store 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Andrew Nesbitt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # [Node Wii Controller](http://libraries.io/npm/wii-controller) 2 | 3 | Interface for Wiimote game controller into Node.js 4 | 5 | Mac OSX driver: https://code.google.com/p/wjoy/ 6 | 7 | ## Usage 8 | 9 | var wiiController = require('wii-controller') 10 | var wii = new wiiController 11 | 12 | wii.on("CWIID_BTN_1:press", function(key) { 13 | console.log("CWIID_BTN_1 press"); 14 | }); 15 | 16 | wii.on("CWIID_BTN_2:press", function(key) { 17 | console.log("CWIID_BTN_2 press"); 18 | }); 19 | 20 | wii.on("CWIID_BTN_HOME:press", function(key) { 21 | console.log("CWIID_BTN_HOME press"); 22 | }); 23 | 24 | wii.on("CWIID_BTN_LEFT:press", function(key) { 25 | console.log("CWIID_BTN_LEFT press (left)"); 26 | }); 27 | 28 | wii.on("CWIID_BTN_RIGHT:press", function(key) { 29 | console.log("CWIID_BTN_RIGHT press (right)"); 30 | }); 31 | 32 | wii.on("CWIID_BTN_RIGHT:release", function(key) { 33 | console.log("CWIID_BTN_RIGHT release"); 34 | }); 35 | 36 | 37 | wii.on("CWIID_BTN_A:press", function(key) { 38 | console.log("CWIID_BTN_A press"); 39 | }); 40 | 41 | wii.on("move", function(position) { 42 | console.log("move", position); 43 | }); 44 | 45 | ## TODO 46 | 47 | * rumble control 48 | * LED control 49 | * motion output 50 | 51 | ## Copyright 52 | 53 | Copyright (c) 2013 Andrew Nesbitt. See [LICENSE](https://github.com/andrew/node-wii-controller/blob/master/LICENSE) for details. 54 | -------------------------------------------------------------------------------- /lib/wii.js: -------------------------------------------------------------------------------- 1 | var HID = require('node-hid'), 2 | colors = require('colors'), 3 | events = require('events'), 4 | util = require('util'); 5 | 6 | var dead = 10 7 | 8 | var buttons = { 9 | CWIID_BTN_2: { 10 | block: 1, 11 | bitwise: 100 12 | }, 13 | CWIID_BTN_1: { 14 | block: 1, 15 | bitwise: 10 16 | }, 17 | CWIID_BTN_B: { 18 | block: 0, 19 | bitwise: 100000 20 | }, 21 | CWIID_BTN_A: { 22 | block: 0, 23 | bitwise: 10000 24 | }, 25 | CWIID_BTN_MINUS: { 26 | block: 0, 27 | bitwise: 10000000 28 | }, 29 | CWIID_BTN_HOME: { 30 | block: 1, 31 | bitwise: 1 32 | }, 33 | CWIID_BTN_LEFT: { 34 | block: 0, 35 | bitwise: 1 36 | }, 37 | CWIID_BTN_RIGHT: { 38 | block: 0, 39 | bitwise: 10 40 | }, 41 | CWIID_BTN_DOWN: { 42 | block: 0, 43 | bitwise: 1000 44 | }, 45 | CWIID_BTN_UP: { 46 | block: 0, 47 | bitwise: 100 48 | }, 49 | CWIID_BTN_PLUS: { 50 | block: 0, 51 | bitwise: 1000000 52 | }, 53 | CWIID_NUNCHUK_BTN_Z: { 54 | block: 1, 55 | bitwise: 10000 56 | }, 57 | CWIID_NUNCHUK_BTN_C: { 58 | block: 1, 59 | bitwise: 1000 60 | } 61 | } 62 | 63 | function wiiController() { 64 | 65 | var devices = HID.devices(); 66 | 67 | this.states = {}; 68 | 69 | devices.forEach((function(d) { 70 | if(typeof d === 'object' && d.product.toLowerCase().indexOf('wiimote') !== -1) { 71 | 72 | device = new HID.HID(d.path) 73 | } 74 | }).bind(this)) 75 | 76 | try{ 77 | this.hid = device 78 | 79 | for (var key in buttons) { 80 | this[key] = 0; 81 | } 82 | this.states.x = 0; 83 | this.states.y = 0; 84 | this.hid.read(this.interpretData.bind(this)); 85 | } 86 | catch ( ex ){ 87 | console.log( 'error: '.red, 'Wii controller could not be found.' ); 88 | } 89 | } 90 | 91 | util.inherits(wiiController, events.EventEmitter); 92 | 93 | wiiController.prototype.interpretData = function(error, data) { 94 | 95 | y = data[11] 96 | if (y >= 128){ 97 | // up 98 | y = (128 - (y - 128)) 99 | } else { 100 | // down 101 | y = - y 102 | } 103 | 104 | x = data[10] 105 | if (x >= 128){ 106 | // right 107 | x = (128 - (x - 128)) 108 | } else { 109 | // left 110 | x = - x 111 | } 112 | 113 | if(x ^ this.x | y ^ this.y){ 114 | if (x > -1*dead && x < dead ){ 115 | x = 0 116 | } 117 | if (y > -1*dead && y < dead ){ 118 | y = 0 119 | } 120 | 121 | this.emit('move', {x: x, y: y}) 122 | this.states.x = x 123 | this.states.y = y 124 | } 125 | 126 | for (var key in buttons) { 127 | var address = buttons[key] 128 | var state = data[address.block] & parseInt(address.bitwise, 2); 129 | 130 | if(state ^ this.states[key]){ 131 | this.emit((state ? key+':press': key+':release'), key); 132 | this.states[key] = state; 133 | } 134 | } 135 | 136 | this.hid.read(this.interpretData.bind(this)); 137 | } 138 | 139 | module.exports = wiiController 140 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wii-controller", 3 | "version": "0.1.0", 4 | "description": "Wii controller for node", 5 | "main": "./lib/wii.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git://github.com/andrew/node-wii-controller.git" 12 | }, 13 | "keywords": [ 14 | "wii", 15 | "controller", 16 | "gaming" 17 | ], 18 | "dependencies": { 19 | "node-hid": ">=0" 20 | }, 21 | "author": "Andrew Nesbitt", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/andrew/node-wii-controller/issues" 25 | } 26 | } 27 | --------------------------------------------------------------------------------