/**
* @file example_user_ble_remote.c
* @author www.tuya.com
* @version 0.1
* @date 2022-05-20
*
* @copyright Copyright (c) tuya.inc 2022
*
*/

#include "tuya_cloud_types.h"

#if defined(ENABLE_BT_REMOTE_CTRL) && (ENABLE_BT_REMOTE_CTRL==1)
#include "tal_log.h"
#include "tuya_bt.h"
#include "app_ble.h"
#include "app_pwm.h"
#include "dp_process.h"
#include "dp_process.h"
/***********************************************************
*************************micro define***********************
***********************************************************/
#define ADV_DATA_TYPE_SERVICE_UUID 0x07
#define ADV_DATA_TYPE_FLAGS 0x01
#define SERVICE_UUID_AD_LENGTH 0x11  // 17 bytes

// 厂商产品ID (柏曼ID：0201)
#define VENDOR_PRODUCT_ID 0x0201

// 按键ID定义 - 根据协议表格更新
#define KEY_ID_POWER          0xa1  // 总开/关 K3
#define KEY_ID_BRIGHT_UP      0xa2  // 亮度增加 K4
#define KEY_ID_BRIGHT_DOWN    0xa3  // 亮度减少 K8
#define KEY_ID_GROUP1_SWITCH  0xa4  // 分组1开/关 K9
#define KEY_ID_GROUP2_SWITCH  0xa5  // 分组2开/关 K10
#define KEY_ID_COLOR_UP       0xa6  // 色温增加 K12
#define KEY_ID_MAIN_SWITCH    0xa7  // 主光/下光开/关 K13
#define KEY_ID_MAIN_BRIGHT_UP 0xa8  // 主光/下光亮度+ K14
#define KEY_ID_MAIN_BRIGHT_DOWN 0xa9 // 主光/下光亮度- K15
#define KEY_ID_COLOR_DOWN     0xaa  // 色温减少 K16
#define KEY_ID_AMBIENT_SWITCH 0xab  // 氛围光/上光开/关 K18
#define KEY_ID_AMBIENT_BRIGHT_UP 0xac // 氛围光/上光亮度+ K19
#define KEY_ID_AMBIENT_BRIGHT_DOWN 0xad // 氛围光/上光亮度- K20
#define KEY_ID_NIGHT_LIGHT    0xae  // 夜灯 K21
#define KEY_ID_MODE           0xaf  // 模式 K25
#define KEY_RESET             0xb0  //配网复位
#define KEY_ALIGNMENT         0xb1  //对码
#define KEY_CLEAR             0xb2  //清码

// 分组ID定义
#define GROUP_ID_MAIN         0x00  // 总分组
#define GROUP_ID_GROUP1       0x01  // 分组1
#define GROUP_ID_GROUP2       0x02  // 分组2
#define GROUP_ID_AMBIENT      0x02  // 氛围光分组

// 操作类型定义
#define OP_TYPE_SHORT_PRESS   0x00  // 短按松手
#define OP_TYPE_LONG_PRESS    0x01  // 长按
#define OP_TYPE_LONG_RELEASE  0x02  // 长按松手
#define OP_TYPE_COMBO         0x03  // 组合键

// 错误码定义
#define PARSE_SUCCESS     0
#define PARSE_ERROR       1
#define CHECKSUM_ERROR    2

// 在全局变量定义处添加
STATIC BOOL_T sg_in_pairing_mode = FALSE;  // 是否处于配对模式
STATIC UINT32_T sg_pairing_start_time = 0; // 配对开始时间
STATIC UINT32_T sg_pairing_timeout = 30000; // 配对超时时间30秒

// 绑定状态枚举
typedef enum {
    BIND_MODE_IDLE = 0,     // 空闲状态
    BIND_MODE_PAIRING,      // 配对中
    BIND_MODE_PAIRED        // 已配对
} BIND_MODE_E;

/***********************************************************
***********************typedef define***********************
***********************************************************/

// 遥控器数据结构
typedef struct {
    UINT16_T vendor_product_id;  // 厂商产品ID
    UINT32_T remote_id;          // 遥控器ID
    UINT8_T  command_id;         // 指令ID
    UINT8_T  group_id;           // 分组ID
    UINT8_T  random_id;          // 随机ID
    UINT8_T  key_id;             // 按键ID
    UINT8_T  reserved[5];        // 预留字段
    UINT8_T  checksum;           // 校验位
} USER_REMOTE_DATA_T;

// 解析结果结构
typedef struct {
    USER_REMOTE_DATA_T remote_data;
    UINT8_T mac_addr[6];         // 设备MAC地址
    INT8_T  rssi;                // 信号强度
    UINT8_T parse_result;        // 解析结果
    UINT8_T operation_type;      // 操作类型
    UINT8_T additional_param;    // 附加参数
} REMOTE_PARSE_RESULT_T;

/***********************************************************
***********************variable define**********************
***********************************************************/

STATIC UINT32_T sg_last_key_time = 0;      // 上次按键时间戳
STATIC UINT8_T sg_last_key_id = 0;         // 上次按键ID
STATIC UINT32_T sg_debounce_delay = 1200;   // 消抖延时1200ms

// 遥控器ID白名单（可配置接受的遥控器）
// STATIC UINT32_T sg_remote_whitelist[] = {
//     // 0x0112D6A1,
//     // 0x01137929,
//     0x11111111,
// };

STATIC UINT8_T sg_whitelist_count = 0;

