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

#include "led.h"

#define LED0_NODE DT_ALIAS(led0)

#define LED_STACK_SIZE 1024
#define LED_PRIORITY   7

static const struct gpio_dt_spec led =
    GPIO_DT_SPEC_GET(LED0_NODE, gpios);

K_THREAD_STACK_DEFINE(led_stack, LED_STACK_SIZE);
static struct k_thread led_thread_data;

static void led_thread(void *p1, void *p2, void *p3)
{
    ARG_UNUSED(p1);
    ARG_UNUSED(p2);
    ARG_UNUSED(p3);

    while (1) {
        gpio_pin_toggle_dt(&led);
        k_msleep(500);
    }
}

void led_init(void)
{
    if (!gpio_is_ready_dt(&led)) {
        return;
    }

    if (gpio_pin_configure_dt(&led, GPIO_OUTPUT_INACTIVE) < 0) {
        return;
    }

    k_thread_create(&led_thread_data,
                    led_stack,
                    K_THREAD_STACK_SIZEOF(led_stack),
                    led_thread,
                    NULL, NULL, NULL,
                    LED_PRIORITY,
                    0,
                    K_NO_WAIT);
}