Skip to content

Commit

Permalink
Add support for more boards e.g. iMX7d
Browse files Browse the repository at this point in the history
Add more board variants to support their respective namings of GPIO pins
for input button

Bug: 63540960

Change-Id: Iaa1c4a03f55e74c6d95cc2334bdb1237671f8e08
  • Loading branch information
shawngit authored and Shawn S committed Aug 9, 2017
1 parent d6a9f46 commit 398601d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2016, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.androidthings.doorbell;

import android.os.Build;

import com.google.android.things.pio.PeripheralManagerService;

import java.util.List;

@SuppressWarnings("WeakerAccess")
public class BoardDefaults {
private static final String DEVICE_EDISON_ARDUINO = "edison_arduino";
private static final String DEVICE_EDISON = "edison";
private static final String DEVICE_JOULE = "joule";
private static final String DEVICE_RPI3 = "rpi3";
private static final String DEVICE_IMX6UL_PICO = "imx6ul_pico";
private static final String DEVICE_IMX6UL_VVDN = "imx6ul_iopb";
private static final String DEVICE_IMX7D_PICO = "imx7d_pico";
private static String sBoardVariant = "";

/**
* Return the GPIO pin that the Button is connected on.
*/
public static String getGPIOForButton() {
switch (getBoardVariant()) {
case DEVICE_RPI3:
return "BCM21";
case DEVICE_IMX7D_PICO:
return "GPIO_174";
default:
throw new IllegalStateException("Unknown Build.DEVICE " + Build.DEVICE);
}
}

private static String getBoardVariant() {
if (!sBoardVariant.isEmpty()) {
return sBoardVariant;
}
sBoardVariant = Build.DEVICE;
// For the edison check the pin prefix
// to always return Edison Breakout pin name when applicable.
if (sBoardVariant.equals(DEVICE_EDISON)) {
PeripheralManagerService pioService = new PeripheralManagerService();
List<String> gpioList = pioService.getGpioList();
if (gpioList.size() != 0) {
String pin = gpioList.get(0);
if (pin.startsWith("IO")) {
sBoardVariant = DEVICE_EDISON_ARDUINO;
}
}
}
return sBoardVariant;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class DoorbellActivity extends Activity {
/*
* Driver for the doorbell button;
*/
private ButtonInputDriver mButton;
private ButtonInputDriver mButtonInputDriver;

/**
* A {@link Handler} for running Camera tasks in the background.
Expand All @@ -73,12 +73,6 @@ public class DoorbellActivity extends Activity {
*/
private HandlerThread mCloudThread;

/**
* The GPIO pin to activate to listen for button presses.
*/
private final String BUTTON_GPIO_PIN = "BCM21";


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -104,17 +98,26 @@ public void onCreate(Bundle savedInstanceState) {
mCloudHandler = new Handler(mCloudThread.getLooper());

// Initialize the doorbell button driver
try {
mButton = new ButtonInputDriver(BUTTON_GPIO_PIN,
Button.LogicState.PRESSED_WHEN_LOW, KeyEvent.KEYCODE_ENTER);
} catch (IOException e) {
Log.e(TAG, "button driver error", e);
}
initPIO();

// Camera code is complicated, so we've shoved it all in this closet class for you.
mCamera = DoorbellCamera.getInstance();
mCamera.initializeCamera(this, mCameraHandler, mOnImageAvailableListener);
}

private void initPIO() {
try {
mButtonInputDriver = new ButtonInputDriver(
BoardDefaults.getGPIOForButton(),
Button.LogicState.PRESSED_WHEN_LOW,
KeyEvent.KEYCODE_ENTER);
mButtonInputDriver.register();
} catch (IOException e) {
mButtonInputDriver = null;
Log.w(TAG, "Could not open GPIO pins", e);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
Expand All @@ -123,7 +126,7 @@ protected void onDestroy() {
mCameraThread.quitSafely();
mCloudThread.quitSafely();
try {
mButton.close();
mButtonInputDriver.close();
} catch (IOException e) {
Log.e(TAG, "button driver error", e);
}
Expand Down

0 comments on commit 398601d

Please # to comment.