// 当前模式状态
STATIC UINT8_T sg_current_mode = 0x11; // 默认白光模式
/***********************************************************
***********************function define**********************
***********************************************************/

/**
* @brief 计算校验和
*/
STATIC UINT8_T __calculate_checksum(UINT8_T *data, UINT8_T len)
{
    UINT8_T sum = 0;
    while (len--) {
        sum += *data++;
    }
    return sum;
}

/**
* @brief 检查遥控器ID是否在白名单中
*/
STATIC BOOL_T __check_remote_whitelist(UINT32_T remote_id)
{
    extern DEMO_INFO_T sg_demo_info;
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_demo_info.sg_remote_whitelist[i] == remote_id) {
            return TRUE;
        }
    }
    return FALSE;
}

/**
* @brief 进入配对模式
*/
OPERATE_RET user_ble_remote_start_pairing(VOID)
{
    if (sg_in_pairing_mode) {
        TAL_PR_NOTICE("Already in pairing mode");
        return OPRT_OK;
    }
    
    sg_in_pairing_mode = TRUE;
    sg_pairing_start_time = tal_system_get_millisecond();
    
    // 打开涂鸦的绑定窗口（确保能接收到蓝牙数据）
    tuya_ble_open_bind_window();
    
    // 这里可以添加配网指示灯效果
    TAL_PR_NOTICE("=== Enter Pairing Mode ===");
    TAL_PR_NOTICE("Pairing window opened for 30 seconds");
    
    return OPRT_OK;
}

/**
* @brief 退出配对模式
*/
OPERATE_RET user_ble_remote_stop_pairing(VOID)
{
    if (!sg_in_pairing_mode) {
        return OPRT_OK;
    }
    
    sg_in_pairing_mode = FALSE;
    // 关闭绑定窗口（可选）
    // tuya_ble_set_bind_window(0);
    
    TAL_PR_NOTICE("=== Exit Pairing Mode ===");
    return OPRT_OK;
}

/**
* @brief 检查配对超时
*/
STATIC VOID __check_pairing_timeout(VOID)
{
    if (sg_in_pairing_mode) {
        UINT32_T current_time = tal_system_get_millisecond();
        if ((current_time - sg_pairing_start_time) > sg_pairing_timeout) {
            TAL_PR_NOTICE("Pairing timeout, exit pairing mode");
            user_ble_remote_stop_pairing();
        }
    }
}

