#ifndef _UART_H_
#define _UART_H_

#include <xs1.h>
#include <platform.h>
#include <xclib.h>
#include <stdio.h>
#include <stdint.h>

typedef enum {
    AUDIO_PCM_44100 = 0,
    AUDIO_PCM_48000,
    AUDIO_PCM_88200,
    AUDIO_PCM_96000,
    AUDIO_PCM_176400,
    AUDIO_PCM_192000,
    AUDIO_PCM_352800,
    AUDIO_PCM_384000,
    AUDIO_PCM_705600,
    AUDIO_PCM_768000,
    AUDIO_PCM_1441200,
    AUDIO_PCM_1536000,
    
    AUDIO_PCM_32000,
    AUDIO_PCM_64000,
    AUDIO_PCM_128000,
    AUDIO_PCM_256000,
    AUDIO_PCM_512000,

    AUDIO_DSD_64,
    AUDIO_DSD_128,
    AUDIO_DSD_256,
    AUDIO_DSD_512,
    AUDIO_DSD_1024,

    AUDIO_MQA_44100,
    AUDIO_MQA_88200,
    AUDIO_MQA_176400,
    AUDIO_MQA_352800,
    AUDIO_MQA_705600,
    AUDIO_MQA_1411200,
    AUDIO_MQA_2822400,
    AUDIO_MQA_5644800,

    AUDIO_MQA_48000,
    AUDIO_MQA_96000,
    AUDIO_MQA_192000,
    AUDIO_MQA_384000,
    AUDIO_MQA_768000,
    AUDIO_MQA_1536000,
    AUDIO_MQA_3072000,
    AUDIO_MQA_6144000,

    AUDIO_MQA_64000,
    AUDIO_MQA_128000,
    AUDIO_MQA_256000,
    AUDIO_MQA_512000,
    AUDIO_MQA_1024000,
    AUDIO_MQA_2048000,
    AUDIO_MQA_4096000,
    AUDIO_MQA_8192000,

    AUDIO_NO_USED = 0xFF,
} audio_sampling;

typedef enum {
    PCM         = 0, /**< PCM */
    Reserve     = 1, /**< Reserved */
    MQA         = 2, /**< MQA */
    MQB         = 3, /**< MQB */
    MQA_Studio  = 4, /**< MQA Studio */
    DSD         = 5, /**< DSD Native*/
} audio_type;

typedef enum
{
    USER_CMD_VOL_UP = 0x00,
    USER_CMD_VOL_DOWN,
    USER_CMD_PLAY,
    USER_CMD_NEXT,
    USER_CMD_PREV,
    USER_CMD_FORWARD,
    USER_CMD_REWIND,
    USER_CMD_MUTE
} ButtonEvent;

typedef enum {
    AUDIO_FREQ_44100 = 0,
    AUDIO_FREQ_48000,
    AUDIO_FREQ_88200,
    AUDIO_FREQ_96000,
    AUDIO_FREQ_176400,
    AUDIO_FREQ_192000,
    AUDIO_FREQ_352800,
    AUDIO_FREQ_384000,
    AUDIO_FREQ_705600,
    AUDIO_FREQ_768000,
    AUDIO_FREQ_1411200,
    AUDIO_FREQ_1536000
} AudioFreq;

typedef enum {
    AUDIO_CLASS_UAC1 = 0,
    AUDIO_CLASS_UAC2
} AudioClass;

typedef enum {
    AUDIO_WIDTH_16BIT = 0,
    AUDIO_WIDTH_24BIT = 1,
    AUDIO_WIDTH_32BIT = 2
} AudioWidth;

typedef enum {
    MODE_USB = 0,
    MODE_OPT,
    MODE_COAX,
    MODE_BT,
    MODE_HDMI_ARC,
    MODE_WIFI,
    MODE_AIRPLAY,
    MODE_SPOTIFY
} UserMode;

