This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Detect button callback trigger

Hello,

In relation to button press detection, I'm curious if I'm utilizing both GPIO_INT_EDGE and GPIO_IN_DOUBLE_EDGE if I can pass the interrupt type to my callback to know whether it was triggered by rising or falling of the button? At the moment I'm doing this by keeping a count of button presses and then using basic math to determine if the value is even or odd. Even = button down, odd = button up. Is there a better way to do this?

Parents
  • Hello,

    There are a couple approaches for this. One is to actually read the pin state in the interrupt. 

    Do you use any libraries for your button handling? bsp or app_button, for example?

    If you want a bit more than just the basic button handler, such as whether the button was pressed or not, I would suggest that you look into using the app_button library directly (which is used by the bsp library). The reason for this is that the bsp library strips away quite a lot from the app_button functionality.

    If you take a closer look in the bsp example found in SDK\examples\peripheral\bsp, and look inside the bsp_init() function, you can see that it calls app_button_init(), with app_buttons as an argument. In app_buttons, there is an event handler; bsp_button_event_handler(). In this event handler you can see that it checks whether the button was pressed or released.

    NB: Note that the app_button library requires you to start the app_timer in order to work. (It is used for debounce)

    Best regards,

    Edvin

  • Edvin,

    Thank you for your detailed reply. I'll give the app_button library a look and test implementing it. I should have been more specific rather than just using a tag of nRF9160, but I'm using the connect SDK and also using this same button as a wakeup_pin to wake the nRF9160 module from deep sleep. Do you see any issues with this functioning under Connect SDK?

  • Hello, Sorry. I didn't see that you used the nRF9160 tag. I thought you were using the nRF52. Let me look into this.

  • Ok, can you try to set up two different handlers for the events? One for rising and one for falling? Alternatively, you can read the pin state in the callback, but if so, you may want to add a delay to prevent reading the wrong state when the pin is bouncing, which it typically is on a button press.

  • Perfect... I'm not sure why I decided to use math prior instead of just reading the pin state. I created a simplified function where I do this all through my code, but never crossed my mind for the button press. This method works perfect, so I'll go that route. Thank your for your time

Reply Children
No Data
Related