/**
* @brief 解析遥控器数据
*/
/**
* @brief 解析遥控器数据
*/
STATIC UINT8_T __parse_remote_data(UINT8_T *service_data, USER_REMOTE_DATA_T *remote_data, UINT8_T *op_type, UINT8_T *add_param)
{
    // if (service_data == NULL || remote_data == NULL) {
    //     return PARSE_ERROR;
    // }
    
    // TAL_PR_NOTICE("Raw data:");
    // for (int i = 0; i < 16; i++) {
    //     TAL_PR_NOTICE("[%d]: 0x%02X", i, service_data[i]);
    // }

    // 解析16字节的服务数据
    remote_data->vendor_product_id = (service_data[0] << 8) | service_data[1];
    remote_data->remote_id = (service_data[2] << 24) | (service_data[3] << 16) | 
                            (service_data[4] << 8) | service_data[5];
    remote_data->command_id = service_data[6];
    remote_data->group_id = service_data[7];
    remote_data->random_id = service_data[8];
    remote_data->key_id = service_data[9];
    memcpy(remote_data->reserved, &service_data[10], 5);
    remote_data->checksum = service_data[15];

    TAL_PR_NOTICE("key_id = %02x\r\n",remote_data->key_id);
    
    // 验证厂商产品ID
    if (remote_data->vendor_product_id != VENDOR_PRODUCT_ID) {
        // TAL_PR_NOTICE("Vendor product ID mismatch: 0x%04X", remote_data->vendor_product_id);
        return PARSE_ERROR;
    }
    
    // 验证校验和
    UINT8_T calculated_checksum = __calculate_checksum(service_data, 15);
    if (calculated_checksum != remote_data->checksum) {
        TAL_PR_NOTICE("Checksum error: calculated=0x%02X, received=0x%02X", 
                     calculated_checksum, remote_data->checksum);
        return CHECKSUM_ERROR;
    }
    
    // 检查遥控器ID白名单
    if (!__check_remote_whitelist(remote_data->remote_id)) {
        TAL_PR_NOTICE("Remote ID not in whitelist: 0x%08X", remote_data->remote_id);
        // user_ble_remote_add_whitelist(remote_data->remote_id);
        // return PARSE_ERROR;
    }

    // 根据协议表格解析操作类型和附加参数
    *op_type = OP_TYPE_SHORT_PRESS; // 默认短按
    
    // 根据命令ID和分组ID判断操作类型
    TAL_PR_NOTICE("remote_id = %08x,group_id = %02x\r\n",remote_data->remote_id,remote_data->group_id);
    switch (remote_data->group_id){
        case 0x00:
            if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x00)){
                remote_data->key_id = KEY_ID_POWER;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        case 0x01:
            if((remote_data->key_id == 0xb0)&&(remote_data->reserved[4] >= 0x05)){
                remote_data->key_id = KEY_ALIGNMENT;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            if(remote_data->key_id == 0x1c)
            {
                remote_data->key_id = KEY_ID_GROUP1_SWITCH;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        break;
        case 0x02:
            if((remote_data->key_id == 0xb0)&&(remote_data->reserved[4] >= 0x05)){
                remote_data->key_id = KEY_CLEAR;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            else{
                remote_data->key_id = KEY_ID_GROUP2_SWITCH;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
        break;
        default:
        TAL_PR_NOTICE("group_id = %02x\r\n",remote_data->group_id);
        break;
    }
    TAL_PR_NOTICE("key_id = %02x\r\n",remote_data->key_id);
    switch (remote_data->key_id) {
        case 0x97: // 开关类命令
            if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x01)){
                remote_data->key_id = KEY_ID_MAIN_SWITCH;
            } 
            else if((remote_data->key_id == 0x97)&&(remote_data->reserved[0] == 0x02)){
                remote_data->key_id = KEY_ID_AMBIENT_SWITCH;
            }
            break;
            
        case 0x25: // 亮度增加类命令
            if(remote_data->reserved[0] == 0x00){
                remote_data->key_id = KEY_ID_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            else if(remote_data->reserved[0] == 0x01){
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            else if(remote_data->reserved[0] == 0x02){
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x26: // 亮度减少类命令
            if (remote_data->reserved[0] == 0x00) {
                // 亮度减少 K8
                remote_data->key_id = KEY_ID_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            } else if (remote_data->reserved[0] == 0x01) {
                // 主光/下光亮度- K15
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            } else if (remote_data->reserved[0] == 0x02) {
                // 氛围光/上光亮度- K20
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x27: // 色温增加 K12
            if (remote_data->reserved[0] == 0x00) {
                remote_data->key_id = KEY_ID_COLOR_UP;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
            
        case 0x28: // 色温减少 K16
            if (remote_data->reserved[0] == 0x00) {
                remote_data->key_id = KEY_ID_COLOR_DOWN;
                *op_type = OP_TYPE_SHORT_PRESS;
            }
            break;
        case 0x32:
            remote_data->key_id = KEY_ID_NIGHT_LIGHT;
        break;
            
        case 0x3E: // 长按亮度增加
            if (remote_data->reserved[0] == 0x00) {
                // 亮度增加长按 K4
                remote_data->key_id = KEY_ID_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->reserved[0] == 0x01) {
                // 主光/下光亮度+长按 K14
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->reserved[0] == 0x02) {
                // 氛围光/上光亮度+长按 K19
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_UP;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        case 0x3F: // 长按亮度减少
            if (remote_data->group_id == 0x00) {
                // 亮度减少长按 K8
                remote_data->key_id = KEY_ID_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->group_id == 0x01) {
                // 主光/下光亮度-长按 K15
                remote_data->key_id = KEY_ID_MAIN_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            } else if (remote_data->group_id == 0x02) {
                // 氛围光/上光亮度-长按 K20
                remote_data->key_id = KEY_ID_AMBIENT_BRIGHT_DOWN;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        case 0x40: // 色温增加长按 K12
            remote_data->key_id = KEY_ID_COLOR_UP;
            *op_type = OP_TYPE_LONG_PRESS;
            break;
            
        case 0x41: // 色温减少长按 K16
            remote_data->key_id = KEY_ID_COLOR_DOWN;
            *op_type = OP_TYPE_LONG_PRESS;
            break;
            
        case 0x29: // 模式 K25
            remote_data->key_id = KEY_ID_MODE;
            *op_type = OP_TYPE_SHORT_PRESS;
            *add_param = remote_data->reserved[0]; // 模式参数
            break;
            
        case 0x00: // 长按松手
            // 长按松手命令，通过reserved字段判断
            if (remote_data->reserved[0] == 0x1F && 
                remote_data->reserved[1] == 0xFF && 
                remote_data->reserved[2] == 0xFF && 
                remote_data->reserved[3] == 0xFF) {
                *op_type = OP_TYPE_LONG_RELEASE;
            }
            break;
            
        case 0xBB: // 组合键 - 色温增加+色温减少
            if(remote_data->reserved[4] >= 0x03){
                remote_data->key_id = KEY_RESET;
                *op_type = OP_TYPE_LONG_PRESS;
            }
            break;
            
        default:
            TAL_PR_NOTICE("Unknown command ID: 0x%02X", remote_data->key_id);
            break;
    }
    
    return PARSE_SUCCESS;
}

/**
* @brief 处理按键事件
*/
/**
* @brief 处理按键事件
*/
STATIC VOID __handle_key_event(REMOTE_PARSE_RESULT_T *result)
{
    if (result == NULL || result->parse_result != PARSE_SUCCESS) {
        return;
    }
    
    USER_REMOTE_DATA_T *remote_data = &result->remote_data;
    UINT32_T current_time = tal_system_get_millisecond();
    
    // 检查是否为重复按键（相同按键ID且在消抖时间内）
    if (remote_data->key_id == sg_last_key_id && 
        (current_time - sg_last_key_time) < sg_debounce_delay) {
        TAL_PR_NOTICE("Debounce: Ignore duplicate key event (Key ID: 0x%02X)", remote_data->key_id);
        return;
    }
    
    // 更新按键记录
    sg_last_key_id = remote_data->key_id;
    sg_last_key_time = current_time;
    
    // 根据协议表格处理按键
    switch (remote_data->key_id) {
        case KEY_ID_POWER: // K3 总开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Total Power Toggle");
                // 执行总开/关操作
                if(sg_demo_info.switch_status == 0){
                    sg_demo_info.switch_status = 1;
                    sg_demo_info.white_switch = 0;
                    sg_demo_info.aux_switch = 0;
                    pwm_gradual_duty_set(BRIGHT_PWM,0);
                    //tkl_pwm_start(BRIGHT_PWM);
                    pwm_gradual_duty_set(TEMP_PWM,0);
                    //tkl_pwm_start(TEMP_PWM);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,0);
                    //tkl_pwm_start(AUX_BRIGHT_PWM);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,0);
                    //tkl_pwm_start(AUX_TEMP_PWM);
                    upload_device_bool_status(DPID_SWITCH,0);
                    upload_device_bool_status(LIGHT_SWITCH,0);
                    upload_device_bool_status(AUX_SWITCH,0);
                    upload_device_value_status(DPID_WHITE_BRIGHT,0);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,0);
                    upload_device_value_status(DPID_TEMP_VALUE,0);
                    upload_device_enum_status(DPID_WORK_MODE,16);
                }else{
                    sg_demo_info.switch_status == 0;
                    sg_demo_info.white_switch = 1;
                    sg_demo_info.aux_switch = 1;
                    sg_demo_info.night_switch = 0;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    //tkl_pwm_start(BRIGHT_PWM);
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                    //tkl_pwm_start(TEMP_PWM);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.white_temp);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    //tkl_pwm_start(AUX_BRIGHT_PWM);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                    //tkl_pwm_start(AUX_TEMP_PWM);
                    upload_device_bool_status(AUX_SWITCH,1);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    upload_device_enum_status(DPID_WORK_MODE,16);
                    upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                }
            }
            break;
            
        case KEY_ID_BRIGHT_UP: // K4 亮度增加
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Brightness +10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright <= 90)
                        sg_demo_info.white_bright += 10;
                    else
                        sg_demo_info.white_bright = 100;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright <= 90)
                        sg_demo_info.aux_bright += 10;
                    else
                        sg_demo_info.aux_bright = 100;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
                if(sg_demo_info.night_switch == 1){
                    if(sg_demo_info.night_bright <= 90)
                        sg_demo_info.night_bright += 10;
                    else
                        sg_demo_info.night_bright = 100;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,sg_demo_info.night_bright*100);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Brightness Continuous +");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright <= 90)
                        sg_demo_info.white_bright += 10;
                    else
                        sg_demo_info.white_bright = 100;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright <= 90)
                        sg_demo_info.aux_bright += 10;
                    else
                        sg_demo_info.aux_bright = 100;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
                if(sg_demo_info.night_switch == 1){
                    if(sg_demo_info.night_bright <= 90)
                        sg_demo_info.night_bright += 10;
                    else
                        sg_demo_info.night_bright = 100;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,sg_demo_info.night_bright*100);
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Brightness Change Stop");

            }
            break;
            
        case KEY_ID_BRIGHT_DOWN: // K8 亮度减少
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Brightness -10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright > 10)
                        sg_demo_info.white_bright -=10;
                    else
                        sg_demo_info.white_bright = 1;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright > 10)
                        sg_demo_info.aux_bright -=10;
                    else
                        sg_demo_info.aux_bright = 1;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
                if(sg_demo_info.night_switch == 1){
                    if(sg_demo_info.night_bright > 10)
                        sg_demo_info.night_bright -=10;
                    else
                        sg_demo_info.night_bright = 1;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,sg_demo_info.night_bright*100);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Brightness Continuous -");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright > 10)
                        sg_demo_info.white_bright -=10;
                    else
                        sg_demo_info.white_bright = 1;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright > 10)
                        sg_demo_info.aux_bright -=10;
                    else
                        sg_demo_info.aux_bright = 1;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
                if(sg_demo_info.night_switch == 1){
                    if(sg_demo_info.night_bright > 10)
                        sg_demo_info.night_bright -=10;
                    else
                        sg_demo_info.night_bright = 1;
                    upload_device_value_status(NIGHT_LIGHT_VALUE,sg_demo_info.night_bright);
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,sg_demo_info.night_bright*100);
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Brightness Change Stop");
            }
            break;
            
        case KEY_ID_GROUP1_SWITCH: // K9 分组1开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Group 1 Switch");
            }
            break;
            
        case KEY_ID_GROUP2_SWITCH: // K10 分组2开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Group 2 Switch");
            }
            break;
            
        case KEY_ID_COLOR_UP: // K12 色温增加
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature +10%%");
                if(sg_demo_info.white_temp <= 90){
                    sg_demo_info.white_temp += 10;
                    sg_demo_info.aux_temp += 10;
                } else{
                    sg_demo_info.white_temp = 100;
                    sg_demo_info.aux_temp = 100;
                }
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.white_temp);
                }
                if(sg_demo_info.aux_switch == 1){
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.aux_temp);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature Continuous +");
                if(sg_demo_info.white_temp <= 90){
                    sg_demo_info.white_temp += 10;
                    sg_demo_info.aux_temp += 10;
                } else{
                    sg_demo_info.white_temp = 100;
                    sg_demo_info.aux_temp = 100;
                }
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.white_temp);
                }
                if(sg_demo_info.aux_switch == 1){
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.aux_temp);
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Color Temperature Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_SWITCH: // K13 主光/下光开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Switch");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 0){
                    sg_demo_info.white_switch = 1;
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    sg_demo_info.switch_status = 1;
                    if(sg_demo_info.night_switch == 1){
                        pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                        //tkl_pwm_start(NIGHT_BRIGHT_PWM);
                        sg_demo_info.night_switch = 0;
                        upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    }
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }else{
                    sg_demo_info.white_switch = 0;
                    upload_device_bool_status(LIGHT_SWITCH,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,0);
                    pwm_gradual_duty_set(TEMP_PWM,0);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Adjust");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_BRIGHT_UP: // K14 主光/下光亮度+
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness +10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright <= 90)
                        sg_demo_info.white_bright +=10;
                    else
                        sg_demo_info.white_bright = 100;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
                // else{

                // }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Continuous +");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright <= 90)
                        sg_demo_info.white_bright +=10;
                    else
                        sg_demo_info.white_bright = 100;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_MAIN_BRIGHT_DOWN: // K15 主光/下光亮度-
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness -10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright > 10)
                        sg_demo_info.white_bright -=10;
                    else
                        sg_demo_info.white_bright = 1;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Main Light Brightness Continuous -");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    if(sg_demo_info.white_bright > 10)
                        sg_demo_info.white_bright -=10;
                    else
                        sg_demo_info.white_bright = 1;
                    upload_device_value_status(DPID_WHITE_BRIGHT,sg_demo_info.white_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Main Light Change Stop");
            }
            break;
            
        case KEY_ID_COLOR_DOWN: // K16 色温减少
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature -10%%");
                if(sg_demo_info.white_temp > 10){
                    sg_demo_info.white_temp -= 10;
                    sg_demo_info.aux_temp -= 10;
                } else{
                    sg_demo_info.white_temp = 1;
                    sg_demo_info.aux_temp = 1;
                }
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.white_temp);
                }
                if(sg_demo_info.aux_switch == 1){
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.aux_temp);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Color Temperature Continuous -");
                if(sg_demo_info.white_temp > 10){
                    sg_demo_info.white_temp -= 10;
                    sg_demo_info.aux_temp -= 10;
                } else{
                    sg_demo_info.white_temp = 1;
                    sg_demo_info.aux_temp = 1;
                }
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.white_switch == 1){
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.white_bright*sg_demo_info.white_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.white_bright*(101-sg_demo_info.white_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.white_temp);
                }
                if(sg_demo_info.aux_switch == 1){
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                    upload_device_value_status(DPID_TEMP_VALUE,sg_demo_info.aux_temp);
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Color Temperature Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_SWITCH: // K18 氛围光/上光开/关
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Switch");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.aux_switch == 0){
                    sg_demo_info.aux_switch = 1;
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(AUX_SWITCH,1);
                    if(sg_demo_info.night_switch == 1){
                        pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                        //tkl_pwm_start(NIGHT_BRIGHT_PWM);
                        sg_demo_info.night_switch = 0;
                        upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    }
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(AUX_TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }else{
                    sg_demo_info.aux_switch = 0;
                    upload_device_bool_status(AUX_SWITCH,0);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,0);
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Adjust");
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_BRIGHT_UP: // K19 氛围光/上光亮度+
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness +10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright <= 90)
                        sg_demo_info.aux_bright += 10;
                    else
                        sg_demo_info.aux_bright = 100;
                    
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Continuous +");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright <= 90)
                        sg_demo_info.aux_bright += 10;
                    else
                        sg_demo_info.aux_bright = 100;
                    
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_AMBIENT_BRIGHT_DOWN: // K20 氛围光/上光亮度-
            if (result->operation_type == OP_TYPE_SHORT_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness -10%%");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright > 10)
                        sg_demo_info.aux_bright -=10;
                    else
                        sg_demo_info.aux_bright = 1;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Ambient Light Brightness Continuous -");
                upload_device_enum_status(DPID_WORK_MODE,16);
                if(sg_demo_info.aux_switch == 1){
                    if(sg_demo_info.aux_bright > 10)
                        sg_demo_info.aux_bright -=10;
                    else
                        sg_demo_info.aux_bright = 1;
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,sg_demo_info.aux_bright);
                    pwm_gradual_duty_set(BRIGHT_PWM,(sg_demo_info.aux_bright*sg_demo_info.aux_temp));
                    pwm_gradual_duty_set(TEMP_PWM,(sg_demo_info.aux_bright*(101-sg_demo_info.aux_temp)));
                }
            } else if (result->operation_type == OP_TYPE_LONG_RELEASE) {
                TAL_PR_NOTICE("Action: Ambient Light Change Stop");
            }
            break;
            
        case KEY_ID_NIGHT_LIGHT: // K21 夜灯
            TAL_PR_NOTICE("Action: Night Light Mode");
            upload_device_enum_status(DPID_WORK_MODE,16);
            if(sg_demo_info.night_switch == 0){
                sg_demo_info.night_switch = 1;
                upload_device_bool_status(DPID_SWITCH,0);
                upload_device_bool_status(LIGHT_SWITCH,0);
                upload_device_bool_status(AUX_SWITCH,0);
                upload_device_value_status(NIGHT_LIGHT_VALUE,sg_demo_info.night_bright);
                sg_demo_info.white_switch = 0;
                sg_demo_info.switch_status = 0;
                sg_demo_info.aux_switch = 0;
                pwm_gradual_duty_set(BRIGHT_PWM,0);
                pwm_gradual_duty_set(TEMP_PWM,0);
                pwm_gradual_duty_set(AUX_BRIGHT_PWM,0);
                pwm_gradual_duty_set(AUX_TEMP_PWM,0);
                pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,sg_demo_info.night_bright*100);
            }
            else{
                sg_demo_info.night_switch = 0;
                pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
            }
            break;
            
        case KEY_ID_MODE: // K25 模式
            TAL_PR_NOTICE("Action: Mode Switch");
            // 模式循环: 白光(0x11) -> 黄光(0x12) -> 中性光(0x13) -> 白光(0x11)
            switch (sg_current_mode) {
                case 0x11: // 白光
                    sg_current_mode = 0x12; // 切换到黄光
                    sg_demo_info.white_switch = 1;
                    sg_demo_info.switch_status = 1;
                    sg_demo_info.aux_switch = 1;
                    sg_demo_info.white_bright = 100;
                    sg_demo_info.white_temp = 100;
                    sg_demo_info.aux_bright = 100;
                    sg_demo_info.aux_temp = 100;
                    sg_demo_info.night_switch = 0;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,100);
                    pwm_gradual_duty_set(TEMP_PWM,100);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,100);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,100);
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    upload_device_bool_status(AUX_SWITCH,1);
                    upload_device_value_status(DPID_WHITE_BRIGHT,100);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,100);
                    upload_device_value_status(DPID_TEMP_VALUE,100);
                    upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    TAL_PR_NOTICE("Switch to Yellow Light Mode");
                    break;
                case 0x12: // 黄光
                    sg_current_mode = 0x13; // 切换到中性光
                    sg_demo_info.white_switch = 1;
                    sg_demo_info.switch_status = 1;
                    sg_demo_info.aux_switch = 1;
                    sg_demo_info.white_bright = 100;
                    sg_demo_info.white_temp = 1;
                    sg_demo_info.aux_bright = 100;
                    sg_demo_info.aux_temp = 1;
                    sg_demo_info.night_switch = 0;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,100);
                    pwm_gradual_duty_set(TEMP_PWM,1);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,100);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,1);
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    upload_device_bool_status(AUX_SWITCH,1);
                    upload_device_value_status(DPID_WHITE_BRIGHT,100);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,100);
                    upload_device_value_status(DPID_TEMP_VALUE,1);
                    upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    TAL_PR_NOTICE("Switch to Neutral Light Mode");
                    break;
                case 0x13: // 中性光
                    sg_current_mode = 0x11; // 切换回白光
                    sg_demo_info.white_switch = 1;
                    sg_demo_info.switch_status = 1;
                    sg_demo_info.aux_switch = 1;
                    sg_demo_info.white_bright = 100;
                    sg_demo_info.white_temp = 50;
                    sg_demo_info.aux_bright = 100;
                    sg_demo_info.aux_temp = 50;
                    sg_demo_info.night_switch = 0;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,100);
                    pwm_gradual_duty_set(TEMP_PWM,50);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,100);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,50);
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    upload_device_bool_status(AUX_SWITCH,1);
                    upload_device_value_status(DPID_WHITE_BRIGHT,100);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,100);
                    upload_device_value_status(DPID_TEMP_VALUE,50);
                    upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    TAL_PR_NOTICE("Switch to White Light Mode");
                    break;
                default:
                    sg_current_mode = 0x11; // 默认白光
                    sg_demo_info.white_switch = 1;
                    sg_demo_info.switch_status = 1;
                    sg_demo_info.aux_switch = 1;
                    sg_demo_info.white_bright = 100;
                    sg_demo_info.white_temp = 100;
                    sg_demo_info.aux_bright = 100;
                    sg_demo_info.aux_temp = 100;
                    sg_demo_info.night_switch = 0;
                    pwm_gradual_duty_set(NIGHT_BRIGHT_PWM,0);
                    pwm_gradual_duty_set(BRIGHT_PWM,100);
                    pwm_gradual_duty_set(TEMP_PWM,100);
                    pwm_gradual_duty_set(AUX_BRIGHT_PWM,100);
                    pwm_gradual_duty_set(AUX_TEMP_PWM,100);
                    upload_device_bool_status(DPID_SWITCH,1);
                    upload_device_bool_status(LIGHT_SWITCH,1);
                    upload_device_bool_status(AUX_SWITCH,1);
                    upload_device_value_status(DPID_WHITE_BRIGHT,100);
                    upload_device_value_status(DPID_AUX_BRIGHT_VALUE,100);
                    upload_device_value_status(DPID_TEMP_VALUE,100);
                    upload_device_bool_status(DPID_SWITCH_NIGHT_LIGHT,0);
                    break;
            }
            break;
            
        case KEY_RESET: // 配网复位
            if (result->operation_type == OP_TYPE_LONG_PRESS) {
                TAL_PR_NOTICE("Action: Network Reset - Long Press");
                tuya_iot_wf_gw_unactive();
                // 执行配网复位操作
            }
            break;
            
        // case KEY_ALIGNMENT: // 对码
        //     if (result->operation_type == OP_TYPE_LONG_PRESS) {
        //         TAL_PR_NOTICE("Action: Alignment/Pairing - Long Press");
        //         user_ble_remote_add_whitelist(remote_data->remote_id);
        //         // 执行对码操作
        //     }
        //     break;
            
        // case KEY_CLEAR: // 清码
        //     if (result->operation_type == OP_TYPE_LONG_PRESS) {
        //         TAL_PR_NOTICE("Action: Clear Codes - Long Press");
        //         // 执行清码操作
        //     }
        //     break;
    case KEY_ALIGNMENT: // 对码
        if (result->operation_type == OP_TYPE_LONG_PRESS) {
        TAL_PR_NOTICE("Action: Alignment/Pairing - Long Press");
        
        // 只有在配对模式下才允许对码
        if (sg_in_pairing_mode) {
            OPERATE_RET ret = user_ble_remote_add_whitelist(remote_data->remote_id);
            if (ret == OPRT_OK) {
                TAL_PR_NOTICE("Pairing successful! Remote ID: 0x%08X added to whitelist", 
                             remote_data->remote_id);
                
                // 配对成功，退出配对模式
                // user_ble_remote_stop_pairing();
                
                // 这里可以添加配对成功的提示（如LED闪烁、蜂鸣器等）
                // pairing_success_indicate();
                
                // 上报绑定状态到云端
                // tuya_dev_report_dp_state("bind_status", 1);
            }
        } else {
            TAL_PR_NOTICE("Not in pairing mode, please enter pairing mode first");
        }
    }
    break;
    
    case KEY_CLEAR: // 清码
        if (result->operation_type == OP_TYPE_LONG_PRESS) {
            TAL_PR_NOTICE("Action: Clear Codes - Long Press");
            
            OPERATE_RET ret = user_ble_remote_remove_whitelist(remote_data->remote_id);
            if (ret == OPRT_OK) {
                TAL_PR_NOTICE("Remote ID: 0x%08X removed from whitelist", remote_data->remote_id);
                
                // 清码成功后，可以自动进入配对模式
                user_ble_remote_start_pairing();
                
                // 上报解绑状态到云端
                // tuya_dev_report_dp_state("bind_status", 0);
            }
        }
    break;
            
        default:
            TAL_PR_NOTICE("Action: Unknown key (0x%02X)", remote_data->key_id);
            break;
    }
}

