#include "app_button.h"
#include "app_timer.h"
#include "band_nctu.h"
#include "button.h"
#include "error.h"
#include "nrf_drv_gpiote.h"
#include "nrf_log.h"

#define PIN_IN F1_BUTTON_1
#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;

#if 1
void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
    switch (action) {
        case NRF_GPIOTE_POLARITY_LOTOHI:
        {
            NRF_LOG_DEBUG("NRF_GPIOTE_POLARITY_LOTOHI\n");
        }
        break;
            
        case NRF_GPIOTE_POLARITY_HITOLO:
        {
            NRF_LOG_DEBUG("NRF_GPIOTE_POLARITY_HITOLO\n");
        }
        break;
        
        case NRF_GPIOTE_POLARITY_TOGGLE:
        {
            NRF_LOG_DEBUG("NRF_GPIOTE_POLARITY_TOGGLE\n");
        }
        break;
    }        
}


#endif 

int buttonInit(void) {
    ret_code_t errCode;
#if 1
    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP;

    errCode = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
    if (errCode != NRF_SUCCESS) {
        NRF_LOG_WARNING("nrf_drv_gpiote_in_init() failed, errCode=0x%x\n", errCode);
        return ERROR_BUTTON_INITIALIZE_FAIL;
    }    

    nrf_drv_gpiote_in_event_enable(PIN_IN, true);

#endif    

    _gButtonInitialized = true;
    return 0;
}

#if 0
void button_events_init(void) {
    nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
    
    timerButtonScanStart();    
}
#endif

