/*
 * Copyright (c) 2012-2014 Wind River Systems, Inc.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <sys/printk.h>
#include <hal/nrf_gpio.h>
#include <nrfx_gpiote.h>

#include <logging/log.h>
LOG_MODULE_REGISTER(nrfx_sample, LOG_LEVEL_INF);

 
#define INPUT_PIN                   NRF_GPIO_PIN_MAP(0,28)   //GPIOTE input pin 
#define INPUT_PIN1		  NRF_GPIO_PIN_MAP(0,05)
#define INPUT_PIN2		 NRF_GPIO_PIN_MAP(0,26)
#define INPUT_PIN3					NRF_GPIO_PIN_MAP(0,23)
//#define INPUT_PIN                   NRF_GPIO_PIN_MAP(0,23)
//#define RX_PIN                   NRF_GPIO_PIN_MAP(0,22)
#define PIN_TESTING		 NRF_GPIO_PIN_MAP(0,04) //Testing PIN connect this pin to GPIOTE Input pin it will trigger GPIOTE EVENT
#define Deal_PIN		 NRF_GPIO_PIN_MAP(0,06)
#define LED_PIN                  NRF_GPIO_PIN_MAP(0,29)

#define STACK_SIZE (4096)

static K_THREAD_STACK_DEFINE(tstack,STACK_SIZE);
static struct k_thread scheduler_thread;

//#define INPUT_PIN	DT_GPIO_PIN(DT_ALIAS(sw0), gpios)

static K_SEM_DEFINE(gpio_sem, 0, 1);
static void gpio_test_func(struct k_timer *timer_id);
K_TIMER_DEFINE(send_timer, gpio_test_func, NULL);


ISR_DIRECT_DECLARE(gpiote_isr)
{
    nrfx_gpiote_irq_handler();
    return 1;
}

static void schdeuler_task_handler(void *p1, void *p2,void *p3)
{
    while(true)
    {
    
    }
}

static void sch_task_init(void)
{
  k_thread_create(&scheduler_thread
                  ,tstack
                  ,STACK_SIZE
                  ,schdeuler_task_handler
                  ,NULL
                  ,NULL
                  ,NULL
                  ,K_PRIO_PREEMPT(2)
                  ,0
                  ,K_NO_WAIT);
}

static void gpio_test_func(struct k_timer *timer_id)
{
     nrf_gpio_pin_clear(PIN_TESTING);
  //   LOG_INF("set PIN");
}



static void port_event_handler(void)
{
    LOG_INF("input PIN event");
   nrf_gpio_pin_set(PIN_TESTING);
   nrf_gpio_pin_set(Deal_PIN);
 //  nrf_gpio_pin_toggle(LED_PIN);
   k_sem_give(&gpio_sem);

}

void main(void)
{
	LOG_INF("IDO nrfx_gpiote sample on %s", CONFIG_BOARD);
        nrfx_err_t err;
    /*    
        IRQ_CONNECT(GPIOTE0_IRQn,
		    0,
		    nrfx_isr, nrfx_gpiote_irq_handler, 0);
                    */
        
        // Connect GPIOTE_0 IRQ to nrfx_gpiote_irq_handler 
        /*
	IRQ_CONNECT(DT_IRQN(DT_NODELABEL(gpiote)),
		    DT_IRQ(DT_NODELABEL(gpiote), priority),
		    nrfx_isr, nrfx_gpiote_irq_handler, 0);
                    */
                    
        /* Initialize GPIOTE (the interrupt priority passed as the parameter
	 * here is ignored, see nrfx_glue.h).
	 */
        //IRQ_CONNECT();
        IRQ_DIRECT_CONNECT(GPIOTE0_IRQn,0,nrfx_gpiote_irq_handler,0);

	err = nrfx_gpiote_init(0);
	if (err != NRFX_SUCCESS) {
		LOG_ERR("nrfx_gpiote_init error: %08x", err);
		return;
	}


        nrfx_gpiote_in_config_t const in_config = {
		.sense = NRF_GPIOTE_POLARITY_HITOLO,
		.pull = NRF_GPIO_PIN_PULLUP,
		.is_watcher = false,
		.hi_accuracy = true,
		.skip_gpio_setup = false,
	};
       
       /* Initialize input pin to generate event on high to low transition
	 * (falling edge) and call button_handler()
	 */
	err = nrfx_gpiote_in_init(INPUT_PIN, &in_config, port_event_handler);
	if (err != NRFX_SUCCESS) {
		LOG_ERR("nrfx_gpiote_in_init error: %08x", err);
		return;
	}
         //setting for Lowpower
       //  nrf_gpio_cfg_sense_set(INPUT_PIN,NRF_GPIO_PIN_SENSE_LOW);


        nrf_gpio_cfg_output(PIN_TESTING);
        nrf_gpio_cfg_output(Deal_PIN);
   //     nrf_gpio_cfg_output(LED_PIN);
        nrf_gpio_pin_set(PIN_TESTING);
        nrf_gpio_pin_clear(Deal_PIN);
     //   sch_task_init();

        //setting for Lowpower
    // nrfx_gpiote_latency_set(NRF_GPIOTE_LATENCY_LOWPOWER);
       nrfx_gpiote_latency_set(NRF_GPIOTE_LATENCY_LOWLATENCY);
        NVIC_ClearPendingIRQ(GPIOTE0_IRQn);
        nrfx_gpiote_in_event_enable(INPUT_PIN, true);
      
 /*       

	err = nrfx_gpiote_in_init(INPUT_PIN1, &in_config, port_event_handler);
	if (err != NRFX_SUCCESS) {
		LOG_ERR("nrfx_gpiote_in_init error: %08x", err);
		return;
	}

    nrfx_gpiote_in_event_enable(INPUT_PIN1, true);
	
	err = nrfx_gpiote_in_init(INPUT_PIN2, &in_config, port_event_handler);
	if (err != NRFX_SUCCESS) {
		LOG_ERR("nrfx_gpiote_in_init error: %08x", err);
		return;
	}

    nrfx_gpiote_in_event_enable(INPUT_PIN2, true);
	
	err = nrfx_gpiote_in_init(INPUT_PIN3, &in_config, port_event_handler);
	if (err != NRFX_SUCCESS) {
		LOG_ERR("nrfx_gpiote_in_init error: %08x", err);
		return;
	}

    nrfx_gpiote_in_event_enable(INPUT_PIN3, true);
    */
	
	k_timer_start(&send_timer, K_MSEC(200), K_MSEC(200));

    for(;;)
    {
        k_sem_take(&gpio_sem, K_FOREVER);
        nrf_gpio_pin_clear(Deal_PIN);
        LOG_INF("Deal done start again");

      
    }

}