/**
* @brief 查找服务UUID数据
*/
STATIC UINT8_T *__find_service_uuid_data(UINT8_T *adv_data, UINT8_T adv_len)
{
    UINT8_T *p = adv_data;
    UINT8_T pos = 0;
    
    while (pos < adv_len) {
        UINT8_T field_len = p[0];
        if (field_len == 0) break;
        
        if (pos + field_len + 1 > adv_len) break;
        
        UINT8_T field_type = p[1];
        
        if (field_type == ADV_DATA_TYPE_SERVICE_UUID && field_len == SERVICE_UUID_AD_LENGTH) {
            // 找到服务UUID数据，返回数据部分（跳过length和type）
            // TAL_PR_NOTICE("success\r\n");
            return &p[2];
        }
        
        p += (field_len + 1);
        pos += (field_len + 1);
    }
    
    return NULL;
}

/**
* @brief 解析完整的BLE广播包
*/
STATIC VOID __parse_ble_adv_packet(TAL_BLE_ADV_REPORT_T *scan_info, REMOTE_PARSE_RESULT_T *result)
{
    if (scan_info == NULL || result == NULL) {
        return;
    }
    
    // 初始化结果结构
    memset(result, 0, sizeof(REMOTE_PARSE_RESULT_T));
    result->rssi = scan_info->rssi;
    memcpy(result->mac_addr, scan_info->peer_addr.addr, 6);
    
    // 查找服务UUID数据
    UINT8_T *service_data = __find_service_uuid_data(scan_info->p_data, scan_info->data_len);
    if (service_data == NULL) {
        // TAL_PR_NOTICE("No service UUID data found in ADV packet");
        result->parse_result = PARSE_ERROR;
        return;
    }
    
    // 解析遥控器数据
    result->parse_result = __parse_remote_data(service_data, &result->remote_data,&result->operation_type, &result->additional_param);
}

