/*
 * Copyright (c) 2025 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
 */

#include <zephyr/kernel.h> /* k_busy_wait() */
#include <zephyr/sys_clock.h> /* USEC_PER_MSEC */
#include <zephyr/logging/log.h>
#include <zephyr/logging/log_ctrl.h>

#include <nrfx_gpiote.h>
#include <hal/nrf_gpio.h>


LOG_MODULE_REGISTER(app, CONFIG_APP_LEDS_LOG_LEVEL);

//---------------------------------------------------------------------------------------------
// GPIOTE
#define BUTTON_0                        NRF_PIN_PORT_TO_PIN_NUMBER(13, 1)
#define BUTTON_1                        NRF_PIN_PORT_TO_PIN_NUMBER(9, 1)
#define BUTTON_2                        NRF_PIN_PORT_TO_PIN_NUMBER(8, 1)
#define BUTTON_3                        NRF_PIN_PORT_TO_PIN_NUMBER(4, 0)

#define GPIOTE_INST                     NRF_GPIOTE20
#define CONFIG_APP_GPIOTE_IRQ_PRIO      0

/* GPIOTE30 实例 */
static nrfx_gpiote_t gpiote_inst = NRFX_GPIOTE_INSTANCE(GPIOTE_INST);

/* PORT 事件回调：多个引脚共用同一 handler */
static void port_event_handler(nrfx_gpiote_pin_t pin,
                                nrfx_gpiote_trigger_t trigger,
                                void *context)
{
    /* nrfx 驱动内部已自动清除事件 */
    if (pin == BUTTON_0) {
        LOG_INF("BUTTON_0");
        /* 处理 BUTTON_0 事件 */
    } else if (pin == BUTTON_1) {
        LOG_INF("BUTTON_1");
        /* 处理 BUTTON_1 事件 */
    }
}

ISR_DIRECT_DECLARE(gpiote_30_direct_isr)
{
	//nrfx_gpiote_irq_handler(&gpiote_inst);
	 nrfx_gpiote_irq_handler(&gpiote_inst);  /* 或使用 NRFX_GPIOTE_INST_HANDLER_GET(20) */
	return 0;
}

void Key_Init(void)
{
    int err;

    /* 初始化 GPIOTE20 */
    IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(GPIOTE_INST),
			   CONFIG_APP_GPIOTE_IRQ_PRIO,
			   gpiote_20_direct_isr, 0);

	irq_enable(NRFX_IRQ_NUMBER_GET(GPIOTE_INST));
    
    err = nrfx_gpiote_init(&gpiote_inst, CONFIG_APP_GPIOTE_IRQ_PRIO);
    if (err) {
        LOG_ERR("nrfx_gpiote_init error: 0x%08X", err);
        return;
    }

    /* 使用 PORT 事件（无需分配独立通道，p_in_channel = NULL） */
    nrf_gpio_pin_pull_t pull_config = NRF_GPIO_PIN_PULLUP;

    /* PORT 事件触发配置：p_in_channel 设为 NULL 表示使用 PORT 事件组 */
    nrfx_gpiote_trigger_config_t trigger_config = {
        .trigger      = NRFX_GPIOTE_TRIGGER_HITOLO,
        .p_in_channel = NULL,  /* NULL = 使用 PORT 事件，不占用独立通道 */
    };

    nrfx_gpiote_handler_config_t handler_config = {
        .handler   = port_event_handler,
        .p_context = NULL,
    };

    nrfx_gpiote_input_pin_config_t input_config = {
        .p_pull_config    = &pull_config,
        .p_trigger_config = &trigger_config,
        .p_handler_config = &handler_config,
    };

    /* 配置 BUTTON_0 */
    err = nrfx_gpiote_input_configure(&gpiote_inst, BUTTON_0, &input_config);
    if (err) {
        LOG_ERR("BUTTON_0 configure error: 0x%08X", err);
        return;
    }
    nrfx_gpiote_trigger_enable(&gpiote_inst, BUTTON_0, true);

    /* 配置 BUTTON_1（共用同一 handler） */
    err = nrfx_gpiote_input_configure(&gpiote_inst, BUTTON_1, &input_config);
    if (err) {
        LOG_ERR("BUTTON_1 configure error: 0x%08X", err);
        return;
    }
    nrfx_gpiote_trigger_enable(&gpiote_inst, BUTTON_1, true);
}

int main(void)
{
	LOG_INF("LEDs sample started");

	/* Initialize the LED */
	
    Key_Init();
	while (true) {
		while (LOG_PROCESS()) {
		}

		/* Turn the LED on *

		/* Turn the LED off */


	}

	return 0;
}
