Git Product home page Git Product logo

mobilecontrol2-sdk's Introduction

Mobile Control II SDK

The Mobile Control II SDK provides access to the throttle, keys and LEDs.

Current Version: 1.1.1

Installation

  1. Open the build.gradle file of your application.
  2. Add the SDK to the dependencies section:
dependencies {
    ...
    compile 'eu.esu.mobilecontrol2:mobilecontrol2-sdk:1.1.1'
}

Usage

You can check if your app is running on the Mobile Control II by calling

boolean runsOnMc2 = MobileControl2.isMobileControl2();

The ThrottleFragment, StopButtonFragment as well as the MobileControl2.setLedState() methods are designed to do nothing when not running on the Mobile Control II, so you will not need to check this very often.

Throttle

To access the throttle just add the ThrottleFragment to your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
        
    mThrottleFragment = ThrottleFragment.newInstance(1);
    mThrottleFragment.setOnThrottleListener(mOnThrottleListener);
    getSupportFragmentManager().beginTransaction()
            .add(mThrottleFragment, "mc2:throttle")
            .commit();
    ...
}

Make sure you ignore the ThrottleFragment.KEYCODE_THROTTLE_WAKEUP KeyCode to prevent user input:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == ThrottleFragment.KEYCODE_THROTTLE_WAKEUP) {
        return true;
    }

    ...
}

To change the throttle position call moveThrottle() with a value between 0 (start) and 255 (end):

// Move the throttle to the middle
int position = 127; 
mThrottleFragment.moveThrottle(position);

Use the ThrottleFragment.OnThrottleListener to receive callbacks:

private ThrottleFragment.OnThrottleListener mOnThrottleListener = new OnThrottleListener() {
    @Override
    public void onButtonDown() {
        // The throttle button is in pressed state.
    }

    @Override
    public void onButtonUp() {
        // The throttle button is in released state.
    }

    @Override
    public void onPositionChanged(int position) {
        // The new throttle position from 0 to 255.
    }
}; 

ThrottleScale

The ThrottleScale class provides a convenient way to convert the throttle position to speed steps and vice versa. The example code shows how to use ThrotleScale for values from 0 - 28.

   ThrottleScale mThrottleScale = new ThrottleScale(10, 29);
   
   // Convert throttle position to speed step
   int step = mThrottleScale.positionToStep(position);
   // ... or step to position
   int position = mThrottleScale.stepToPosition(step);

The constructor takes two parameters:
The first parameter defines the zero Range: If the throttle position is between 0 - 10 positionToStep() will return 0. The second parameter defines the actual number of steps including zero. 0 - 28 <=> 29 steps.

Stop Button

You will also need to add the StopButtonFragment to your activity if you want to listen to Stop button events.

@override
protected void onCreate(Bundle savedInstanceState) {
    ...
    StopButtonFragment stopButtonFragment = StopButtonFragment.newInstance();
    stopButtonFragment.setOnStopButtonListener(mStopButtonListener);
    
    getSupportFragmentManager().beginTransaction()
        .add(stopButtonFragment, "mc2:stopButton")
        .commit();
}

Use the StopButtonFragment.OnStopButtonListener to receive events.

private StopButtonFragment.OnStopButtonListener mOnStopButtonListener  = new StopButtonFragment.OnStopButtonListener() {
        @Override
        public void onStopButtonDown() {
            // The stop button is pressed.
        }

        @Override
        public void onStopButtonUp() {
            // The stop button is released.
        }
    };

LEDs

Turn a LED on:

MobileControl2.setLedState(MobileControl2.LED_RED, true);

Turn a LED off:

MobileControl2.setLedState(MobileControl2.LED_GREEN, false);

LED flashing is also supported:

MobileControl2.setLedState(MobileControl2.LED_RED, 250, 250);

Available LEDs are MobileControl2.LED_GREEN and MobileControl2.LED_RED.

Side buttons

The side buttons of the Mobile Control II are using existing Android key codes. The MobileControl2 class provides constants:

MobileControl2.KEYCODE_TOP_RIGHT: Top right.
MobileControl2.KEYCODE_BOTTOM_RIGHT: Bottom right.
MobileControl2.KEYCODE_TOP_LEFT: Top left.
MobileControl2.KEYCODE_BOTTOM_LEFT: Bottom left.

Make sure you check you are running on a Mobile Control II before using the key codes:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (MobileControl2.isMobileControl2()) {
        // Handle Mobile Control II keys
    }
    else {
       // Handle default keys.
    }
}  

Sample App

Checkout the Mobile Control II SDK sample app.

Javadoc

Javadoc is available at http://esugmbh.github.io/mobilecontrol2-sdk

License

Licensed under the MIT License

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.