STATIC VOID __ty_remote_receive_callback(UCHAR_T *data, UCHAR_T len, UCHAR_T type, UCHAR_T* mac)
{
    // 涂鸦标准遥控器数据处理
    // 如果需要支持涂鸦标准遥控器，可以在这里添加处理逻辑
    
    TAL_PR_NOTICE("Tuya standard remote received, MAC: %02X:%02X:%02X:%02X:%02X:%02X", 
                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    
    return;
}

STATIC VOID __user_remote_receive_callback(TAL_BLE_ADV_REPORT_T *scan_info)
{
    // TAL_PR_NOTICE("---------------- BLE Remote Data Received ----------------");
    // TAL_PR_NOTICE("Adv Type: %d, RSSI: %d, Data Len: %d", 
    //              scan_info->adv_type, scan_info->rssi, scan_info->data_len);
    // 检查配对超时
    __check_pairing_timeout();
    
    // 如果不是配对模式且已有绑定设备，检查遥控器是否在白名单中
    if (!sg_in_pairing_mode && sg_whitelist_count > 0) {
        // 这里可以添加白名单验证逻辑
        // 如果遥控器不在白名单中，直接返回不处理
    }
    // 解析BLE广播包
    REMOTE_PARSE_RESULT_T parse_result;
    __parse_ble_adv_packet(scan_info, &parse_result);
    
    if (parse_result.parse_result == PARSE_SUCCESS) {
        TAL_PR_NOTICE("Remote data parsed successfully");
        
        // 处理按键事件
        __handle_key_event(&parse_result);
        
        TAL_PR_NOTICE("---------------- BLE Parse Success ----------------");
    }
}

/***********************************************************
***********************external API*************************
***********************************************************/

/**
* @brief 添加遥控器到白名单
*/
OPERATE_RET user_ble_remote_add_whitelist(UINT32_T remote_id)
{
    extern DEMO_INFO_T sg_demo_info;
    // 检查是否已存在
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_demo_info.sg_remote_whitelist[i] == remote_id) {
            TAL_PR_NOTICE("Remote ID already in whitelist: 0x%08X", remote_id);
            return OPRT_OK;
        }
    }
    
    // 添加到白名单
    sg_demo_info.sg_remote_whitelist[sg_whitelist_count] = remote_id;
    sg_whitelist_count++;
    device_config_save();
    
    TAL_PR_NOTICE("Remote ID added to whitelist: 0x%08X", remote_id);
    return OPRT_OK;
}

