#pragma once

#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif

/* Includes ----------------------------------------------------------------- */
#include <cmsis_os2.h>

#include "component/user_math.h"
#include "device.h"

/* USER INCLUDE BEGIN */

/* USER INCLUDE END */

/* USER DEFINE BEGIN */

/* USER DEFINE END */

/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/* Exported types ----------------------------------------------------------- */
typedef struct __packed {
    uint8_t sof_1;
    uint8_t sof_2;
    uint64_t ch_0:11;
    uint64_t ch_1:11;
    uint64_t ch_2:11;
    uint64_t ch_3:11;
    uint64_t mode_sw:2;
    uint64_t pause:1;
    uint64_t fn_1:1;
    uint64_t fn_2:1;
    uint64_t wheel:11;
    uint64_t trigger:1;

    int16_t mouse_x;
    int16_t mouse_y;
    int16_t mouse_z;
    uint8_t mouse_left:2;
    uint8_t mouse_right:2;
    uint8_t mouse_middle:2;
    uint16_t key;
    uint16_t crc16;
} VT13_RawData_t;

typedef enum {
  VT13_SW_ERR = 0,
  VT13_SW_UP = 1,
  VT13_SW_MID = 3,
  VT13_SW_DOWN = 2,
} VT13_SwitchPos_t;

/* 键盘按键值 */
typedef enum {
  VT13_KEY_W = 0,
  VT13_KEY_S,
  VT13_KEY_A,
  VT13_KEY_D,
  VT13_KEY_SHIFT,
  VT13_KEY_CTRL,
  VT13_KEY_Q,
  VT13_KEY_E,
  VT13_KEY_R,
  VT13_KEY_F,
  VT13_KEY_G,
  VT13_KEY_Z,
  VT13_KEY_X,
  VT13_KEY_C,
  VT13_KEY_V,
  VT13_KEY_B,
  VT13_L_CLICK,
  VT13_R_CLICK,
  VT13_M_CLICK,
  VT13_KEY_NUM,
} VT13_Key_t;

typedef struct {
  float ch_l_x; /* 遥控器左侧摇杆横轴值，上为正 */
  float ch_l_y; /* 遥控器左侧摇杆纵轴值，右为正 */
  float ch_r_x; /* 遥控器右侧摇杆横轴值，上为正 */
  float ch_r_y; /* 遥控器右侧摇杆纵轴值，右为正 */

  float ch_res; /* 第五通道值 */

  VT13_SwitchPos_t sw_r; /* 右侧拨杆位置 */
  VT13_SwitchPos_t sw_l; /* 左侧拨杆位置 */
  VT13_SwitchPos_t sw_mode; /* 模式拨杆位置 */
  VT13_SwitchPos_t sw_pause; /* 暂停拨杆位置 */
  VT13_SwitchPos_t sw_trig; /* 扳机位置 */
  
  struct {
    int16_t x;
    int16_t y;
    int16_t z;
    bool l_click; /* 左键 */
    bool m_click; /* 中键 */
    bool r_click; /* 右键 */
  } mouse;        /* 鼠标值 */

  union {
    bool key[VT13_KEY_NUM]; /* 键盘按键值 */
    uint16_t value;        /* 键盘按键值的位映射 */
  } keyboard;

  uint16_t res; /* 保留，未启用 */
} VT13_Data_t;

typedef struct {
  DEVICE_Header_t header;
  VT13_RawData_t raw_data;
  VT13_Data_t data;
} VT13_t;

/* Exported functions prototypes -------------------------------------------- */
int8_t VT13_Init(VT13_t *VT13);
int8_t VT13_Restart(void);
int8_t VT13_StartDmaRecv(VT13_t *VT13);
bool   VT13_WaitDmaCplt(uint32_t timeout);
int8_t VT13_ParseData(VT13_t *VT13);
int8_t VT13_Offline(VT13_t *VT13);

/* USER FUNCTION BEGIN */

/* USER FUNCTION END */

#ifdef __cplusplus
}
#endif
