/******************************************************************************
* Copyright (C) 2023 Advanced Micro Devices, Inc. All Rights Reserved.
* SPDX-License-Identifier: MIT
******************************************************************************/
/*
 * GPIO LED Test Application
 *
 */


#include "xgpio.h"
#include "xparameters.h"
#include "xstatus.h"
#include "xil_printf.h"
#include "sleep.h"

/* GPIO configuration */
#define LEDS_DEVICE_ID 0x40000000
#define LED_CHANNEL    1

static XGpio LedGpio;

/* Initialize LEDs */
int leds_init() {
    int status = XGpio_Initialize(&LedGpio, LEDS_DEVICE_ID);
    if (status != XST_SUCCESS) {
        xil_printf("LED GPIO init failed!\r\n");
        return XST_FAILURE;
    }
    XGpio_SetDataDirection(&LedGpio, LED_CHANNEL, 0x00); // All outputs
    return XST_SUCCESS;
}

/* Set LED pattern */
void leds_set(u32 mask) {
    XGpio_DiscreteWrite(&LedGpio, LED_CHANNEL, mask);
}

/* Main Application */
int main() {
    xil_printf("LED Control Baremetal Demo\r\n");

    if (leds_init() != XST_SUCCESS) {
        return -1;
    }

    xil_printf("Starting LED pattern...\r\n");

    for (int i = 0; i < 10; i++) {
        u32 pattern = (1 << (i % 8)); // Light up one LED at a time
        leds_set(pattern);
        xil_printf("Iteration %d: LED mask = 0x%02X\r\n", i+1, pattern);
        usleep(500000); // 500ms delay
    }

    xil_printf("Turning all LEDs ON for 2 seconds...\r\n");
    leds_set(0xFF);
    sleep(2);

    xil_printf("Turning all LEDs OFF.\r\n");
    leds_set(0x00);

    xil_printf("LED demo completed.\r\n");
    return 0;
}
