Search in sources :

Example 1 with ButtonInputDriver

use of com.google.android.things.contrib.driver.button.ButtonInputDriver in project sample-button by androidthings.

the class ButtonActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG, "Starting ButtonActivity");
    PeripheralManager pioService = PeripheralManager.getInstance();
    try {
        Log.i(TAG, "Configuring GPIO pins");
        mLedGpio = pioService.openGpio(BoardDefaults.getGPIOForLED());
        mLedGpio.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
    } catch (IOException e) {
        Log.e(TAG, "Error configuring GPIO pins", e);
    }
    try {
        Log.i(TAG, "Registering button driver " + BoardDefaults.getGPIOForButton());
        // Initialize and register the InputDriver that will emit SPACE key events
        // on GPIO state changes.
        mButtonInputDriver = new ButtonInputDriver(BoardDefaults.getGPIOForButton(), Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_SPACE);
        mButtonInputDriver.register();
    } catch (IOException e) {
        Log.e(TAG, "Error configuring GPIO pins", e);
    }
}
Also used : ButtonInputDriver(com.google.android.things.contrib.driver.button.ButtonInputDriver) PeripheralManager(com.google.android.things.pio.PeripheralManager) IOException(java.io.IOException)

Example 2 with ButtonInputDriver

use of com.google.android.things.contrib.driver.button.ButtonInputDriver in project robocar by androidthings.

the class RobocarActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // init AdvertisingInfo
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    mAdvertisingInfo = PreferenceUtils.loadAdvertisingInfo(prefs);
    if (mAdvertisingInfo == null) {
        mAdvertisingInfo = AdvertisingInfo.generateAdvertisingInfo();
        PreferenceUtils.saveAdvertisingInfo(prefs, mAdvertisingInfo);
    }
    try {
        mMotorHat = new MotorHat(BoardDefaults.getI2cBus());
    } catch (IOException e) {
        throw new RuntimeException("Failed to create MotorHat", e);
    }
    try {
        mDisplay = new AlphanumericDisplay(BoardDefaults.getI2cBus());
        mDisplay.setEnabled(true);
        mDisplay.setBrightness(0.5f);
        mDisplay.clear();
    } catch (IOException e) {
        // We may not have a display, which is OK. CarController only uses it if it's not null.
        Log.e(TAG, "Failed to open display.", e);
        mDisplay = null;
    }
    try {
        mButtonInputDriver = new ButtonInputDriver(BoardDefaults.getButtonGpioPin(), LogicState.PRESSED_WHEN_HIGH, KeyEvent.KEYCODE_A);
        mButtonInputDriver.register();
    } catch (IOException e) {
        Log.e(TAG, "Failed to open button driver.", e);
        mButtonInputDriver = null;
    }
    String[] ledPins = BoardDefaults.getLedGpioPins();
    mLed = new TricolorLed(ledPins[0], ledPins[1], ledPins[2]);
    mCarController = new CarController(mMotorHat, mLed, mDisplay);
    mResetHandler = new Handler();
    mViewModel = ViewModelProviders.of(this).get(RobocarViewModel.class);
    mNearbyAdvertiser = mViewModel.getRobocarAdvertiser();
    mNearbyAdvertiser.setAdvertisingInfo(mAdvertisingInfo);
    mNearbyAdvertiser.setPairedDiscovererInfo(PreferenceUtils.loadDiscovererInfo(prefs));
    mNearbyAdvertiser.getAdvertisingLiveData().observe(this, new Observer<Boolean>() {

        @Override
        public void onChanged(@Nullable Boolean value) {
            mIsAdvertising = value == null ? false : value;
            updateUi();
        }
    });
    mNearbyAdvertiser.getCompanionConnectionLiveData().observe(this, new Observer<CompanionConnection>() {

        @Override
        public void onChanged(@Nullable CompanionConnection connection) {
            setConnection(connection);
        }
    });
    if (savedInstanceState == null) {
        // First launch. Attach the connector fragment and give it our client to connect.
        ConnectorFragment.attachTo(this, mViewModel.getGoogleApiClient());
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) Handler(android.os.Handler) IOException(java.io.IOException) MotorHat(com.google.android.things.contrib.driver.motorhat.MotorHat) AlphanumericDisplay(com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay) ButtonInputDriver(com.google.android.things.contrib.driver.button.ButtonInputDriver)

Example 3 with ButtonInputDriver

use of com.google.android.things.contrib.driver.button.ButtonInputDriver in project sample-tensorflow-imageclassifier by androidthings.

the class ImageClassifierActivity method initPIO.

/**
 * This method should only be called when running on an Android Things device.
 */
private void initPIO() {
    PeripheralManager pioManager = PeripheralManager.getInstance();
    try {
        mReadyLED = pioManager.openGpio(BoardDefaults.getGPIOForLED());
        mReadyLED.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
        mButtonDriver = new ButtonInputDriver(BoardDefaults.getGPIOForButton(), Button.LogicState.PRESSED_WHEN_LOW, SHUTTER_KEYCODE);
        mButtonDriver.register();
    } catch (IOException e) {
        mButtonDriver = null;
        Log.w(TAG, "Could not open GPIO pins", e);
    }
}
Also used : ButtonInputDriver(com.google.android.things.contrib.driver.button.ButtonInputDriver) PeripheralManager(com.google.android.things.pio.PeripheralManager) IOException(java.io.IOException)

Aggregations

ButtonInputDriver (com.google.android.things.contrib.driver.button.ButtonInputDriver)3 IOException (java.io.IOException)3 PeripheralManager (com.google.android.things.pio.PeripheralManager)2 SharedPreferences (android.content.SharedPreferences)1 Handler (android.os.Handler)1 AlphanumericDisplay (com.google.android.things.contrib.driver.ht16k33.AlphanumericDisplay)1 MotorHat (com.google.android.things.contrib.driver.motorhat.MotorHat)1