Android Open Source - shiftknob-android Arduino Hardware






From Project

Back to project page shiftknob-android.

License

The source code is released under:

Copyright (c) 2013 Ford Motor Company All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are me...

If you think the Android project shiftknob-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.openxc.shiftindicator;
// w  ww.  ja v  a 2  s  .c o  m
import jp.ksksue.driver.serial.FTDriver;

import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.util.Log;

public class ArduinoHardware {

    private static final String TAG = "ArduinoHardware";

    private static final String SHIFT_NAME = "shift";
    private static final String COLOR_NAME = "color";
    private static final String DIGIT_NAME = "digit";

    private static final int UPSHIFT_VALUE = 1;
    private static final int NO_SHIFT_VALUE = 0;

    private static FTDriver mFTDriver;
    private int baudRate = FTDriver.BAUD115200;
    private UsbManager mUsbManager = null;

    /**
     * This class handles all communication and device setup for the
     * connection between Shift Indicator app and the Shift Knob
     * hardware. It can serve as a template for future Arduino and/or
     * serial devices attached to an Android device.
     */
    public ArduinoHardware(UsbManager u) {
        mUsbManager = u;
    }

    public void turnOnShiftIndication() {
        String outString = JsonBuilder.builder(SHIFT_NAME, UPSHIFT_VALUE);
        sendOut(outString);
    }

    public void turnOffShiftIndication() {
        String outString = JsonBuilder.builder(SHIFT_NAME, NO_SHIFT_VALUE);
        sendOut(outString);
    }

    public void sendDigit(int i) {
        String outString = JsonBuilder.builder(DIGIT_NAME, i);
        sendOut(outString);
    }

    public void sendColor(int i) {
        String outString = JsonBuilder.builder(COLOR_NAME, i);
        sendOut(outString);
    }

    private void sendOut(String s) {
        if (mFTDriver.isConnected()) {
            try {
                mFTDriver.write(s);
            } catch (Exception e) {
                Log.d(TAG, "FTDriver.write() just threw an exception.  Is the cable plugged in?");
            }
        }
    }

    public void connect() {
        if (mFTDriver == null) {
            mFTDriver = new FTDriver(mUsbManager);
        }

        if (mFTDriver.isConnected()) {
            return;
        }

        mFTDriver.begin(baudRate);
        if (!mFTDriver.isConnected()) {
            Log.d(TAG, "mFTDriver.begin() failed.");
        } else {
            Log.d(TAG, "mFTDriver.begin() success!.");
            sendDigit(0);
        }
    }

    public void usbDetached(Intent intent) {
        mFTDriver.usbDetached(intent);
    }

    public void end() {
        mFTDriver.end();
    }

}




Java Source Code List

com.openxc.shiftindicator.ArduinoHardware.java
com.openxc.shiftindicator.JsonBuilder.java
com.openxc.shiftindicator.MainActivity.java
com.openxc.shiftindicator.SettingsActivity.java
com.openxc.shiftindicator.ShiftRecommendation.java