如何使用 AndroidThings 初始化 Cap12xxInputDriver?

How can I initialise a Cap12xxInputDriver with AndroidThings?

我已经在以下位置克隆了用于电容式触摸的 androidthings 示例: https://github.com/androidthings/drivers-samples/tree/master/cap12xx

下面是用来初始化驱动的:

mInputDriver = new Cap12xxInputDriver(this, BoardDefaults.getI2CPort(), null,Cap12xx.Configuration.CAP1208,keyCodes);

当它被击中时,我得到如下异常。我不确定我做错了什么或如何解决这个问题,因为我看不到 I2cDevice class 中发生了什么。硬件设置是安装在 Raspberry Pi Model 3 B 中的 RainbowHAT。显示、温度、压力和 LED 工作正常。

如何初始化 Cap12xxInputDriver?

04-29 20:33:45.159 1238-1238/? W/CaptouchActivity: Unable to open driver connection
com.google.android.things.pio.PioException: android.os.ServiceSpecificException: I/O error
at com.google.android.things.pio.I2cDevice.writeRegByte(I2cDevice.java:198)
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.setInputsEnabled(Cap12xx.java:418)
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.init(Cap12xx.java:241)
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.<init>(Cap12xx.java:188)
at com.google.android.things.contrib.driver.cap12xx.Cap12xxInputDriver.<init>(Cap12xxInputDriver.java:92)
at com.google.android.things.contrib.driver.cap12xx.Cap12xxInputDriver.<init>(Cap12xxInputDriver.java:69)
at com.example.androidthings.driversamples.CaptouchActivity.onCreate(CaptouchActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.os.ServiceSpecificException: I/O error
at android.os.Parcel.readException(Parcel.java:1697)
at android.os.Parcel.readException(Parcel.java:1636)
at com.google.android.things.pio.IPeripheralManagerClient$Stub$Proxy.I2cWriteRegByte(IPeripheralManagerClient.java:1517)
at com.google.android.things.pio.I2cDevice.writeRegByte(I2cDevice.java:196)
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.setInputsEnabled(Cap12xx.java:418) 
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.init(Cap12xx.java:241) 
at com.google.android.things.contrib.driver.cap12xx.Cap12xx.<init>(Cap12xx.java:188) 
at com.google.android.things.contrib.driver.cap12xx.Cap12xxInputDriver.<init>(Cap12xxInputDriver.java:92) 
at com.google.android.things.contrib.driver.cap12xx.Cap12xxInputDriver.<init>(Cap12xxInputDriver.java:69) 
at com.example.androidthings.driversamples.CaptouchActivity.onCreate(CaptouchActivity.java:51) 
at android.app.Activity.performCreate(Activity.java:6662) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

Rainbow HAT 上没有 CAP1208 芯片,因此尝试在 I2C 总线上初始化一个芯片会在开始写入初始化命令时抛出错误,因为该设备不存在。 Rainbow HAT上的电容按键内部管理,外露为纯GPIO引脚。

如果您想体验 CAP12xx 系列的电容触控功能,您可以尝试 Explorer HAT Pro Raspberry Pi。

正如 Devunwired 所说,Rainbow HAT 电容式按钮是通过 GPIO 公开的。它们非常易于使用。

您可以在这里找到密码:Technical Reference - 3 Touch Inputs via AT42QT1070

这里如何使用触摸按钮:https://github.com/blundell/androidthings-gpio-input

下面是在 Activity 中使用彩虹帽上的电容式按钮 A 的示例:

public class MainActivity extends Activity {

    private static final String TOUCH_BUTTON_A_PIN = "BCM21";

    private Gpio bus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        PeripheralManager service = PeripheralManager.newInstance();

        try {
            bus = service.openGpio(TOUCH_BUTTON_A_PIN);
        } catch (IOException e) {
            throw new IllegalStateException(TOUCH_BUTTON_A_PIN + " bus cannot be opened.", e);
        }

        try {
            bus.setDirection(Gpio.DIRECTION_IN);
            bus.setActiveType(Gpio.ACTIVE_LOW);
        } catch (IOException e) {
            throw new IllegalStateException(TOUCH_BUTTON_A_PIN + " bus cannot be configured.", e);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        try {
            bus.setEdgeTriggerType(Gpio.EDGE_BOTH);
            bus.registerGpioCallback(touchButtonACallback);
        } catch (IOException e) {
            throw new IllegalStateException(TOUCH_BUTTON_A_PIN + " bus cannot be monitored.", e);
        }
    }

    private final GpioCallback touchButtonACallback = new GpioCallback() {
        @Override
        public boolean onGpioEdge(Gpio gpio) {
            try {
                if (gpio.getValue()) {
                    Log.i("TUT", "ON PRESSED DOWN");
                } else {
                    Log.i("TUT", "ON PRESSED UP");
                }
            } catch (IOException e) {
                throw new IllegalStateException(TOUCH_BUTTON_A_PIN + " cannot be read.", e);
            }
            return true;
        }
    };

    @Override
    protected void onStop() {
        bus.unregisterGpioCallback(touchButtonACallback);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            bus.close();
        } catch (IOException e) {
            Log.e("TUT", TOUCH_BUTTON_A_PIN + " bus cannot be closed, you may experience errors on next launch.", e);
        }
    }
}