Git Product home page Git Product logo

shieldcontrollerextensions's Introduction

ShieldControllerExtensions

This Android library enables access to rumble and other special functionality on NVIDIA Shield controllers by communicating with the Shield Accessories service included on the NVIDIA Shield Android TV firmware.

This is required because the NVIDIA Shield Controller does not work with the standard APIs such as getVibrator(), getVibrationManager(), getBatteryState(), etc.

Getting the library

The ShieldControllerExtensions library is hosted using JitPack.

To use it, simply add the JitPack repository to your root build.gradle file:

    allprojects {
        repositories {
            maven { url 'https://jitpack.io' }
        }
    }

then add a dependency on the library itself in your app's build.gradle file:

    dependencies {
        implementation 'com.github.cgutman:ShieldControllerExtensions:1.0'
    }

Usage

The SceManager class provides the primary interface to the library. It is designed to easily integrate with existing KeyEvent and MotionEvent processing code because it works exclusively with InputDevice references which are easily obtained from such events.

Instantiating a SceManager

sceManager = new SceManager(activity);
sceManager.start();

You may add an SceManager.SceDeviceListener using setDeviceListener() prior to calling start(), but this is not required.

Destroying a SceManager

sceManager.stop();

Listening for device add/remove events

sceManager = new SceManager(this);
sceManager.setDeviceListener(new SceManager.SceDeviceListener() {
    @Override
    public void onDeviceAdded(int inputDeviceId) {
        Log.i(TAG, "Device added: " + inputDeviceId);
    }

    @Override
    public void onDeviceRemoved(int inputDeviceId) {
        Log.i(TAG, "Device removed: " + inputDeviceId);
    }
});
sceManager.start();

For your listener to receive the initial onDeviceAdded() callbacks for existing devices, it must be registered prior to calling start().

SceManager.SceDeviceListener contains support for many additional device state callbacks that can be optionally implemented.

Determining if an InputDevice is compatible with SceManager APIs

if (sceManager.isRecognizedDevice(inputDevice)) {
    // Use SceManager to do something cool
}
else {
    // This doesn't look like a SHIELD controller
}

Rumbling an NVIDIA Shield Controller

The rumble() function provided by SceManager behaves similarly to common APIs like XInputSetState() or SDL_GameControllerRumble(). It takes left and right actuator amplitude in a 0 to 65535 range and rumbles continuously until cancelled by a zero amplitude rumble, or SceManager.stop() is called.

The following code rumbles a Shield Controller for 1 second at maximum amplitude:

sceManager.rumble(inputDevice, 65535, 65535);
Thread.sleep(1000);
sceManager.rumble(inputDevice, 0, 0);

For a more dynamic example, this Activity code will rumble based upon trigger input:

    @Override
    public boolean onGenericMotionEvent(MotionEvent event) {
        InputDevice device = event.getDevice();
        if (device != null && sceManager.isRecognizedDevice(device)) {
            float leftTrigger = event.getAxisValue(MotionEvent.AXIS_LTRIGGER);
            float rightTrigger = event.getAxisValue(MotionEvent.AXIS_RTRIGGER);

            sceManager.rumble(device, (int)(leftTrigger * 65535), (int)(rightTrigger * 65535));
        }

        return super.onGenericMotionEvent(event);
    }

Identifying a device

The identify() function can be used to play a haptic effect or activate some other device-specific method of self-identification.

The following example triggers identification when the A button is presssed:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        InputDevice device = event.getDevice();
        if (device != null && sceManager.isRecognizedDevice(device)) {
            if (keyCode == KeyEvent.KEYCODE_BUTTON_A) {
                sceManager.identify(device);
            }
        }

        return super.onKeyDown(keyCode, event);
    }

Code Sample

This repository includes an example app that can be used as a reference for interaction with the library.

shieldcontrollerextensions's People

Contributors

cgutman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.