/*
 * Copyright (c) 2016 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdio.h>
#include <zephyr/kernel.h>
#include <zephyr/drivers/gpio.h>

/* 1000 msec = 1 sec */
#define SLEEP_TIME_MS   1000

/* The devicetree node identifier for the "led0" alias. */
#define LED0_NODE DT_ALIAS(led0)
#define SWITCH_NODE   DT_NODELABEL(input_switch)
/*
 * A build error on this line means your board is unsupported.
 * See the sample documentation for information on how to fix this.
 */
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);

static const struct gpio_dt_spec switch_spec = GPIO_DT_SPEC_GET(SWITCH_NODE,gpios);
static struct gpio_callback switch_cb_data;


static void switch_handler(const struct device *dev, struct gpio_callback *cb,
			  uint32_t pins)
{
	gpio_pin_toggle_dt(&led);
}


int main(void)
{
	int ret;
	bool led_state = true;

	if (!gpio_is_ready_dt(&led)) {
		return 0;
	}

	ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
	if (ret < 0) {
		return 0;
	}

	gpio_pin_set_dt(&led,0);

	gpio_pin_configure_dt(&switch_spec,GPIO_INPUT);
    gpio_pin_interrupt_configure_dt(&switch_spec,GPIO_INT_EDGE_RISING);
    gpio_init_callback(&switch_cb_data,switch_handler,BIT(switch_spec.pin));
    gpio_add_callback(switch_spec.port,&switch_cb_data);

	while (1) {
		// ret = gpio_pin_toggle_dt(&led);
		// if (ret < 0) {
		// 	return 0;
		// }

		// led_state = !led_state;
		// printf("LED state: %s\n", led_state ? "ON" : "OFF");
		k_msleep(SLEEP_TIME_MS);
	}
	return 0;
}
