diff --git a/app/src/main/java/com/example/androidthings/doorbell/BoardDefaults.java b/app/src/main/java/com/example/androidthings/doorbell/BoardDefaults.java new file mode 100644 index 0000000..6f73699 --- /dev/null +++ b/app/src/main/java/com/example/androidthings/doorbell/BoardDefaults.java @@ -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 gpioList = pioService.getGpioList(); + if (gpioList.size() != 0) { + String pin = gpioList.get(0); + if (pin.startsWith("IO")) { + sBoardVariant = DEVICE_EDISON_ARDUINO; + } + } + } + return sBoardVariant; + } +} diff --git a/app/src/main/java/com/example/androidthings/doorbell/DoorbellActivity.java b/app/src/main/java/com/example/androidthings/doorbell/DoorbellActivity.java index 2f9c7f6..39d515b 100644 --- a/app/src/main/java/com/example/androidthings/doorbell/DoorbellActivity.java +++ b/app/src/main/java/com/example/androidthings/doorbell/DoorbellActivity.java @@ -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. @@ -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); @@ -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(); @@ -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); }