androidthings / contrib-drivers

Open source peripheral drivers
Apache License 2.0
558 stars 174 forks source link

Button driver #120

Closed ghost closed 5 years ago

ghost commented 5 years ago

Button presses are only recognized once. Only after a device reboot is a button press recognized again.

I followed the sample code usage from the readme, and inserted this into the onCreate() method to setup the button on device launch

pButton = new Button(GPIO_P_BUTTON, Button.LogicState.PRESSED_WHEN_HIGH);

        pButton.setOnButtonEventListener(new Button.OnButtonEventListener() {
            @Override
            public void onButtonEvent(Button button, boolean pressed) {
                doStuff();
            }

That code is obviously surrounded by a try/catch block.

I've added this to my module build.gradle file:

compile 'com.google.android.things.contrib:driver-button:1.0'

And this to my onDestroy method

if(pButton != null){
            pButton.close();
        }

I tried different GPIO pins, different cables and different buttons. Always the same result.

Any idea what I could do to change that behavior?

PaulTR commented 5 years ago

Any chance you can share an image of how things are plugged in?

On Sun, Jan 6, 2019, 7:45 AM Yoey notifications@github.com wrote:

Button presses are only recognized once. Only after a device reboot is a button press recognized again.

I followed the sample code usage from the readme, and inserted this into the onCreate() method to setup the button on device launch

pButton = new Button(GPIO_P_BUTTON, Button.LogicState.PRESSED_WHEN_HIGH);

    pButton.setOnButtonEventListener(new Button.OnButtonEventListener() {
        @Override
        public void onButtonEvent(Button button, boolean pressed) {
            doStuff();
        }

That code is obviously surrounded by a try/catch block.

I've added this to my module build.gradle file:

compile 'com.google.android.things.contrib:driver-button:1.0'

And this to my onDestroy method

if(pButton != null){ pButton.close(); }

I tried different GPIO pins, different cables and different buttons. Always the same result.

Any idea what I could do to change that behavior?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/androidthings/contrib-drivers/issues/120, or mute the thread https://github.com/notifications/unsubscribe-auth/ACOhb9yHjm2f6ZlkmxDcpZOVmeWycpX_ks5vAgwUgaJpZM4ZydHD .

ghost commented 5 years ago

I hope you can see everything you need to see with this

img_20190106_165706 - kopie

jdkoren commented 5 years ago

@YoeyHD In your photo, your button seems to be lacking some connections.

With electrical switches (such as buttons), you need an additional connection on the output side (the side that goes to your GPIO) to "pull" the signal towards either power or ground, whichever is opposite the signal on the input side.

For example, take a look at the button on this diagram from one of our samples. Notice the input side of the button connects directly to ground. On the output side, there are 2 connections: one to the GPIO, and one to 5V using a resistor. This is a pull-up resistor. When the button is not being pressed, the GPIO signal is being pulled up to 5V. (You could also swap 5V and ground to make a button that gets pulled down.)

Without a pull-up or pull-down resistor, the GPIO signal can "float" somewhere between high and low, which produces unintended behavior. Try connecting your button like the diagram and see if that helps. (This may not be your only issue, but it's worth fixing just so you can rule it out.)

ghost commented 5 years ago

It works! Thanks a lot! Since this is my first time with GPIO and everything, is there a way to trigger doStuff() once? Right now, it triggers more than once, usually twice. Sometimes even more, especially if I keep the button pressed for a longer period of time. The latter behavior wouldn't be such an issue. However, the former would be an issue

jdkoren commented 5 years ago

@YoeyHD You can check the value of pressed in the callback and compare it to a previous value.

It also may depend on what resistor value you are using... I think most of the time I was using 10K resistors and typically didn't see a press register twice. Of course, that's not as foolproof as checking for a value change in the callback.