/**
 * @file led_control.h
 * @brief LED控制API (基于HTR3236)
 * @version 1.0
 */

#ifndef LED_CONTROL_H
#define LED_CONTROL_H

#include <stdint.h>
#include "htr3236.h"

#ifdef __XC__
extern "C" {
#endif

/*=========================================================================
    LED类型定义
    -----------------------------------------------------------------------*/
typedef enum {
    // 功能LED (单个LED)
    LED_MUSIC = 2,         // OUT2 - 音乐模式
    LED_ANC = 1,           // OUT1 - ANC模式
    LED_FOOTSTEP_MODE = 3, // OUT3 - 脚步模式
    LED_AI7_1 = 35,        // OUT34 - AI7.1模式
    LED_GAME_MODE = 34,    // OUT35 - 游戏模式
    // OUT36未使用
    
    // L系列LED (OUT4-OUT18)
    LED_L1 = 4,    LED_L2 = 5,    LED_L3 = 6,    LED_L4 = 7,    LED_L5 = 8,
    LED_L6 = 9,    LED_L7 = 10,   LED_L8 = 11,   LED_L9 = 12,   LED_L10 = 13,
    LED_L11 = 14,  LED_L12 = 15,  LED_L13 = 16,  LED_L14 = 17,  LED_L15 = 18,
    
    // D系列LED (OUT19-OUT33)
    LED_D1 = 19,   LED_D2 = 20,   LED_D3 = 21,   LED_D4 = 22,   LED_D5 = 23,
    LED_D6 = 24,   LED_D7 = 25,   LED_D8 = 26,   LED_D9 = 27,   LED_D10 = 28,
    LED_D11 = 29,  LED_D12 = 30,  LED_D13 = 31,  LED_D14 = 32,  LED_D15 = 33,
    
    LED_MAX = 36
} led_id_t;

/*=========================================================================
    LED组定义
    -----------------------------------------------------------------------*/
typedef enum {
    LED_GROUP_L_SERIES,     // L1-L15
    LED_GROUP_D_SERIES,     // D1-D15
    LED_GROUP_FUNCTION,     // 功能LED (ANC, MUSIC, FOOTSTEP, AI7_7, GAME)
    LED_GROUP_ALL           // 所有LED
} led_group_t;

/*=========================================================================
    LED控制上下文
    -----------------------------------------------------------------------*/
typedef struct {
    htr3236_t *htr3236_dev;           // HTR3236设备指针
    CLIENT_INTERFACE(i2c_master_if,i2c); // I2C接口
    uint8_t brightness[36];            // 当前亮度缓存
} led_control_t;

/*=========================================================================
    LED物理映射表
    -----------------------------------------------------------------------*/
// 逻辑LED编号到物理OUT口的映射表
extern const uint8_t led_l_physical_map[15];
extern const uint8_t led_d_physical_map[15];

// 改动原因：麦克风音量协议从旧版38级(0~37)扩展为49级(0~48)，LED映射表长度同步扩展。
extern const uint8_t mic_gain_to_led[49];
// 改动原因：按 SY102dac对应表.csv，DAC音量协议从30级(0~29)扩展为49级(0~48)，LED映射表长度同步扩展。
extern const uint8_t dac_gain_to_led[49];

/*=========================================================================
    基础控制API
    -----------------------------------------------------------------------*/

/**
 * @brief 初始化LED控制系统
 * @param ctx 上下文指针
 * @param dev HTR3236设备指针
 * @param i2c I2C接口
 */
void led_control_init(led_control_t *ctx,
                      htr3236_t *dev,
                      CLIENT_INTERFACE(i2c_master_if,i2c));

/**
 * @brief 设置单个LED亮度
 * @param ctx 上下文指针
 * @param led LED ID
 * @param brightness 亮度值 (0-255)
 * @return 0:成功, -1:失败
 */
int led_set_brightness(led_control_t *ctx,
                       led_id_t led,
                       uint8_t brightness);

/**
 * @brief 开启单个LED (最大亮度)
 * @param ctx 上下文指针
 * @param led LED ID
 * @return 0:成功, -1:失败
 */
int led_on(led_control_t *ctx, led_id_t led);

/**
 * @brief 关闭单个LED
 * @param ctx 上下文指针
 * @param led LED ID
 * @return 0:成功, -1:失败
 */
int led_off(led_control_t *ctx, led_id_t led);

/**
 * @brief 切换LED状态
 * @param ctx 上下文指针
 * @param led LED ID
 * @return 0:成功, -1:失败
 */
int led_toggle(led_control_t *ctx, led_id_t led);

/**
 * @brief 更新所有LED显示 (调用htr3236_update)
 * @param ctx 上下文指针
 * @return 0:成功, -1:失败
 */
int led_update_all(led_control_t *ctx);

/*=========================================================================
    组控制API
    -----------------------------------------------------------------------*/

/**
 * @brief 设置一组LED的亮度
 * @param ctx 上下文指针
 * @param group LED组
 * @param brightness 亮度值 (0-255)
 * @return 0:成功, -1:失败
 */
int led_group_set_brightness(led_control_t *ctx,
                             led_group_t group,
                             uint8_t brightness);

/**
 * @brief 开启一组LED
 * @param ctx 上下文指针
 * @param group LED组
 * @return 0:成功, -1:失败
 */
int led_group_on(led_control_t *ctx, led_group_t group);

/**
 * @brief 关闭一组LED
 * @param ctx 上下文指针
 * @param group LED组
 * @return 0:成功, -1:失败
 */
int led_group_off(led_control_t *ctx, led_group_t group);

#ifdef __XC__
}
#endif

#endif /* LED_CONTROL_H */