#include "app_button.h"
#include "app_timer.h"
#include "band_nctu.h"
#include "button.h"
#include "error.h"
#include "nrf_log.h"

#define BUTTON_NUMBER           1
#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 bool _gButtonInitialized = false;
static void _buttonEventHandler(uint8_t pinNo, uint8_t buttonAction);
static const app_button_cfg_t _gAppButton[BUTTONS_NUMBER] = {
    {BUTTON_1, false, BUTTON_PULL, _buttonEventHandler},
};

static void _buttonEventHandler(uint8_t pinNo, uint8_t buttonAction) {
    switch (buttonAction) {
        case APP_BUTTON_PUSH:
        {
            NRF_LOG_DEBUG("APP_BUTTON_PUSH\n");
        }    
        break;

        case APP_BUTTON_RELEASE:
        {
            NRF_LOG_DEBUG("APP_BUTTON_RELEASE\n");
        }    
        break;
    }    
}    
 

int buttonInit(void) {
    ret_code_t errCode;
    errCode = app_button_init(_gAppButton,
                               BUTTON_NUMBER,
                               BUTTON_DETECTION_DELAY);
    NRF_LOG_DEBUG("app_button_init() errCode=0x%x\n", errCode);
    if (errCode != NRF_SUCCESS) {
        NRF_LOG_WARNING("app_button_init() failed, errCode=0x%x\n", errCode);
        return ERROR_BUTTON_INITIALIZE_FAIL;
    }

    errCode = app_button_enable();
    NRF_LOG_DEBUG("app_button_enable() errCode=0x%x\n", errCode);
    if (errCode != NRF_SUCCESS) {
        NRF_LOG_WARNING("app_button_enable() failed, errCode=0x%x\n", errCode);
        return ERROR_BUTTON_INITIALIZE_FAIL;
    }

    _gButtonInitialized = true;
    return 0;
}

#if 0
void button_events_init(void) {
    nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
    
    timerButtonScanStart();    
}
#endif

