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

Please tell me the basic usage of app_button.

Hello, everyone.

I'm a novice who just started Nordic. My goal is to apply button input and timer to implement two functions with one button. (Based on the time the button was placed.)

To do this, you want to use a simple app_button_push and app_button_release. Below is a code generated in the bsp example (SDK v17).

There is no problem with uploading, but pressing the button does not work.

#include <stdbool.h>
#include <stdint.h>
#include "boards.h"
#include "bsp.h"
#include "app_timer.h"
#include "nordic_common.h"
#include "nrf_error.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "bsp_config.h"
#include "app_button.h"

#define BUTTON_DETECTION_DELAY          APP_TIMER_TICKS(50)                     /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */

static void button_event_handler(uint8_t pin_no, uint8_t button_action)
{
    if (button_action == APP_BUTTON_PUSH)
    {
        switch (pin_no)
        {
            case BSP_BUTTON_0:
		        LEDS_INVERT(BSP_LED_0_MASK);
		        break;		
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    }
    else if (button_action == APP_BUTTON_RELEASE)
    {
        switch (pin_no)
        {
            case BSP_BUTTON_0:
		        LEDS_INVERT(BSP_LED_0_MASK);
		        break;
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    }
}

void clock_initialization()
{
    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.
    }
}

static void buttons_init(void)
{
    ret_code_t err_code;

    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[] =
    {
        {BSP_BUTTON_0, false, BUTTON_PULL, button_event_handler}
    };

    err_code = app_button_init(buttons, ARRAY_SIZE(buttons),
                               BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);
}


static void leds_init(void)
{
    bsp_board_init(BSP_INIT_LEDS);
}

static void timers_init(void)
{
    // Initialize timer module, making it use the scheduler
    ret_code_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);
}


/**
 * @brief Function for application main entry.
 */
int main(void)
{
    ret_code_t err_code;

    err_code = app_button_enable();
    APP_ERROR_CHECK(err_code);
    
    timers_init();
    buttons_init();
    leds_init();

    clock_initialization();

    err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("BSP example started.");
    //bsp_configuration();

    while (true)
    {
        NRF_LOG_FLUSH();
        __SEV();
        __WFE();
        __WFE();
        // no implementation needed
    }
}

Can I get a solution to this problem?

Thank you.

Related