/**
 * @file uart_cmd.h
 * @brief UART0 命令解析：中断接收 + 环形缓冲区 + 状态机
 *
 * 串口参数：9600bps，8N1
 * 引脚：P31 (RX)
 *
 * 命令协议：
 *   33 59 01          → 进入 DEBUG 模式
 *   33 59 00          → 进入 AUTO 模式
 *   AB CD 00 [140B]   → 写入 layer[0]
 *   AB CD 01 [140B]   → 写入 layer[1]
 *   12 34 43 21       → 查询当前查找表（HEX 打印）
 *   12 34 56 78       → 查询当前 5 路捕获值
 *
 * DEBUG 模式追加：
 *   进入 DEBUG 后收到 5 字节 (R G B W C) → 直接设置输出
 */

#ifndef UART_CMD_H
#define UART_CMD_H

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

/* =========================================================
 * 环形缓冲区
 * ========================================================= */
typedef struct {
    uint8_t  buf[UART_RX_BUF_SIZE];
    volatile uint8_t head;   /* 写入位置（中断更新） */
    volatile uint8_t tail;   /* 读取位置（主循环更新） */
} RingBuf_t;

/* =========================================================
 * 命令解析状态机状态
 * ========================================================= */
typedef enum {
    UCMD_IDLE = 0,         /* 空闲，等待第一个字节 */
    UCMD_GOT_33,           /* 收到 0x33 */
    UCMD_GOT_33_59,        /* 收到 0x33 0x59 */
    UCMD_GOT_AB,           /* 收到 0xAB */
    UCMD_GOT_AB_CD,        /* 收到 0xAB 0xCD */
    UCMD_GOT_AB_CD_LAYER,  /* 收到层索引，等待 140 字节数据 */
    UCMD_GOT_12,           /* 收到 0x12 */
    UCMD_GOT_12_34,        /* 收到 0x12 0x34 */
    UCMD_DEBUG_WAIT_RGB,   /* DEBUG 模式：等待 5 字节输出数据 */
} UartCmdState_t;

/* =========================================================
 * 调试输出直通值（DEBUG 模式使用）
 * ========================================================= */
typedef struct {
    uint8_t r, g, b, w, c;
    uint8_t updated;   /* 新数据标志，主循环读取后清零 */
} DebugOutput_t;

/* =========================================================
 * 全局变量（uart_cmd.c 定义，main.c/其他模块引用）
 * ========================================================= */
extern RingBuf_t  g_uart_rxbuf;
extern DebugOutput_t g_debug_out;

/* =========================================================
 * 函数声明
 * ========================================================= */

/**
 * @brief 初始化 UART0（配置波特率、中断、引脚）
 *
 * DDL 调用：
 *   Gpio_SetFunc(P31, GpioFuncUart0Rx)  — P31 配置为 UART0 RX 功能
 *   Uart_Init(UART0, 9600, 8, None, 1)  — 初始化 UART0，9600bps，8N1
 *   Uart_EnableRxInt(UART0)             — 使能接收中断
 *   NVIC_EnableIRQ(UART0_IRQn)          — 使能 NVIC 中断
 */
void UartCmd_Init(void);

/**
 * @brief UART0 接收中断服务函数（在 main.c 中的 ISR 中调用）
 *
 * DDL 调用：
 *   Uart_GetRxData(UART0)  — 读取接收寄存器数据
 *   Uart_ClearRxInt(UART0) — 清除接收中断标志
 */
void UartCmd_RxIsr(void);

/**
 * @brief 主循环中调用：解析环形缓冲区中的命令
 * @param mode_ptr  运行模式指针（可被命令修改）
 */
void UartCmd_Process(RunMode_t *mode_ptr);

/**
 * @brief 通过 UART 发送一个字节
 * DDL 调用：Uart_SendData(UART0, byte)
 */
void UartCmd_SendByte(uint8_t byte);

/**
 * @brief 通过 UART 发送字符串
 */
void UartCmd_SendStr(const char *str);

/**
 * @brief 打印当前查找表（HEX 格式）
 */
void UartCmd_PrintLut(void);

/**
 * @brief 打印当前 5 路 PWM 捕获值
 * @param w, c, r, g, b  各路占空比 (0-255)
 */
void UartCmd_PrintCapture(uint8_t w, uint8_t c, uint8_t r, uint8_t g, uint8_t b);

#endif /* UART_CMD_H */