/**
* @brief 移除遥控器从白名单
*/
OPERATE_RET user_ble_remote_remove_whitelist(UINT32_T remote_id)
{
    extern DEMO_INFO_T sg_demo_info;
    for (UINT8_T i = 0; i < sg_whitelist_count; i++) {
        if (sg_demo_info.sg_remote_whitelist[i] == remote_id) {
            // 移动后续元素
            for (UINT8_T j = i; j < sg_whitelist_count - 1; j++) {
                sg_demo_info.sg_remote_whitelist[j] = sg_demo_info.sg_remote_whitelist[j + 1];
            }
            sg_whitelist_count--;
            TAL_PR_NOTICE("Remote ID removed from whitelist: 0x%08X", remote_id);
            return OPRT_OK;
        }
    }
    device_config_save();
    
    TAL_PR_NOTICE("Remote ID not found in whitelist: 0x%08X", remote_id);
    return OPRT_OK;
}

/**
* @brief 清空白名单
*/
OPERATE_RET user_ble_remote_clear_whitelist(VOID)
{
    sg_whitelist_count = 0;
    TAL_PR_NOTICE("Remote whitelist cleared");
    return OPRT_OK;
}

/**
* @brief 检查是否已绑定遥控器
*/
BOOL_T user_ble_remote_is_paired(VOID)
{
    return (sg_whitelist_count > 0);
}

