Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button driver #120

Closed
ghost opened this issue Jan 6, 2019 · 5 comments
Closed

Button driver #120

ghost opened this issue Jan 6, 2019 · 5 comments

Comments

@ghost
Copy link

ghost commented Jan 6, 2019

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
Copy link
Contributor

PaulTR commented Jan 6, 2019 via email

@ghost
Copy link
Author

ghost commented Jan 6, 2019

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

img_20190106_165706 - kopie

@jdkoren
Copy link
Member

jdkoren commented Jan 7, 2019

@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
Copy link
Author

ghost commented Jan 8, 2019

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
Copy link
Member

jdkoren commented Jan 8, 2019

@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.

@jdkoren jdkoren closed this as completed Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants