node-glfw-joystick

0.1.6 • Public • Published

NodeJS binding to Joystick (Gamepad) via GLFW

In this project, we find and init joystick (gamepad) devices via GLFW (This is the work done by node-glfw), and wrap their polled states as NodeJS events for a generic Joystick (gamepad). We map the device buttons and axes to match our generic one.

We identify the device by the name attribute and button count returned from GLFW. GLFW doesn't provide vendorId nor productId attributes, making it hard to truly distinguish devices (e.g Device1 from CompanyX: name is Controller and has 10 buttons, Device2 from CompanyY: name is Controller and has 10 buttons)

We have provided button/axes maps for the XBox360, PS3, PS4 controllers. The PS4 controller, when connected via Bluetooth, seems to behave different and return 0 button count. We recommend using a PS3 Wireless Controller instead.

Also, this project uses a modified built for GLFW3, since it will segfault when connecting a controller in Maverics.

Dependencies

Installation

npm install node-glfw-joystick

Usage

First, we initialize our object

var JoystickManager = require('node-glfw-joystick');
var joystickMgr = new JoystickManager();
joystickMgr.init();

Then, there are two ways of getting events ::Animate([delay]) or ::Poll()

// With Animate

joystickMgr.animate();

joystickMgr.on('JOYSTICK_BUTTON_DOWN', function(deviceId, buttonId) { ... });

if you have a drawing function or some other loop function that updates your application, then you can use Poll

// With Polling

yourloopFunction() {
    joystickMgr.poll();
}

joystickMgr.on('JOYSTICK_BUTTON_DOWN', function(deviceId, buttonId) { ... });

Test

In the folder tests

node test.js

For a more visual test, use

node joystickTest.js

Pressed the home button to reset if the triangle gets lost!

Events

There are 5 type of events you can listen for

JOYSTICK_AXES_UPDATE			---> function(deviceId, axeId, value) {}
JOYSTICK_BUTTON_UP 				---> function(deviceId, buttonId) {}
JOYSTICK_BUTTON_DOWN			---> function(deviceId, buttonId) {}
JOYSTICK_BUTTON_RELEASED	---> function(deviceId, buttonId) {}
JOYSTICK_BUTTON_PRESSED		---> function(deviceId, buttonId) {}

ButtonIds

The buttonId is the name of a button found in our genericJoystick class. There are 31 generic buttons

JOYSTICK_UNDEFINED
JOYSTICK_BTN_UP
JOYSTICK_BTN_DOWN
JOYSTICK_BTN_LEFT
JOYSTICK_BTN_RIGHT
JOYSTICK_BTN_X_PS
JOYSTICK_BTN_Y
JOYSTICK_BTN_A
JOYSTICK_BTN_B
JOYSTICK_BTN_SQUARE
JOYSTICK_BTN_TRIANGLE
JOYSTICK_BTN_X_XBOX
JOYSTICK_BTN_CIRCLE
JOYSTICK_BTN_SELECT
JOYSTICK_BTN_START
JOYSTICK_BTN_HOME
JOYSTICK_BTN_JS_LEFT
JOYSTICK_BTN_JS_RIGHT
JOYSTICK_BTN_R1
JOYSTICK_BTN_R2
JOYSTICK_BTN_L1
JOYSTICK_BTN_L2
JOYSTICK_BTN_PAD_PS4

We have wrapped hat switches with events as well.

JOYSTICK_BTN_LEFT_ANALOG_UP
JOYSTICK_BTN_LEFT_ANALOG_DOWN
JOYSTICK_BTN_LEFT_ANALOG_LEFT
JOYSTICK_BTN_LEFT_ANALOG_RIGHT
JOYSTICK_BTN_RIGHT_ANALOG_UP
JOYSTICK_BTN_RIGHT_ANALOG_DOWN
JOYSTICK_BTN_RIGHT_ANALOG_LEFT
JOYSTICK_BTN_RIGHT_ANALOG_RIGHT

AxesIds

There are 6 axesIds

JOYSTICK_AXE_LEFTJS_UP
JOYSTICK_AXE_LEFTJS_LEFT
JOYSTICK_AXE_RIGHTJS_UP
JOYSTICK_AXE_RIGHTJS_LEFT
JOYSTICK_AXE_L2
JOYSTICK_AXE_R2

Readme

Keywords

none

Package Sidebar

Install

npm i node-glfw-joystick

Weekly Downloads

7

Version

0.1.6

License

none

Last publish

Collaborators

  • juanandresfloored