typedef enum {
  START_BOOT            = 0,
  READ_PRODUCT          = 1,
  READ_POR_CONFIG       = 2,
  READ_AUDIO_MODE       = 3,
  READ_USER_CONFIG      = 4,
  START_FINISH          = 5,
  REPORT_APP_STATUS     = 0x20,
  MEDIA_CONTROL         = 0x21,
  SEND_AUDIO_FORMAT     = 0x22,
  SWITCH_AUDIO_MODE     = 0x23,
  SEND_OUT_VOLUME       = 0x24,
  SEND_IN_VOLUME        = 0x25,
  SET_DAC_VOLUME        = 0x26,
  SET_DAC_MODE          = 0x27,
  SET_DAC_M_GAIN        = 0x32,
  TEST_CMD              = 0xF0,
  FW_VERSION            = 0xF1,
  FLASH_ID              = 0xF2,
  // EQ命令定义 (0x40-0x5C)
  SET_EQ_MODE           = 0x40,  // 切换EQ模式
  GET_EQ_MODE           = 0x41,  // 获取当前EQ模式信息
  SET_MODE_GAIN_AND_NAME = 0x42, // 设置模式整体增益和名称
  SET_EQ_PARAMS         = 0x43,  // 发送EQ参数
  GET_EQ_PARAMS         = 0x44,  // 读取EQ参数
  GET_DEVICE_INFO       = 0x45,  // 获取设备信息
  RESET_EQ_PARAMS       = 0x46,  // 复位EQ参数
  GET_EQ_MODE_COUNT     = 0x47,  // 获取EQ模式总数
  SET_AND_SAVE_EQ_MODE  = 0x48,  // 设置并保存EQ模式
  SET_VOLUME            = 0x49,  // 设置音量级别
  GET_VOLUME            = 0x4A,  // 获取音量级别
  GET_LED_INFO          = 0x4B,  // 获取LED信息
  SET_LED_SWITCH        = 0x4C,  // 设置LED开关
  GET_LED_SWITCH        = 0x4D,  // 获取LED开关
  GET_LED_STATUS        = 0x4E,  // 获取LED状态
  GET_LED_COUNT         = 0x4F,  // 获取LED总数
  GET_UAC_MODE_INFO     = 0x50,  // 获取UAC模式信息
  SET_UAC_MODE          = 0x51,  // 设置UAC模式
  GET_CURRENT_UAC_MODE  = 0x52,  // 获取当前UAC模式
  SET_EQ_ENABLE         = 0x53,  // 设置EQ使能开关
  GET_EQ_ENABLE         = 0x54,  // 获取EQ使能开关
  GET_SAMPLE_FORMAT     = 0x55,  // 获取采样率和格式
  SET_GAIN_MODE         = 0x56,  // 设置增益模式
  GET_GAIN_MODE         = 0x57,  // 获取增益模式
  SET_FILTER_MODE       = 0x58,  // 设置滤波器模式
  GET_FILTER_MODE       = 0x59,  // 获取滤波器模式
  SET_GAME_MODE         = 0x5A,  // 设置游戏模式
  GET_GAME_MODE         = 0x5B,  // 获取游戏模式
  GET_FIRMWARE_VERSION  = 0x5C,  // 获取固件版本
  CMD_NONE              = 0xFF
} UartCmdType;

#define CMD_HEAD1_POS                   0
#define CMD_HEAD2_POS                   1
#define CMD_VERSION_POS                 2
#define CMD_CMD_POS                     3
#define CMD_DATA_LEN_POS                4
#define CMD_DATA_POS                    5

#define BOOT_CMD_DATA_LEN               17
#define FINISH_CMD_DATA_LEN             15
#define PRODUCT_CMD_DATA_LEN            61
#define CONFIG_CMD_DATA_LEN             14
#define AUDIO_MODE_CMD_DATA_LEN         5

#define UART_HEAD1            0x55
#define UART_HEAD2            0xAA
#define UART_HEAD             0x55
#define UART_HEAD_LEN         0x05

#if !__XC__
typedef struct {
    uint8_t I2S_Mode : 1;       // I2S Mode: 0 - Master, 1 - Slave
    uint8_t Sync_Mode : 1;      // Sync Mode: 0 - Async, 1 - Sync
    uint8_t MIDI_Enable : 1;    // MIDI Enable: 0 - Disable, 1 - Enable
    uint8_t SPDIF_In : 1;       // SPDIF Input: 0 - Disable, 1 - Enable
    uint8_t SPDIF_Out : 1;      // SPDIF Output: 0 - Disable, 1 - Enable
    uint8_t ADAT_In : 1;        // ADAT Input: 0 - Disable, 1 - Enable
    uint8_t ADAT_Out : 1;       // ADAT Output: 0 - Disable, 1 - Enable
    uint8_t DSD_Out : 1;        // DSD Output: 0 - Disable, 1 - Enable

    uint8_t Audio_Sample_Rate : 4; // Audio Sample Rate
    uint8_t MQA_Enable : 1;        // MQA Enable: 0 - Disable, 1 - Enable
    uint8_t Audio_Class : 1;       // Audio Class: 0 - UAC 1.0, 1 - USB UAC 2.0
    uint8_t Audio_Width : 2;       // Audio Width: 0 - 16bit, 1 - 24bit, 2 - 32bit

    uint16_t Input_Channel : 6;        // Input Channel Count
    uint16_t Output_Channel : 6;       // Output Channel Count
    uint16_t Other_Cfg : 4;            // Other Configuration

    UserMode Mode;                     // Mode
} AudioMode;



typedef struct {
    uint16_t vid1;
    uint16_t pid1;
    uint16_t vid2;
    uint16_t pid2;
    uint8_t ManufactureName[16];
    uint8_t ProductName[16];
    uint8_t SerialNumber[16];
    uint32_t crc;
} ProductInfo;

typedef struct {
    AudioMode audioMode;
    uint16_t muteTime;
    uint8_t micVol;
    uint8_t dacLvol;
    uint8_t dacRvol;
    uint32_t crc;
} AppConfigs;

#endif

typedef enum {
    USB_MODE    = 1 ,
    OPT_MODE        ,
    COAX_MODE       ,
    BT_MODE         ,
    HDMI_ARC_MODE   ,
    WIFI_MODE       ,
    AIRPLAY_MODE    ,
    SPOTIFY_MODE    ,
} UsbMode;



typedef struct {
  unsigned char startFlag;
  unsigned char startConfig;
  unsigned char startApp;
  unsigned char cmdCount;
  unsigned char cmdPending;
  unsigned char pendingReboot;
} UserCmdConfig;

#endif
