Hey, I recently got the nrf52 DK and tried to make use of the app_button library to have easy to use debouncing. As far as I understood the library, the event should be fired when the button is pressed. The thing is, when the button is released, the event is also fired. Is that working as intended? I tried to work around this with the app_button_is_pushed query, but this will also be true even after I released the button. I guess I'm doing something wrong here. Here is my code.
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "nrf_gpiote.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "nrf_drv_ppi.h"
#include "nrf_drv_timer.h"
#include "nrf_drv_gpiote.h"
#include "app_button.h"
#include "app_timer.h"
#include "app_error.h"
#include "app_scheduler.h"
#include "nrf_delay.h"
#define NUM_OF_BUTTONS 1
#define APP_TIMER_PRESCALER 0
#define APP_TIMER_OP_QUEUE_SIZE 2
static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
if(app_button_is_pushed(BUTTON_1) == true)
{
bsp_board_led_on(1);
nrf_delay_ms(500);
bsp_board_led_off(1);
}
//code run on button state change
}
//Configure a button with pullup and detection on low state
static const app_button_cfg_t app_buttons[NUM_OF_BUTTONS] =
{
{BUTTON_1, false, BUTTON_PULL, button_event_handler}
};
static void lfclk_config(void)
{
NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
NRF_CLOCK->TASKS_LFCLKSTART = 1;
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
{
//Do nothing.
}
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}
int main(void)
{
lfclk_config();
uint32_t err_code;
//app_button uses app_timer, if this is not initialize, then initialize it here
APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
//init app_button module, 50ms detection delay (button debouncing)
err_code = app_button_init((app_button_cfg_t *)app_buttons,
NUM_OF_BUTTONS,
APP_TIMER_TICKS(50, APP_TIMER_PRESCALER));
APP_ERROR_CHECK(err_code);
err_code = app_button_enable();
APP_ERROR_CHECK(err_code);
bsp_board_leds_init();
// Enter main loop.
while(true)
{
}
}
Someone knows if I did something wrong or how I can edit the code so the led only blinks if I press the button and not when I release it? Thanks in advance.