/**
* @brief 获取绑定状态
*/
BIND_MODE_E user_ble_remote_get_bind_status(VOID)
{
    if (sg_in_pairing_mode) {
        return BIND_MODE_PAIRING;
    } else if (user_ble_remote_is_paired()) {
        return BIND_MODE_PAIRED;
    } else {
        return BIND_MODE_IDLE;
    }
}

/**
* @brief 初始化绑定状态
*/
VOID user_ble_remote_init_bind_status(VOID)
{
    // 如果白名单为空，自动进入配对模式
    if (sg_whitelist_count == 0) {
        TAL_PR_NOTICE("No paired remote found, auto enter pairing mode");
        user_ble_remote_start_pairing();
    } else {
        TAL_PR_NOTICE("Found %d paired remote(s)", sg_whitelist_count);
    }
}

/**
* @brief ble scanf adv bind check callback
*
* @param[in] type: bind type for bluetooth remote controller
* @param[in] data: data
* @param[in] len: data len
* @return OPRT_OK
*/
STATIC OPERATE_RET __ty_remote_bind_check_callback(TUYA_BLE_BIND_TYPE type, UCHAR_T *data, UCHAR_T len)
{
    INT_T i = 0;
    
    TAL_PR_NOTICE("----------------scan adv bind check cb-----------------");
    TAL_PR_NOTICE("ble bind type : %d, data len: %d", type, len);
    for(i=0;i<len;i++) {
        TAL_PR_NOTICE("data[%d] : %d", i, data[i]);
    }

    return OPRT_OK;
}

