/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : led.c
  * @brief          : LED driver module source file
  ******************************************************************************
  */
/* USER CODE END Header */

#include "led.h"

/**
 * @brief Initialize LED module
 * @param led: Pointer to LED instance
 * @param config: Pointer to LED configuration
 */
void led_init(led_t *led, const led_config_t *config) {
    if (led == NULL || config == NULL) {
        return;
    }
    
    led->config = *config;
    led->state = 0;
    
    /* Ensure LED is initially off */
    hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
}

/**
 * @brief Turn on LED
 * @param led: Pointer to LED instance
 */
void led_on(led_t *led) {
    if (led == NULL) {
        return;
    }
    
    hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_SET);
    led->state = 1;
}

/**
 * @brief Turn off LED
 * @param led: Pointer to LED instance
 */
void led_off(led_t *led) {
    if (led == NULL) {
        return;
    }
    
    hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
    led->state = 0;
}

/**
 * @brief Toggle LED state
 * @param led: Pointer to LED instance
 */
void led_toggle(led_t *led) {
    if (led == NULL) {
        return;
    }
    
    hal_gpio_toggle_pin(led->config.gpio_port, led->config.gpio_pin);
    led->state = !led->state;
}

/**
 * @brief Get LED current state
 * @param led: Pointer to LED instance
 * @retval 0: LED is off, 1: LED is on
 */
uint8_t led_get_state(const led_t *led) {
    if (led == NULL) {
        return 0;
    }
    
    return led->state;
}
