#include "drv_rgb.h"
#include "drv_rgb_pwm.h"
#include "rgb_player.h"
#include "rgb_effect.h"


static struct rgb_drv_cfg g_rgb_drv_cfg;

/**
 * @brief 初始化RGB驱动
 * 
 * @return rt_err_t 错误码，RT_EOK表示成功，-RT_ERROR表示失败
 */ 
rt_err_t rgb_drv_init(void)
{
    struct rgb_drv_cfg *cfg = &g_rgb_drv_cfg;

    if (cfg->is_initlized) {
        rt_kprintf("func: %s, rgb driver is initialized.\r\n", __func__);
        return -RT_ERROR;
    }
    
    // cfg->rgb_strip_dev = rt_device_find(RGB_STRIP_DRV_NAME);
    // if (!cfg->rgb_strip_dev) {
    //     rt_kprintf("func: %s, rgb strip driver %s find fail.\r\n", __func__, RGB_STRIP_DRV_NAME);
    //     return -RT_ERROR;   
    // }
    
    cfg->rgb_circle_dev = rt_device_find(RGB_CIRCLE_DRV_NAME);
    if (!cfg->rgb_circle_dev) {
        rt_kprintf("func: %s, rgb circle driver %s find fail.\r\n", __func__, RGB_CIRCLE_DRV_NAME);
        return -RT_ERROR;
    }

    cfg->is_initlized = true;

    rt_kprintf("func: %s, rgb driver init success.\r\n", __func__);

    return RT_EOK;
}

/**
 * @brief 设置RGB灯条颜色
 * 
 * @param[in] array RGB颜色数组
 * @param[in] count RGB颜色数量
 * @return rt_err_t 错误码，RT_EOK表示成功，-RT_ERROR表示失败
 */
rt_err_t rgb_strip_set_color(uint32_t *array, uint32_t count)
{
    struct rgb_drv_cfg *cfg = &g_rgb_drv_cfg;

    if (cfg->rgb_strip_dev == NULL) {
        rt_kprintf("func: %s, rgb strip driver is not initialized.\r\n", __func__);
        return -RT_ERROR;
    }

    if (array == NULL || count == 0) {
        rt_kprintf("func: %s, rgb bar array is null or count is 0.\r\n", __func__); 
        return -RT_ERROR;
    }

    // cfg->rgb_strip_cfg.count = count > RGB_STRIP_RGB_NUM ? RGB_STRIP_RGB_NUM : count;
    // cfg->rgb_strip_cfg.array = array;
    // rt_kprintf("func: %s, rgb strip set color count: %d, strip name: %s.\r\n", __func__, cfg->rgb_strip_cfg.count, RGB_STRIP_DRV_NAME);

    rt_err_t err = rt_device_control(cfg->rgb_strip_dev, 0, &cfg->rgb_strip_cfg);
    if(err != RT_EOK) {
        rt_kprintf("func: %s, rgb strip set color fail: %d.\r\n", __func__, err);
        return -RT_ERROR;
    }

    return RT_EOK;
}

/**
 * @brief 设置RGB灯环颜色
 * 
 * @param[in] array RGB颜色数组
 * @param[in] count RGB颜色数量
 * @return rt_err_t 错误码，RT_EOK表示成功，-RT_ERROR表示失败
 */ 
rt_err_t rgb_circle_set_color(uint32_t *array, uint32_t count)
{
    struct rgb_drv_cfg *cfg = &g_rgb_drv_cfg;

    if (cfg->rgb_circle_dev == NULL) {
        rt_kprintf("func: %s, rgb circle driver is not initialized.\r\n", __func__);
        return -RT_ERROR;
    }

    if (array == NULL || count == 0) {
        rt_kprintf("func: %s, rgb panel array is null or count is 0.\r\n", __func__); 
        return -RT_ERROR;
    }

    cfg->rgb_circle_cfg.count = count > RGB_CIRCLE_RGB_NUM ? RGB_CIRCLE_RGB_NUM : count;
    cfg->rgb_circle_cfg.array = array;

    rt_err_t err = rt_device_control(cfg->rgb_circle_dev, 0, &cfg->rgb_circle_cfg);
    if(err != RT_EOK) {
        rt_kprintf("func: %s, rgb circle set color fail: %d.\r\n", __func__, err);
        return -RT_ERROR;
    }

    return RT_EOK;
}


