UNPKG

johnny-five

Version:

The JavaScript Arduino Programming Framework.

130 lines (109 loc) 2.31 kB
"use strict"; var util = require("util"), Emitter = require("events").EventEmitter, HID = require("node-hid"); var READ_SPEED = 10, JOYSTICK_THRESHOLD = 20; function scale(x, fromLow, fromHigh, toLow, toHigh) { return (x - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; } var path, controller; HID.devices().forEach(function(dev) { if (dev.product === "PLAYSTATION(R)3 Controller") { path = dev.path; } }); function PS3() { Emitter.call(this); if (path) { controller = new HID.HID(path); } else { console.log("No PS3 controller found."); } this.keysDown = {}; controller.read(function read(error, data) { if (error) { console.error(error); } else { [this.keys.triggers.UP, this.keys.triggers.DOWN].forEach(function(key) { if (data[key] > 0) { if (!this.keysDown[key]) { this.keypress(key); } this.keysDown[key] = true; } else { this.keysDown[key] = false; } }); this.throttle(data[this.keys.triggers.RIGHT_STICK_Y]); } setTimeout(read.bind(this), READ_SPEED); }.bind9this); } util.inherits(PS3, Emitter); PS3.prototype.keypress = function(val) { this.emit("keypress", val); }; PS3.prototype.throttle = function (val) { var temp = -(val - 128), throttle = 0; if (temp < JOYSTICK_THRESHOLD / -2 || temp > JOYSTICK_THRESHOLD / 2) { throttle = scale(temp, -128, 128, -20, 20); this.emit("throttle", throttle); } }; PS3.prototype.close = function () { if (controller) { controller.close(); } }; PS3.keys = { resolution: 256, triggers: { LEFT_LK: 2, RIGHT_LK: 3, PS: 4, L1: 20, L2: 18, R1: 21, R2: 19, UP: 14, DOWN: 16, LEFT: 17, RIGHT: 15, TRIANGLE: 22, SQUARE: 25, CIRCLE: 23, CROSS: 24, LEFT_STICK_X: 6, LEFT_STICK_Y: 7, RIGHT_STICK_X: 8, RIGHT_STICK_Y: 9, AXIS_ROLL: 42, AXIS_PITCH: 44, WIGGLE: 46 }, left: { UP: 16, DOWN: 64, LEFT: 28, RIGHT: 32, LEFT_STICK: 2, RIGHT_STICK: 4, SELECT: 1, START: 8 }, right: { L1: 4, L2: 1, R1: 8, R2: 2, TRIANGLE: 16, SQUARE: 128, CIRCLE: 32, CROSS: 64 } }; module.exports = PS3;