/**
* @brief ble scan adv bind notify callback
*
* @param[in] type: bind type for bluetooth remote controller
* @return none
*/
STATIC VOID_T __ty_remote_bind_notify_callback(TUYA_BLE_BIND_TYPE type, int rslt)
{
    TAL_PR_NOTICE("------------------ble scan adv bind notify cb----------------");
    TAL_PR_NOTICE("ble bind type : %d", type);
    TAL_PR_NOTICE("result : %d", rslt);

    return ;
}

/**
* @brief tuya bluetooth remote initialization
*/
VOID user_ble_remote(INT_T argc, CHAR_T *argv[])
{
    OPERATE_RET rt = OPRT_OK;

    TUYA_BLE_SCAN_ADV_HANDLE_CBS ble_scan_cfg = {0};
    ble_scan_cfg.bind_check = __ty_remote_bind_check_callback;
    ble_scan_cfg.bind_notify = __ty_remote_bind_notify_callback;
    /* enable ble scan function */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_cb(__ty_remote_receive_callback), __EXIT);

    /* register user remote */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_raw_scan_adv_cb(__user_remote_receive_callback), __EXIT);
    // tuya_ble_open_bind_window();

    /* tuya remote bind notify */
    TUYA_CALL_ERR_GOTO(tuya_ble_reg_app_scan_adv_handle_cbs(&ble_scan_cfg), __EXIT);
    tuya_ble_set_bind_window(0xFFFFFFFF);
    sg_demo_info.sg_remote_whitelist[0] = 0x01137929;
    sg_whitelist_count++;
    device_config_save();
    // 初始化绑定状态
    user_ble_remote_init_bind_status();
    
    
    TAL_PR_NOTICE("User BLE Remote Parser initialized successfully");
    TAL_PR_NOTICE("Vendor Product ID: 0x%04X", VENDOR_PRODUCT_ID);
    TAL_PR_NOTICE("Whitelist count: %d", sg_whitelist_count);

__EXIT:
    if (OPRT_OK != rt) {
        TAL_PR_NOTICE("User BLE Remote Parser initialize failed: %d", rt);
    }
    return;
}
#endif